generate multi insert SQL for MySQL.
require_once('path/to/libdir/src/SQL/Maker/InsertMulti.php');
$builder = new SQL_Maker_InsertMulti("your_table", array('fields' => array('id', 'name', 'created_at'));
foreach ( $data as $row ) {
$builder->bindRow(array(
'id' => $row['id'],
'name' => $row['name'],
'created_at' => $row['created_at'],
);
}
$stmt = $pdo->prepare($builer->toQuery());
$stmt->execute($builer->binds());
// in case with bindValue
require_once('path/to/libdir/src/SQL/Maker/InsertMulti.php');
$builder = new SQL_Maker_InsertMulti("your_table", array('fields' => array(
'id' => \PDO::PARAM_INT,
'name' => \PDO::PARAM_STR,
'created_at' => \PDO::PARAM_STR,
));
foreach ( $data as $row ) {
$builder->bindRow(array(
'id' => $row['id'],
'name' => $row['name'],
'created_at' => $row['created_at'],
);
}
$stmt = $pdo->prepare($builer->toQuery());
$builer->bindValues($stmt);
$stmt->execute();
Key concept of this library is followings:
- Don't handle connection.
- Don't handle query
- Easy to write multi insert code.
There are many libraries already for this porpose with connection. But, I'd like to handle connection and query carefully. So, I wrote it.
Since it only require "prepare" and "binds" API, you might be able to apply this library with many PDO's wrapper.
This library inspired from perl's library: SQL::Maker.
The MIT License (MIT)
Copyright (c) 2013 Keiji Yoshimi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.