Drupal 8  8.0.2
Sql Class Reference
Inheritance diagram for Sql:

Public Member Functions

 init (ViewExecutable $view, DisplayPluginBase $display, array &$options=NULL)
 
 setCountField ($table, $field, $alias=NULL)
 
 buildOptionsForm (&$form, FormStateInterface $form_state)
 
 submitOptionsForm (&$form, FormStateInterface $form_state)
 
 addRelationship ($alias, JoinPluginBase $join, $base, $link_point=NULL)
 
 addTable ($table, $relationship=NULL, JoinPluginBase $join=NULL, $alias=NULL)
 
 queueTable ($table, $relationship=NULL, JoinPluginBase $join=NULL, $alias=NULL)
 
 ensureTable ($table, $relationship=NULL, JoinPluginBase $join=NULL)
 
 getJoinData ($table, $base_table)
 
 getTableInfo ($table)
 
 addField ($table, $field, $alias= '', $params=array())
 
 clearFields ()
 
 addWhere ($group, $field, $value=NULL, $operator=NULL)
 
 addWhereExpression ($group, $snippet, $args=array())
 
 addHavingExpression ($group, $snippet, $args=array())
 
 addOrderBy ($table, $field=NULL, $order= 'ASC', $alias= '', $params=array())
 
 addGroupBy ($clause)
 
 addTag ($tag)
 
 placeholder ($base= 'views')
 
 query ($get_count=FALSE)
 
 getWhereArgs ()
 
 alter (ViewExecutable $view)
 
 build (ViewExecutable $view)
 
 execute (ViewExecutable $view)
 
 loadEntities (&$results)
 
 getCacheTags ()
 
 getCacheMaxAge ()
 
 addSignature (ViewExecutable $view)
 
 getAggregationInfo ()
 
 aggregationMethodSimple ($group_type, $field)
 
 aggregationMethodDistinct ($group_type, $field)
 
 getDateField ($field)
 
 setupTimezone ()
 
 getDateFormat ($field, $format, $string_date=FALSE)
 
- Public Member Functions inherited from QueryPluginBase
 query ($get_count=FALSE)
 
 alter (ViewExecutable $view)
 
 build (ViewExecutable $view)
 
 execute (ViewExecutable $view)
 
 addSignature (ViewExecutable $view)
 
 getAggregationInfo ()
 
 validateOptionsForm (&$form, FormStateInterface $form_state)
 
 submitOptionsForm (&$form, FormStateInterface $form_state)
 
 summaryTitle ()
 
 calculateDependencies ()
 
 setLimit ($limit)
 
 setOffset ($offset)
 
 getLimit ()
 
 setWhereGroup ($type= 'AND', $group=NULL, $where= 'where')
 
 setGroupOperator ($type= 'AND')
 
 loadEntities (&$results)
 
 getDateField ($field)
 
 setupTimezone ()
 
 getDateFormat ($field, $format, $string_date=FALSE)
 
 getEntityTableInfo ()
 
 getCacheMaxAge ()
 
 getCacheContexts ()
 
 getCacheTags ()
 
- Public Member Functions inherited from PluginBase
 __construct (array $configuration, $plugin_id, $plugin_definition)
 
 init (ViewExecutable $view, DisplayPluginBase $display, array &$options=NULL)
 
 filterByDefinedOptions (array &$storage)
 
 unpackOptions (&$storage, $options, $definition=NULL, $all=TRUE, $check=TRUE)
 
 destroy ()
 
 buildOptionsForm (&$form, FormStateInterface $form_state)
 
 validateOptionsForm (&$form, FormStateInterface $form_state)
 
 submitOptionsForm (&$form, FormStateInterface $form_state)
 
 query ()
 
 themeFunctions ()
 
 validate ()
 
 summaryTitle ()
 
 pluginTitle ()
 
 usesOptions ()
 
 globalTokenReplace ($string= '', array $options=array())
 
 getAvailableGlobalTokens ($prepared=FALSE, array $types=array())
 
 globalTokenForm (&$form, FormStateInterface $form_state)
 
 calculateDependencies ()
 
 getProvider ()
 
