Skip to content

Commit 243cb92

Browse files
committed
Added BULK IMPORT MODE command. Added bulk insert functionality.
1 parent 02ebbb6 commit 243cb92

File tree

9 files changed

+125
-15
lines changed

9 files changed

+125
-15
lines changed

support/db.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
// CubicleSoft generic database base class.
3-
// (C) 2014 CubicleSoft. All Rights Reserved.
3+
// (C) 2015 CubicleSoft. All Rights Reserved.
44

55
class CSDB
66
{
@@ -607,9 +607,30 @@ protected function ProcessINSERT(&$master, &$sql, &$opts, $queryinfo, $args, $su
607607
$vals[] = $val;
608608
}
609609
}
610-
$sql .= " (" . implode(", ", $keys) . ") VALUES (" . implode(", ", $vals) . ")";
610+
$sql .= " (" . implode(", ", $keys) . ") VALUES ";
611+
$origsql = $sql;
612+
$sql .= "(" . implode(", ", $vals) . ")";
613+
614+
// Handle bulk inserts.
615+
$bulkinsert = (isset($supported["BULKINSERT"]) && $supported["BULKINSERT"]);
616+
if (!$bulkinsert) $sql = array($sql);
617+
for ($x = 3; isset($queryinfo[$x]) && isset($queryinfo[$x + 1]); $x += 2)
618+
{
619+
$vals = array();
620+
foreach ($queryinfo[$x] as $key => $val)
621+
{
622+
$vals[] = "?";
623+
$args[] = $val;
624+
}
625+
626+
// Avoid this if possible.
627+
foreach ($queryinfo[$x + 1] as $key => $val) $vals[] = $val;
628+
629+
if ($bulkinsert) $sql .= ", (" . implode(", ", $vals) . "}";
630+
else $sql[] = $origsql . "(" . implode(", ", $vals) . ")";
631+
}
611632

