aymanelarian/import-query-generator-pg
Composer 安装命令:
composer require aymanelarian/import-query-generator-pg
包简介
An efficient Postgres query generator for mass resource import, distinguishing between new records and records to update.
README 文档
README
An efficient query generator for mass resource import, distinguishing between new records and records to update. This library uses Postgree's ON CONFLICT DO UPDATE feature.
its forked from kfirba/import-query-generator and adjusted for postgree sql.
Preface
I highly recommend you at least skim through my blog about this library 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:
composer require AymanElarian/import-query-generator-pg
Usage
use AymanElarian\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 and we can talk about it 🤓.
License
This package is open-sourced software licensed under the MIT license.
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-25