Skip to content
Permalink
Browse files

added suport for modifiers in delete queries

  • Loading branch information...
lorenzo committed Feb 22, 2016
1 parent 747eb2d commit e4ffac7c25c116615f1f94a71f00ab0394f26a21
Showing with 39 additions and 19 deletions.
  1. +6 −18 src/Database/QueryCompiler.php
  2. +1 −1 src/Database/SqlserverCompiler.php
  3. +32 −0 tests/TestCase/Database/QueryTest.php
@@ -139,9 +139,9 @@ protected function _sqlCompiler(&$sql, $query, $generator)
protected function _buildSelectPart($parts, $query, $generator)
{
$driver = $query->connection()->driver();
$select = 'SELECT %s%s%s';
$select = 'SELECT%s %s%s';
if ($this->_orderedUnion && $query->clause('union')) {
$select = '(SELECT %s%s%s';
$select = '(SELECT%s %s%s';
}
$distinct = $query->clause('distinct');
$modifiers = $this->_buildModifierPart($query->clause('modifier'), $query, $generator);
@@ -159,16 +159,12 @@ protected function _buildSelectPart($parts, $query, $generator)
$distinct = 'DISTINCT ';
}
if ($modifiers !== null) {
$modifiers .= ' ';
}
if (is_array($distinct)) {
$distinct = $this->_stringifyExpressions($distinct, $generator);
$distinct = sprintf('DISTINCT ON (%s) ', implode(', ', $distinct));
}
return sprintf($select, $distinct, $modifiers, implode(', ', $normalized));
return sprintf($select, $modifiers, $distinct, implode(', ', $normalized));
}
/**
@@ -288,11 +284,7 @@ protected function _buildInsertPart($parts, $query, $generator)
$columns = $this->_stringifyExpressions($parts[1], $generator);
$modifiers = $this->_buildModifierPart($query->clause('modifier'), $query, $generator);
if ($modifiers !== null) {
$modifiers .= ' ';
}
return sprintf('INSERT %sINTO %s (%s)', $modifiers, $table, implode(', ', $columns));
return sprintf('INSERT%s INTO %s (%s)', $modifiers, $table, implode(', ', $columns));
}
/**
@@ -321,11 +313,7 @@ protected function _buildUpdatePart($parts, $query, $generator)
$table = $this->_stringifyExpressions($parts, $generator);
$modifiers = $this->_buildModifierPart($query->clause('modifier'), $query, $generator);
if ($modifiers !== null) {
$modifiers .= ' ';
}
return sprintf('UPDATE %s%s', $modifiers, implode(',', $table));
return sprintf('UPDATE%s %s', $modifiers, implode(',', $table));
}
/**
@@ -341,7 +329,7 @@ protected function _buildModifierPart($parts, $query, $generator)
if ($parts === []) {
return null;
}
return implode(' ', $this->_stringifyExpressions($parts, $generator, false));
return ' ' . implode(' ', $this->_stringifyExpressions($parts, $generator, false));
}
/**
@@ -73,7 +73,7 @@ protected function _buildInsertPart($parts, $query, $generator)
$modifiers .= ' ';
}
return sprintf('INSERT %sINTO %s (%s) OUTPUT INSERTED.*',
return sprintf('INSERT%s INTO %s (%s) OUTPUT INSERTED.*',
$modifiers,
$table,
implode(', ', $columns)
@@ -3712,6 +3712,38 @@ public function testUpdateModifiers()
);
}
/**
* Test use of modifiers in a DELETE query
*
* Testing the generated SQL since the modifiers are usually different per driver
*
* @return void
*/
public function testDeleteModifiers()
{
$query = new Query($this->connection);
$result = $query->delete()
->from('authors')
->where('1 = 1')
->modifier('IGNORE');
$this->assertQuotedQuery(
'DELETE IGNORE FROM <authors> WHERE 1 = 1',
$result->sql(),
!$this->autoQuote
);
$query = new Query($this->connection);
$result = $query->delete()
->from('authors')
->where('1 = 1')
->modifier(['IGNORE', 'QUICK']);
$this->assertQuotedQuery(
'DELETE IGNORE QUICK FROM <authors> WHERE 1 = 1',
$result->sql(),
!$this->autoQuote
);
}
/**
* Assertion for comparing a table's contents with what is in it.
*

0 comments on commit e4ffac7

Please sign in to comment.
You can’t perform that action at this time.