class Api4SelectQuery extends SelectQuery

A query node may be in one of three formats:

  • leaf: [$fieldName, $operator, $criteria]
  • negated: ['NOT', $node]
  • branch: ['OR|NOT', [$node, $node, ...]]

Leaf operators are one of:

  • '=', '<=', '>=', '>', '<', 'LIKE', "<>", "!=",
  • "NOT LIKE", 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN',
  • 'IS NOT NULL', or 'IS NULL'.

Constants

MAX_JOINS

MAIN_TABLE_ALIAS

Properties

protected string $entity from SelectQuery
$select from SelectQuery
$where from SelectQuery
$orderBy from SelectQuery
$limit from SelectQuery
$offset from SelectQuery
protected array $selectFields from SelectQuery
bool $isFillUniqueFields from SelectQuery
protected CRM_Utils_SQL_Select $query from SelectQuery
protected array $joins from SelectQuery
protected array $apiFieldSpec from SelectQuery
protected array $entityFieldNames from SelectQuery
protected array $aclFields from SelectQuery
protected string|bool $checkPermissions from SelectQuery
protected int $apiVersion
protected array $fkSelectAliases
protected Joinable[] $joinedTables

Methods

__construct(string $entity, bool $checkPermissions, array $fields)

No description

array|int
run()

Why walk when you can

merge(CRM_Utils_SQL_Select $sqlFragment)

No description

array|null
addFkField($fkFieldName, $side)

Joins onto an fk field

getJoinInfo($fkField, $stack)

Get join info for dynamically-joined fields (e.g. "entity_id", "option_group")

array
addCustomField(array $customField, string $side, string $baseTable = self::MAIN_TABLE_ALIAS)

Joins onto a custom field

array|null
getField(string $fieldName)

Fetch a field from the getFields list

validateNestedInput($fieldName, $value)

Perform input validation on params that use the join syntax

bool
checkPermissionToJoin(string $entity, array $fieldStack)

Check permission to join onto another api entity

array
getAclClause(string $tableAlias, string $baoName, array $stack = [])

Get acl clause for an entity

buildOrderBy()

No description

join(string $side, string $tableName, string $tableAlias, array $conditions)

No description

buildWhereClause()

No description

buildSelectFields()

Populate $this->selectFields

array
getFields()

No description

addJoins()

Gets all FK fields and does the required joins

string
treeWalkWhereClause(array $clause)

Recursively validate and transform a branch or leaf clause array to SQL.

string
validateClauseAndComposeSql(array $clause)

Validate and transform a leaf clause array to SQL.

joinFK($key)

No description

$this
addJoinedTable(Joinable $joinable)

No description

FALSE|string
getFrom()

No description

string
getEntity()

No description

array
getSelect()

No description

array
getWhere()

No description

array
getOrderBy()

No description

mixed
getLimit()

No description

mixed
getOffset()

No description

array
getSelectFields()

No description

bool
isFillUniqueFields()

No description

getQuery()

No description

array
getJoins()

No description

array
getApiFieldSpec()

No description

array
getEntityFieldNames()

No description

array
getAclFields()

No description

bool|string
getCheckPermissions()

No description

int
getApiVersion()

No description

array
getFkSelectAliases()

No description

getJoinedTables()

No description

getJoinedTable($alias)

No description

void
getTableName(string $baoName)

Get table name on basis of entity

array
getPathJoinTypes(string $pathString)

Separates a string like 'emails.location_type.label' into an array, where each value in the array tells whether it is 1-1 or 1-n join type

Details

at line 77
__construct(string $entity, bool $checkPermissions, array $fields)

Parameters

string $entity
bool $checkPermissions
array $fields

at line 99
array|int run()

Why walk when you can

Return Value

array|int

in SelectQuery at line 174
SelectQuery merge(CRM_Utils_SQL_Select $sqlFragment)

Parameters

CRM_Utils_SQL_Select $sqlFragment

Return Value

SelectQuery

in SelectQuery at line 194
protected array|null addFkField($fkFieldName, $side)

Joins onto an fk field

Adds one or more joins to the query to make this field available for use in a clause.

Enforces permissions at the api level and by appending the acl clause for that entity to the join.

Parameters

$fkFieldName
$side

Return Value

