cryptoman3/doctrine-dbal-bulk

Bulk inserts and updates for Doctrine DBAL

1.x-dev 2023-07-24 12:17 UTC

This package is not auto-updated.

Last update: 2024-11-12 16:44:42 UTC


README

Contributing

This repo is READ ONLY, in order to contribute to Flow PHP project, please open PR against flow monorepo.

Changes merged to monorepo are automatically propagated into sub repositories.

Description

Doctrine Bulk is missing bulk upsert/insert abstraction for Doctrine DBAL.

Installation

composer require flow-php/doctrine-dbal-bulk:1.x@dev

Usage Examples

Insert:

$bulk = Bulk::create();
$bulk->insert(
    $dbalConnection,
    'your-table-name',
    new BulkData([
        ['id' => 1, 'name' => 'Name One', 'description' => 'Description One'],
        ['id' => 2, 'name' => 'Name Two', 'description' => 'Description Two'],
        ['id' => 3, 'name' => 'Name Three', 'description' => 'Description Three'],
    ])
);

Update:

$bulk = Bulk::create();
$bulk->update(
    $dbalConnection,
    'your-table-name',
    new BulkData([
        ['id' => 1, 'name' => 'Name One', 'description' => 'Description One'],
        ['id' => 2, 'name' => 'Name Two', 'description' => 'Description Two'],
        ['id' => 3, 'name' => 'Name Three', 'description' => 'Description Three'],
    ]),
    [
        'primary_key_columns' => ['id'],
        'update_columns' => ['name']
    ]
);

Supported Dialects

  • PostgreSQL

Adding new Dialects

Dialect is basic abstraction of this library.
The main role of Dialect is to prepare SQL insert/update statement based on BulkData and provided options.

  • $insertOptions
  • $updateOptions

Options are key => value maps without predefined structure that allows to manipulate building SQL statement. Each dialect should define it own structure for options in order to support db engine features, including those that are specific for given engine.

QueryFactory is abstraction for creating queries, there is currently only one implementation, DbalPlatform. QueryFactory insertOptions and updateOptions is combination of all options provided by supported Dialects where each entry must be optional.

example: dialect_option?: string

DbalPlatform is a factory that detects which Dialect should be used for given Doctrine DBAL Platform.