getFragment(Query\Select::class) ) ) { $select->add($field); } else { $select = new Query\Select(); $select->set($field); $this->push($select); } return $this; } public function insert(array $dataset) : self { if ( null === $this->getFragment(Query\Insert::class) ) { $insert = new Query\Insert(); $this->push($insert); } $insert->set($dataset); return $this; } public function values(array $dataset) : self { $this->addValues(array_values($dataset)); return $this; } 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 from(string $table, ? string $alias, ? string $schema) : self { return $this; } public function dn(string $dn) : self { if ( null === ( $update = $this->getFragment(Query\Dn::class) ) ) { $update = new Query\Dn(); $this->push($update); } $update->dn = $dn; return $this; } public function add(string $dn) : self { if ( null === ( $add = $this->getFragment(Query\Add::class) ) ) { $add = new Query\Add(); $this->push($add); } $add->dn = $dn; return $this; } public function update(string $dn) : self { if ( null === ( $update = $this->getFragment(Query\Update::class) ) ) { $update = new Query\Update(); $this->push($update); } $update->dn = $dn; return $this; } public function delete(string $dn) : self { if ( null === ( $delete = $this->getFragment(Query\Delete::class) ) ) { $delete = new Query\Delete(); $this->push($delete); } $delete->dn = $dn; return $this; } public function open(string $condition = Ulmus\Query\Where::CONDITION_AND) : self { if ( null !== ($this->where ?? null) ) { $this->where->conditionList[] = $new = new Query\Filter($this, $condition); $this->where = $new; } else { $this->where = new Query\Filter($this, $condition); $this->push($this->where); $this->where->conditionList[] = $new = new Query\Filter($this, $condition); $this->where = $new; } return $this; } 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; } return $this; } public function where(/* stringable*/ $field, $value, string $operator = Ulmus\Query\Where::OPERATOR_EQUAL, string $condition = Ulmus\Query\Where::CONDITION_AND, bool $not = false) : self { # Empty IN case if ( [] === $value ) { return $this; } if ( $this->where ?? false ) { $where = $this->where; } elseif ( null === ( $where = $this->getFragment(Query\Filter::class) ) ) { $this->where = $where = new Query\Filter($this); $this->push($where); } $this->whereConditionOperator = $operator; $where->add($field, $value, $operator, $condition, $not); return $this; } public function notWhere($field, $value, string $operator = Ulmus\Query\Where::OPERATOR_EQUAL, string $condition = Ulmus\Query\Where::CONDITION_AND) : self { return $this->where($field, $value, $operator, $condition, true); } public function limit(int $value) : self { if ( null === $limit = $this->getFragment(Ulmus\Query\Limit::class) ) { $limit = new Query\Limit(); $this->push($limit); } $limit->set($value); return $this; } public function offset(int $value) : self { if ( null === $offset = $this->getFragment(Ulmus\Query\Offset::class) ) { $offset = new Ulmus\Query\Offset(); $this->push($offset); } $offset->set($value); return $this; } public function orderBy(string $field, ? string $direction = null) : self { return $this; } public function create(array $fieldlist, string $table, ? string $database = null, ? string $schema = null) : self { return $this; } public function push(Ulmus\Query\QueryFragmentInterface $queryFragment) : self { $this->queryStack[] = $queryFragment; return $this; } public function pull(Ulmus\Query\QueryFragmentInterface $queryFragment) : self { return array_shift($this->queryStack); } public function render(bool $skipToken = false) : array { $stack = []; foreach($this->queryStack as $fragment) { $stack = array_merge($stack, (array) $fragment->render($skipToken)); } return $stack; } public function reset() : void { $this->parameters = $this->values = $this->queryStack = []; $this->whereConditionOperator = Ulmus\Query\Where::CONDITION_AND; $this->havingConditionOperator = Ulmus\Query\Where::CONDITION_AND; $this->parameterIndex = 0; unset($this->where, $this->having); } public function getFragment(string $class, int $index = 0) : ? Ulmus\Query\Fragment { foreach($this->queryStack as $item) { if ( get_class($item) === $class ) { if ( $index-- === 0 ) { return $item; } } } return null; } public function removeFragment(array|string|Ulmus\Query\QueryFragmentInterface|\Stringable $fragment) : void { foreach($this->queryStack as $key => $item) { if ( $item === $fragment ) { unset($this->queryStack[$key]); } } } public function __toString() : string { return ""; # return $this->render(); } 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; return $key; } public function addValues(array $values) : void { $this->values = $values; } public function hashSerializedQuery(): string { return serialize($this->render()); } }