Skip to content

Commit

Permalink
Implement UpdateQuery
Browse files Browse the repository at this point in the history
closes #4
  • Loading branch information
g105b committed Sep 22, 2020
1 parent 47bbe9d commit e36a6ee
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/Query/DeleteQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class DeleteQuery extends SqlQuery {
public function __toString():string {
$query = $this->processClauseList([
return $this->processClauseList([
self::PRE_QUERY_COMMENT => $this->preQuery(),
"delete from" => $this->from(),
"partition" => $this->partition(),
Expand All @@ -12,8 +12,6 @@ public function __toString():string {
"limit" => $this->limit(),
self::POST_QUERY_COMMENT => $this->postQuery(),
]);

return $query;
}

public function from():array {
Expand Down
50 changes: 50 additions & 0 deletions src/Query/UpdateQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
namespace Gt\SqlBuilder\Query;

class UpdateQuery extends SqlQuery {
public function __toString():string {
return $this->processClauseList([
self::PRE_QUERY_COMMENT => $this->preQuery(),
"update" => $this->update(),
"set" => $this->normaliseSet($this->set()),
"where" => $this->where(),
"order by" => $this->orderBy(),
"limit" => $this->limit(),
self::POST_QUERY_COMMENT => $this->postQuery(),
]);
}

public function update():array {
return [];
}

public function set():array {
return [];
}

public function where():array {
return [];
}

public function orderBy():array {
return [];
}

public function limit():array {
return [];
}

protected function normaliseSet(array $setData):array {
$normalised = [];
foreach($setData as $i => $name) {
if(is_int($i)) {
$normalised[$name] = ":$name";
}
else {
$normalised[$i] = $name;
}
}

return $normalised;
}
}
18 changes: 18 additions & 0 deletions test/phpunit/Helper/Query/UpdateExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Gt\SqlBuilder\Test\Helper\Query;

use Gt\SqlBuilder\Query\UpdateQuery;

class UpdateExample extends UpdateQuery {
public function update():array {
return [
"student",
];
}

public function set():array {
return [
"processedAt",
];
}
}
11 changes: 11 additions & 0 deletions test/phpunit/Helper/Query/UpdateExtendsExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace Gt\SqlBuilder\Test\Helper\Query;

class UpdateExtendsExample extends UpdateExample {
public function where():array {
return [
...parent::where(),
"createdAt > date_sub(now(), interval 30 day)"
];
}
}
20 changes: 20 additions & 0 deletions test/phpunit/Query/UpdateQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Gt\SqlBuilder\Test\Query;

use Gt\SqlBuilder\Test\Helper\Query\UpdateExample;
use Gt\SqlBuilder\Test\Helper\Query\UpdateExtendsExample;
use Gt\SqlBuilder\Test\QueryTestCase;

class UpdateQueryTest extends QueryTestCase {
public function testUpdateSimple() {
$sut = new UpdateExample();
$sql = self::normalise($sut);
self::assertEquals("update student set processedAt = :processedAt", $sql);
}

public function testUpdateExtends() {
$sut = new UpdateExtendsExample();
$sql = self::normalise($sut);
self::assertEquals("update student set processedAt = :processedAt where createdAt > date_sub(now(), interval 30 day)", $sql);
}
}

0 comments on commit e36a6ee

Please sign in to comment.