45 public function __construct(
protected readonly Connection $backup_connection) {
46 parent::__construct($backup_connection);
57 return $this->backup_connection;
70 $qb->from($table, $alias);
88 $expr = $clause->
prepare($this, $alias);
89 if ($clause instanceof
WhereClause && ($expr instanceof CompositeExpression || is_string($expr))) {
90 $this->andWhere($expr);
105 if ($prefix ===
'') {
109 if (!str_starts_with($table, $prefix)) {
110 return "{$prefix}{$table}";
146 $parameters = $this->getParameters();
153 $type = ParameterType::INTEGER;
158 $type = ParameterType::INTEGER;
162 $type = ParameterType::INTEGER;
166 $type = ParameterType::INTEGER;
171 $type = ParameterType::STRING;
176 $type = ParameterType::INTEGER;
186 if (
$type === ParameterType::INTEGER) {
187 $type = ArrayParameterType::INTEGER;
189 $type = ArrayParameterType::STRING;
202 public function execute(
bool $track_query =
true) {
204 if ($this instanceof
Select) {
205 return parent::executeQuery();
207 return parent::executeStatement();
212 if ($this instanceof
Select) {
213 return parent::executeQuery();
215 return parent::executeStatement();
225 public function from(
string $table, ?
string $alias = null): self {
226 $this->table_name =
$table;
227 $this->table_alias = $alias;
229 return parent::from($this->
prefix($table), $alias);
238 $this->table_name =
$table;
240 return parent::insert($this->
prefix($table));
249 $this->table_name =
$table;
251 return parent::update($this->
prefix($table));
259 public function delete(
string $table):
self {
260 $this->table_name =
$table;
268 public function join(
string $fromAlias,
string $join,
string $alias, ?
string $condition = null): self {
269 return parent::join($fromAlias, $this->
prefix($join), $alias, $condition);
275 public function innerJoin(
string $fromAlias,
string $join,
string $alias, ?
string $condition = null): self {
276 return parent::innerJoin($fromAlias, $this->
prefix($join), $alias, $condition);
282 public function leftJoin(
string $fromAlias,
string $join,
string $alias, ?
string $condition = null): self {
283 return parent::leftJoin($fromAlias, $this->
prefix($join), $alias, $condition);
289 public function rightJoin(
string $fromAlias,
string $join,
string $alias, ?
string $condition = null): self {
290 return parent::rightJoin($fromAlias, $this->
prefix($join), $alias, $condition);
296 public function orderBy(
string $sort, ?
string $order = null): self {
297 if (isset($order) && !in_array(
strtoupper($order), [
'ASC',
'DESC'])) {
298 throw new DomainException(
"'{$order}' is not a valid order by direction");
301 return parent::orderBy($sort, $order);
307 public function addOrderBy(
string $sort, ?
string $order = null): self {
308 if (isset($order) && !in_array(
strtoupper($order), [
'ASC',
'DESC'])) {
309 throw new DomainException(
"'{$order}' is not a valid order by direction");
312 return parent::addOrderBy($sort, $order);
323 public function merge($parts = null, $boolean =
'AND') {
328 $parts = (array) $parts;
330 $parts = array_filter($parts,
function ($e) {
335 if (!$e instanceof CompositeExpression && !is_string($e)) {
345 if (
count($parts) === 1) {
346 return array_shift($parts);
351 $parts = array_values($parts);
353 return call_user_func_array([$this->expr(),
'or'], $parts);
356 return call_user_func_array([$this->expr(),
'and'], $parts);
375 public function compare(
string $x,
string $comparison, $y = null,
string $type = null,
bool $case_sensitive = null) {
389 public function between(
string $x, $lower = null, $upper = null,
string $type = null) {
399 return $this->
merge($wheres);
410 return "qbt{$this->join_index}";
423 public function joinEntitiesTable(
string $from_alias =
'',
string $from_column =
'guid', ?
string $join_type =
'inner',
string $joined_alias = null): string {
424 if (in_array($joined_alias, $this->joins)) {
425 return $joined_alias;
429 $from_column =
"{$from_alias}.{$from_column}";
432 $hash = sha1(serialize([
438 if (!isset($joined_alias) && !empty($this->joins[$hash])) {
439 return $this->joins[$hash];
442 $condition =
function (
QueryBuilder $qb, $joined_alias) use ($from_column) {
443 return $qb->
compare(
"{$joined_alias}.guid",
'=', $from_column);
447 $joined_alias = $clause->prepare($this, $from_alias);
449 $this->joins[$hash] = $joined_alias;
451 return $joined_alias;
465 public function joinMetadataTable(
string $from_alias =
'',
string $from_column =
'guid',
$name = null, ?
string $join_type =
'inner',
string $joined_alias = null): string {
466 if (in_array($joined_alias, $this->joins)) {
467 return $joined_alias;
471 $from_column =
"{$from_alias}.{$from_column}";
474 $hash = sha1(serialize([
481 if (!isset($joined_alias) && !empty($this->joins[$hash])) {
482 return $this->joins[$hash];
485 $condition =
function (
QueryBuilder $qb, $joined_alias) use ($from_column, $name) {
487 $qb->
compare(
"{$joined_alias}.entity_guid",
'=', $from_column),
494 $joined_alias = $clause->prepare($this, $from_alias);
496 $this->joins[$hash] = $joined_alias;
498 return $joined_alias;
512 public function joinAnnotationTable(
string $from_alias =
'',
string $from_column =
'guid',
$name = null, ?
string $join_type =
'inner',
string $joined_alias = null): string {
513 if (in_array($joined_alias, $this->joins)) {
514 return $joined_alias;
518 $from_column =
"{$from_alias}.{$from_column}";
521 $hash = sha1(serialize([
528 if (!isset($joined_alias) && !empty($this->joins[$hash])) {
529 return $this->joins[$hash];
532 $condition =
function (
QueryBuilder $qb, $joined_alias) use ($from_column, $name) {
534 $qb->
compare(
"{$joined_alias}.entity_guid",
'=', $from_column),
541 $joined_alias = $clause->prepare($this, $from_alias);
543 $this->joins[$hash] = $joined_alias;
545 return $joined_alias;
560 public function joinRelationshipTable(
string $from_alias =
'',
string $from_column =
'guid',
$name = null,
bool $inverse =
false, ?
string $join_type =
'inner',
string $joined_alias = null): string {
561 if (in_array($joined_alias, $this->joins)) {
562 return $joined_alias;
566 $from_column =
"{$from_alias}.{$from_column}";
569 $hash = sha1(serialize([
577 if (!isset($joined_alias) && !empty($this->joins[$hash])) {
578 return $this->joins[$hash];
581 $condition =
function (
QueryBuilder $qb, $joined_alias) use ($from_column, $name, $inverse) {
584 $parts[] = $qb->
compare(
"{$joined_alias}.guid_one",
'=', $from_column);
586 $parts[] = $qb->
compare(
"{$joined_alias}.guid_two",
'=', $from_column);
590 return $qb->
merge($parts);
595 $joined_alias = $clause->prepare($this, $from_alias);
597 $this->joins[$hash] = $joined_alias;
599 return $joined_alias;
getTableName()
Returns the name of the primary table.
joinAnnotationTable(string $from_alias= '', string $from_column= 'guid', $name=null,?string $join_type= 'inner', string $joined_alias=null)
Join annotations table from alias and return joined table alias.
Utility class for building composite comparison expression.
static normalizeGuids(...$args)
Flatten an array of data into an array of GUIDs.
from(string $table,?string $alias=null)
{}
innerJoin(string $fromAlias, string $join, string $alias,?string $condition=null)
{}
param($value, string $type=ELGG_VALUE_STRING, string $key=null)
Sets a new parameter assigning it a unique parameter key/name if none provided Returns the name of th...
Saves user notification settings.
if(!$user||!$user->canDelete()) $name
subquery(string $table, string $alias=null)
Creates a new SelectQueryBuilder for join/where sub queries using the DB connection of the primary Qu...
Interface that allows resolving statements and/or extending query builder.
const ELGG_VALUE_INTEGER
Value types.
addClause(Clause $clause, string $alias=null)
Apply clause to this instance.
Exception thrown if a value does not adhere to a defined valid data domain.
Extends QueryBuilder with JOIN clauses.
Database abstraction query builder.
__construct(protected readonly Connection $backup_connection)
Initializes a new QueryBuilder.
getConnection()
Returns the connection.
orderBy(string $sort,?string $order=null)
{}
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
prepare(QueryBuilder $qb, $table_alias=null)
Build an expression and/or apply it to an instance of query builder.
static normalizeTimestamp($time)
Returns timestamp value of the time representation.
compare(string $x, string $comparison, $y=null, string $type=null, bool $case_sensitive=null)
Build value comparison clause.
static normalizeIds(...$args)
Prepare IDs.
joinEntitiesTable(string $from_alias= '', string $from_column= 'guid',?string $join_type= 'inner', string $joined_alias=null)
Join entity table from alias and return joined table alias.
joinRelationshipTable(string $from_alias= '', string $from_column= 'guid', $name=null, bool $inverse=false,?string $join_type= 'inner', string $joined_alias=null)
Join relationship table from alias and return joined table alias.
getNextJoinAlias()
Get an index of the next available join alias.
const ELGG_VALUE_TIMESTAMP
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
joinMetadataTable(string $from_alias= '', string $from_column= 'guid', $name=null,?string $join_type= 'inner', string $joined_alias=null)
Join metadata table from alias and return joined table alias.
getTableAlias()
Returns the alias of the primary table.
Builds a clause from closure or composite expression.
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
addOrderBy(string $sort,?string $order=null)
{}
prefix(string $table)
Prefixes the table name with installation DB prefix.
execute(bool $track_query=true)
rightJoin(string $fromAlias, string $join, string $alias,?string $condition=null)
{}
_elgg_services()
Get the global service provider.
join(string $fromAlias, string $join, string $alias,?string $condition=null)
{}
leftJoin(string $fromAlias, string $join, string $alias,?string $condition=null)
{}
between(string $x, $lower=null, $upper=null, string $type=null)
Build a between clause.
Query builder for fetching data from the database.