diff options
-rw-r--r-- | includes/common.inc | 25 | ||||
-rw-r--r-- | includes/database/pgsql/query.inc | 10 | ||||
-rw-r--r-- | includes/database/query.inc | 4 |
3 files changed, 29 insertions, 10 deletions
diff --git a/includes/common.inc b/includes/common.inc index 9dd8ecc1d..3b353756c 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -3978,7 +3978,20 @@ function drupal_write_record($table, &$object, $primary_keys = array()) { // 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 @@ function drupal_write_record($table, &$object, $primary_keys = array()) { // 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 diff --git a/includes/database/pgsql/query.inc b/includes/database/pgsql/query.inc index f3a4932ea..39a132bdd 100644 --- a/includes/database/pgsql/query.inc +++ b/includes/database/pgsql/query.inc @@ -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 @@ class InsertQuery_pgsql extends InsertQuery { 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); diff --git a/includes/database/query.inc b/includes/database/query.inc index 8cdaaf5e1..db0cd31ed 100644 --- a/includes/database/query.inc +++ b/includes/database/query.inc @@ -288,7 +288,9 @@ class InsertQuery extends Query { 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; |