From 9fb98201f209c8976d43b8003aabdabadda03932 Mon Sep 17 00:00:00 2001 From: lionel-rowe Date: Tue, 26 Apr 2022 15:00:38 +0100 Subject: [PATCH 1/2] [ticket/8071] Add sql_last_inserted_id alias for sql_nextid Fixes [DBAL function sql_nextid - name is misleading](https://tracker.phpbb.com/browse/PHPBB3-8071). Per the tracker issue: > This function gets the ID of the last inserted row, not the ID of the next row to be inserted (which is difficult due to different autoincrement steps and also lends itself nicely to race conditions). @iwisdom replied that the naming is consistent with how PHP itself names this functionality, but the link they posted doesn't support that (I guess this is referring to a much older version of PHP? The comment is from 2009). PHPBB3-8071 --- phpBB/phpbb/db/driver/driver_interface.php | 33 +++++++++++++++++++--- phpBB/phpbb/db/driver/factory.php | 12 ++++++-- phpBB/phpbb/db/driver/mssql_odbc.php | 12 ++++++-- phpBB/phpbb/db/driver/mssqlnative.php | 12 ++++++-- phpBB/phpbb/db/driver/mysqli.php | 12 ++++++-- phpBB/phpbb/db/driver/oracle.php | 12 ++++++-- phpBB/phpbb/db/driver/postgres.php | 12 ++++++-- phpBB/phpbb/db/driver/sqlite3.php | 14 +++++++-- 8 files changed, 100 insertions(+), 19 deletions(-) diff --git a/phpBB/phpbb/db/driver/driver_interface.php b/phpBB/phpbb/db/driver/driver_interface.php index e269fac585..7615030a5a 100644 --- a/phpBB/phpbb/db/driver/driver_interface.php +++ b/phpBB/phpbb/db/driver/driver_interface.php @@ -289,12 +289,37 @@ interface driver_interface public function cast_expr_to_bigint($expression); /** - * Get last inserted id after insert statement - * - * @return string Autoincrement value of the last inserted row - */ + * Gets the ID of the **last** inserted row immediately after an INSERT + * statement. + * + * **Note**: Despite the name, the returned ID refers to the row that has + * just been inserted, rather than the hypothetical ID of the next row if a + * new one was to be inserted. + * + * The returned value can be used for selecting the item that has just been + * inserted or for updating another table with an ID pointing to that item. + * + * Will be deprecated in a future version of phpBB in favor of + * `sql_last_inserted_id`. + * + * @return string|false Auto-incremented value of the last inserted row + */ public function sql_nextid(); + /** + * Gets the ID of the last inserted row immediately after an INSERT + * statement. The returned value can be used for selecting the item that has + * just been inserted or for updating another table with an ID pointing to + * that item. + * + * Alias of `sql_nextid`. + * + * @return string|false Auto-incremented value of the last inserted row + * + * @since 3.3.8-RC1 + */ + public function sql_last_inserted_id(); + /** * Add to query count * diff --git a/phpBB/phpbb/db/driver/factory.php b/phpBB/phpbb/db/driver/factory.php index b2a5707120..d8865dc524 100644 --- a/phpBB/phpbb/db/driver/factory.php +++ b/phpBB/phpbb/db/driver/factory.php @@ -314,13 +314,21 @@ class factory implements driver_interface } /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function sql_nextid() { return $this->get_driver()->sql_nextid(); } + /** + * {@inheritdoc} + */ + public function sql_last_inserted_id() + { + return $this->get_driver()->sql_nextid(); + } + /** * {@inheritdoc} */ diff --git a/phpBB/phpbb/db/driver/mssql_odbc.php b/phpBB/phpbb/db/driver/mssql_odbc.php index 06cdce7a15..6a08f1100f 100644 --- a/phpBB/phpbb/db/driver/mssql_odbc.php +++ b/phpBB/phpbb/db/driver/mssql_odbc.php @@ -269,8 +269,8 @@ class mssql_odbc extends \phpbb\db\driver\mssql_base } /** - * {@inheritDoc} - */ + * {@inheritDoc} + */ function sql_nextid() { $result_id = @odbc_exec($this->db_connect_id, 'SELECT @@IDENTITY'); @@ -289,6 +289,14 @@ class mssql_odbc extends \phpbb\db\driver\mssql_base return false; } + /** + * {@inheritdoc} + */ + public function sql_last_inserted_id() + { + return $this->sql_nextid(); + } + /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/db/driver/mssqlnative.php b/phpBB/phpbb/db/driver/mssqlnative.php index 30ef9d9bc4..c1955f8b84 100644 --- a/phpBB/phpbb/db/driver/mssqlnative.php +++ b/phpBB/phpbb/db/driver/mssqlnative.php @@ -271,8 +271,8 @@ class mssqlnative extends \phpbb\db\driver\mssql_base } /** - * {@inheritDoc} - */ + * {@inheritDoc} + */ function sql_nextid() { $result_id = @sqlsrv_query($this->db_connect_id, 'SELECT @@IDENTITY'); @@ -290,6 +290,14 @@ class mssqlnative extends \phpbb\db\driver\mssql_base } } + /** + * {@inheritdoc} + */ + public function sql_last_inserted_id() + { + return $this->sql_nextid(); + } + /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/db/driver/mysqli.php b/phpBB/phpbb/db/driver/mysqli.php index 1b7f6252f6..1c4a48cbb6 100644 --- a/phpBB/phpbb/db/driver/mysqli.php +++ b/phpBB/phpbb/db/driver/mysqli.php @@ -289,13 +289,21 @@ class mysqli extends \phpbb\db\driver\mysql_base } /** - * {@inheritDoc} - */ + * {@inheritDoc} + */ function sql_nextid() { return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false; } + /** + * {@inheritdoc} + */ + public function sql_last_inserted_id() + { + return $this->sql_nextid(); + } + /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/db/driver/oracle.php b/phpBB/phpbb/db/driver/oracle.php index 3f6bc49b35..c96201ef0c 100644 --- a/phpBB/phpbb/db/driver/oracle.php +++ b/phpBB/phpbb/db/driver/oracle.php @@ -570,8 +570,8 @@ class oracle extends \phpbb\db\driver\driver } /** - * {@inheritDoc} - */ + * {@inheritDoc} + */ function sql_nextid() { $query_id = $this->query_result; @@ -607,6 +607,14 @@ class oracle extends \phpbb\db\driver\driver return false; } + /** + * {@inheritdoc} + */ + public function sql_last_inserted_id() + { + return $this->sql_nextid(); + } + /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/db/driver/postgres.php b/phpBB/phpbb/db/driver/postgres.php index 3ee4b2b00e..0f87f5a8f0 100644 --- a/phpBB/phpbb/db/driver/postgres.php +++ b/phpBB/phpbb/db/driver/postgres.php @@ -342,8 +342,8 @@ class postgres extends \phpbb\db\driver\driver } /** - * {@inheritDoc} - */ + * {@inheritDoc} + */ function sql_nextid() { $query_id = $this->query_result; @@ -370,6 +370,14 @@ class postgres extends \phpbb\db\driver\driver return false; } + /** + * {@inheritdoc} + */ + public function sql_last_inserted_id() + { + return $this->sql_nextid(); + } + /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/db/driver/sqlite3.php b/phpBB/phpbb/db/driver/sqlite3.php index 61b87d86b5..638b9035ed 100644 --- a/phpBB/phpbb/db/driver/sqlite3.php +++ b/phpBB/phpbb/db/driver/sqlite3.php @@ -242,13 +242,21 @@ class sqlite3 extends \phpbb\db\driver\driver } /** - * {@inheritDoc} - */ - public function sql_nextid() + * {@inheritDoc} + */ + function sql_nextid() { return ($this->db_connect_id) ? $this->dbo->lastInsertRowID() : false; } + /** + * {@inheritdoc} + */ + public function sql_last_inserted_id() + { + return $this->sql_nextid(); + } + /** * {@inheritDoc} */ From b5c316c9c39b083970e065892ef6c3187e1d10f6 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 31 Jan 2023 17:17:42 +0100 Subject: [PATCH 2/2] [ticket/8071] Make nextid alias of sql_last_inserted_id and add deprecation PHPBB3-8071 --- phpBB/phpbb/db/driver/driver.php | 8 ++++++++ phpBB/phpbb/db/driver/driver_interface.php | 9 +++------ phpBB/phpbb/db/driver/factory.php | 4 ++-- phpBB/phpbb/db/driver/mssql_odbc.php | 12 ++---------- phpBB/phpbb/db/driver/mssqlnative.php | 12 ++---------- phpBB/phpbb/db/driver/mysqli.php | 10 +--------- phpBB/phpbb/db/driver/oracle.php | 12 ++---------- phpBB/phpbb/db/driver/postgres.php | 12 ++---------- phpBB/phpbb/db/driver/sqlite3.php | 10 +--------- 9 files changed, 23 insertions(+), 66 deletions(-) diff --git a/phpBB/phpbb/db/driver/driver.php b/phpBB/phpbb/db/driver/driver.php index cd9f1f058e..690cfbf1e9 100644 --- a/phpBB/phpbb/db/driver/driver.php +++ b/phpBB/phpbb/db/driver/driver.php @@ -634,6 +634,14 @@ abstract class driver implements driver_interface return $expression; } + /** + * {@inheritDoc} + */ + public function sql_nextid() + { + return $this->sql_last_inserted_id(); + } + /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/db/driver/driver_interface.php b/phpBB/phpbb/db/driver/driver_interface.php index 7615030a5a..73aabaf4a0 100644 --- a/phpBB/phpbb/db/driver/driver_interface.php +++ b/phpBB/phpbb/db/driver/driver_interface.php @@ -299,8 +299,9 @@ interface driver_interface * The returned value can be used for selecting the item that has just been * inserted or for updating another table with an ID pointing to that item. * - * Will be deprecated in a future version of phpBB in favor of - * `sql_last_inserted_id`. + * Alias of `sql_last_inserted_id`. + * + * @deprecated 3.3.11-RC1 Replaced by sql_last_inserted_id(), to be removed in 4.1.0-a1 * * @return string|false Auto-incremented value of the last inserted row */ @@ -312,11 +313,7 @@ interface driver_interface * just been inserted or for updating another table with an ID pointing to * that item. * - * Alias of `sql_nextid`. - * * @return string|false Auto-incremented value of the last inserted row - * - * @since 3.3.8-RC1 */ public function sql_last_inserted_id(); diff --git a/phpBB/phpbb/db/driver/factory.php b/phpBB/phpbb/db/driver/factory.php index d8865dc524..2541c28481 100644 --- a/phpBB/phpbb/db/driver/factory.php +++ b/phpBB/phpbb/db/driver/factory.php @@ -318,7 +318,7 @@ class factory implements driver_interface */ public function sql_nextid() { - return $this->get_driver()->sql_nextid(); + return $this->get_driver()->sql_last_inserted_id(); } /** @@ -326,7 +326,7 @@ class factory implements driver_interface */ public function sql_last_inserted_id() { - return $this->get_driver()->sql_nextid(); + return $this->get_driver()->sql_last_inserted_id(); } /** diff --git a/phpBB/phpbb/db/driver/mssql_odbc.php b/phpBB/phpbb/db/driver/mssql_odbc.php index 6a08f1100f..ac66d94d0d 100644 --- a/phpBB/phpbb/db/driver/mssql_odbc.php +++ b/phpBB/phpbb/db/driver/mssql_odbc.php @@ -269,9 +269,9 @@ class mssql_odbc extends \phpbb\db\driver\mssql_base } /** - * {@inheritDoc} + * {@inheritdoc} */ - function sql_nextid() + public function sql_last_inserted_id() { $result_id = @odbc_exec($this->db_connect_id, 'SELECT @@IDENTITY'); @@ -289,14 +289,6 @@ class mssql_odbc extends \phpbb\db\driver\mssql_base return false; } - /** - * {@inheritdoc} - */ - public function sql_last_inserted_id() - { - return $this->sql_nextid(); - } - /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/db/driver/mssqlnative.php b/phpBB/phpbb/db/driver/mssqlnative.php index c1955f8b84..357047ace0 100644 --- a/phpBB/phpbb/db/driver/mssqlnative.php +++ b/phpBB/phpbb/db/driver/mssqlnative.php @@ -271,9 +271,9 @@ class mssqlnative extends \phpbb\db\driver\mssql_base } /** - * {@inheritDoc} + * {@inheritdoc} */ - function sql_nextid() + public function sql_last_inserted_id() { $result_id = @sqlsrv_query($this->db_connect_id, 'SELECT @@IDENTITY'); @@ -290,14 +290,6 @@ class mssqlnative extends \phpbb\db\driver\mssql_base } } - /** - * {@inheritdoc} - */ - public function sql_last_inserted_id() - { - return $this->sql_nextid(); - } - /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/db/driver/mysqli.php b/phpBB/phpbb/db/driver/mysqli.php index 1c4a48cbb6..d7a929315b 100644 --- a/phpBB/phpbb/db/driver/mysqli.php +++ b/phpBB/phpbb/db/driver/mysqli.php @@ -288,20 +288,12 @@ class mysqli extends \phpbb\db\driver\mysql_base return ($query_id) ? @mysqli_data_seek($query_id, $rownum) : false; } - /** - * {@inheritDoc} - */ - function sql_nextid() - { - return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false; - } - /** * {@inheritdoc} */ public function sql_last_inserted_id() { - return $this->sql_nextid(); + return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false; } /** diff --git a/phpBB/phpbb/db/driver/oracle.php b/phpBB/phpbb/db/driver/oracle.php index c96201ef0c..dc5eb994ec 100644 --- a/phpBB/phpbb/db/driver/oracle.php +++ b/phpBB/phpbb/db/driver/oracle.php @@ -570,9 +570,9 @@ class oracle extends \phpbb\db\driver\driver } /** - * {@inheritDoc} + * {@inheritdoc} */ - function sql_nextid() + public function sql_last_inserted_id() { $query_id = $this->query_result; @@ -607,14 +607,6 @@ class oracle extends \phpbb\db\driver\driver return false; } - /** - * {@inheritdoc} - */ - public function sql_last_inserted_id() - { - return $this->sql_nextid(); - } - /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/db/driver/postgres.php b/phpBB/phpbb/db/driver/postgres.php index 0f87f5a8f0..02ffa4e3e7 100644 --- a/phpBB/phpbb/db/driver/postgres.php +++ b/phpBB/phpbb/db/driver/postgres.php @@ -342,9 +342,9 @@ class postgres extends \phpbb\db\driver\driver } /** - * {@inheritDoc} + * {@inheritdoc} */ - function sql_nextid() + public function sql_last_inserted_id() { $query_id = $this->query_result; @@ -370,14 +370,6 @@ class postgres extends \phpbb\db\driver\driver return false; } - /** - * {@inheritdoc} - */ - public function sql_last_inserted_id() - { - return $this->sql_nextid(); - } - /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/db/driver/sqlite3.php b/phpBB/phpbb/db/driver/sqlite3.php index 638b9035ed..93a1c36ee9 100644 --- a/phpBB/phpbb/db/driver/sqlite3.php +++ b/phpBB/phpbb/db/driver/sqlite3.php @@ -241,20 +241,12 @@ class sqlite3 extends \phpbb\db\driver\driver return is_object($query_id) ? @$query_id->fetchArray(SQLITE3_ASSOC) : false; } - /** - * {@inheritDoc} - */ - function sql_nextid() - { - return ($this->db_connect_id) ? $this->dbo->lastInsertRowID() : false; - } - /** * {@inheritdoc} */ public function sql_last_inserted_id() { - return $this->sql_nextid(); + return ($this->db_connect_id) ? $this->dbo->lastInsertRowID() : false; } /**