-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #4
- Loading branch information
Showing
5 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |