Fix MySQL error were prepared statement contains too many placeholders (#10153)

* Fixes a MySQL error were the prepared statement contains too many placeholders. Modifies dbBulkInsert to run array_chunk on incoming data and inserts those smaller chunks.

* Trying to address some automatic checks about blank lines.

* Trying to make Code Climate happy with reduced cognitive complexity.

* Trying to make Code Climate happy with reduced cognitive complexity.
This commit is contained in:
David Nelson 2019-05-06 19:42:25 -07:00 committed by Tony Murray
parent a46fc9d329
commit 223d3070d5
1 changed files with 14 additions and 8 deletions

View File

@ -144,21 +144,27 @@ function dbBulkInsert($data, $table)
if (empty($data)) {
return false;
}
// make sure we have fields to insert
$fields = array_keys(reset($data));
if (empty($fields)) {
return false;
}
try {
// $result = Eloquent::DB()->insert($sql.$values);
$result = Eloquent::DB()->table($table)->insert((array)$data);
// Break into managable chunks to prevent situations where insert
// fails due to prepared statement having too many placeholders.
$data_chunks = array_chunk($data, 10000, true);
recordDbStatistic('insert', $time_start);
return $result;
} catch (PDOException $pdoe) {
// FIXME query?
dbHandleException(new QueryException("Bulk insert $table", $data, $pdoe));
foreach ($data_chunks as $data_chunk) {
try {
$result = Eloquent::DB()->table($table)->insert((array)$data_chunk);
recordDbStatistic('insert', $time_start);
return $result;
} catch (PDOException $pdoe) {
// FIXME query?
dbHandleException(new QueryException("Bulk insert $table", $data_chunk, $pdoe));
}
}
return false;