Commit e5350c19 authored by webchick's avatar webchick

#445214 by Josh Waihi: Fix drupal_write_record() to correctly deal with NULL serial columns.

parent 9df38a58
......@@ -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
......
......@@ -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 @@ public function execute() {
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);
......
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment