diff options
Diffstat (limited to 'includes')
-rw-r--r-- | includes/database.mysql-common.inc | 12 | ||||
-rw-r--r-- | includes/database.mysql.inc | 25 | ||||
-rw-r--r-- | includes/database.mysqli.inc | 19 | ||||
-rw-r--r-- | includes/database.pgsql.inc | 24 | ||||
-rw-r--r-- | includes/file.inc | 4 | ||||
-rw-r--r-- | includes/form.inc | 5 | ||||
-rw-r--r-- | includes/menu.inc | 60 |
7 files changed, 50 insertions, 99 deletions
diff --git a/includes/database.mysql-common.inc b/includes/database.mysql-common.inc index 80f78ff36..99fde133e 100644 --- a/includes/database.mysql-common.inc +++ b/includes/database.mysql-common.inc @@ -448,6 +448,18 @@ function db_update_field(&$ret, $table, $field) { } /** + * Returns the last insert id. + * + * @param $table + * The name of the table you inserted into. + * @param $field + * The name of the autoincrement field. + */ +function db_last_insert_id($table, $field) { + return db_result(db_query('SELECT LAST_INSERT_ID()')); +} + +/** * @} End of "ingroup schemaapi". */ diff --git a/includes/database.mysql.inc b/includes/database.mysql.inc index 45426fff2..c3c6b0710 100644 --- a/includes/database.mysql.inc +++ b/includes/database.mysql.inc @@ -47,12 +47,6 @@ function db_version() { /** * Initialize a database connection. - * - * Note that you can change the mysql_connect() call to mysql_pconnect() if you - * want to use persistent connections. This is not recommended on shared hosts, - * and might require additional database/webserver tuning. It can increase - * performance, however, when the overhead to connect to your database is high - * (e.g. your database and web server live on different machines). */ function db_connect($url) { $url = parse_url($url); @@ -239,25 +233,6 @@ function db_error() { } /** - * Return a new unique ID in the given sequence. - * - * For compatibility reasons, Drupal does not use auto-numbered fields in its - * database tables. Instead, this function is used to return a new unique ID - * of the type requested. If necessary, a new sequence with the given name - * will be created. - * - * Note that the table name should be in curly brackets to preserve compatibility - * with table prefixes. For example, db_next_id('{node}_nid'); - */ -function db_next_id($name) { - global $active_db; - $name = db_prefix_tables($name); - db_query('INSERT INTO {sequences} VALUES ("%s", LAST_INSERT_ID(1)) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id + 1)', $name); - - return mysql_insert_id($active_db); -} - -/** * Determine the number of rows changed by the preceding query. */ function db_affected_rows() { diff --git a/includes/database.mysqli.inc b/includes/database.mysqli.inc index baac124de..3bc058da8 100644 --- a/includes/database.mysqli.inc +++ b/includes/database.mysqli.inc @@ -231,25 +231,6 @@ function db_error() { } /** - * Return a new unique ID in the given sequence. - * - * For compatibility reasons, Drupal does not use auto-numbered fields in its - * database tables. Instead, this function is used to return a new unique ID - * of the type requested. If necessary, a new sequence with the given name - * will be created. - * - * Note that the table name should be in curly brackets to preserve compatibility - * with table prefixes. For example, db_next_id('{node}_nid'); - */ -function db_next_id($name) { - global $active_db; - $name = db_prefix_tables($name); - db_query('INSERT INTO {sequences} VALUES ("%s", LAST_INSERT_ID(1)) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id + 1)', $name); - - return mysqli_insert_id($active_db); -} - -/** * Determine the number of rows changed by the preceding query. */ function db_affected_rows() { diff --git a/includes/database.pgsql.inc b/includes/database.pgsql.inc index deb6bfe58..399a72178 100644 --- a/includes/database.pgsql.inc +++ b/includes/database.pgsql.inc @@ -43,12 +43,6 @@ function db_version() { /** * Initialize a database connection. - * - * Note that you can change the pg_connect() call to pg_pconnect() if you - * want to use persistent connections. This is not recommended on shared hosts, - * and might require additional database/webserver tuning. It can increase - * performance, however, when the overhead to connect to your database is high - * (e.g. your database and web server live on different machines). */ function db_connect($url) { // Check if MySQL support is present in PHP @@ -258,19 +252,15 @@ function db_error() { } /** - * Return a new unique ID in the given sequence. - * - * For compatibility reasons, Drupal does not use auto-numbered fields in its - * database tables. Instead, this function is used to return a new unique ID - * of the type requested. If necessary, a new sequence with the given name - * will be created. + * Returns the last insert id. This function is thread safe. * - * Note that the table name should be in curly brackets to preserve compatibility - * with table prefixes. For example, db_next_id('{node}_nid'); + * @param $table + * The name of the table you inserted into. + * @param $field + * The name of the autoincrement field. */ -function db_next_id($name) { - $id = db_result(db_query("SELECT nextval('%s_seq')", db_prefix_tables($name))); - return $id; +function db_last_insert_id($table, $field) { + return db_result(db_query("SELECT currval('%s_seq')", db_prefix_tables('{'. $table .'}') . '_'. $field)); } /** diff --git a/includes/file.inc b/includes/file.inc index 9bf86930b..32f331b5a 100644 --- a/includes/file.inc +++ b/includes/file.inc @@ -574,8 +574,8 @@ function file_save_upload($source, $validators = array(), $dest = FALSE, $replac } // If we made it this far it's safe to record this file in the database. - $file->fid = db_next_id('{files}_fid'); - db_query("INSERT INTO {files} (fid, uid, filename, filepath, filemime, filesize, status, timestamp) VALUES (%d, %d, '%s', '%s', '%s', %d, %d, %d)", $file->fid, $user->uid, $file->filename, $file->filepath, $file->filemime, $file->filesize, FILE_STATUS_TEMPORARY, time()); + db_query("INSERT INTO {files} (uid, filename, filepath, filemime, filesize, status, timestamp) VALUES (%d, '%s', '%s', '%s', %d, %d, %d)", $user->uid, $file->filename, $file->filepath, $file->filemime, $file->filesize, FILE_STATUS_TEMPORARY, time()); + $file->fid = db_last_insert_id('files', 'fid'); // Add file to the cache. $upload_cache[$source] = $file; diff --git a/includes/form.inc b/includes/form.inc index 81ee1ea16..1bd9542b6 100644 --- a/includes/form.inc +++ b/includes/form.inc @@ -1867,7 +1867,6 @@ function batch_set($batch_definition) { // Initialize the batch if (empty($batch)) { $batch = array( - 'id' => db_next_id('{batch}_bid'), 'sets' => array(), ); } @@ -1948,7 +1947,9 @@ function batch_process($redirect = NULL, $url = NULL) { $batch['destination'] = $_REQUEST['edit']['destination']; unset($_REQUEST['edit']['destination']); } - db_query("INSERT INTO {batch} (bid, token, timestamp, batch) VALUES (%d, %d, %d, '%s')", $batch['id'], drupal_get_token($batch['id']), time(), serialize($batch)); + db_query('INSERT INTO {batch} (timestamp) VALUES (%d)', time()); + $batch['id'] = db_last_insert_id('batch', 'bid'); + db_query("UPDATE {batch} SET token = '%s', batch = '%s' WHERE bid = %d", drupal_get_token($batch['id']), serialize($batch), $batch['id']); drupal_goto($batch['url'], 'op=start&id='. $batch['id']); } else { diff --git a/includes/menu.inc b/includes/menu.inc index 9bb816276..888715548 100644 --- a/includes/menu.inc +++ b/includes/menu.inc @@ -1320,10 +1320,7 @@ function menu_link_save(&$item) { $existing_item = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE menu_name = '%s' AND link_path = '%s'", $menu_name, $item['link_path'])); } - if (!$existing_item) { - $item['mlid'] = db_next_id('{menu_links}_mlid'); - } - else { + if ($existing_item) { $item['mlid'] = $existing_item['mlid']; } @@ -1346,6 +1343,21 @@ function menu_link_save(&$item) { else { $item['plid'] = $parent['mlid']; } + db_query("INSERT INTO {menu_links} ( + menu_name, plid, link_path, + hidden, external, has_children, + expanded, weight, + module, link_title, options) VALUES ( + '%s', %d, '%s', + %d, %d, %d, + %d, %d, + '%s', '%s', '%s')", + $item['menu_name'], $item['plid'], $item['link_path'], + $item['hidden'], $item['_external'], $item['has_children'], + $item['expanded'], $item['weight'], + $item['module'], $item['link_title'], serialize($item['options'])); + $item['mlid'] = db_last_insert_id('menu_links', 'mlid'); + if (!$item['plid']) { $item['p1'] = $item['mlid']; @@ -1390,36 +1402,16 @@ function menu_link_save(&$item) { } } } - if ($existing_item) { - db_query("UPDATE {menu_links} SET menu_name = '%s', plid = %d, link_path = '%s', - router_path = '%s', hidden = %d, external = %d, has_children = %d, - expanded = %d, weight = %d, depth = %d, - p1 = %d, p2 = %d, p3 = %d, p4 = %d, p5 = %d, p6 = %d, - module = '%s', link_title = '%s', options = '%s' WHERE mlid = %d", - $item['menu_name'], $item['plid'], $item['link_path'], - $item['router_path'], $item['hidden'], $item['_external'], $item['has_children'], - $item['expanded'], $item['weight'], $item['depth'], - $item['p1'], $item['p2'], $item['p3'], $item['p4'], $item['p5'], $item['p6'], - $item['module'], $item['link_title'], serialize($item['options']), $item['mlid']); - } - else { - db_query("INSERT INTO {menu_links} ( - menu_name, mlid, plid, link_path, - router_path, hidden, external, has_children, - expanded, weight, depth, - p1, p2, p3, p4, p5, p6, - module, link_title, options) VALUES ( - '%s', %d, %d, '%s', - '%s', %d, %d, %d, - %d, %d, %d, - %d, %d, %d, %d, %d, %d, - '%s', '%s', '%s')", - $item['menu_name'], $item['mlid'], $item['plid'], $item['link_path'], - $item['router_path'], $item['hidden'], $item['_external'], $item['has_children'], - $item['expanded'], $item['weight'], $item['depth'], - $item['p1'], $item['p2'], $item['p3'], $item['p4'], $item['p5'], $item['p6'], - $item['module'], $item['link_title'], serialize($item['options'])); - } + db_query("UPDATE {menu_links} SET menu_name = '%s', plid = %d, link_path = '%s', + router_path = '%s', hidden = %d, external = %d, has_children = %d, + expanded = %d, weight = %d, depth = %d, + p1 = %d, p2 = %d, p3 = %d, p4 = %d, p5 = %d, p6 = %d, + module = '%s', link_title = '%s', options = '%s' WHERE mlid = %d", + $item['menu_name'], $item['plid'], $item['link_path'], + $item['router_path'], $item['hidden'], $item['_external'], $item['has_children'], + $item['expanded'], $item['weight'], $item['depth'], + $item['p1'], $item['p2'], $item['p3'], $item['p4'], $item['p5'], $item['p6'], + $item['module'], $item['link_title'], serialize($item['options']), $item['mlid']); // Check the has_children status of the parent. if ($item['plid']) { $parent_has_children = (bool)db_result(db_query("SELECT COUNT(*) FROM {menu_links} WHERE plid = %d AND hidden = 0", $item['plid'])); |