From 95bf5e88b86731a69ac453f9ce6d1dd168484573 Mon Sep 17 00:00:00 2001 From: Daniel Barbero Date: Mon, 3 Oct 2022 15:33:01 +0200 Subject: [PATCH] force insert with primary key pandora_enterprise#9536 --- pandora_console/include/db/mysql.php | 35 ++++++++++-- pandora_console/include/functions_db.php | 73 +++++++++++++++++------- 2 files changed, 83 insertions(+), 25 deletions(-) diff --git a/pandora_console/include/db/mysql.php b/pandora_console/include/db/mysql.php index 22690dee30..6b15b2823d 100644 --- a/pandora_console/include/db/mysql.php +++ b/pandora_console/include/db/mysql.php @@ -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 */ -function mysql_db_process_sql_insert($table, $values) +function mysql_db_process_sql_insert($table, $values, $sqltostring=false) { - // Empty rows or values not processed - if (empty($values)) { + global $config; + + // Empty rows or values not processed. + if (empty($values) === true) { return false; } @@ -368,7 +370,7 @@ function mysql_db_process_sql_insert($table, $values) $i = 1; $max = count($values); foreach ($values as $field => $value) { - // Add the correct escaping to values + // Add the correct escaping to values. if ($field[0] != '`') { $field = '`'.$field.'`'; } @@ -396,7 +398,30 @@ function mysql_db_process_sql_insert($table, $values) $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 + ); } diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index 2d8525a412..a519c317c2 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -1338,30 +1338,42 @@ function db_get_cached_queries() * This function comes back with an array in case of SELECT * in case of UPDATE, DELETE etc. with affected rows * 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. + * 'affected_rows' will return mysql_affected_rows (default value) + * 'insert_id' will return the ID of an autoincrement value + * '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 boolean $autocommit Set autocommit transaction mode true/false (Only oracle). + * @param array $values Values (Only type insert). * - * @param string What type of info to return in case of INSERT/UPDATE. - * 'affected_rows' will return mysql_affected_rows (default value) - * 'insert_id' will return the ID of an autoincrement value - * 'info' will return the full (debug) information of a query - * - * @param string $status The status and type of query (support only postgreSQL). - * - * @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; $rc = false; switch ($config['dbtype']) { case 'mysql': default: - $rc = @mysql_db_process_sql($sql, $rettype, $dbconnection, $cache); + $rc = @mysql_db_process_sql( + $sql, + $rettype, + $dbconnection, + $cache + ); break; case 'postgresql': @@ -1373,7 +1385,13 @@ function db_process_sql($sql, $rettype='affected_rows', $dbconnection='', $cache break; } - db_sync($dbconnection, $sql, $rc); + db_sync( + $dbconnection, + $sql, + $rc, + $rettype, + $values_insert + ); return $rc; } @@ -1388,8 +1406,13 @@ function db_process_sql($sql, $rettype='affected_rows', $dbconnection='', $cache * * @return void */ -function db_sync($dbconnection, $sql, $rc) -{ +function db_sync( + $dbconnection, + $sql, + $rc, + $rettype='affected_rows', + $values_insert=[] +) { global $config; if (enterprise_hook('is_metaconsole') === true && isset($config['centralized_management']) === true @@ -1401,6 +1424,16 @@ function db_sync($dbconnection, $sql, $rc) // Synchronize changes to nodes if needed. $sync = new Synchronizer(); 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) { // Launch events per failed query. $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 */ -function db_process_sql_insert($table, $values, $autocommit=true) +function db_process_sql_insert($table, $values, $autocommit=true, $sqltostring=false) { global $config; switch ($config['dbtype']) { case 'mysql': - return mysql_db_process_sql_insert($table, $values); + return mysql_db_process_sql_insert($table, $values, $sqltostring); break; case 'postgresql':