Action |
Query |
STATIC QUERY EXAMPLE
|
$query = "SELECT nid, title FROM {node} WHERE type = :type";
$placeholders = [
':type' => 'article',
];
$records = db_query($query, $placeholders);
foreach ($records as $record) {
echo $record->nid;
echo $record->title;
}
|
DYNAMIC QUERY EXAMPLE
|
$table = 'node';
$alias = 'n';
$columns = [
'nid',
'title',
];
$field = 'type';
$value = 'article';
$query = db_select($table, $alias)
->fields($alias, $columns)
->condition($field, $value);
$records = $query->execute();
foreach ($records as $record) {
echo $record->nid;
echo $record->title;
}
|
SELECT QUERY
[Dynamic]
|
$query = db_select($table, $alias)
->fields($alias, $columns)
->condition($field, $value, $operator);
$records = $query->execute();
$options = [
'target' => 'slave',
'fetch' => PDO::FETCH_ASSOC,
];
db_query($query, $placeholders, $options);
|
MANAGE COLUMNS/FIELDS
[Dynamic/Chainable]
|
$query = db_select($table, $alias)
->fields($alias, $columns);
$query = db_select($table, $alias)
->fields($alias);
|
ADD COLUMN/FIELD
[Dynamic/Non-Chainable]
|
$field = 'title';
$field_alias = 'my_title';
$query = db_select($table, $alias);
$alias_used = $query->addField($alias, $field, $field_alias);
|
BASIC WHERE CLAUSE OPERATORS
[Dynamic/Chainable]
|
$field = 'type';
$value = 'article';
$operator = '=';
$query = db_select($table, $alias)
->fields($alias, $columns)
->condition($field, $value, $operator);
|
RAW WHERE CLAUSE
[Dynamic/Chainable]
|
$snippet = "type = :type";
$placeholders = [
':type' => 'article',
];
$query = db_select($table, $alias)
->fields($alias, $columns)
->where($snippet, $placeholders);
|
AND OPERATOR/CONDITION
[Dynamic/Chainable]
|
$query = db_select($table, $alias)
->fields($alias, $columns)
->condition($field1, $value1, $operator1)
->condition($field2, $value2, $operator2);
$and_condition = db_and()
->condition($field1, $value1, $operator1)
->condition($field2, $value2, $operator2);
$query = db_select($table, $alias)
->fields($alias, $columns)
->condition($and_condition);
|
OR OPERATOR/CONDITION
[Dynamic/Chainable]
|
$or_condition = db_or()
->condition($field1, $value1, $operator1)
->condition($field2, $value2, $operator2);
$query = db_select($table, $alias)
->fields($alias, $columns)
->condition($or_condition);
|
XOR OPERATOR/CONDITION
[Dynamic/Chainable]
|
$xor_condition = db_xor()
->condition($field1, $value1, $operator1)
->condition($field2, $value2, $operator2);
$query = db_select($table, $alias)
->fields($alias, $columns)
->condition($xor_condition);
|
NULL CONDITION
[Dynamic/Chainable]
|
$query->isNull($field);
$query->isNotNull($field);
|
LIKE OPERATOR
[Dynamic/Chainable]
|
$field = 'type';
$value = 'artic';
$operator = 'LIKE';
$query = db_select($table, $alias)
->fields($alias, $columns)
->condition($field, '%' . db_like($value) . '%', $operator);
|
DISTINCT CLAUSE
[Dynamic/Chainable]
|
$query = db_select($table, $alias)
->fields($alias, $columns)
->distinct();
|
ORDER BY CLAUSE
[Dynamic/Chainable]
|
$sort_ascending = 'ASC';
$sort_descending = 'DESC';
$query = db_select($table, $alias)
->fields($alias, $columns)
->orderBy($field1, $sort_ascending)
->orderBy($field2, $sort_descending);
$query->orderBy($field)->orderRandom();
|
GROUP BY CLAUSE
[Dynamic/Chainable]
|
$query = db_select($table, $alias)
->fields($alias, $columns)
->groupBy($field1)
->groupBy($field2);
|
HAVING CLAUSE
[Dynamic/Chainable]
|
$snippet = 'COUNT(nid) >= :count';
$placeholders = [
':count' => 10,
];
$query->having($snippet, $placeholders);
$query->havingCondition($field, $value, $operator);
|
EXPRESSIONS
[Dynamic/Non-Chainable]
|
$expression = 'salary + (salary * :increment/100)';
$field_alias = 'new_salary';
$placeholders = [
':increment' => '30',
];
$alias_used = $query->addExpression($expression, $field_alias , $placeholders);
|
IN OPERATOR
[Static/Dynamic]
|
$query = "SELECT * FROM {node} WHERE nid IN (:nids)";
$placeholders = [
':nids' => [11, 22, 33],
];
$operator = 'IN';
$value = [11, 22, 33];
$query = db_select($table, $alias)
->fields($alias, $columns)
->condition($field, $value, $operator);
$query = "SELECT * FROM {node} WHERE nid IN (:nids[])";
$placeholders = [
':nids[]' => [11, 22, 33],
];
$operator = 'IN';
$value = [11, 22, 33];
$query = db_select($table, $alias)
->fields($alias, $columns)
->condition($field, $value, $operator);
|
NOT IN OPERATOR
[Static/Dynamic]
|
$query = "SELECT * FROM {node} WHERE nid NOT IN (:nids)";
$placeholders = [
':nids' => [11, 22, 33],
];
$operator = 'NOT IN';
$value = [11, 22, 33];
$query = db_select($table, $alias)
->fields($alias, $columns)
->condition($field, $value, $operator);
$query = "SELECT * FROM {node} WHERE nid NOT IN (:nids[])";
$placeholders = [
':nids[]' => [11, 22, 33],
];
$operator = 'NOT IN';
$value = [11, 22, 33];
$query = db_select($table, $alias)
->fields($alias, $columns)
->condition($field, $value, $operator);
|
BETWEEN OPERATOR
[Static/Dynamic]
|
$query = "SELECT * FROM {node} WHERE nid BETWEEN :nid1 AND :nid2";
$placeholders = [
':nid1' => 11,
':nid2' => 33,
];
$operator = 'BETWEEN';
$value = [11, 33];
$query = db_select($table, $alias)
->fields($alias, $columns)
->condition($field, $value, $operator);
|
INSERT QUERY
[Dynamic]
|
$values = [
$field1 => $value1,
$field2 => $value2,
];
$untrusted = db_insert($table)
->fields($values)
->execute();
|
MULTIPLE INSERT QUERY
[Dynamic]
|
$columns = [
$field1,
$field2,
];
$values = [
[
$field1 => $value1_1,
$field2 => $value2_1,
],
[
$field1 => $value1_2,
$field2 => $value2_2,
],
];
$query = db_insert($table)->fields($columns);
foreach ($values as $record) {
$query->values($record);
}
$untrusted = $query->execute();
|
INSERT QUERY (USING DEFAULT VALUES)
[Dynamic/Chainable]
|
$columns = [
'name',
'city',
];
$untrusted = db_insert($table)
->useDefaults($columns)
->execute();
|
INSERT QUERY (USING SELECT QUERY RESULT)
[Dynamic/Chainable]
|
$query = db_select($table, $alias)
->fields($alias, $columns)
->condition($field, $value, $operator);
$untrusted = db_insert($insert_in_table)
->from($query)
->execute();
|
UPDATE QUERY
[Dynamic]
|
$values = [
$field1 => $value1,
$field2 => $value2,
];
$row_count = db_update($table)
->fields($values)
->condition($field, $value, $operator)
->execute();
$snippet = "type = :type";
$placeholders = [
':type' => 'article',
];
$row_count = db_update($table)
->fields($values)
->where($snippet, $placeholders)
->execute();
|
DELETE QUERY
[Dynamic]
|
$row_count = db_delete($table)
->condition($field, $value)
->execute();
|
TRUNCATE QUERY
|
db_truncate($table)->execute();
|
MERGE QUERY
[Dynamic]
|
$if_column_values_exists = [
'title' => $title,
];
$values = [
$field1 => $value1,
$field2 => $value2,
];
$merge_type = db_merge($table)
->key($if_column_values_exists)
->fields($values)
->execute();
|
CONDITIONAL MERGE QUERY
[Dynamic]
|
$insert_values = [
$field1 => $value1,
$field2 => $value2,
];
$update_values = [
$field3 => $value3,
];
$merge_type = db_merge($table)
->key($if_column_values_exists)
->insertFields($insert_values)
->updateFields($update_values)
->execute();
|
JOINS
[Dynamic/Non-Chainable]
|
$table = 'node';
$join_table = 'user';
$alias = 'n';
$join_alias = 'u';
$join_on = 'n.uid = u.uid AND u.uid = :uid';
$placeholders = [
':uid' => 1,
];
$query = db_select($table, $alias);
$alias_used = $query->join($join_table, $join_alias, $join_on, $placeholders);
$query->fields($alias, $columns)
->condition($field, $value, $operator);
$resultant_table = db_select($table, $alias)
->fields($alias, $columns)
->condition($field, $value, $operator);
$query = db_select($new_table, $new_alias);
$query->join($resultant_table, $resultant_alias, $join_on);
|
UNION QUERY
[Dynamic/Chainable]
|
$append_table = db_select($table2, $alias2)
->fields($alias2, $columns2);
$query = db_select($table1, $alias1)
->fields($alias1, $columns1)
->union($append_table);
|
SUB-QUERY
[Dynamic]
|
$subquery = db_select($table, $alias)
->fields($alias, [$column]);
$query->condition('sl.nid', $subquery, 'NOT IN');
|
LIMITING RESULTS
[Dynamic/Chainable]
|
$query = db_select($table, $alias)
->fields($alias, $columns)
->range($offset, $limit);
|
COUNT QUERY
[Static/Dynamic/Chainable]
|
$row_count = db_query($query, $placeholders)
->rowCount();
$row_count = db_select($table, $alias)
->fields($alias, $columns)
->execute()
->rowCount();
$row_count = db_select($table, $alias)
->countQuery()
->execute()
->fetchField();
|
ADD TAG (QUERY TAGGING)
[Dynamic/Chainable]
|
$tag = 'tag_name';
$query = db_select($table, $alias)
->fields($alias, $columns)
->addTag($tag);
$query->hasTag('tag_name');
$query->hasAllTags('tag_name1', 'tag_name2');
$query->hasAnyTag('tag_name1', 'tag_name2');
|
ADD META DATA (QUERY TAGGING)
[Dynamic/Chainable]
|
$node = node_load($nid);
$query = db_select($table, $alias)
->fields($alias, $columns)
->addMetaData('my_data', $node);
$node = $query->getMetaData('my_data');
|
HOOK QUERY ALTER
|
$query->hasTag('example');
$query->hasAllTags('example1', 'example2');
$query->hasAnyTag('example1', 'example2');
$node = $query->getMetaData('node');
$fields =& $query->getFields();
$expressions =& $query->getExpressions();
$tables =& $query->getTables();
$order =& $query->getOrderBy();
$where =& $query->conditions();
$having =& $query->havingConditions();
$group =& $query->getGroupBy();
$order =& $query->getOrderBy();
unset($order['n.nid']);
|
DB TRANSACTION
|
$transaction = db_transaction();
try {
$id = db_insert($table)
->fields($values)
->execute();
//throw new Exception("drupaldrug");
return $id;
}
catch (Exception $e) {
$transaction->rollback();
watchdog_exception('drupaldrug', $e);
}
|
EXTENDERS
[Dynamic/Non-Chainable]
|
$extender = 'PagerDefault';
$query = $query->extend($extender);
|
RESULTS
[Static/Dynamic]
|
$records = db_query($query, $placeholders);
$records = $query->execute();
foreach ($records as $record) {
echo $record->nid;
echo $record->title;
}
|
FETCHING NEXT RECORD (EXPLICITLY)
[Static/Dynamic/Chainable]
|
$record = $records->fetch();
$record = $records->fetchObject();
while ($record = $records->fetch()) {
echo $record->nid;
echo $record->title;
}
$record = $records->fetchAssoc();
while ($record = $records->fetchAssoc()) {
echo $record['nid'];
echo $record['title'];
}
$field_index = 1;
$field = $records->fetchField($field_index);
while ($field = $records->fetchField($field_index)) {
echo $field;
}
|
FETCHING ALL RECORDS AT ONCE
[Static/Dynamic/Chainable]
|
$formatted_records = $records->fetchAll();
$field = 'nid';
$formatted_records = $records->fetchAllAssoc($field);
$field_index1 = 0;
$field_index2 = 2;
$formatted_records = $records->fetchAllKeyed($field_index1, $field_index2);
$field_index = 2;
$formatted_records = $records->fetchCol($field_index);
|
SETTINGS.PHP (CONFIGURATION)
|
$host = 'localhost';
$driver = 'mysql';
$pdo_options = [
'PDO::MYSQL_ATTR_COMPRESS' => 1,
];
$db_params = [
'driver' => $driver,
'database' => 'my_master_db',
'username' => 'my_db_username',
'password' => 'my_db_password',
'host' => $host,
'pdo' => $pdo_options,
];
$key = 'default';
$target = 'default';
$databases[$key][$target] = $db_params;
$target = 'slave';
$db_params['database'] = 'my_slave1_db';
$db_params['host'] = 'my_slave1_ip';
$databases[$key][$target][] = $db_params;
$target = 'slave';
$db_params['database'] = 'my_slave2_db';
$db_params['host'] = 'my_slave2_ip';
$databases[$key][$target][] = $db_params;
$key = 'my_key';
$target = 'default';
$databases[$key][$target] = [
'driver' => 'sqlite',
'database' => 'files/my_db.sqlite',
];
|
DATABASE CONNECTION
|
$key = 'my_key';
$target = 'slave';
$conn = Database::getConnection($target, $key);
$conn = Database::getConnection();
|
SWITCH DATABASE (ON THE FLY)
[Dynamic]
|
$records = $query->execute();
$key = 'my_key';
db_set_active($key);
$new_dbrecords = $new_dbquery->execute();
db_set_active();
|
SETTING AND SWITCHING DATABASE (ON THE FLY)
|
$records = $query->execute();
$db_params = [
'driver' => $driver,
'database' => 'my_master_db',
'username' => 'my_db_username',
'password' => 'my_db_password',
'host' => $host,
];
$key = 'my_key';
$target = 'default';
Database::addConnectionInfo($key, $target, $db_params);
db_set_active($key);
$new_dbrecords = $new_dbquery->execute();
db_set_active();
|
CHAINING FUNCTIONS
Dynamic
|
$query = db_select($table, $alias);
$query->fields($alias);
$query->range($offset, $limit);
$query = db_select($table, $alias)
->fields($alias)
->range($offset, $limit);
Reference list of functions that supports chaining:
Chainable |
Non-Chainable |
addMetaData() |
addExpression() |
addTag() |
addField() |
comment() |
addJoin() |
condition() |
extend() |
countQuery() |
join() |
distinct() |
innerJoin() |
execute() |
leftJoin() |
exists() |
rightJoin() |
fields() |
|
forUpdate() |
|
groupBy() |
|
having() |
|
havingCondition() |
|
isNotNull() |
|
isNull() |
|
notExists() |
|
orderBy() |
|
orderRandom() |
|
range() |
|
union() |
|
where() |
|
|
DEBUG
|
print_r($query->__toString());
print_r($query->arguments());
|
Comments
Post a Comment