Merge pull request #6507 from marc1706/ticket/12439

[ticket/12439] Unify behavior and deduplicate sql_multi_insert
This commit is contained in:
Marc Alexander 2023-08-03 19:46:24 +02:00 committed by GitHub
commit 60c2abdfda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -724,38 +724,28 @@ abstract class driver implements driver_interface
return false; return false;
} }
if ($this->multi_insert) $ary = array();
foreach ($sql_ary as $_sql_ary)
{ {
$ary = array(); // If by accident the sql array is only one-dimensional we build a normal insert statement
foreach ($sql_ary as $id => $_sql_ary) if (!is_array($_sql_ary))
{ {
// If by accident the sql array is only one-dimensional we build a normal insert statement return $this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $sql_ary));
if (!is_array($_sql_ary)) }
{
return $this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $sql_ary));
}
// Add values to set to $ary if multi insert is supported, otherwise run every insert query separately
if ($this->multi_insert)
{
$values = array(); $values = array();
foreach ($_sql_ary as $key => $var) foreach ($_sql_ary as $var)
{ {
$values[] = $this->_sql_validate_value($var); $values[] = $this->_sql_validate_value($var);
} }
$ary[] = '(' . implode(', ', $values) . ')'; $ary[] = '(' . implode(', ', $values) . ')';
} }
else
return $this->sql_query('INSERT INTO ' . $table . ' ' . ' (' . implode(', ', array_keys($sql_ary[0])) . ') VALUES ' . implode(', ', $ary));
}
else
{
foreach ($sql_ary as $ary)
{ {
if (!is_array($ary)) $result = $this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $_sql_ary));
{
return false;
}
$result = $this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $ary));
if (!$result) if (!$result)
{ {
return false; return false;
@ -763,6 +753,11 @@ abstract class driver implements driver_interface
} }
} }
if ($this->multi_insert)
{
return $this->sql_query('INSERT INTO ' . $table . ' ' . ' (' . implode(', ', array_keys($sql_ary[0])) . ') VALUES ' . implode(', ', $ary));
}
return true; return true;
} }