From af6d27da6894aed702c40da3738542ddfce3bf86 Mon Sep 17 00:00:00 2001 From: mdean Date: Wed, 8 Aug 2001 03:14:05 +0000 Subject: [PATCH] Added support for get_last_insert_id($table, $field). pgsql is currently the only class making use of the params since it returns the unique OID for the system. The params are required so pgsql compatibility is forcibly maintained. --- phpgwapi/inc/class.db_mssql.inc.php | 22 ++++++++++++++++++++++ phpgwapi/inc/class.db_mysql.inc.php | 9 +++++++-- phpgwapi/inc/class.db_pgsql.inc.php | 27 +++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/phpgwapi/inc/class.db_mssql.inc.php b/phpgwapi/inc/class.db_mssql.inc.php index 6084ec62fe..273b95a16e 100644 --- a/phpgwapi/inc/class.db_mssql.inc.php +++ b/phpgwapi/inc/class.db_mssql.inc.php @@ -256,6 +256,28 @@ } /* public: table locking */ + function get_last_insert_id($table, $field) + { + /* This will get the last insert ID created on the current connection. Should only be called + * after an insert query is run on a table that has an auto incrementing field. Of note, table + * and field are required for pgsql compatiblity. MSSQL uses a query to retrieve the last + * identity on the connection, so table and field are ignored here as well. + */ + if (!isset($table) || $table == '' || !isset($field) || $field == '') + return -1; + + $result = @mssql_query("select @@identity", $this->Link_ID); + if (!$result) + return -1; + + $Record = @mssql_fetch_row(0); + @mssql_free_result($result); + if (!is_array($Record)) /* no identity? */ + return -1; + + return $Record[0]; + } + function lock($table, $mode="write") { return 1; // FIXME: fill it in! diff --git a/phpgwapi/inc/class.db_mysql.inc.php b/phpgwapi/inc/class.db_mysql.inc.php index c9e89c9079..15706d096d 100644 --- a/phpgwapi/inc/class.db_mysql.inc.php +++ b/phpgwapi/inc/class.db_mysql.inc.php @@ -289,12 +289,17 @@ return True; } - function get_last_insert_id() + function get_last_insert_id($table, $field) { /* This will get the last insert ID created on the current connection. Should only be called - * after an insert query is run on a table that has an auto incrementing field. + * after an insert query is run on a table that has an auto incrementing field. $table and + * $field are required, but unused here since it's unnecessary for mysql. For compatibility + * with pgsql, the params must be supplied. */ + if (!isset($table) || $table == '' || !isset($field) || $field == '') + return -1; + return @mysql_insert_id($this->Link_ID); } diff --git a/phpgwapi/inc/class.db_pgsql.inc.php b/phpgwapi/inc/class.db_pgsql.inc.php index 72820a85fc..2160e0adc7 100644 --- a/phpgwapi/inc/class.db_pgsql.inc.php +++ b/phpgwapi/inc/class.db_pgsql.inc.php @@ -273,6 +273,33 @@ return pg_Exec($this->Link_ID,'rollback'); } + function get_last_insert_id($table, $field) + { + /* This will get the last insert ID created on the current connection. Should only be called + * after an insert query is run on a table that has an auto incrementing field. Of note, table + * and field are required because pgsql returns the last inserted OID, which is unique across + * an entire installation. These params allow us to retrieve the sequenced field without adding + * conditional code to the apps. + */ + if (!isset($table) || $table == '' || !isset($field) || $field == '') + return -1; + + $oid = pg_getlastoid($this->Query_ID); + if ($oid == -1) + return -1; + + $result = @pg_Exec($this->Link_ID, "select $field from $table where oid=$oid"); + if (!$result) + return -1; + + $Record = @pg_fetch_array($result, 0); + @pg_freeresult($result); + if (!is_array($Record)) /* OID not found? */ + return -1; + + return $Record[0]; + } + function lock($table, $mode = 'write') { $result = $this->transaction_begin();