Unverified Commit cf096205 authored by alexpott's avatar alexpott
Browse files

Issue #3211866 by mondrake, daffie: Upsert::execute() return values are inconsistent

(cherry picked from commit 8e4a7d250dd2a8c24353b7d54bdb892efaa9cd67)
parent 21733d27
...@@ -20,6 +20,7 @@ public function execute() { ...@@ -20,6 +20,7 @@ public function execute() {
} }
$stmt = $this->connection->prepareStatement((string) $this, $this->queryOptions); $stmt = $this->connection->prepareStatement((string) $this, $this->queryOptions);
$stmt->allowRowCount = TRUE;
// Fetch the list of blobs and sequences used on that table. // Fetch the list of blobs and sequences used on that table.
$table_information = $this->connection->schema()->queryTableInformation($this->table); $table_information = $this->connection->schema()->queryTableInformation($this->table);
...@@ -82,13 +83,12 @@ public function execute() { ...@@ -82,13 +83,12 @@ public function execute() {
try { try {
$stmt->execute(NULL, $options); $stmt->execute(NULL, $options);
$this->connection->releaseSavepoint(); $this->connection->releaseSavepoint();
return $stmt->rowCount();
} }
catch (\Exception $e) { catch (\Exception $e) {
$this->connection->rollbackSavepoint(); $this->connection->rollbackSavepoint();
throw $e; throw $e;
} }
return TRUE;
} }
/** /**
......
...@@ -88,7 +88,12 @@ protected function preExecute() { ...@@ -88,7 +88,12 @@ protected function preExecute() {
} }
/** /**
* {@inheritdoc} * Executes the UPSERT operation.
*
* @return int
* An integer indicating the number of rows affected by the operation. Do
* not rely on this value as a precise indication of the actual rows
* affected: different database engines return different values.
*/ */
public function execute() { public function execute() {
if (!$this->preExecute()) { if (!$this->preExecute()) {
...@@ -103,12 +108,12 @@ public function execute() { ...@@ -103,12 +108,12 @@ public function execute() {
} }
} }
$last_insert_id = $this->connection->query((string) $this, $values, $this->queryOptions); $affected_rows = $this->connection->query((string) $this, $values, $this->queryOptions);
// Re-initialize the values array so that we can re-use this query. // Re-initialize the values array so that we can re-use this query.
$this->insertValues = []; $this->insertValues = [];
return $last_insert_id; return $affected_rows;
} }
} }
...@@ -37,7 +37,9 @@ public function testUpsert() { ...@@ -37,7 +37,9 @@ public function testUpsert() {
'name' => 'Meredith', 'name' => 'Meredith',
]); ]);
$upsert->execute(); $result = $upsert->execute();
$this->assertIsInt($result);
$this->assertGreaterThanOrEqual(2, $result, 'The result of the upsert operation should report that at least two rows were affected.');
$num_records_after = $connection->query('SELECT COUNT(*) FROM {test_people}')->fetchField(); $num_records_after = $connection->query('SELECT COUNT(*) FROM {test_people}')->fetchField();
$this->assertEqual($num_records_before + 1, $num_records_after, 'Rows were inserted and updated properly.'); $this->assertEqual($num_records_before + 1, $num_records_after, 'Rows were inserted and updated properly.');
...@@ -75,7 +77,9 @@ public function testUpsertWithKeywords() { ...@@ -75,7 +77,9 @@ public function testUpsertWithKeywords() {
'update' => 'Update value 1 updated', 'update' => 'Update value 1 updated',
]); ]);
$upsert->execute(); $result = $upsert->execute();
$this->assertIsInt($result);
$this->assertGreaterThanOrEqual(2, $result, 'The result of the upsert operation should report that at least two rows were affected.');
$num_records_after = $this->connection->query('SELECT COUNT(*) FROM {select}')->fetchField(); $num_records_after = $this->connection->query('SELECT COUNT(*) FROM {select}')->fetchField();
$this->assertEquals($num_records_before + 1, $num_records_after, 'Rows were inserted and updated properly.'); $this->assertEquals($num_records_before + 1, $num_records_after, 'Rows were inserted and updated properly.');
......
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