Only in drupal-6.x-dev-db-lock-patch: .DS_Store Only in drupal-6.x-dev-db-lock-patch/includes: .DS_Store diff -urp drupal-6.x-dev/includes/bootstrap.inc drupal-6.x-dev-db-lock-patch/includes/bootstrap.inc --- drupal-6.x-dev/includes/bootstrap.inc 2007-05-08 11:36:55.000000000 -0500 +++ drupal-6.x-dev-db-lock-patch/includes/bootstrap.inc 2007-05-22 12:11:06.000000000 -0500 @@ -444,10 +444,11 @@ function variable_get($name, $default) { function variable_set($name, $value) { global $conf; - db_lock_table('variable'); - db_query("DELETE FROM {variable} WHERE name = '%s'", $name); - db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, serialize($value)); - db_unlock_tables(); + $serialized_value = serialize($value); + db_query_replace( + array('INSERT INTO {variable} (name, value) VALUES ("%s", "%s")', $name, $serialized_value), + array('UPDATE {variable} SET value = "%s" WHERE name = "%s"', $serialized_value, $name), + ); cache_clear_all('variables', 'cache'); diff -urp drupal-6.x-dev/includes/cache.inc drupal-6.x-dev-db-lock-patch/includes/cache.inc --- drupal-6.x-dev/includes/cache.inc 2007-05-12 13:26:15.000000000 -0500 +++ drupal-6.x-dev-db-lock-patch/includes/cache.inc 2007-05-22 12:10:19.000000000 -0500 @@ -104,12 +104,11 @@ function cache_set($cid, $data, $table = $data = serialize($data); $serialized = 1; } - db_lock_table($table); - db_query("UPDATE {". $table ."} SET data = %b, created = %d, expire = %d, headers = '%s', serialized = %d WHERE cid = '%s'", $data, time(), $expire, $headers, $serialized, $cid); - if (!db_affected_rows()) { - @db_query("INSERT INTO {". $table ."} (cid, data, created, expire, headers, serialized) VALUES ('%s', %b, %d, %d, '%s', %d)", $cid, $data, time(), $expire, $headers, $serialized); - } - db_unlock_tables(); + $created = time(); + db_query_replace( + array('INSERT INTO {%s} (cid, data, created, expire, headers) VALUES ("%s", %b, %d, %d, "%s")', $table, $cid, $data, $created, $expire, $headers), + array('UPDATE {%s} SET data = %b, created = %d, expire = %d, headers = "%s" WHERE cid = "%s"', $table, $data, $created, $expire, $headers, $cid), + ); } /** diff -urp drupal-6.x-dev/includes/database.inc drupal-6.x-dev-db-lock-patch/includes/database.inc --- drupal-6.x-dev/includes/database.inc 2007-05-08 11:36:55.000000000 -0500 +++ drupal-6.x-dev-db-lock-patch/includes/database.inc 2007-05-22 11:44:54.000000000 -0500 @@ -170,6 +170,23 @@ function _db_query_callback($match, $ini define('DB_QUERY_REGEXP', '/(%d|%s|%%|%f|%b)/'); /** + * Prepares a query by prefixing table names and escaping arguments. + * + * @param $query + * A string containing an SQL query. + * @param $args + * An array of arguments to integrate into the SQL query. + * + * @return + * An SQL query with prefixed table names and escaped arguments. + */ +function _db_query_prepare($query, $args) { + $query = db_prefix_tables($query); + _db_query_callback($args, TRUE); + return preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query); +} + +/** * Runs a basic query in the active database. * * User-supplied arguments to the query should be passed in as separate @@ -196,12 +213,10 @@ define('DB_QUERY_REGEXP', '/(%d|%s|%%|%f function db_query($query) { $args = func_get_args(); array_shift($args); - $query = db_prefix_tables($query); if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax $args = $args[0]; } - _db_query_callback($args, TRUE); - $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query); + $query = _db_query_prepare($query, $args); return _db_query($query); } diff -urp drupal-6.x-dev/includes/database.mysql.inc drupal-6.x-dev-db-lock-patch/includes/database.mysql.inc --- drupal-6.x-dev/includes/database.mysql.inc 2007-05-08 11:36:55.000000000 -0500 +++ drupal-6.x-dev-db-lock-patch/includes/database.mysql.inc 2007-05-22 12:54:48.000000000 -0500 @@ -248,12 +248,13 @@ function db_error() { * with table prefixes. For example, db_next_id('{node}_nid'); */ function db_next_id($name) { - $name = db_prefix_tables($name); - db_query('LOCK TABLES {sequences} WRITE'); - $id = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $name)) + 1; - db_query("REPLACE INTO {sequences} VALUES ('%s', %d)", $name, $id); - db_query('UNLOCK TABLES'); - + global $active_db; + $name = db_prefix_tables($name); + db_query('INSERT INTO {sequences} VALUES ("%s", %d) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id + 1)', $name, 1); + $id = mysql_insert_id($active_db); + if ($id == 0) { + $id = 1; + } return $id; } @@ -435,7 +436,35 @@ function db_distinct_field($table, $fiel } /** - * @} End of "ingroup database". - */ + * Perform a row INSERT or UPDATE. + * + * @param $insert + * An array, where the first value is a query that inserts a new row, + * subsequent values are arguments. Same syntax as db_query. + * @param $update + * An array, where the first value is a query that updates an existing row, + * subsequent values are arguments. Same syntax as db_query. + */ +function db_query_replace($insert, $update) { + // Queries must be prepared separately to properly handle extra supplied args + + // Prepare the INSERT query + $insert_sql = array_shift($insert); + $insert_sql = _db_query_prepare($insert_sql, $insert); + + // Prepare the UPDATE query + $update_sql = array_shift($update); + $update_sql = _db_query_prepare($update_sql, $update); + + // Identify the SET clause of the UPDATE query + preg_match('SET (.*) WHERE', $update_sql, $matches); + $update_set = $matches[1]; + $sql = $insert . ' ON DUPLICATE KEY UPDATE ' . $update_set; + + return db_query($sql); +} +/** + * @} End of "ingroup database". + */ diff -urp drupal-6.x-dev/includes/database.mysqli.inc drupal-6.x-dev-db-lock-patch/includes/database.mysqli.inc --- drupal-6.x-dev/includes/database.mysqli.inc 2007-04-21 13:08:41.000000000 -0500 +++ drupal-6.x-dev-db-lock-patch/includes/database.mysqli.inc 2007-05-22 12:54:42.000000000 -0500 @@ -239,12 +239,13 @@ function db_error() { * with table prefixes. For example, db_next_id('{node}_nid'); */ function db_next_id($name) { - $name = db_prefix_tables($name); - db_query('LOCK TABLES {sequences} WRITE'); - $id = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $name)) + 1; - db_query("REPLACE INTO {sequences} VALUES ('%s', %d)", $name, $id); - db_query('UNLOCK TABLES'); - + global $active_db; + $name = db_prefix_tables($name); + db_query('INSERT INTO {sequences} VALUES ("%s", %d) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id + 1)', $name, 1); + $id = mysqli_insert_id($active_db); + if ($id == 0) { + $id = 1; + } return $id; } @@ -426,6 +427,35 @@ function db_distinct_field($table, $fiel } /** + * Perform a row INSERT or UPDATE. + * + * @param $insert + * An array, where the first value is a query that inserts a new row, + * subsequent values are arguments. Same syntax as db_query. + * @param $update + * An array, where the first value is a query that updates an existing row, + * subsequent values are arguments. Same syntax as db_query. + */ +function db_query_replace($insert, $update) { + // Queries must be prepared separately to properly handle extra supplied args + + // Prepare the INSERT query + $insert_sql = array_shift($insert); + $insert_sql = _db_query_prepare($insert_sql, $insert); + + // Prepare the UPDATE query + $update_sql = array_shift($update); + $update_sql = _db_query_prepare($update_sql, $update); + + // Identify the SET clause of the UPDATE query + preg_match('SET (.*) WHERE', $update_sql, $matches); + $update_set = $matches[1]; + $sql = $insert . ' ON DUPLICATE KEY UPDATE ' . $update_set; + + return db_query($sql); +} + +/** * @} End of "ingroup database". */ diff -urp drupal-6.x-dev/includes/database.pgsql.inc drupal-6.x-dev-db-lock-patch/includes/database.pgsql.inc --- drupal-6.x-dev/includes/database.pgsql.inc 2007-04-21 13:08:41.000000000 -0500 +++ drupal-6.x-dev-db-lock-patch/includes/database.pgsql.inc 2007-05-22 12:40:41.000000000 -0500 @@ -433,6 +433,28 @@ function db_distinct_field($table, $fiel } /** + * Perform a row INSERT or UPDATE. + * + * @param $insert + * An array, where the first value is a query that inserts a new row, + * subsequent values are arguments. Same syntax as db_query. + * @param $update + * An array, where the first value is a query that updates an existing row, + * subsequent values are arguments. Same syntax as db_query. + */ +function db_query_replace($insert, $update) { + // Extract the INSERT query SQL + $insert_sql = trim(array_shift($insert), ';'); + + // Prepare the UPDATE query + // The UPDATE SQL must be prepared early to handle extra arguments + $update_sql = trim(array_shift($update), ';'); + $update_sql = _db_query_prepare($update_sql, $update); + + db_query($update_sql . '; IF NOT FOUND THEN ' . $insert_sql . '; END IF;', $insert); +} + +/** * @} End of "ingroup database". */