- Public Member Functions inherited from PluginInspectionInterface
 getPluginId ()
 
 getPluginDefinition ()
 
- Public Member Functions inherited from DerivativeInspectionInterface
 getBaseId ()
 
 getDerivativeId ()
 

Data Fields

 $tables = array()
 
 $relationships = array()
 
 $where = array()
 
 $having = array()
 
 $orderby = array()
 
 $groupby = array()
 
 $fields = array()
 
 $distinct = FALSE
 
 $tags = array()
 
- Data Fields inherited from QueryPluginBase
 $pager = NULL
 
- Data Fields inherited from PluginBase
const INCLUDE_NEGOTIATED = 16
 
const INCLUDE_ENTITY = 32
 
const VIEWS_QUERY_LANGUAGE_SITE_DEFAULT = '***LANGUAGE_site_default***'
 
 $options = array()
 
 $view = NULL
 
 $displayHandler
 
 $definition
 

Protected Member Functions

 setDistinct ($value=TRUE)
 
 defineOptions ()
 
 markTable ($table, $relationship, $alias)
 
 ensurePath ($table, $relationship=NULL, $join=NULL, $traced=array(), $add=array())
 
 adjustJoin ($join, $relationship)
 
 getFieldAlias ($table_alias, $field)
 
 buildCondition ($where= 'where')
 
 getNonAggregates ()
 
 compileFields ($query)
 
 getAllEntities ()
 
- Protected Member Functions inherited from PluginBase
 defineOptions ()
 
 setOptionDefaults (array &$storage, array $options)
 
 doFilterByDefinedOptions (array &$storage, array $options)
 
 viewsTokenReplace ($text, $tokens)
 
 listLanguages ($flags=LanguageInterface::STATE_ALL, array $current_values=NULL)
 
 getRenderer ()
 

Protected Attributes

 $tableQueue = array()
 
 $groupOperator = 'AND'
 
 $hasAggregate = FALSE
 
 $getCountOptimized = NULL
 
 $fieldAliases = array()
 
 $noDistinct
 
- Protected Attributes inherited from QueryPluginBase
 $limit
 
- Protected Attributes inherited from PluginBase
 $usesOptions = FALSE
 
 $renderer
 

Additional Inherited Members

- Static Public Member Functions inherited from PluginBase
static create (ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition)
 
static preRenderAddFieldsetMarkup (array $form)
 
static preRenderFlattenData ($form)
 
static queryLanguageSubstitutions ()
 

Detailed Description

Views query plugin for an SQL query.

( id = "views_query", title = ("SQL Query"), help = ("Query will be generated and run using the Drupal database API.") )

Member Function Documentation

addField (   $table,
  $field,
  $alias = '',
  $params = array() 
)

Add a field to the query table, possibly with an alias. This will automatically call ensureTable to make sure the required table exists, unless $table is unset.

Parameters
$tableThe table this field is attached to. If NULL, it is assumed this will be a formula; otherwise, ensureTable is used to make sure the table exists.
$fieldThe name of the field to add. This may be a real field or a formula.
$aliasThe alias to create. If not specified, the alias will be $table_$field unless $table is NULL. When adding formulae, it is recommended that an alias be used.
$paramsAn array of parameters additional to the field that will control items such as aggregation functions and DISTINCT. Some values that are recognized:
  • function: An aggregation function to apply, such as SUM.
  • aggregate: Set to TRUE to indicate that this value should be aggregated in a GROUP BY.
Returns
$name The name that this field can be referred to as. Usually this is the alias.

References Sql\ensureTable().

Referenced by Sql\addOrderBy(), and Sql\query().

Here is the call graph for this function:

Here is the caller graph for this function:

addGroupBy (   $clause)

Add a simple GROUP BY clause to the query. The caller is responsible for ensuring that the fields are fully qualified and the table is properly added.

