Merge branch 'master' of https://git.mcnd.ca/mcndave/ulmus
This commit is contained in:
commit
27d5fd0b08
15 files changed
+565
-244
No files matched your search
+52
-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,13 +216,7 @@ class QueryBuilder
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function notWhere($field, $value, string $operator = Query\Where::CONDITION_AND) : self
|
||||
{
|
||||
return $this->where($field, $value, $operator, true);
|
||||
}
|
||||
|
||||
public function limit(int $value) : self
|
||||
{
|
||||
if ( null === $limit = $this->getFragment(Query\Limit::class) ) {
|
||||
@@ -265,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);
|
||||
|
||||
@@ -276,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 = [];
|
||||
|
||||
@@ -295,7 +327,7 @@ class QueryBuilder
|
||||
});
|
||||
|
||||
foreach($this->queryStack as $fragment) {
|
||||
$sql[] = $fragment->render();
|
||||
$sql[] = $fragment->render($skipToken);
|
||||
}
|
||||
|
||||
return implode(" ", $sql);
|
||||
@@ -305,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
|
||||
@@ -338,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