force insert with primary key pandora_enterprise#9536

This commit is contained in:
Daniel Barbero 2022-10-03 15:33:01 +02:00
parent ea83b3cbe6
commit 95bf5e88b8
2 changed files with 83 additions and 25 deletions

View File

@ -353,10 +353,12 @@ function mysql_db_get_all_rows_in_table($table, $order_field='', $order='ASC')
* *
* @return mixed False in case of error or invalid values passed. Affected rows otherwise * @return mixed False in case of error or invalid values passed. Affected rows otherwise
*/ */
function mysql_db_process_sql_insert($table, $values) function mysql_db_process_sql_insert($table, $values, $sqltostring=false)
{ {
// Empty rows or values not processed global $config;
if (empty($values)) {
// Empty rows or values not processed.
if (empty($values) === true) {
return false; return false;
} }
@ -368,7 +370,7 @@ function mysql_db_process_sql_insert($table, $values)
$i = 1; $i = 1;
$max = count($values); $max = count($values);
foreach ($values as $field => $value) { foreach ($values as $field => $value) {
// Add the correct escaping to values // Add the correct escaping to values.
if ($field[0] != '`') { if ($field[0] != '`') {
$field = '`'.$field.'`'; $field = '`'.$field.'`';
} }
@ -396,7 +398,30 @@ function mysql_db_process_sql_insert($table, $values)
$query .= ' VALUES ('.$values_str.')'; $query .= ' VALUES ('.$values_str.')';
return db_process_sql($query, 'insert_id'); $values_insert = [];
if (enterprise_hook('is_metaconsole') === true
&& isset($config['centralized_management']) === true
&& (bool) $config['centralized_management'] === true
) {
$values_insert = [
'table' => $table,
'values' => $values,
];
}
if ($sqltostring === true) {
return $query;
}
return db_process_sql(
$query,
'insert_id',
'',
true,
$status,
true,
$values_insert
);
} }

View File

@ -1338,30 +1338,42 @@ function db_get_cached_queries()
* This function comes back with an array in case of SELECT * This function comes back with an array in case of SELECT
* in case of UPDATE, DELETE etc. with affected rows * in case of UPDATE, DELETE etc. with affected rows
* an empty array in case of SELECT without results * an empty array in case of SELECT without results
* Queries that return data will be cached so queries don't get repeated * Queries that return data will be cached so queries don't get repeated.
* *
* @param string SQL statement to execute * @param string $sql SQL statement to execute.
* * @param string $rettype What type of info to return in case of INSERT/UPDATE.
* @param string What type of info to return in case of INSERT/UPDATE.
* 'affected_rows' will return mysql_affected_rows (default value) * 'affected_rows' will return mysql_affected_rows (default value)
* 'insert_id' will return the ID of an autoincrement value * 'insert_id' will return the ID of an autoincrement value
* 'info' will return the full (debug) information of a query * 'info' will return the full (debug) information of a query.
* * @param string $dbconnection Info conecction.
* @param boolean $cache Cache.
* @param string $status The status and type of query (support only postgreSQL). * @param string $status The status and type of query (support only postgreSQL).
* @param boolean $autocommit Set autocommit transaction mode true/false (Only oracle).
* @param array $values Values (Only type insert).
* *
* @param boolean $autocommit (Only oracle) Set autocommit transaction mode true/false * @return mixed An array with the rows, columns and values in a multidimensional array or false in error.
*
* @return mixed An array with the rows, columns and values in a multidimensional array or false in error
*/ */
function db_process_sql($sql, $rettype='affected_rows', $dbconnection='', $cache=true, &$status=null, $autocommit=true) function db_process_sql(
{ $sql,
$rettype='affected_rows',
$dbconnection='',
$cache=true,
&$status=null,
$autocommit=true,
$values_insert=[]
) {
global $config; global $config;
$rc = false; $rc = false;
switch ($config['dbtype']) { switch ($config['dbtype']) {
case 'mysql': case 'mysql':
default: default:
$rc = @mysql_db_process_sql($sql, $rettype, $dbconnection, $cache); $rc = @mysql_db_process_sql(
$sql,
$rettype,
$dbconnection,
$cache
);
break; break;
case 'postgresql': case 'postgresql':
@ -1373,7 +1385,13 @@ function db_process_sql($sql, $rettype='affected_rows', $dbconnection='', $cache
break; break;
} }
db_sync($dbconnection, $sql, $rc); db_sync(
$dbconnection,
$sql,
$rc,
$rettype,
$values_insert
);
return $rc; return $rc;
} }
@ -1388,8 +1406,13 @@ function db_process_sql($sql, $rettype='affected_rows', $dbconnection='', $cache
* *
* @return void * @return void
*/ */
function db_sync($dbconnection, $sql, $rc) function db_sync(
{ $dbconnection,
$sql,
$rc,
$rettype='affected_rows',
$values_insert=[]
) {
global $config; global $config;
if (enterprise_hook('is_metaconsole') === true if (enterprise_hook('is_metaconsole') === true
&& isset($config['centralized_management']) === true && isset($config['centralized_management']) === true
@ -1401,6 +1424,16 @@ function db_sync($dbconnection, $sql, $rc)
// Synchronize changes to nodes if needed. // Synchronize changes to nodes if needed.
$sync = new Synchronizer(); $sync = new Synchronizer();
if ($sync !== null) { if ($sync !== null) {
if ($rettype === 'insert_id') {
$forceSql = $sync->updateInsertQueryAddPrimaryKey(
$values_insert,
$rc
);
if (empty($forceSql) === false) {
$sql = $forceSql;
}
}
if ($sync->queue($sql, $rc) === false) { if ($sync->queue($sql, $rc) === false) {
// Launch events per failed query. // Launch events per failed query.
$errors = $sync->getLatestErrors(); $errors = $sync->getLatestErrors();
@ -1696,13 +1729,13 @@ function db_process_delete_temp($table, $row, $value, $custom_value=false)
* *
* @return mixed False in case of error or invalid values passed. Affected rows otherwise * @return mixed False in case of error or invalid values passed. Affected rows otherwise
*/ */
function db_process_sql_insert($table, $values, $autocommit=true) function db_process_sql_insert($table, $values, $autocommit=true, $sqltostring=false)
{ {
global $config; global $config;
switch ($config['dbtype']) { switch ($config['dbtype']) {
case 'mysql': case 'mysql':
return mysql_db_process_sql_insert($table, $values); return mysql_db_process_sql_insert($table, $values, $sqltostring);
break; break;
case 'postgresql': case 'postgresql':