- A lot of bugfixes and fitting to accomodate ldap adapter

This commit is contained in:
Dave Mc Nicoll committed 2022-04-13 03:31:48 +00:00
1 parent f14347f01d
commit 16d76470da
15 files changed
+350 -216

No files matched your search

+71 -59
View File
@@ -9,45 +9,45 @@ class QueryBuilder implements Query\QueryBuilderInterface
public Query\Where $where;
public Query\Having $having;
public QueryBuilderInterface $parent;
/**
* Those are the parameters we are going to bind to PDO.
*/
public array $parameters = [];
/**
*
* Those values are to be inserted or updated
*/
public array $values = [];
public string $whereConditionOperator = Query\Where::CONDITION_AND;
public string $havingConditionOperator = Query\Where::CONDITION_AND;
protected int $parameterIndex = 0;
protected array $queryStack = [];
public function __clone()
public function __clone()
{
if ($this->where ?? false) {
#$this->where = clone $this->where;
#$this->where->queryBuilder = $this;
}
if ($this->having ?? false) {
#$this->having = clone $this->having;
#$this->having->queryBuiler = $this;
}
if ($this->parent ?? false) {
#$this->parent = clone $this->parent;
}
}
public function select($field, bool $distinct = false) : self
{
if ( null !== ( $select = $this->getFragment(Query\Select::class) ) ) {
@@ -63,18 +63,18 @@ class QueryBuilder implements Query\QueryBuilderInterface
return $this;
}
public function insert(array $fieldlist, string $table, ? string $alias = null, ? string $database = null, ? string $schema = null, bool $replace = false) : self
{
if ( null === $this->getFragment(Query\Insert::class) ) {
if ( null === $this->getFragment(Query\Insert::class) ) {
if ( $schema ) {
$table = "$schema.$table";
}
if ( $database ) {
$table = "$database.$table";
}
$insert = new Query\Insert();
$this->push($insert);
@@ -87,32 +87,32 @@ class QueryBuilder implements Query\QueryBuilderInterface
return $this;
}
public function values(array $dataset) : self
public function values(array $dataset) : self
{
if ( null === ( $values = $this->getFragment(Query\Values::class) ) ) {
$values = new Query\Values($this);
$this->push($values);
}
$values->add($dataset);
return $this;
}
public function update(string $table, ? string $alias = null, ? string $database = null, ? string $schema = null) : self
{
if ( ! $this->getFragment(Query\Update::class) ) {
if ( $schema ) {
$table = "$schema.$table";
}
if ( $database ) {
$table = "$database.$table";
}
$update = new Query\Update();
$this->push($update);
$update->alias = $alias;
$update->table = $table;
}
@@ -122,17 +122,16 @@ class QueryBuilder implements Query\QueryBuilderInterface
public function set(array $dataset, ? array $escapedFields = null) : self
{
if ( null === ( $set = $this->getFragment(Query\Set::class) ) ) {
$set = new Query\Set($this);
$this->push($set);
}
$set->set($dataset, $escapedFields);
return $this;
}
public function delete() : self
{
if ( ! $this->getFragment(Query\Delete::class) ) {
@@ -147,7 +146,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
if ( $schema ) {
$table = "$schema.$table";
}
if ( $database ) {
$table = "$database.$table";
}
@@ -158,7 +157,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
else {
$from = new Query\From($this);
$this->push($from);
$from->set($alias ? [ $alias => $table ] : [ $table ]);
}
@@ -184,12 +183,12 @@ class QueryBuilder implements Query\QueryBuilderInterface
public function close() : self
{
if ( null !== ($this->where ?? null) && $this->where->parent ) {
# if an enclosure was opened, and nothing done, we must remove the unused node
if ( empty($this->where->conditionList) && (count($this->where->parent->conditionList) === 1) ) {
unset($this->where->parent->conditionList);
}
$this->where = $this->where->parent;
}
@@ -202,7 +201,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
if ( [] === $value ) {
return $this;
}
if ( $this->where ?? false ) {
$where = $this->where;
}
@@ -214,7 +213,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
$this->whereConditionOperator = $operator;
$where->add($field, $value, $operator, $condition, $not);
return $this;
}
@@ -235,10 +234,10 @@ class QueryBuilder implements Query\QueryBuilderInterface
$this->havingConditionOperator = $operator;
$having->add($field, $value, $operator, $condition, $not);
return $this;
}
public function limit(int $value) : self
{
if ( null === $limit = $this->getFragment(Query\Limit::class) ) {
@@ -247,7 +246,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
}
$limit->set($value);
return $this;
}
@@ -259,7 +258,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
}
$offset->set($value);
return $this;
}
@@ -271,7 +270,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
}
$orderBy->add($field, $direction);
return $this;
}
@@ -283,48 +282,50 @@ class QueryBuilder implements Query\QueryBuilderInterface
}
$groupBy->add($field, $direction);
return $this;
}
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);
$this->push($join);
$join->set($type, $table, $field, $value);
$join->outer = $outer;
$join->alias = $alias;
$join->joinOrder = $this->nextJoinOrder();
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;
}
@@ -334,7 +335,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
if ( $schema ) {
$table = "$schema.$table";
}
if ( $database ) {
$table = "$database.$table";
}
@@ -387,14 +388,14 @@ class QueryBuilder implements Query\QueryBuilderInterface
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);
@@ -405,7 +406,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
$sql = [];
usort($this->queryStack, function($q1, $q2) {
return $q1->order <=> $q2->order;
return $q1->order() <=> $q2->order();
});
foreach($this->queryStack as $fragment) {
@@ -414,7 +415,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
return implode(" ", $sql);
}
public function reset() : void
{
$this->parameters = $this->values = $this->queryStack = [];
@@ -456,33 +457,44 @@ class QueryBuilder implements Query\QueryBuilderInterface
}
}
public function getFragments() : array
public function getFragments(/*? Query\Fragment|array*/ $fragment = null) : array
{
return $this->queryStack;
return $fragment === null ? array_filter($this->queryStack, fn($e) => get_class($e) === $fragment) : $this->queryStack;
}
public function __toString() : string
{
return $this->render();
}
public function addParameter($value, string $key = null) : string
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;
}
public function addValues(array $values) : void
{
$this->values = $values;
}
protected function nextJoinOrder() : float
{
$next = 0;
foreach($this->getFragments(Query\Join::class) as $join) {
$next = max($next, $join->joinOrder);
}
return $next + 1;
}
}