- Added the HAVING and TRUNCATE keyword
- Fixed GroupBy which was not implemented yet - Added a new SQL raw() function - From statement now accepts subqueries
This commit is contained in:
1 parent
538af38769
commit
139504f8dd
17 files changed
+566
-251
No files matched your search
+59
-16
@@ -8,6 +8,8 @@ class QueryBuilder
|
||||
|
||||
public Query\Having $having;
|
||||
|
||||
public QueryBuilder $parent;
|
||||
|
||||
/**
|
||||
* Those are the parameters we are going to bind to PDO.
|
||||
*/
|
||||
@@ -18,10 +20,10 @@ class QueryBuilder
|
||||
* Those values are to be inserted or updated
|
||||
*/
|
||||
public array $values = [];
|
||||
|
||||
|
||||
public string $whereConditionOperator = Query\Where::CONDITION_AND;
|
||||
|
||||
public string $havingConditionOperator = Query\Having::CONDITION_AND;
|
||||
public string $havingConditionOperator = Query\Where::CONDITION_AND;
|
||||
|
||||
protected int $parameterIndex = 0;
|
||||
|
||||
@@ -194,7 +196,12 @@ class QueryBuilder
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function having($field, $value, string $operator = Query\Having::OPERATOR_EQUAL, string $condition = Query\Having::CONDITION_AND, bool $not = false) : self
|
||||
public function notWhere($field, $value, string $operator = Query\Where::CONDITION_AND) : self
|
||||
{
|
||||
return $this->where($field, $value, $operator, true);
|
||||
}
|
||||
|
||||
public function having($field, $value, string $operator = Query\Where::OPERATOR_EQUAL, string $condition = Query\Where::CONDITION_AND, bool $not = false) : self
|
||||
{
|
||||
if ( $this->having ?? false ) {
|
||||
$having = $this->having;
|
||||
@@ -209,15 +216,16 @@ class QueryBuilder
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function notWhere($field, $value, string $operator = Query\Where::CONDITION_AND) : self
|
||||
public function groupBy(string $field, ? string $direction = null) : self
|
||||
{
|
||||
return $this->where($field, $value, $operator, true);
|
||||
}
|
||||
if ( null === $groupBy = $this->getFragment(Query\GroupBy::class) ) {
|
||||
$groupBy = new Query\GroupBy();
|
||||
$this->push($groupBy);
|
||||
}
|
||||
|
||||
public function groupBy() : self
|
||||
{
|
||||
$groupBy->add($field, $direction);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -258,6 +266,13 @@ class QueryBuilder
|
||||
}
|
||||
|
||||
public function join(string $type, /*string | QueryBuilder*/ $table, $field, $value, bool $outer = false, ? string $alias = null) : self
|
||||
{
|
||||
$this->withJoin(...func_get_args());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withJoin(string $type, $table, $field, $value, bool $outer = false, ? string $alias = null) : Query\Join
|
||||
{
|
||||
$join = new Query\Join($this);
|
||||
|
||||
@@ -269,17 +284,41 @@ class QueryBuilder
|
||||
|
||||
$join->alias = $alias;
|
||||
|
||||
return $join;
|
||||
}
|
||||
|
||||
public function truncate(string $table, ? string $alias = null, ? string $database = null, ? string $schema = null) : self
|
||||
{
|
||||
if ( $schema ) {
|
||||
$table = "$schema.$table";
|
||||
}
|
||||
|
||||
if ( $database ) {
|
||||
$table = "$database.$table";
|
||||
}
|
||||
|
||||
$truncate = new Query\Truncate($this);
|
||||
|
||||
$this->push($truncate);
|
||||
|
||||
$truncate->set($table);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function push(Query\Fragment $queryFragment) : self
|
||||
{
|
||||
$this->queryStack[] = $queryFragment;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function pull(Query\Fragment $queryFragment) : self
|
||||
{
|
||||
return array_shift($this->queryStack);
|
||||
}
|
||||
|
||||
public function render() : string
|
||||
public function render(bool $skipToken = false) : string
|
||||
{
|
||||
$sql = [];
|
||||
|
||||
@@ -288,7 +327,7 @@ class QueryBuilder
|
||||
});
|
||||
|
||||
foreach($this->queryStack as $fragment) {
|
||||
$sql[] = $fragment->render();
|
||||
$sql[] = $fragment->render($skipToken);
|
||||
}
|
||||
|
||||
return implode(" ", $sql);
|
||||
@@ -298,10 +337,10 @@ class QueryBuilder
|
||||
{
|
||||
$this->parameters = $this->values = $this->queryStack = [];
|
||||
$this->whereConditionOperator = Query\Where::CONDITION_AND;
|
||||
$this->havingConditionOperator = Query\Having::CONDITION_AND;
|
||||
$this->havingConditionOperator = Query\Where::CONDITION_AND;
|
||||
$this->parameterIndex = 0;
|
||||
|
||||
unset($this->where);
|
||||
unset($this->where, $this->having);
|
||||
}
|
||||
|
||||
public function getFragment(string $class) : ? Query\Fragment
|
||||
@@ -331,12 +370,16 @@ class QueryBuilder
|
||||
|
||||
public function addParameter($value, string $key = null) : string
|
||||
{
|
||||
if ( $this->parent ?? false ) {
|
||||
return $this->parent->addParameter($value, $key);
|
||||
}
|
||||
|
||||
if ( $key === null ) {
|
||||
$key = ":p" . $this->parameterIndex++;
|
||||
}
|
||||
|
||||
$this->parameters[$key] = $value;
|
||||
|
||||
$this->parameters[$key] = $value;
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user