Skip to content

Commit

Permalink
Implement DeleteQuery
Browse files Browse the repository at this point in the history
closes #5
  • Loading branch information
g105b committed Sep 22, 2020
1 parent 7532444 commit 47bbe9d
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Query/DeleteQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace Gt\SqlBuilder\Query;

class DeleteQuery extends SqlQuery {
public function __toString():string {
$query = $this->processClauseList([
self::PRE_QUERY_COMMENT => $this->preQuery(),
"delete from" => $this->from(),
"partition" => $this->partition(),
"where" => $this->where(),
"order by" => $this->orderBy(),
"limit" => $this->limit(),
self::POST_QUERY_COMMENT => $this->postQuery(),
]);

return $query;
}

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

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

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

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

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

use Gt\SqlBuilder\Query\DeleteQuery;

class DeleteOrderByExample extends DeleteQuery {
public function from():array {
return [
"student",
];
}

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

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

use Gt\SqlBuilder\Query\DeleteQuery;

class DeleteExample extends DeleteQuery {
public function from():array {
return [
"student",
];
}

public function where():array {
return [
"id = :id",
];
}

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

use Gt\SqlBuilder\Query\DeleteQuery;

class DeletePartitionExample extends DeleteQuery {
public function from():array {
return [
"student",
];
}

public function partition():array {
return [
"exp1",
"exp2",
"exp3",
];
}

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

use Gt\SqlBuilder\Test\Helper\DeleteOrderByExample;
use Gt\SqlBuilder\Test\Helper\Query\DeleteExample;
use Gt\SqlBuilder\Test\Helper\Query\DeletePartitionExample;
use Gt\SqlBuilder\Test\QueryTestCase;

class DeleteQueryTest extends QueryTestCase {
public function testDelete() {
$sut = new DeleteExample();
$sql = self::normalise($sut);
self::assertEquals("delete from student where id = :id limit 1", $sql);
}

public function testDeletePartition() {
$sut = new DeletePartitionExample();
$sql = self::normalise($sut);
self::assertEquals("delete from student partition ( exp1, exp2, exp3 ) where toDelete = 1", $sql);
}

public function testDeleteOrderBy() {
$sut = new DeleteOrderByExample();
$sql = self::normalise($sut);
self::assertEquals("delete from student order by createdAt desc limit 10", $sql);
}
}

0 comments on commit 47bbe9d

Please sign in to comment.