Skip to content

Commit

Permalink
support for batch INSERTs
Browse files Browse the repository at this point in the history
  • Loading branch information
alixaxel committed Oct 3, 2013
1 parent 0000bfa commit bc9df54
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* The MIT License
* http://creativecommons.org/licenses/MIT/
*
* ArrestDB 1.4.0 (github.com/alixaxel/ArrestDB/)
* ArrestDB 1.5.0 (github.com/alixaxel/ArrestDB/)
* Copyright (c) 2013 Alix Axel <alix.axel@gmail.com>
**/

Expand Down Expand Up @@ -275,22 +275,58 @@
);
}

else
else if (is_array($_POST) === true)
{
$data = array();
$queries = array();

foreach ($_POST as $key => $value)
if (count($_POST) == count($_POST, COUNT_RECURSIVE))
{
$data[sprintf('`%s`', $key)] = '?';
$_POST = array($_POST);
}

$query = array
(
sprintf('INSERT INTO `%s` (%s) VALUES (%s)', $table, implode(', ', array_keys($data)), implode(', ', $data)),
);
foreach ($_POST as $row)
{
$data = array();

$query = sprintf('%s;', implode(' ', $query));
$result = ArrestDB::Query($query, $_POST);
foreach ($row as $key => $value)
{
$data[sprintf('`%s`', $key)] = '?';
}

$query = array
(
sprintf('INSERT INTO `%s` (%s) VALUES (%s)', $table, implode(', ', array_keys($data)), implode(', ', $data)),
);

$queries[] = array
(
sprintf('%s;', implode(' ', $query)),
$data,
);
}

if (count($queries) > 1)
{
ArrestDB::Query()->beginTransaction();

while (is_null($query = array_shift($queries)) !== true)
{
if (($result = ArrestDB::Query($query[0], $query[1])) === false)
{
ArrestDB::Query()->rollBack(); break;
}
}

if (($result !== false) && (ArrestDB::Query()->inTransaction() === true))
{
$result = ArrestDB::Query()->commit();
}
}

else if (is_null($query = array_shift($queries)) !== true)
{
$result = ArrestDB::Query($query[0], $query[1]);
}

if ($result === false)
{
Expand Down Expand Up @@ -334,7 +370,7 @@
);
}

else
else if (is_array($GLOBALS['_PUT']) === true)
{
$data = array();

Expand Down Expand Up @@ -464,14 +500,14 @@ public static function Query($query = null)
'cache_size' => '8192',
'encoding' => '"UTF-8"',
'foreign_keys' => 'ON',
'journal_mode' => 'WAL',
'journal_size_limit' => '67110000',
'legacy_file_format' => 'OFF',
'page_size' => '4096',
'recursive_triggers' => 'ON',
'secure_delete' => 'ON',
'synchronous' => 'NORMAL',
'temp_store' => 'MEMORY',
'journal_mode' => 'WAL',
'wal_autocheckpoint' => '4096',
);

Expand Down

0 comments on commit bc9df54

Please sign in to comment.