API / Minn\Query / MetaSql

MetaSql final class

Minn\Query\MetaSql · public/minn/src/Minn/Query/MetaSql.php · 291 lines

The JOIN and WHERE fragments of a meta query in the reference's shape: one meta-table join per clause (the first under the table's own name, then mt1, mt2, ...), INNER joins unless an OR relation or a NOT EXISTS clause needs LEFT ones, and casts by the clause's declared type. Under an OR relation a clause shares an equality-shaped sibling's join.

Uses 1

Constants and properties

privateCOMPATIBLE = array ( 0 => '=', 1 => 'IN', 2 => 'BETWEEN', 3 => 'LIKE', 4 => 'REGEXP', 5 => 'RLIKE', 6 => '>', 7 => '>=', 8 => '<', 9 => '<=', )
privateOPERATORS = array ( 0 => '=', 1 => '!=', 2 => '>', 3 => '>=', 4 => '<', 5 => '<=', 6 => 'LIKE', 7 => 'NOT LIKE', 8 => 'IN', 9 => 'NOT IN', 10 => 'BETWEEN', 11 => 'NOT BETWEEN', 12 => 'EXISTS', 13 => 'NOT EXISTS', 14 => 'REGEXP', 15 => 'NOT REGEXP', 16 => 'RLIKE', )
privatearray $clauses
privateint $aliasCount
private?Closure $aliasFilter
privatereadonly string $metaTable

Promoted from the constructor.

privatereadonly string $objectColumn

Promoted from the constructor.

privatereadonly string $primaryTable

Promoted from the constructor.

privatereadonly string $primaryId

Promoted from the constructor.

Public

new__construct(string $metaTable, string $objectColumn, string $primaryTable, string $primaryId)#
staticsanitize(array $queries): array#

Normalises a raw meta_query: clauses get key/value/compare/type defaults, a nested group its relation, and a "relation" key sits on every level. Named clauses keep their names.

staticisFirstOrder(array $query): bool#

A clause rather than a group: it names a key or a value.

methodbuild(array $queries): array#

The JOIN and WHERE fragments for a meta query.

@return array{join: string, where: string}
methodsqlClauses(array $queries): array#

The whole query's JOIN and WHERE (get_sql_clauses), each clause given its alias in place. @return array{join: string, where: string}

@return array{join: string, where: string}
methodquerySql(array $queries, int $depth): array#

A group's JOIN and WHERE (get_sql_for_query), indented for its depth; each clause is given its alias in place, so its siblings can share it.

@return array{join: string, where: string}
methodclauseSql(array $clause, array $parent, string $key): array#

One clause's JOIN and WHERE pieces (get_sql_for_clause): the clause is settled in place (compare, compare_key, then its alias and cast), its alias shared with a compatible sibling or a new join.

@return array{where: list<string>, join: list<string>}
methodfilterAliasesWith(Closure $filter): self#

Has every clause's shared alias (or false) pass through a filter (WP_Meta_Query hands it meta_query_find_compatible_table_alias).

methodcompatibleAlias(array $clause, array $parent): string|false#

The alias of a sibling the clause can share (find_compatible_table_alias): under OR, the first sibling already given an alias whose comparison, like the clause's, reads the same join; false otherwise.

methodclauses(): array#

The clauses the last build resolved, by name.

@return array<string, array{alias: string, cast: string}> clauses seen while building, by name or alias
statichasOr(array $queries): bool#

Whether any level of the query relates two or more clauses by OR.

staticcast(string $type): string#

The CAST target for a clause type; CHAR means no cast.

Internals 7

Private and protected methods: how the class does its work. Not a contract; they change whenever the code is tidied.

privatenormalized(array $clause): array#

A clause with its compare and compare_key settled: upper case, IN for a list, = for anything unknown. @return array<string, mixed>

@return array<string, mixed>
privatehasOrWithNotExists(array $queries): bool#

An OR group holding a NOT EXISTS clause makes every join a LEFT one.

privateclause(array $clause): string#

A settled clause's WHERE piece, read from its alias.

privatekeyClause(string $alias, array $clause): string#
privatevalueClause(string $compare, mixed $value): string#
privatevalues(mixed $value): array#
@return list<string> a list, or a comma-separated string split
privatenewAlias(array $clause): array#

A new alias for a clause, and the join that reads it: LEFT for NOT EXISTS (with the key), INNER otherwise. @return array{0: string, 1: list<string>}

@return array{0: string, 1: list<string>}