- More work done on Update, Insert and Delete clause

- Added TOP property to select and delete to fit MSSQL limit's term.
- Added a new interface allowing object's manipulation on save() and load() queries
This commit is contained in:
Dave Mc Nicoll committed 2020-02-07 16:36:38 -05:00
1 parent 3dc82b1f0d
commit 2f43fb4559
13 files changed
+169 -58

No files matched your search

+28 -17
View File
@@ -25,7 +25,7 @@ class QueryBuilder
public function select($field) : self
{
if ( false !== ( $select = $this->has(Query\Select::class) ) ) {
if ( null !== ( $select = $this->getFragment(Query\Select::class) ) ) {
$select->add($field);
}
else {
@@ -39,7 +39,7 @@ class QueryBuilder
public function insert(array $fieldlist, string $table, ? string $alias = null, ? string $database = null, ? string $schema = null) : self
{
if ( ! $this->has(Query\Insert::class) ) {
if ( null === $this->getFragment(Query\Insert::class) ) {
if ( $database ) {
$table = "\"$database\".$table";
}
@@ -61,8 +61,7 @@ class QueryBuilder
public function values(array $dataset) : self
{
if ( false === ( $values = $this->has(Query\Values::class) ) ) {
if ( null === ( $values = $this->getFragment(Query\Values::class) ) ) {
$values = new Query\Values($this);
$this->push($values);
}
@@ -74,7 +73,7 @@ class QueryBuilder
public function update(string $table, ? string $alias = null, ? string $database = null, ? string $schema = null) : self
{
if ( ! $this->has(Query\Update::class) ) {
if ( ! $this->getFragment(Query\Update::class) ) {
if ( $database ) {
$table = "\"$database\".$table";
}
@@ -96,7 +95,7 @@ class QueryBuilder
public function set(array $dataset) : self
{
if ( false === ( $values = $this->has(Query\Set::class) ) ) {
if ( null === ( $values = $this->getFragment(Query\Set::class) ) ) {
$set = new Query\Set($this);
$this->push($set);
}
@@ -108,7 +107,7 @@ class QueryBuilder
public function delete() : self
{
if ( ! $this->has(Query\Delete::class) ) {
if ( ! $this->getFragment(Query\Delete::class) ) {
$this->push(new Query\Delete());
}
@@ -125,14 +124,14 @@ class QueryBuilder
$table = "\"$schema\".$table";
}
if ( false !== ( $from = $this->has(Query\From::class) ) ) {
if ( null !== ( $from = $this->getFragment(Query\From::class) ) ) {
$from->add($alias ? [ $alias => $table ] : $table);
}
else {
$from = new Query\From($this);
$this->push($from);
$from->set($alias ? [ $alias => $table ] : $table);
$from->set($alias ? [ $alias => $table ] : [ $table ]);
}
return $this;
@@ -140,7 +139,7 @@ class QueryBuilder
public function open(string $condition = Query\Where::CONDITION_AND) : self
{
if ( false !== ($this->where ?? false) ) {
if ( null !== ($this->where ?? false) ) {
$this->where->conditionList[] = $new = new Query\Where($this, $condition);
$this->where = $new;
}
@@ -150,7 +149,7 @@ class QueryBuilder
public function close() : self
{
if ( false !== ($this->where ?? false) && $this->where->parent ) {
if ( null !== ($this->where ?? false) && $this->where->parent ) {
$this->where = $this->where->parent;
}
@@ -162,7 +161,7 @@ class QueryBuilder
if ( $this->where ?? false ) {
$where = $this->where;
}
elseif ( false === ( $where = $this->has(Query\Where::class) ) ) {
elseif ( null === ( $where = $this->getFragment(Query\Where::class) ) ) {
$this->where = $where = new Query\Where($this);
$this->push($where);
}
@@ -186,29 +185,31 @@ class QueryBuilder
public function limit(int $value) : self
{
if ( false === $limit = $this->has(Query\Limit::class) ) {
if ( null === $limit = $this->getFragment(Query\Limit::class) ) {
$limit = new Query\Limit();
$this->push($limit);
}
$limit->set($value);
return $this;
}
public function offset(int $value) : self
{
if ( false === $offset = $this->has(Query\Offset::class) ) {
if ( null === $offset = $this->getFragment(Query\Offset::class) ) {
$offset = new Query\Offset();
$this->push($offset);
}
$offset->set($value);
return $this;
}
public function orderBy(string $field, ? string $direction = null) : self
{
if ( false === $orderBy = $this->has(Query\OrderBy::class) ) {
if ( null === $orderBy = $this->getFragment(Query\OrderBy::class) ) {
$orderBy = new Query\OrderBy();
$this->push($orderBy);
}
@@ -240,16 +241,26 @@ class QueryBuilder
return implode(" ", $sql);
}
public function has($class) {
public function getFragment(string $class) : ? Query\Fragment
{
foreach($this->queryStack as $item) {
if ( get_class($item) === $class ) {
return $item;
}
}
return false;
return null;
}
public function removeFragment(Query\Fragment $fragment) : void
{
foreach($this->queryStack as $key => $item) {
if ( $item === $fragment ) {
unset($this->queryStack[$key]);
}
}
}
public function __toString() : string
{
return $this->render();