Commit ee96cf6b authored by catch's avatar catch
Browse files

Issue #1889328 by gielfeldt, gumanist: Fixed Not all objects respect the query...

Issue #1889328 by gielfeldt, gumanist: Fixed Not all objects respect the query option throw_exception.
parent 31bd2006
...@@ -403,42 +403,57 @@ public function __toString() { ...@@ -403,42 +403,57 @@ public function __toString() {
} }
public function execute() { public function execute() {
if (!count($this->condition)) { // Default options for merge queries.
throw new InvalidMergeQueryException(t('Invalid merge query: no conditions')); $this->queryOptions += array(
} 'throw_exception' => TRUE,
$select = $this->connection->select($this->conditionTable) );
->condition($this->condition);
$select->addExpression('1'); try {
if (!$select->execute()->fetchField()) { if (!count($this->condition)) {
try { throw new InvalidMergeQueryException(t('Invalid merge query: no conditions'));
$insert = $this->connection->insert($this->table)->fields($this->insertFields); }
if ($this->defaultFields) { $select = $this->connection->select($this->conditionTable)
$insert->useDefaults($this->defaultFields); ->condition($this->condition);
$select->addExpression('1');
if (!$select->execute()->fetchField()) {
try {
$insert = $this->connection->insert($this->table)->fields($this->insertFields);
if ($this->defaultFields) {
$insert->useDefaults($this->defaultFields);
}
$insert->execute();
return self::STATUS_INSERT;
}
catch (IntegrityConstraintViolationException $e) {
// The insert query failed, maybe it's because a racing insert query
// beat us in inserting the same row. Retry the select query, if it
// returns a row, ignore the error and continue with the update
// query below.
if (!$select->execute()->fetchField()) {
throw $e;
}
} }
$insert->execute();
return self::STATUS_INSERT;
} }
catch (IntegrityConstraintViolationException $e) { if ($this->needsUpdate) {
// The insert query failed, maybe it's because a racing insert query $update = $this->connection->update($this->table)
// beat us in inserting the same row. Retry the select query, if it ->fields($this->updateFields)
// returns a row, ignore the error and continue with the update ->condition($this->condition);
// query below. if ($this->expressionFields) {
if (!$select->execute()->fetchField()) { foreach ($this->expressionFields as $field => $data) {
throw $e; $update->expression($field, $data['expression'], $data['arguments']);
}
} }
$update->execute();
return self::STATUS_UPDATE;
} }
} }
if ($this->needsUpdate) { catch (\Exception $e) {
$update = $this->connection->update($this->table) if ($this->queryOptions['throw_exception']) {
->fields($this->updateFields) throw $e;
->condition($this->condition); }
if ($this->expressionFields) { else {
foreach ($this->expressionFields as $field => $data) { return NULL;
$update->expression($field, $data['expression'], $data['arguments']);
}
} }
$update->execute();
return self::STATUS_UPDATE;
} }
} }
} }
...@@ -589,7 +589,7 @@ public function groupBy($field) { ...@@ -589,7 +589,7 @@ public function groupBy($field) {
public function countQuery() { public function countQuery() {
$count = $this->prepareCountQuery(); $count = $this->prepareCountQuery();
$query = $this->connection->select($count); $query = $this->connection->select($count, NULL, $this->queryOptions);
$query->addExpression('COUNT(*)'); $query->addExpression('COUNT(*)');
return $query; return $query;
......
...@@ -208,7 +208,25 @@ function testMergeUpdateWithoutUpdate() { ...@@ -208,7 +208,25 @@ function testMergeUpdateWithoutUpdate() {
*/ */
function testInvalidMerge() { function testInvalidMerge() {
try { try {
// This query should die because there is no key field specified. // This query will fail because there is no key field specified.
// Normally it would throw an exception but we are supressing it with
// the throw_exception option.
$options['throw_exception'] = FALSE;
db_merge('test_people', $options)
->fields(array(
'age' => 31,
'name' => 'Tiffany',
))
->execute();
$this->pass('$options[\'throw_exception\'] is FALSE, no InvalidMergeQueryException thrown.');
}
catch (InvalidMergeQueryException $e) {
$this->fail('$options[\'throw_exception\'] is FALSE, but InvalidMergeQueryException thrown for invalid query.');
return;
}
try {
// This query will fail because there is no key field specified.
db_merge('test_people') db_merge('test_people')
->fields(array( ->fields(array(
'age' => 31, 'age' => 31,
......
...@@ -376,4 +376,40 @@ function testSelectDuplicateAlias() { ...@@ -376,4 +376,40 @@ function testSelectDuplicateAlias() {
$alias2 = $query->addField('t', 'age', 'the_alias'); $alias2 = $query->addField('t', 'age', 'the_alias');
$this->assertNotIdentical($alias1, $alias2, 'Duplicate aliases are renamed.'); $this->assertNotIdentical($alias1, $alias2, 'Duplicate aliases are renamed.');
} }
/**
* Tests that an invalid merge query throws an exception.
*/
function testInvalidSelectCount() {
try {
// This query will fail because the table does not exist.
// Normally it would throw an exception but we are supressing
// it with the throw_exception option.
$options['throw_exception'] = FALSE;
db_select('some_table_that_doesnt_exist', 't', $options)
->fields('t')
->countQuery()
->execute();
$this->pass('$options[\'throw_exception\'] is FALSE, no Exception thrown.');
}
catch (\Exception $e) {
$this->fail('$options[\'throw_exception\'] is FALSE, but Exception thrown for invalid query.');
return;
}
try {
// This query will fail because the table does not exist.
db_select('some_table_that_doesnt_exist', 't')
->fields('t')
->countQuery()
->execute();
}
catch (\Exception $e) {
$this->pass('Exception thrown for invalid query.');
return;
}
$this->fail('No Exception thrown.');
}
} }
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