Referenced by Sql\query().

Here is the caller graph for this function:

addHavingExpression (   $group,
  $snippet,
  $args = array() 
)

Add a complex HAVING clause to the query. The caller is responsible for ensuring that all fields are fully qualified (TABLE.FIELD) and that the table and an appropriate GROUP BY already exist in the query. Internally the dbtng method "having" is used.

Parameters
$groupThe HAVING group to add these to; groups are used to create AND/OR sections. Groups cannot be nested. Use 0 as the default group. If the group does not yet exist it will be created as an AND group.
$snippetThe snippet to check. This can be either a column or a complex expression like "COUNT(table.field) > 3"
$argsAn associative array of arguments.
See Also
QueryConditionInterface::having()

References QueryPluginBase\setWhereGroup().

Here is the call graph for this function:

addOrderBy (   $table,
  $field = NULL,
  $order = 'ASC',
  $alias = '',
  $params = array() 
)

Add an ORDER BY clause to the query.

Parameters
$tableThe table this field is part of. If a formula, enter NULL. If you want to orderby random use "rand" as table and nothing else.
$fieldThe field or formula to sort on. If already a field, enter NULL and put in the alias.
$orderEither ASC or DESC.
$aliasThe alias to add the field as. In SQL, all fields in the order by must also be in the SELECT portion. If an $alias isn't specified one will be generated for from the $field; however, if the $field is a formula, this alias will likely fail.
$paramsAny params that should be passed through to the addField.

References Sql\addField(), and Sql\ensureTable().

Here is the call graph for this function:

addRelationship (   $alias,
JoinPluginBase  $join,
  $base,
  $link_point = NULL 
)

A relationship is an alternative endpoint to a series of table joins. Relationships must be aliases of the primary table and they must join either to the primary table or to a pre-existing relationship.

An example of a relationship would be a nodereference table. If you have a nodereference named 'book_parent' which links to a parent node, you could set up a relationship 'node_book_parent' to 'node'. Then, anything that links to 'node' can link to 'node_book_parent' instead, thus allowing all properties of both nodes to be available in the query.

Parameters
$aliasWhat this relationship will be called, and is also the alias for the table.
\Drupal\views\Plugin\views\join\JoinPluginBase$joinA Join object (or derived object) to join the alias in.
$baseThe name of the 'base' table this relationship represents; this tells the join search which path to attempt to use when finding the path to this relationship.
$link_pointIf this relationship links to something other than the primary table, specify that table here. For example, a 'track' node might have a relationship to an 'album' node, which might have a relationship to an 'artist' node.

References Sql\adjustJoin().

Here is the call graph for this function:

addTable (   $table,
  $relationship = NULL,
JoinPluginBase  $join = NULL,
  $alias = NULL 
)

Add a table to the query, ensuring the path exists.

This function will test to ensure that the path back to the primary table is valid and exists; if you do not wish for this testing to occur, use $query->queueTable() instead.

Parameters
$tableThe name of the table to add. It needs to exist in the global table array.
$relationshipAn alias of a table; if this is set, the path back to this table will be tested prior to adding the table, making sure that all intermediary tables exist and are properly aliased. If set to NULL the path to the primary table will be ensured. If the path cannot be made, the table will NOT be added.
\Drupal\views\Plugin\views\join\JoinPluginBase$joinIn some join configurations this table may actually join back through a different method; this is most likely to be used when tracing a hierarchy path. (node->parent->parent2->parent3). This parameter will specify how this table joins if it is not the default.
$aliasA specific alias to use, rather than the default alias.
Returns
$alias The alias of the table; this alias can be used to access information about the table and should always be used to refer to the table when adding parts to the query. Or FALSE if the table was not able to be added.

References Sql\adjustJoin(), Sql\ensurePath(), and Sql\queueTable().

Here is the call graph for this function:

addTag (   $tag)

Adds a query tag to the sql object.

See Also
SelectQuery::addTag()

Referenced by Sql\query().

