From 54dd165917b085bd13863e1f765cdaa2a26af447 Mon Sep 17 00:00:00 2001 From: Louis Rossouw Date: Thu, 16 Jul 2015 00:15:27 +0200 Subject: [PATCH 1/4] Create dbBulkInsert which allows bulk insertion of records. --- includes/dbFacile.php | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/includes/dbFacile.php b/includes/dbFacile.php index ca661def0..90d7c6762 100644 --- a/includes/dbFacile.php +++ b/includes/dbFacile.php @@ -108,6 +108,63 @@ function dbInsert($data, $table) { }//end dbInsert() +/* + * Passed an array and a table name, it attempts to insert the data into the table. + * $data is an array (rows) of key value pairs. keys are fields. Rows need to have same fields. + * Check for boolean false to determine whether insert failed + * */ + + +function dbBulkInsert($data, $table) { + global $fullSql; + global $db_stats; + // the following block swaps the parameters if they were given in the wrong order. + // it allows the method to work for those that would rather it (or expect it to) + // follow closer with SQL convention: + // insert into the TABLE this DATA + if (is_string($data) && is_array($table)) { + $tmp = $data; + $data = $table; + $table = $tmp; + // trigger_error('QDB - Parameters passed to insert() were in reverse order, but it has been allowed', E_USER_NOTICE); + } + if (count($data) === 0) { + return false; + } + if (count($data[0]) === 0) { + return false; + } + + $sql = 'INSERT INTO `'.$table.'` (`'.implode('`,`', array_keys($data[0])).'`) VALUES '; + $values =''; + + foreach ($data as $row) { + if ($values != '') { + $values .= ','; + } + $rowvalues=''; + foreach ($row as $key => $value) { + if ($rowvalues != '') { + $rowvalues .= ','; + } + $rowvalues .= "'".mres($value)."'"; + } + $values .= "(".$rowvalues.")"; + } + + $time_start = microtime(true); + $result = dbQuery($sql.$values); + + // logfile($fullSql); + $time_end = microtime(true); + $db_stats['insert_sec'] += number_format(($time_end - $time_start), 8); + $db_stats['insert']++; + + return $id; + +}//end dbBulkInsert() + + /* * Passed an array, table name, WHERE clause, and placeholder parameters, it attempts to update a record. * Returns the number of affected rows From 650369603df9240fc74fdc872f3de234f0158607 Mon Sep 17 00:00:00 2001 From: Louis Rossouw Date: Thu, 16 Jul 2015 00:16:25 +0200 Subject: [PATCH 2/4] Revise unix-agent (processes section) to use dbBulkInsert. --- includes/polling/unix-agent.inc.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/includes/polling/unix-agent.inc.php b/includes/polling/unix-agent.inc.php index 174973383..a759313a4 100644 --- a/includes/polling/unix-agent.inc.php +++ b/includes/polling/unix-agent.inc.php @@ -112,20 +112,16 @@ if ($device['os_group'] == 'unix') { if (!empty($agent_data['ps'])) { echo 'Processes: '; dbDelete('processes', 'device_id = ?', array($device['device_id'])); - $query = "INSERT INTO `processes` (`device_id`,`pid`,`user`,`vsz`,`rss`,`cputime`,`command`) VALUES "; - $values = ""; + $data=array(); foreach (explode("\n", $agent_data['ps']) as $process) { $process = preg_replace('/\((.*),([0-9]*),([0-9]*),([0-9\:]*),([0-9]*)\)\ (.*)/', '\\1|\\2|\\3|\\4|\\5|\\6', $process); list($user, $vsz, $rss, $cputime, $pid, $command) = explode('|', $process, 6); if (!empty($command)) { - if ($values != "") { - $values .= ","; - } - $values .= "('".mres($device['device_id'])."','".mres($pid)."','".mres($user)."','".mres($vsz)."','".mres($rss)."','".mres($cputime)."','".mres($command)."')"; + $data[]=array('device_id' => $device['device_id'], 'pid' => $pid, 'user' => $user, 'vsz' => $vsz, 'rss' => $rss, 'cputime' => $cputime, 'command' => $command); } } - if ($values != "") { - dbQuery($query.$values); + if (count($data) > 0) { + dbBulkInsert('processes',$data); } echo "\n"; } From c975b275150d2ae01e4370edb3d80e61770594ee Mon Sep 17 00:00:00 2001 From: Louis Rossouw Date: Fri, 17 Jul 2015 06:11:01 +0200 Subject: [PATCH 3/4] Remove fullsql and comment. --- includes/dbFacile.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/includes/dbFacile.php b/includes/dbFacile.php index 90d7c6762..06203ef39 100644 --- a/includes/dbFacile.php +++ b/includes/dbFacile.php @@ -116,7 +116,6 @@ function dbInsert($data, $table) { function dbBulkInsert($data, $table) { - global $fullSql; global $db_stats; // the following block swaps the parameters if they were given in the wrong order. // it allows the method to work for those that would rather it (or expect it to) @@ -126,7 +125,6 @@ function dbBulkInsert($data, $table) { $tmp = $data; $data = $table; $table = $tmp; - // trigger_error('QDB - Parameters passed to insert() were in reverse order, but it has been allowed', E_USER_NOTICE); } if (count($data) === 0) { return false; From c8a5475c08a122c341c607a6add0176051267327 Mon Sep 17 00:00:00 2001 From: Louis Rossouw Date: Fri, 17 Jul 2015 06:15:57 +0200 Subject: [PATCH 4/4] Fix return in dbBulkInsert. --- includes/dbFacile.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/dbFacile.php b/includes/dbFacile.php index 06203ef39..0d1d7150c 100644 --- a/includes/dbFacile.php +++ b/includes/dbFacile.php @@ -158,7 +158,7 @@ function dbBulkInsert($data, $table) { $db_stats['insert_sec'] += number_format(($time_end - $time_start), 8); $db_stats['insert']++; - return $id; + return $result; }//end dbBulkInsert()