From 223d3070d55668a03a780a5c3b40e95fe5bca6ea Mon Sep 17 00:00:00 2001 From: David Nelson Date: Mon, 6 May 2019 19:42:25 -0700 Subject: [PATCH] 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. --- includes/dbFacile.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/includes/dbFacile.php b/includes/dbFacile.php index 7e5262f323..7b464531e6 100644 --- a/includes/dbFacile.php +++ b/includes/dbFacile.php @@ -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;