Here is the caller graph for this function:

addWhere (   $group,
  $field,
  $value = NULL,
  $operator = NULL 
)

Add a simple WHERE clause to the query. The caller is responsible for ensuring that all fields are fully qualified (TABLE.FIELD) and that the table already exists in the query.

Parameters
$groupThe WHERE group to add these to; groups are used to create AND/OR sections. Groups cannot be nested. Use 0 as the default group. If the group does not yet exist it will be created as an AND group.
$fieldThe name of the field to check.
$valueThe value to test the field against. In most cases, this is a scalar. For more complex options, it is an array. The meaning of each element in the array is dependent on the $operator.
$operatorThe comparison operator, such as =, <, or >=. It also accepts more complex options such as IN, LIKE, LIKE BINARY, or BETWEEN. Defaults to =. If $field is a string you have to use 'formula' here.

The $field, $value and $operator arguments can also be passed in with a single DatabaseCondition object, like this:

$this->query->addWhere(
$this->options['group'],
->condition($field, $value, 'NOT IN')
->condition($field, $value, 'IS NULL')
);
See Also
::condition()

References QueryPluginBase\setWhereGroup().

Here is the call graph for this function:

addWhereExpression (   $group,
  $snippet,
  $args = array() 
)

Add a complex WHERE clause to the query.

The caller is responsible for ensuring that all fields are fully qualified (TABLE.FIELD) and that the table already exists in the query. Internally the dbtng method "where" is used.

Parameters
$groupThe WHERE group to add these to; groups are used to create AND/OR sections. Groups cannot be nested. Use 0 as the default group. If the group does not yet exist it will be created as an AND group.
$snippetThe snippet to check. This can be either a column or a complex expression like "UPPER(table.field) = 'value'"
$argsAn associative array of arguments.
See Also
QueryConditionInterface::where()

References QueryPluginBase\setWhereGroup().

Here is the call graph for this function:

adjustJoin (   $join,
  $relationship 
)
protected

Fix a join to adhere to the proper relationship; the left table can vary based upon what relationship items are joined in on.

References Sql\ensureTable().

Referenced by Sql\addRelationship(), Sql\addTable(), Sql\ensurePath(), Sql\ensureTable(), and Sql\queueTable().

Here is the call graph for this function:

Here is the caller graph for this function:

alter ( ViewExecutable  $view)

Let modules modify the query just prior to finalizing it.

References Drupal\moduleHandler().

Here is the call graph for this function:

build ( ViewExecutable  $view)

Builds the necessary info to execute the query.

References ViewExecutable\initPager(), PluginBase\query(), and Sql\setDistinct().

Here is the call graph for this function:

buildCondition (   $where = 'where')
protected

Construct the "WHERE" or "HAVING" part of the query.

As views has to wrap the conditions from arguments with AND, a special group is wrapped around all conditions. This special group has the ID 0. There is other code in filters which makes sure that the group IDs are higher than zero.

Parameters
$where'where' or 'having'.

References Sql\$where, db_and(), and db_or().

Referenced by Sql\query().

Here is the call graph for this function:

Here is the caller graph for this function:

buildOptionsForm ( $form,
FormStateInterface  $form_state 
)

Add settings for the ui.

Implements ViewsPluginInterface.

References t().

Here is the call graph for this function:

clearFields ( )

Remove all fields that may've been added; primarily used for summary mode where we're changing the query because we didn't get data we needed.

compileFields (   $query)
protected

Adds fields to the query.

Parameters
\Drupal\Core\Database\Query\SelectInterface$queryThe drupal query object.

Referenced by Sql\query().

Here is the caller graph for this function:

ensurePath (   $table,
  $relationship = NULL,
  $join = NULL,
  $traced = array(),
  $add = array() 
)
protected

Make sure that the specified table can be properly linked to the primary table in the JOINs. This function uses recursion. If the tables needed to complete the path back to the primary table are not in the query they will be added, but additional copies will NOT be added if the table is already there.

