Skip to content

Commit

Permalink
Add support for limit in update and delete queries for MySQL driver
Browse files Browse the repository at this point in the history
  • Loading branch information
nechutny committed Jul 6, 2016
1 parent 05aa911 commit cf35cfb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Database/Drivers/MySqlDriver.php
Expand Up @@ -254,7 +254,8 @@ public function isSupported($item)
// - http://bugs.mysql.com/bug.php?id=31188
// - http://bugs.mysql.com/bug.php?id=35819
// and more.
return $item === self::SUPPORT_SELECT_UNGROUPED_COLUMNS || $item === self::SUPPORT_MULTI_COLUMN_AS_OR_COND;
return $item === self::SUPPORT_SELECT_UNGROUPED_COLUMNS || $item === self::SUPPORT_MULTI_COLUMN_AS_OR_COND
|| $item === self::SUPPORT_UPDATE_LIMIT || $item === self::SUPPORT_DELETE_LIMIT;
}

}
2 changes: 2 additions & 0 deletions src/Database/ISupplementalDriver.php
Expand Up @@ -17,6 +17,8 @@ interface ISupplementalDriver
SUPPORT_SELECT_UNGROUPED_COLUMNS = 'ungrouped_cols',
SUPPORT_MULTI_INSERT_AS_SELECT = 'insert_as_select',
SUPPORT_MULTI_COLUMN_AS_OR_COND = 'multi_column_as_or',
SUPPORT_UPDATE_LIMIT = 'update_limit',
SUPPORT_DELETE_LIMIT = 'delete_limit',
SUPPORT_SUBSELECT = 'subselect',
SUPPORT_SCHEMA = 'schema';

Expand Down
22 changes: 18 additions & 4 deletions src/Database/Table/SqlBuilder.php
Expand Up @@ -120,19 +120,33 @@ public function buildInsertQuery()

public function buildUpdateQuery()
{
$query = "UPDATE {$this->delimitedTable} SET ?set" . $this->tryDelimite($this->buildConditions());
if ($this->limit !== NULL || $this->offset) {
throw new Nette\NotSupportedException('LIMIT clause is not supported in UPDATE query.');
if($this->driver->isSupported(ISupplementalDriver::SUPPORT_UPDATE_LIMIT)) {
$this->driver->applyLimit($query, $this->limit, $this->offset);
}
else {
throw new Nette\NotSupportedException('LIMIT clause is not supported in UPDATE query.');
}
}
return "UPDATE {$this->delimitedTable} SET ?set" . $this->tryDelimite($this->buildConditions());

return $query;
}


public function buildDeleteQuery()
{
$query = "DELETE FROM {$this->delimitedTable}" . $this->tryDelimite($this->buildConditions());
if ($this->limit !== NULL || $this->offset) {
throw new Nette\NotSupportedException('LIMIT clause is not supported in DELETE query.');
if($this->driver->isSupported(ISupplementalDriver::SUPPORT_DELETE_LIMIT)) {
$this->driver->applyLimit($query, $this->limit, $this->offset);
}
else {
throw new Nette\NotSupportedException('LIMIT clause is not supported in DELETE query.');
}
}
return "DELETE FROM {$this->delimitedTable}" . $this->tryDelimite($this->buildConditions());

return $query;
}


Expand Down

0 comments on commit cf35cfb

Please sign in to comment.