Index: branches/RC/core/kernel/db/db_connection.php =================================================================== diff -u -N -r11408 -r11711 --- branches/RC/core/kernel/db/db_connection.php (.../db_connection.php) (revision 11408) +++ branches/RC/core/kernel/db/db_connection.php (.../db_connection.php) (revision 11711) @@ -617,21 +617,46 @@ return $this->errorMessage; } - function doInsert($fields_hash, $table, $type = 'INSERT') + /** + * Performs insert of given data (useful with small number of queries) + * or stores it to perform multiple insert later (useful with large number of queries) + * + * @param Array $fields_hash + * @param string $table + * @param string $type + * @param bool $insert_now + * @return bool + */ + function doInsert($fields_hash, $table, $type = 'INSERT', $insert_now = true) { - $fields_sql = ''; + static $value_sqls = Array (); + + if ($insert_now) { + $fields_sql = '`' . implode('`,`', array_keys($fields_hash)) . '`'; + } + $values_sql = ''; foreach ($fields_hash as $field_name => $field_value) { - $fields_sql .= '`'.$field_name.'`,'; $values_sql .= ( is_null($field_value) ? 'NULL' : $this->qstr($field_value) ) . ','; } // don't use preg here, as it may fail when string is too long - $fields_sql = rtrim($fields_sql, ','); - $values_sql = rtrim($values_sql, ','); - $sql = strtoupper($type).' INTO `'.$table.'` ('.$fields_sql.') VALUES ('.$values_sql.')'; + $value_sqls[] = rtrim($values_sql, ','); - return $this->ChangeQuery($sql); + $insert_result = true; + if ($insert_now) { + $insert_count = count($value_sqls); + if (($insert_count > 1) && ($value_sqls[$insert_count - 1] == $value_sqls[$insert_count - 2])) { + // last two records are the same + array_pop($value_sqls); + } + + $sql = strtoupper($type) . ' INTO `' . $table . '` (' . $fields_sql . ') VALUES (' . implode('),(', $value_sqls) . ')'; + $insert_result = $this->ChangeQuery($sql); + $value_sqls = Array (); + } + + return $insert_result; } function doUpdate($fields_hash, $table, $key_clause) @@ -643,8 +668,8 @@ $fields_sql .= '`'.$field_name.'` = ' . ( is_null($field_value) ? 'NULL' : $this->qstr($field_value) ) . ','; } - // don't use preg here! - $fields_sql = rtrim($fields_sql, ','); + // don't use preg here, as it may fail when string is too long + $fields_sql = rtrim($fields_sql, ','); $sql = 'UPDATE `'.$table.'` SET '.$fields_sql.' WHERE '.$key_clause;