References Sql\adjustJoin(), Sql\getJoinData(), and Sql\queueTable().

Referenced by Sql\addTable(), and Sql\ensureTable().

Here is the call graph for this function:

Here is the caller graph for this function:

ensureTable (   $table,
  $relationship = NULL,
JoinPluginBase  $join = NULL 
)

Ensure a table exists in the queue; if it already exists it won't do anything, but if it doesn't it will add the table queue. It will ensure a path leads back to the relationship table.

Parameters
$tableThe unaliased name of the table to ensure.
$relationshipThe relationship to ensure the table links to. Each relationship will get a unique instance of the table being added. If not specified, will be the primary table.
\Drupal\views\Plugin\views\join\JoinPluginBase$joinA Join object (or derived object) to join the alias in.
Returns
The alias used to refer to this specific table, or NULL if the table cannot be ensured.

References Sql\adjustJoin(), Sql\ensurePath(), Sql\getJoinData(), and Sql\queueTable().

Referenced by Sql\addField(), Sql\addOrderBy(), and Sql\adjustJoin().

Here is the call graph for this function:

Here is the caller graph for this function:

execute ( ViewExecutable  $view)

Executes the query and fills the associated view object with according values.

Values to set: $view->result, $view->total_rows, $view->execute_time, $view->current_page.

References Sql\loadEntities(), Drupal\moduleHandler(), ViewExecutable\postExecute(), ViewExecutable\preExecute(), and Views\viewsData().

Here is the call graph for this function:

getAllEntities ( )
protected

Gets all the involved entities of the view.

Returns
[]
getCacheMaxAge ( )

{The maximum age for which this object may be cached.

Returns
int The maximum time in seconds that this object may be cached.
}

Implements CacheableDependencyInterface.

getCacheTags ( )

{The cache tags associated with this object.When this object is modified, these cache tags will be invalidated.

Returns
string[] A set of cache tags.
}

Implements CacheableDependencyInterface.

getDateField (   $field)

{}

getDateFormat (   $field,
  $format,
  $string_date = FALSE 
)

{}

getFieldAlias (   $table_alias,
  $field 
)
protected

Returns the alias for the given field added to $table.

protected

See Also
::addField
getJoinData (   $table,
  $base_table 
)

Retrieve join data from the larger join data cache.

Parameters
$tableThe table to get the join information for.
$base_tableThe path we're following to get this join.
Returns
A Join object or child object, if one exists.

References HandlerBase\getTableJoin().

Referenced by Sql\ensurePath(), Sql\ensureTable(), and Sql\queueTable().

Here is the call graph for this function:

Here is the caller graph for this function:

getNonAggregates ( )
protected

Returns a list of non-aggregates to be added to the "group by" clause.

Non-aggregates are fields that have no aggregation function (count, sum, etc) applied. Since the SQL standard requires all fields to either have an aggregation function applied, or to be in the GROUP BY clause, Views gathers those fields and adds them to the GROUP BY clause.

Returns
array An array of the fieldnames which are non-aggregates.

Referenced by Sql\query().

Here is the caller graph for this function:

getTableInfo (   $table)

Get the information associated with a table.

If you need the alias of a table with a particular relationship, use ensureTable().

getWhereArgs ( )

Get the arguments attached to the WHERE and HAVING clauses of this query.

References Sql\$having, and Sql\$where.

init ( ViewExecutable  $view,
DisplayPluginBase  $display,
array &  $options = NULL 
)

{Initialize the plugin.

Parameters
\Drupal\views\ViewExecutable$viewThe view object.
\Drupal\views\Plugin\views\display\DisplayPluginBase$displayThe display handler.
array$optionsThe options configured for this plugin.
}

Implements ViewsPluginInterface.

loadEntities ( $results)

Loads all entities contained in the passed-in $results. If the entity belongs to the base table, then it gets stored in $result->_entity. Otherwise, it gets stored in $result->_relationship_entities[$relationship_id];

Parameters
\Drupal\views\ResultRow[]$results The result of the SQL query.

