Skip to content
Browse files

Add limit, order support for delete and update queries.

  • Loading branch information...
1 parent d77d320 commit 9d5fb6e043325647e05e66bba5630b9246c0d213 @nathggns nathggns committed Feb 21, 2013
View
23 system/classes/database/query/builder.php
@@ -132,6 +132,8 @@ public function limit($start, $end = null) {
$part = [$start];
if (!is_null($end)) $part[] = $end;
+ if (!isset($this->query_opts['limit'])) $this->query_opts['limit'] = [];
+
$this->query_opts['limit'] = array_merge($this->query_opts['limit'], $part);
return $this;
@@ -191,7 +193,16 @@ protected function extract_select() {
protected function extract_update() {
- $args = $this->extract_shuffle(['table'], ['table', 'data', 'where', 'conds'], func_get_args());
+ $args = $this->extract_shuffle([
+ 'table'
+ ], [
+ 'table',
+ 'data',
+ 'where',
+ 'conds',
+ 'limit',
+ 'order'
+ ], func_get_args());
if (isset($args['conds'])) {
$args['where'] = $args['conds'];
@@ -202,7 +213,15 @@ protected function extract_update() {
}
protected function extract_delete() {
- $args = $this->extract_shuffle(['table'], ['table', 'where', 'conds'], func_get_args());
+ $args = $this->extract_shuffle([
+ 'table'
+ ], [
+ 'table',
+ 'where',
+ 'conds',
+ 'limit',
+ 'order'
+ ], func_get_args());
if (isset($args['conds'])) {
$args['where'] = $args['conds'];
View
4 system/classes/database/query/builders/sql.php
@@ -124,6 +124,8 @@ public function update() {
$query = 'UPDATE ' . $table . ' SET ' . $this->pairs($keys, $data);
if (count($where) > 0) $query .= ' ' . $this->where_array($where);
+ if (count($order) > 0) $query .= ' ' . $this->order_array($order);
+ if (count($limit) > 0) $query .= ' ' . $this->limit_array($limit);
$query .= ';';
@@ -149,6 +151,8 @@ public function delete() {
$query = 'DELETE FROM ' . $table;
if (count($where) > 0) $query .= ' ' . $this->where_array($where);
+ if (count($order) > 0) $query .= ' ' . $this->order_array($order);
+ if (count($limit) > 0) $query .= ' ' . $this->limit_array($limit);
$query .= ';';
View
122 tests/DatabaseQueryBuilderSQLTest.php
@@ -288,6 +288,56 @@ public function testUpdate() {
$this->assertEquals('UPDATE `users` SET `name` = \'Bob\' WHERE `name` = \'Paul\';', $sql);
}
+ public function testUpdateWithLimit() {
+ $sql = $this->builder->update([
+ 'table' => 'users',
+ 'data' => [
+ 'name' => 'Bob'
+ ],
+ 'where' => [
+ 'name' => 'Paul'
+ ],
+ 'limit' => [23, 43]
+ ]);
+
+ $this->assertEquals('UPDATE `users` SET `name` = \'Bob\' WHERE `name` = \'Paul\' LIMIT 23, 43;', $sql);
+ }
+
+ public function testUpdateWithOrder() {
+ $sql = $this->builder->update([
+ 'table' => 'users',
+ 'data' => [
+ 'name' => 'Bob'
+ ],
+ 'where' => [
+ 'name' => 'Paul'
+ ],
+ 'order' => [
+ ['name', 'DESC']
+ ]
+ ]);
+
+ $this->assertEquals('UPDATE `users` SET `name` = \'Bob\' WHERE `name` = \'Paul\' ORDER BY `name` DESC;', $sql);
+ }
+
+ public function testUpdateWithOrderAndLimit() {
+ $sql = $this->builder->update([
+ 'table' => 'users',
+ 'data' => [
+ 'name' => 'Bob'
+ ],
+ 'where' => [
+ 'name' => 'Paul'
+ ],
+ 'order' => [
+ ['name', 'DESC']
+ ],
+ 'limit' => [23, 43]
+ ]);
+
+ $this->assertEquals('UPDATE `users` SET `name` = \'Bob\' WHERE `name` = \'Paul\' ORDER BY `name` DESC LIMIT 23, 43;', $sql);
+ }
+
public function testUpdateWithoutWhere() {
$sql = $this->builder->update('users', [
'name' => 'Bob'
@@ -320,6 +370,21 @@ public function testUpdateChained() {
$this->assertEquals('UPDATE `users` SET `name` = \'nat\' WHERE `name` = \'bob\';', $sql);
}
+ public function testUpdateChainedOrder() {
+ $sql = $this->builder->start()->update('users')->set('name', 'nat')->where('name', 'bob')->order('name', 'desc')->end();
+ $this->assertEquals('UPDATE `users` SET `name` = \'nat\' WHERE `name` = \'bob\' ORDER BY `name` DESC;', $sql);
+ }
+
+ public function testUpdateChainedLimit() {
+ $sql = $this->builder->start()->update('users')->set('name', 'nat')->where('name', 'bob')->limit(22, 56)->end();
+ $this->assertEquals('UPDATE `users` SET `name` = \'nat\' WHERE `name` = \'bob\' LIMIT 22, 56;', $sql);
+ }
+
+ public function testUpdateChainedOrderAndLimit() {
+ $sql = $this->builder->start()->update('users')->set('name', 'nat')->where('name', 'bob')->order('name', 'desc')->limit(22, 56)->end();
+ $this->assertEquals('UPDATE `users` SET `name` = \'nat\' WHERE `name` = \'bob\' ORDER BY `name` DESC LIMIT 22, 56;', $sql);
+ }
+
public function testUpdateChainedWithoutStart() {
$sql = $this->builder->update('users')->set('name', 'nat')->where('name', 'bob')->end();
$this->assertEquals('UPDATE `users` SET `name` = \'nat\' WHERE `name` = \'bob\';', $sql);
@@ -351,6 +416,48 @@ public function testDelete() {
$this->assertEquals('DELETE FROM `users` WHERE `name` = \'Harry\';', $sql);
}
+ public function testDeleteWithOrder() {
+ $sql = $this->builder->delete([
+ 'table' => 'users',
+ 'where' => [
+ 'name' => 'Harry'
+ ],
+ 'order' => [
+ ['name', 'DESC']
+ ]
+ ]);
+
+ $this->assertEquals('DELETE FROM `users` WHERE `name` = \'Harry\' ORDER BY `name` DESC;', $sql);
+ }
+
+ public function testDeleteWithLimit() {
+ $sql = $this->builder->delete([
+ 'table' => 'users',
+ 'where' => [
+ 'name' => 'Harry'
+ ],
+ 'order' => [
+ ['name', 'DESC']
+ ],
+ 'limit' => [26, 50]
+ ]);
+
+ $this->assertEquals('DELETE FROM `users` WHERE `name` = \'Harry\' ORDER BY `name` DESC LIMIT 26, 50;', $sql);
+ }
+
+ public function testDeleteWithOrderAndLimit() {
+ $sql = $this->builder->delete([
+ 'table' => 'users',
+ 'where' => [
+ 'name' => 'Harry'
+ ],
+ 'limit' => [26, 50]
+ ]);
+
+ $this->assertEquals('DELETE FROM `users` WHERE `name` = \'Harry\' LIMIT 26, 50;', $sql);
+ }
+
+
public function testDeleteChained() {
$sql = $this->builder->start()->delete('users')->where('name', 'Harry')->end();
$this->assertEquals('DELETE FROM `users` WHERE `name` = \'Harry\';', $sql);
@@ -361,6 +468,21 @@ public function testDeleteChainedWithoutStart() {
$this->assertEquals('DELETE FROM `users` WHERE `name` = \'Harry\';', $sql);
}
+ public function testDeleteChainedWithOrder() {
+ $sql = $this->builder->start()->delete('users')->where('name', 'Harry')->order('name', 'desc')->end();
+ $this->assertEquals('DELETE FROM `users` WHERE `name` = \'Harry\' ORDER BY `name` DESC;', $sql);
+ }
+
+ public function testDeleteChainedWithLimit() {
+ $sql = $this->builder->start()->delete('users')->where('name', 'Harry')->limit(22, 30)->end();
+ $this->assertEquals('DELETE FROM `users` WHERE `name` = \'Harry\' LIMIT 22, 30;', $sql);
+ }
+
+ public function testDeleteChainedWithLimitAndOrder() {
+ $sql = $this->builder->start()->delete('users')->where('name', 'Harry')->order('name', 'desc')->limit(22, 30)->end();
+ $this->assertEquals('DELETE FROM `users` WHERE `name` = \'Harry\' ORDER BY `name` DESC LIMIT 22, 30;', $sql);
+ }
+
public function testDeleteWithComplexWhere() {
$sql = $this->builder->delete('users', [
'logins' => Database::where_gt(5)

0 comments on commit 9d5fb6e

Please sign in to comment.
Something went wrong with that request. Please try again.