Skip to content

Commit

Permalink
added suport for modifiers in delete queries
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 23, 2016
1 parent 747eb2d commit e4ffac7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
24 changes: 6 additions & 18 deletions src/Database/QueryCompiler.php
Expand Up @@ -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);
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Database/SqlserverCompiler.php
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit e4ffac7

Please sign in to comment.