https://github.com/kfirba/import-query-generator
An efficient query generator for mass resource import, distinguishing between new records and records to update.
https://github.com/kfirba/import-query-generator
create-or-update import mysql sql-generation sql-query
Last synced: 10 days ago
JSON representation
An efficient query generator for mass resource import, distinguishing between new records and records to update.
- Host: GitHub
- URL: https://github.com/kfirba/import-query-generator
- Owner: kfirba
- License: mit
- Created: 2018-01-12T08:13:43.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2021-03-08T06:53:02.000Z (almost 5 years ago)
- Last Synced: 2025-10-24T06:22:01.339Z (2 months ago)
- Topics: create-or-update, import, mysql, sql-generation, sql-query
- Language: PHP
- Homepage: https://kfirba.me/blog/performant-mass-update-or-create-strategy-for-data-imports
- Size: 11.7 KB
- Stars: 35
- Watchers: 1
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Import Query Generator
An efficient query generator for mass resource import, **distinguishing between new records and records to update**. This library uses MySQL's `ON DUPLICATE KEY UPDATE` feature.
## Preface
I highly recommend you at least skim through [my blog about this library](https://kfirba.me/blog/performant-mass-update-or-create-strategy-for-data-imports) to get a better understanding of this library.
## Installation
You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):
```bash
composer require kfirba/import-query-generator
```
## Usage
```php
use Kfirba\QueryGenerator;
$table = 'users';
$data = [
['name' => 'John', 'email' => 'john@example.com', 'password' => 'hashed_password', 'created_at' => date('Y-m-d'), 'updated_at' => date('Y-m-d')],
['name' => 'Jane', 'email' => 'jane@example.com', 'password' => 'hashed_password', 'created_at' => date('Y-m-d'), 'updated_at' => date('Y-m-d')],
['name' => 'Susy', 'email' => 'susy@example.com', 'password' => 'hashed_password', 'created_at' => date('Y-m-d'), 'updated_at' => date('Y-m-d')],
];
$excludedColumnsFromUpdate = ['password', 'created_at'];
$queryObject = (new QueryGenerator)->generate($table, $data, $excludedColumnsFromUpdate);
$queryObject->getQuery();
// -> "insert into `users` (`name`,`email`,`password`,`created_at`,`updated_at`) values (?,?,?,?,?),(?,?,?,?,?),(?,?,?,?,?) on duplicate key update `name`=VALUES(`name`),`email`=VALUES(`email`),`updated_at`=VALUES(`updated_at`)"
$queryObject->getBindings();
// -> ['John', 'john@example.com', 'hashed_password', '2018-01-12', '2018-01-12', 'Jane', 'jane@example.com', 'hashed_password', '2018-01-12', '2018-01-12', 'Susy', 'Susy@example.com', 'hashed_password', '2018-01-12', '2018-01-12']
```
As you may have noticed, the generator defaults to `column=VALUES(column)` since this is usually what we use when we attempt to bulk import some data.
Need another behavior? You can submit a PR or just [open an issue](https://github.com/kfirba/import-query-generator/issues/new) and we can talk about it 🤓.
## License
This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).