612-
if (isset($supported["POSTVALUES"]))
633+
if (isset($supported["POSTVALUES"]) && !isset($queryinfo[3]))
613634
{
614635
foreach ($supported["POSTVALUES"] as $key => $mode)
615636
{

support/db_mysql.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
// CubicleSoft MySQL/Maria DB database interface.
3-
// (C) 2013 CubicleSoft. All Rights Reserved.
3+
// (C) 2015 CubicleSoft. All Rights Reserved.
44

55
if (!class_exists("CSDB")) exit();
66

@@ -66,7 +66,8 @@ protected function GenerateSQL(&$master, &$sql, &$opts, $cmd, $queryinfo, $args,
6666
{
6767
$supported = array(
6868
"PREINTO" => array("LOW_PRIORITY" => "bool", "DELAYED" => "bool", "HIGH_PRIORITY" => "bool", "IGNORE" => "bool"),
69-
"SELECT" => true
69+
"SELECT" => true,
70+
"BULKINSERT" => true
7071
);
7172

7273
return $this->ProcessINSERT($master, $sql, $opts, $queryinfo, $args, $subquery, $supported);
@@ -232,6 +233,29 @@ protected function GenerateSQL(&$master, &$sql, &$opts, $cmd, $queryinfo, $args,
232233

233234
return array("success" => true, "filter_opts" => array("mode" => "SHOW CREATE TABLE", "hints" => (isset($queryinfo["EXPORT HINTS"]) ? $queryinfo["EXPORT HINTS"] : array())));
234235
}
236+
case "BULK IMPORT MODE":
237+
{
238+
$master = true;
239+
240+
if ($queryinfo)
241+
{
242+
$sql = array(
243+
"SET autocommit=0",
244+
"SET unique_checks=0",
245+
"SET foreign_key_checks=0",
246+
);
247+
}
248+
else
249+
{
250+
$sql = array(
251+
"SET autocommit=1",
252+
"SET unique_checks=1",
253+
"SET foreign_key_checks=1",
254+
);
255+
}
256+
257+
return array("success" => true);
258+
}
235259
}
236260

237261
return array("success" => false, "error" => CSDB::DB_Translate("Unknown query command '%s'.", $cmd), "errorcode" => "unknown_query_command");

support/db_mysql_lite.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
// CubicleSoft MySQL/Maria DB lightweight database interface.
3-
// (C) 2013 CubicleSoft. All Rights Reserved.
3+
// (C) 2015 CubicleSoft. All Rights Reserved.
44

55
if (!class_exists("CSDB")) exit();
66

@@ -56,7 +56,8 @@ protected function GenerateSQL(&$master, &$sql, &$opts, $cmd, $queryinfo, $args,
5656
{
5757
$supported = array(
5858
"PREINTO" => array("LOW_PRIORITY" => "bool", "DELAYED" => "bool", "HIGH_PRIORITY" => "bool", "IGNORE" => "bool"),
59-
"SELECT" => true
59+
"SELECT" => true,
60+
"BULKINSERT" => true
6061
);
6162

6263
return $this->ProcessINSERT($master, $sql, $opts, $queryinfo, $args, $subquery, $supported);

support/db_oci.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function TableExists($name)
8484

8585
public function QuoteIdentifier($str)
8686
{
87-
return str_replace(array("\"", "?"), array("\"\"", ""), $str);
87+
return str_replace(array("'", "\"", "?"), "", $str);
8888
}
8989

9090
// This function is used to get the last inserted sequence value by table name.
@@ -150,11 +150,22 @@ protected function GenerateSQL(&$master, &$sql, &$opts, $cmd, $queryinfo, $args,
150150
"PREINTO" => array(),
151151
"POSTVALUES" => array("RETURNING" => "key_identifier"),
152152
"SELECT" => true,
153+
"BULKINSERT" => false
153154
);
154155

155156
$result = $this->ProcessINSERT($master, $sql, $opts, $queryinfo, $args, $subquery, $supported);
156157
if ($result["success"] && isset($queryinfo["AUTO INCREMENT"])) $result["filter_opts"] = array("mode" => "INSERT", "queryinfo" => $queryinfo);
157158

159+
// Handle bulk insert by rewriting the queries because, well, Oracle.
160+
// http://stackoverflow.com/questions/39576/best-way-to-do-multi-row-insert-in-oracle
161+
if ($result["success"] && is_array($sql))
162+
{
163+
$sql2 = "INSERT ALL";
164+
foreach ($sql as $entry) $sql2 .= substr($entry, 6);
165+
$sql2 .= " SELECT 1 FROM DUAL";
166+
$sql = $sql2;
167+
}
168+
158169
return $result;
159170
}
160171
case "UPDATE":
@@ -344,6 +355,12 @@ protected function GenerateSQL(&$master, &$sql, &$opts, $cmd, $queryinfo, $args,
344355

345356
return array("success" => true, "filter_opts" => array("mode" => "SHOW CREATE TABLE", "hints" => (isset($queryinfo["EXPORT HINTS"]) ? $queryinfo["EXPORT HINTS"] : array())));
346357
}
358+
case "BULK IMPORT MODE":
359+
{
360+
$master = true;
361+
362+
return array("success" => false, "errorcode" => "skip_sql_query");
363+
}
347364
}
348365

349366
return array("success" => false, "error" => CSDB::DB_Translate("Unknown query command '%s'.", $cmd), "errorcode" => "unknown_query_command");

support/db_oci_lite.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function TableExists($name)
8181

8282
public function QuoteIdentifier($str)
8383
{
84-
return "" . str_replace(array("\"", "?"), array("\"\"", ""), $str) . "";
84+
return str_replace(array("'", "\"", "?"), "", $str);
8585
}
8686

8787
// This function is used to get the last inserted sequence value by table name.
@@ -146,11 +146,22 @@ protected function GenerateSQL(&$master, &$sql, &$opts, $cmd, $queryinfo, $args,
146146
"PREINTO" => array(),
147147
"POSTVALUES" => array("RETURNING" => "key_identifier"),
148148
"SELECT" => true,
149+
"BULKINSERT" => false
149150
);
150151

151152
$result = $this->ProcessINSERT($master, $sql, $opts, $queryinfo, $args, $subquery, $supported);
152153
if ($result["success"] && isset($queryinfo["AUTO INCREMENT"])) $result["filter_opts"] = array("mode" => "INSERT", "queryinfo" => $queryinfo);
153154

155+
// Handle bulk insert by rewriting the queries because, well, Oracle.
156+
// http://stackoverflow.com/questions/39576/best-way-to-do-multi-row-insert-in-oracle
157+
if ($result["success"] && is_array($sql))
158+
{
159+
$sql2 = "INSERT ALL";
160+
foreach ($sql as $entry) $sql2 .= substr($entry, 6);
161+
$sql2 .= " SELECT 1 FROM DUAL";
162+
$sql = $sql2;
163+
}
164+
154165
return $result;
155166
}
156167
case "UPDATE":

support/db_pgsql.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
// CubicleSoft PostgreSQL database interface.
3-
// (C) 2014 CubicleSoft. All Rights Reserved.
3+
// (C) 2015 CubicleSoft. All Rights Reserved.
44

55
if (!class_exists("CSDB")) exit();
66

@@ -72,6 +72,7 @@ protected function GenerateSQL(&$master, &$sql, &$opts, $cmd, $queryinfo, $args,
7272
"PREINTO" => array(),
7373
"POSTVALUES" => array("RETURNING" => "key_identifier"),
7474
"SELECT" => true,
75+
"BULKINSERT" => true
7576
);
7677

7778
// To get the last insert ID via GetInsertID(), the field that contains a 'serial' (auto increment) field must be specified.
@@ -272,6 +273,15 @@ protected function GenerateSQL(&$master, &$sql, &$opts, $cmd, $queryinfo, $args,
272273

273274
return array("success" => true, "filter_opts" => array("mode" => "SHOW CREATE TABLE", "hints" => (isset($queryinfo["EXPORT HINTS"]) ? $queryinfo["EXPORT HINTS"] : array())));
274275
}
276+
case "BULK IMPORT MODE":
277+
{
278+
$master = true;
279+
280+
if ($queryinfo) $sql = "SET synchronous_commit TO OFF";
281+
else $sql = "SET synchronous_commit TO DEFAULT";
282+
283+
return array("success" => true);
284+
}
275285
}
276286

277287
return array("success" => false, "error" => CSDB::DB_Translate("Unknown query command '%s'.", $cmd), "errorcode" => "unknown_query_command");

support/db_pgsql_lite.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
// CubicleSoft PostgreSQL database interface.
3-
// (C) 2014 CubicleSoft. All Rights Reserved.
3+
// (C) 2015 CubicleSoft. All Rights Reserved.
44

55
if (!class_exists("CSDB")) exit();
66

@@ -62,6 +62,7 @@ protected function GenerateSQL(&$master, &$sql, &$opts, $cmd, $queryinfo, $args,
6262
"PREINTO" => array(),
6363
"POSTVALUES" => array("RETURNING" => "key_identifier"),
6464
"SELECT" => true,
65+
"BULKINSERT" => true
6566
);
6667

6768
// To get the last insert ID via GetInsertID(), the field that contains a 'serial' (auto increment) field must be specified.

support/db_sqlite.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
// CubicleSoft SQLite database interface.
3-
// (C) 2013 CubicleSoft. All Rights Reserved.
3+
// (C) 2015 CubicleSoft. All Rights Reserved.
44

55
if (!class_exists("CSDB")) exit();
66

@@ -69,7 +69,8 @@ protected function GenerateSQL(&$master, &$sql, &$opts, $cmd, $queryinfo, $args,
6969
$supported = array(
7070
"DBPREFIX" => $this->dbprefix,
7171
"PREINTO" => array("LOW_PRIORITY" => "bool", "DELAYED" => "bool", "HIGH_PRIORITY" => "bool", "IGNORE" => "bool"),
72-
"SELECT" => true
72+
"SELECT" => true,
73+
"BULKINSERT" => true
7374
);
7475

7576
return $this->ProcessINSERT($master, $sql, $opts, $queryinfo, $args, $subquery, $supported);
@@ -285,6 +286,29 @@ protected function GenerateSQL(&$master, &$sql, &$opts, $cmd, $queryinfo, $args,
285286

286287
return array("success" => true, "filter_opts" => array("mode" => "SHOW CREATE TABLE", "output" => false, "queryinfo" => $queryinfo, "hints" => (isset($queryinfo["EXPORT HINTS"]) ? $queryinfo["EXPORT HINTS"] : array())));
287288
}
289+
case "BULK IMPORT MODE":
290+
{
291+
$master = true;
292+
293+
if ($queryinfo)
294+
{
295+
$sql = array(
296+
"PRAGMA synchronous=OFF",
297+
"PRAGMA journal_mode=MEMORY",
298+
"PRAGMA temp_store=MEMORY",
299+
);
300+
}
301+
else
302+
{
303+
$sql = array(
304+
"PRAGMA synchronous=NORMAL",
305+
"PRAGMA journal_mode=DELETE",
306+
"PRAGMA temp_store=DEFAULT",
307+
);
308+
}
309+
310+
return array("success" => true);
311+
}
288312
}
289313

290314
return array("success" => false, "error" => CSDB::DB_Translate("Unknown query command '%s'.", $cmd), "errorcode" => "unknown_query_command");

support/db_sqlite_lite.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
// CubicleSoft SQLite lightweight database interface.
3-
// (C) 2013 CubicleSoft. All Rights Reserved.
3+
// (C) 2015 CubicleSoft. All Rights Reserved.
44

55
if (!class_exists("CSDB")) exit();
66

@@ -59,7 +59,8 @@ protected function GenerateSQL(&$master, &$sql, &$opts, $cmd, $queryinfo, $args,
5959
$supported = array(
6060
"DBPREFIX" => $this->dbprefix,
6161
"PREINTO" => array("LOW_PRIORITY" => "bool", "DELAYED" => "bool", "HIGH_PRIORITY" => "bool", "IGNORE" => "bool"),
62-
"SELECT" => true
62+
"SELECT" => true,
63+
"BULKINSERT" => true
6364
);
6465

6566
return $this->ProcessINSERT($master, $sql, $opts, $queryinfo, $args, $subquery, $supported);

0 commit comments

Comments
 (0)