array|null Returns the table and field name for adding this field to a SELECT or WHERE clause

Exceptions

API_Exception
UnauthorizedException

in SelectQuery at line 275
protected getJoinInfo($fkField, $stack)

Get join info for dynamically-joined fields (e.g. "entity_id", "option_group")

Parameters

$fkField
$stack

in SelectQuery at line 303
protected array addCustomField(array $customField, string $side, string $baseTable = self::MAIN_TABLE_ALIAS)

Joins onto a custom field

Adds a join to the query to make this field available for use in a clause.

Parameters

array $customField
string $side
string $baseTable

Return Value

array Returns the table and field name for adding this field to a SELECT or WHERE clause

at line 322
protected array|null getField(string $fieldName)

Fetch a field from the getFields list

Parameters

string $fieldName

Return Value

array|null

in SelectQuery at line 329
protected validateNestedInput($fieldName, $value)

Perform input validation on params that use the join syntax

Arguably this should be done at the api wrapper level, but doing it here provides a bit more consistency in that api permissions to perform the join are checked first.

Parameters

$fieldName
$value

Exceptions

Exception

in SelectQuery at line 350
protected bool checkPermissionToJoin(string $entity, array $fieldStack)

Check permission to join onto another api entity

Parameters

string $entity
array $fieldStack The stack of fields leading up to this join

Return Value

bool

in SelectQuery at line 384
protected array getAclClause(string $tableAlias, string $baoName, array $stack = [])

Get acl clause for an entity

Parameters

string $tableAlias
string $baoName
array $stack

Return Value

array

at line 220
protected buildOrderBy()

in SelectQuery at line 442
join(string $side, string $tableName, string $tableAlias, array $conditions)

Parameters

string $side
string $tableName
string $tableAlias
array $conditions

at line 210
protected buildWhereClause()

at line 179
protected buildSelectFields()

Populate $this->selectFields

at line 311
protected array getFields()

Return Value

array

at line 146
protected addJoins()

Gets all FK fields and does the required joins

at line 244
protected string treeWalkWhereClause(array $clause)

Recursively validate and transform a branch or leaf clause array to SQL.

Parameters

array $clause

Return Value

string SQL where clause

at line 280
protected string validateClauseAndComposeSql(array $clause)

Validate and transform a leaf clause array to SQL.

Parameters

array $clause [$fieldName, $operator, $criteria]

Return Value

string SQL

Exceptions

API_Exception
Exception

at line 337
protected joinFK($key)

Parameters

$key

Exceptions

API_Exception

at line 390
$this addJoinedTable(Joinable $joinable)

Parameters

Joinable $joinable

Return Value

$this

at line 399
FALSE|string getFrom()

Return Value

FALSE|string

at line 406
string getEntity()

Return Value

string

at line 413
array getSelect()

Return Value

array

at line 420
array getWhere()

Return Value

array

at line 427
array getOrderBy()

Return Value

array

at line 434
mixed getLimit()

Return Value

mixed

at line 441
mixed getOffset()

Return Value

mixed

at line 448
array getSelectFields()

Return Value

array

at line 455
bool isFillUniqueFields()

Return Value

bool

at line 462
CRM_Utils_SQL_Select getQuery()

Return Value

CRM_Utils_SQL_Select

at line 469
array getJoins()

Return Value

array

at line 476
array getApiFieldSpec()

Return Value

array

at line 483
array getEntityFieldNames()

Return Value

array

at line 490
array getAclFields()

Return Value

array

at line 497
bool|string getCheckPermissions()

Return Value

bool|string

at line 504
int getApiVersion()

Return Value

int

at line 511
array getFkSelectAliases()

Return Value

array

at line 518
Joinable[] getJoinedTables()

Return Value

Joinable[]

at line 525
Joinable getJoinedTable($alias)

Parameters

$alias

Return Value

Joinable

at line 540
void getTableName(string $baoName)

Get table name on basis of entity

Parameters

string $baoName

Return Value

void

at line 561
array getPathJoinTypes(string $pathString)

Separates a string like 'emails.location_type.label' into an array, where each value in the array tells whether it is 1-1 or 1-n join type

Parameters

string $pathString Dot separated path to the field

Return Value

array Index is table alias and value is boolean whether is 1-to-many join