Commit 214070 by webchick
#445214 by Josh Waihi: Fix drupal_write_record() to correctly deal with NULL serial columns.
--- includes/common.inc 2009/05/16 19:58:38 1.904
+++ includes/common.inc 2009/05/20 05:44:02 1.905
@@ -3978,7 +3978,20 @@
// Build the SQL.
if (empty($primary_keys)) {
- $query = db_insert($table)->fields($fields);
+ $options = array('return' => Database::RETURN_INSERT_ID);
+ if (isset($serial) && isset($fields[$serial])) {
+ // If the serial column has been explicitly set with an ID, then we don't
+ // require the database to return the last insert id.
+ if ($fields[$serial]) {
+ $options['return'] = Database::RETURN_AFFECTED;
+ }
+ // If a serial column does exist with no value (i.e. 0) then remove it as
+ // the database will insert the correct value for us.
+ else {
+ unset($fields[$serial]);
+ }
+ }
+ $query = db_insert($table, $options)->fields($fields);
$return = SAVED_NEW;
}
else {
@@ -3992,8 +4005,14 @@
// Execute the SQL.
if ($last_insert_id = $query->execute()) {
if (isset($serial)) {
- // Populate the serial field.
- $object->$serial = $last_insert_id;
+ // If the database was not told to return the last insert id, it will be
+ // because we already know it.
+ if (isset($options) && $options['return'] != Database::RETURN_INSERT_ID) {
+ $object->$serial = $fields[$serial];
+ }
+ else {
+ $object->$serial = $last_insert_id;
+ }
}
}
// If we have a single-field primary key but got no insert ID, the
--- includes/database/pgsql/query.inc 2009/04/20 20:02:30 1.10
+++ includes/database/pgsql/query.inc 2009/05/20 05:44:03 1.11
@@ -14,11 +14,6 @@
class InsertQuery_pgsql extends InsertQuery {
- public function __construct($connection, $table, array $options = array()) {
- parent::__construct($connection, $table, $options);
- $this->queryOptions['return'] = Database::RETURN_NULL;
- }
-
public function execute() {
// Confirm that the user did not try to specify an identical
@@ -69,7 +64,10 @@
if (!empty($table_information->sequences)) {
$options['sequence_name'] = $table_information->sequences[0];
- $options['return'] = Database::RETURN_INSERT_ID;
+ }
+ // If there are no sequences then we can't get a last insert id.
+ elseif ($options['return'] == Database::RETURN_INSERT_ID) {
+ $options['return'] = Database::RETURN_NULL;
}
$last_insert_id = $this->connection->query($stmt, array(), $options);
--- includes/database/query.inc 2009/05/03 08:55:06 1.21
+++ includes/database/query.inc 2009/05/20 05:44:02 1.22
@@ -288,7 +288,9 @@
protected $insertValues = array();
public function __construct($connection, $table, array $options = array()) {
- $options['return'] = Database::RETURN_INSERT_ID;
+ if (!isset($options['return'])) {
+ $options['return'] = Database::RETURN_INSERT_ID;
+ }
$options += array('delay' => FALSE);
parent::__construct($connection, $options);
$this->table = $table;
+++ includes/common.inc 2009/05/20 05:44:02 1.905
@@ -3978,7 +3978,20 @@
// Build the SQL.
if (empty($primary_keys)) {
- $query = db_insert($table)->fields($fields);
+ $options = array('return' => Database::RETURN_INSERT_ID);
+ if (isset($serial) && isset($fields[$serial])) {
+ // If the serial column has been explicitly set with an ID, then we don't
+ // require the database to return the last insert id.
+ if ($fields[$serial]) {
+ $options['return'] = Database::RETURN_AFFECTED;
+ }
+ // If a serial column does exist with no value (i.e. 0) then remove it as
+ // the database will insert the correct value for us.
+ else {
+ unset($fields[$serial]);
+ }
+ }
+ $query = db_insert($table, $options)->fields($fields);
$return = SAVED_NEW;
}
else {
@@ -3992,8 +4005,14 @@
// Execute the SQL.
if ($last_insert_id = $query->execute()) {
if (isset($serial)) {
- // Populate the serial field.
- $object->$serial = $last_insert_id;
+ // If the database was not told to return the last insert id, it will be
+ // because we already know it.
+ if (isset($options) && $options['return'] != Database::RETURN_INSERT_ID) {
+ $object->$serial = $fields[$serial];
+ }
+ else {
+ $object->$serial = $last_insert_id;
+ }
}
}
// If we have a single-field primary key but got no insert ID, the
--- includes/database/pgsql/query.inc 2009/04/20 20:02:30 1.10
+++ includes/database/pgsql/query.inc 2009/05/20 05:44:03 1.11
@@ -14,11 +14,6 @@
class InsertQuery_pgsql extends InsertQuery {
- public function __construct($connection, $table, array $options = array()) {
- parent::__construct($connection, $table, $options);
- $this->queryOptions['return'] = Database::RETURN_NULL;
- }
-
public function execute() {
// Confirm that the user did not try to specify an identical
@@ -69,7 +64,10 @@
if (!empty($table_information->sequences)) {
$options['sequence_name'] = $table_information->sequences[0];
- $options['return'] = Database::RETURN_INSERT_ID;
+ }
+ // If there are no sequences then we can't get a last insert id.
+ elseif ($options['return'] == Database::RETURN_INSERT_ID) {
+ $options['return'] = Database::RETURN_NULL;
}
$last_insert_id = $this->connection->query($stmt, array(), $options);
--- includes/database/query.inc 2009/05/03 08:55:06 1.21
+++ includes/database/query.inc 2009/05/20 05:44:02 1.22
@@ -288,7 +288,9 @@
protected $insertValues = array();
public function __construct($connection, $table, array $options = array()) {
- $options['return'] = Database::RETURN_INSERT_ID;
+ if (!isset($options['return'])) {
+ $options['return'] = Database::RETURN_INSERT_ID;
+ }
$options += array('delay' => FALSE);
parent::__construct($connection, $options);
$this->table = $table;