References Drupal\entityManager().

Referenced by Sql\execute().

Here is the call graph for this function:

Here is the caller graph for this function:

placeholder (   $base = 'views')

Generates a unique placeholder used in the db query.

query (   $get_count = FALSE)

Generate a query and a countquery from all of the information supplied to the object.

Parameters
$get_countProvide a countquery if this is true, otherwise provide a normal query.

An optimized count query includes just the base field instead of all the fields. Determine of this query qualifies by checking for a groupby or distinct.

References Sql\$distinct, Sql\$groupby, Sql\addField(), Sql\addGroupBy(), Sql\addTag(), Sql\buildCondition(), Sql\compileFields(), Drupal\entityManager(), Database\getConnection(), QueryPluginBase\getEntityTableInfo(), Sql\getNonAggregates(), and Drupal\moduleHandler().

Here is the call graph for this function:

queueTable (   $table,
  $relationship = NULL,
JoinPluginBase  $join = NULL,
  $alias = NULL 
)

Add a table to the query without ensuring the path.

This is a pretty internal function to Views and addTable() or ensureTable() should be used instead of this one, unless you are absolutely sure this is what you want.

Parameters
$tableThe name of the table to add. It needs to exist in the global table array.
$relationshipThe primary table alias this table is related to. If not set, the primary table will be used.
\Drupal\views\Plugin\views\join\JoinPluginBase$joinIn some join configurations this table may actually join back through a different method; this is most likely to be used when tracing a hierarchy path. (node->parent->parent2->parent3). This parameter will specify how this table joins if it is not the default.
$aliasA specific alias to use, rather than the default alias.
Returns
$alias The alias of the table; this alias can be used to access information about the table and should always be used to refer to the table when adding parts to the query. Or FALSE if the table was not able to be added.

References Sql\adjustJoin(), and Sql\getJoinData().

Referenced by Sql\addTable(), Sql\ensurePath(), and Sql\ensureTable().

Here is the call graph for this function:

Here is the caller graph for this function:

setCountField (   $table,
  $field,
  $alias = NULL 
)

Set what field the query will count() on for paging.

setDistinct (   $value = TRUE)
protected

Set the view to be distinct (per base field).

Parameters
bool$valueShould the view be distincted.

Referenced by Sql\build().

Here is the caller graph for this function:

setupTimezone ( )

{}

submitOptionsForm ( $form,
FormStateInterface  $form_state 
)

Special submit handling.

Implements ViewsPluginInterface.

References NestedArray\getValue(), FormStateInterface\getValues(), and FormStateInterface\setValueForElement().

Here is the call graph for this function:

Field Documentation

$distinct = FALSE

A flag as to whether or not to make the primary field distinct.

Referenced by Sql\query().

$fieldAliases = array()
protected

An array mapping table aliases and field names to field aliases.

$fields = array()

An array of fields.

$getCountOptimized = NULL
protected

Should this query be optimized for counts, for example no sorts.

$groupby = array()

A simple array of group by clauses.

Referenced by Sql\query().

$groupOperator = 'AND'
protected

The default operator to use when connecting the WHERE groups. May be AND or OR.

$having = array()

An array of sections of the HAVING query. Each section is in itself an array of pieces and a flag as to whether or not it should be AND or OR.

Referenced by Sql\getWhereArgs().

$orderby = array()

A simple array of order by clauses.

$relationships = array()

Holds an array of relationships, which are aliases of the primary table that represent different ways to join the same table in.

$tableQueue = array()
protected

A list of tables in the order they should be added, keyed by alias.

$tables = array()

Holds an array of tables and counts added so that we can create aliases

$tags = array()

Query tags which will be passed over to the dbtng query object.

$where = array()

An array of sections of the WHERE query. Each section is in itself an array of pieces and a flag as to whether or not it should be AND or OR.

Referenced by Sql\buildCondition(), and Sql\getWhereArgs().


The documentation for this class was generated from the following file: