Commit dd24755c authored by webchick's avatar webchick

#308521 by chx: Fix a bunch of stray whitespace. And the trivial patch of the...

#308521 by chx: Fix a bunch of stray whitespace. And the trivial patch of the month award goes to... ;)
parent 00756c87
......@@ -52,21 +52,21 @@ public function where($snippet, $args = array());
*
* This method returns by reference. That allows alter hooks to access the
* data structure directly and manipulate it before it gets compiled.
*
*
* The data structure that is returned is an indexed array of entries, where
* each entry looks like the following:
*
*
* array(
* 'field' => $field,
* 'value' => $value,
* 'operator' => $operator,
* );
*
*
* In the special case that $operator is NULL, the $field is taken as a raw
* SQL snippet (possibly containing a function) and $value is an associative
* SQL snippet (possibly containing a function) and $value is an associative
* array of placeholders for the snippet.
*
* There will also be a single array entry of #conjunction, which is the
*
* There will also be a single array entry of #conjunction, which is the
* conjunction that will be applied to the array, such as AND.
*/
public function &conditions();
......@@ -78,7 +78,7 @@ public function &conditions();
* An associative array of placeholders and values.
*/
public function arguments();
/**
* Compiles the saved conditions for later retrieval.
*
......@@ -96,7 +96,7 @@ public function compile(DatabaseConnection $connection);
* Interface for a query that can be manipulated via an alter hook.
*/
interface QueryAlterableInterface {
/**
* Adds a tag to a query.
*
......@@ -110,7 +110,7 @@ interface QueryAlterableInterface {
* The tag to add.
*/
public function addTag($tag);
/**
* Determines if a given query has a given tag.
*
......@@ -120,7 +120,7 @@ public function addTag($tag);
* TRUE if this query has been marked with this tag, FALSE otherwise.
*/
public function hasTag($tag);
/**
* Determines if a given query has all specified tags.
*
......@@ -141,7 +141,7 @@ public function hasAllTags();
* tags, FALSE otherwise.
*/
public function hasAnyTag();
/**
* Adds additional metadata to the query.
*
......@@ -157,7 +157,7 @@ public function hasAnyTag();
*
*/
public function addMetaData($key, $object);
/**
* Retrieves a given piece of metadata.
*
......@@ -264,7 +264,7 @@ public function __construct($connection, $table, Array $options = array()) {
* Add a set of field->value pairs to be inserted.
*
* This method may only be called once. Calling it a second time will be
* ignored. To queue up multiple sets of values to be inserted at once,
* ignored. To queue up multiple sets of values to be inserted at once,
* use the values() method.
*
* @param $fields
......@@ -327,15 +327,15 @@ public function values(Array $values) {
/**
* Specify fields for which the database-defaults should be used.
*
* If you want to force a given field to use the database-defined default,
* If you want to force a given field to use the database-defined default,
* not NULL or undefined, use this method to instruct the database to use
* default values explicitly. In most cases this will not be necessary
* unless you are inserting a row that is all default values, as you cannot
* specify no values in an INSERT query.
*
*
* Specifying a field both in fields() and in useDefaults() is an error
* and will not execute.
*
*
* @param $fields
* An array of values for which to use the default values
* specified in the table definition.
......@@ -346,20 +346,20 @@ public function useDefaults(Array $fields) {
$this->defaultFields = $fields;
return $this;
}
/**
* Flag this query as being delay-safe or not.
*
* If this method is never called, it is assumed that the query must be
* executed immediately. If delay is set to TRUE, then the query will be
* executed immediately. If delay is set to TRUE, then the query will be
* flagged to run "delayed" or "low priority" on databases that support such
* capabilities. In that case, the database will return immediately and the
* query will be run at some point in the future. That makes it useful for
* logging-style queries.
*
*
* If the database does not support delayed INSERT queries, this method
* has no effect.
*
*
* @param $delay
* If TRUE, this query is delay-safe and will run delayed on supported databases.
* @return
......@@ -387,7 +387,7 @@ public function execute() {
if (array_intersect($this->insertFields, $this->defaultFields)) {
throw new PDOException('You may not specify the same field to have a value and a schema-default value.');
}
// Each insert happens in its own query in the degenerate case. However,
// we wrap it in a transaction so that it is atomic where possible. On many
// databases, such as SQLite, this is also a notable performance boost.
......@@ -405,11 +405,11 @@ public function execute() {
}
public function __toString() {
// Default fields are always placed first for consistency.
$insert_fields = array_merge($this->defaultFields, $this->insertFields);
// For simplicity, we will use the $placeholders array to inject
// For simplicity, we will use the $placeholders array to inject
// default keywords even though they are not, strictly speaking,
// placeholders for prepared statements.
$placeholders = array();
......@@ -503,10 +503,10 @@ public function fields(Array $fields, Array $values = array()) {
$fields = array_combine($fields, $values);
}
$this->insertFields = $fields;
return $this;
}
/**
* Set the key field(s) to be used to insert or update into the table.
*
......@@ -590,7 +590,7 @@ public function updateExcept($exclude_fields) {
$exclude_fields = func_get_args();
}
$this->excludeFields = $exclude_fields;
return $this;
}
......@@ -617,7 +617,7 @@ public function expression($field, $expression, Array $arguments = NULL) {
'expression' => $expression,
'arguments' => $arguments,
);
return $this;
}
......@@ -629,25 +629,25 @@ public function execute() {
// for comprehensibility. Any practical database driver will override
// this method with database-specific logic, so this function serves only
// as a fallback to aid developers of new drivers.
// Wrap multiple queries in a transaction, if the database supports it.
$transaction = $this->connection->startTransaction();
// Manually check if the record already exists.
$select = $this->connection->select($this->table);
foreach ($this->keyFields as $field => $value) {
$select->condition($field, $value);
}
$select = $select->countQuery();
$sql = (string)$select;
$arguments = $select->getArguments();
$num_existing = db_query($sql, $arguments)->fetchField();
if ($num_existing) {
// If there is already an existing record, run an update query.
if ($this->updateFields) {
$update_fields = $this->updateFields;
}
......@@ -672,7 +672,7 @@ public function execute() {
$insert_fields = $this->insertFields + $this->keyFields;
$this->connection->insert($this->table, $this->queryOptions)->fields($insert_fields)->execute();
}
// Commit the transaction.
$transaction->commit();
}
......@@ -711,7 +711,7 @@ public function __construct(DatabaseConnection $connection, $table, Array $optio
$options['return'] = Database::RETURN_AFFECTED;
parent::__construct($connection, $options);
$this->table = $table;
$this->condition = new DatabaseCondition('AND');
}
......@@ -735,7 +735,7 @@ public function where($snippet, $args = array()) {
$this->condition->where($snippet, $args);
return $this;
}
public function compile(DatabaseConnection $connection) {
return $this->condition->compile($connection);
}
......@@ -816,7 +816,7 @@ public function __construct(DatabaseConnection $connection, $table, Array $optio
$options['return'] = Database::RETURN_AFFECTED;
parent::__construct($connection, $options);
$this->table = $table;
$this->condition = new DatabaseCondition('AND');
}
......@@ -840,7 +840,7 @@ public function where($snippet, $args = array()) {
$this->condition->where($snippet, $args);
return $this;
}
public function compile(DatabaseConnection $connection) {
return $this->condition->compile($connection);
}
......@@ -881,12 +881,12 @@ public function expression($field, $expression, Array $arguments = NULL) {
'expression' => $expression,
'arguments' => $arguments,
);
return $this;
}
public function execute() {
// Expressions take priority over literal fields, so we process those first
// and remove any literal fields that conflict.
$fields = $this->fields;
......@@ -897,14 +897,14 @@ public function execute() {
}
unset($fields[$field]);
}
// Because we filter $fields the same way here and in __toString(), the
// placeholders will all match up properly.
$max_placeholder = 0;
foreach ($fields as $field => $value) {
$update_values[':db_update_placeholder_' . ($max_placeholder++)] = $value;
}
if (count($this->condition)) {
$this->condition->compile($this->connection);
$update_values = array_merge($update_values, $this->condition->arguments());
......@@ -922,7 +922,7 @@ public function __toString() {
$update_fields[] = $field . '=' . $data['expression'];
unset($fields[$field]);
}
$max_placeholder = 0;
foreach ($fields as $field => $value) {
$update_fields[] = $field . '=:db_update_placeholder_' . ($max_placeholder++);
......@@ -948,13 +948,13 @@ class DatabaseCondition implements QueryConditionInterface, Countable {
protected $conditions = array();
protected $arguments = array();
protected $changed = TRUE;
public function __construct($conjunction) {
$this->conditions['#conjunction'] = $conjunction;
}
/**
* Return the size of this conditional. This is part of the Countable interface.
*
......@@ -964,16 +964,16 @@ public function __construct($conjunction) {
public function count() {
return count($this->conditions) - 1;
}
public function condition($field, $value = NULL, $operator = '=') {
$this->conditions[] = array(
'field' => $field,
'value' => $value,
'operator' => $operator,
);
$this->changed = TRUE;
return $this;
}
......@@ -984,10 +984,10 @@ public function where($snippet, $args = array()) {
'operator' => NULL,
);
$this->changed = TRUE;
return $this;
}
public function &conditions() {
return $this->conditions;
}
......@@ -1008,10 +1008,10 @@ public function compile(DatabaseConnection $connection) {
static $next_placeholder = 1;
if ($this->changed) {
$condition_fragments = array();
$arguments = array();
$conditions = $this->conditions;
$conjunction = $conditions['#conjunction'];
unset($conditions['#conjunction']);
......@@ -1043,7 +1043,7 @@ public function compile(DatabaseConnection $connection) {
$operator = $this->mapConditionOperator($condition['operator']);
}
$operator += $operator_defaults;
if ($condition['value'] instanceof SelectQuery) {
$placeholders[] = (string)$condition['value'];
$arguments += $condition['value']->arguments();
......@@ -1061,17 +1061,17 @@ public function compile(DatabaseConnection $connection) {
$placeholders[] = $placeholder;
}
$condition_fragments[] = ' (' . $condition['field'] . ' ' . $operator['operator'] . ' ' . $operator['prefix'] . implode($operator['delimiter'], $placeholders) . $operator['postfix'] . ') ';
}
}
}
$this->changed = FALSE;
$this->stringVersion = implode($conjunction, $condition_fragments);
$this->arguments = $arguments;
}
}
public function __toString() {
// If the caller forgot to call compile() first, refuse to run.
if ($this->changed) {
......@@ -1079,7 +1079,7 @@ public function __toString() {
}
return $this->stringVersion;
}
/**
* Gets any special processing requirements for the condition operator.
*
......@@ -1105,7 +1105,7 @@ protected function mapConditionOperator($operator) {
return $return;
}
}
/**
......
......@@ -95,33 +95,33 @@ public function __construct($table, $alias = NULL, DatabaseConnection $connectio
$this->having = new DatabaseCondition('AND');
$this->addJoin(NULL, $table, $alias);
}
/* Implementations of QueryAlterableInterface. */
public function addTag($tag) {
$this->alterTags[$tag] = 1;
}
public function hasTag($tag) {
return isset($this->alterTags[$tag]);
}
public function hasAllTags() {
return !(boolean)array_diff(func_get_args(), array_keys($this->alterTags));
}
public function hasAnyTag() {
return (boolean)array_intersect(func_get_args(), array_keys($this->alterTags));
}
public function addMetaData($key, $object) {
$this->alterMetaData[$key] = $object;
}
public function getMetaData($key) {
return isset($this->alterMetaData[$key]) ? $this->alterMetaData[$key] : NULL;
}
/* Implementations of QueryConditionInterface for the WHERE clause. */
public function condition($field, $value = NULL, $operator = '=') {
......@@ -144,7 +144,7 @@ public function where($snippet, $args = array()) {
$this->where->where($snippet, $args);
return $this;
}
public function compile(DatabaseConnection $connection) {
return $this->where->compile($connection);
}
......@@ -171,13 +171,13 @@ public function having($snippet, $args = array()) {
$this->having->where($snippet, $args);
return $this;
}
public function havingCompile(DatabaseConnection $connection) {
return $this->having->compile($connection);
}
/* Alter accessors to expose the query data to alter hooks. */
/**
* Returns a reference to the fields array for this query.
*
......@@ -186,7 +186,7 @@ public function havingCompile(DatabaseConnection $connection) {
* use of addField() is preferred.
*
* Note that this method must be called by reference as well:
*
*
* @code
* $fields =& $query->getFields();
* @endcode
......@@ -197,7 +197,7 @@ public function havingCompile(DatabaseConnection $connection) {
public function &getFields() {
return $this->fields;
}
/**
* Returns a reference to the expressions array for this query.
*
......@@ -206,18 +206,18 @@ public function &getFields() {
* use of addExpression() is preferred.
*
* Note that this method must be called by reference as well:
*
*
* @code
* $fields =& $query->getExpressions();
* @endcode
*
*
* @return
* A reference to the expression array structure.
*/
public function &getExpressions() {
return $this->expressions;
}
/**
* Returns a reference to the order by array for this query.
*
......@@ -226,18 +226,18 @@ public function &getExpressions() {
* fields, however, the use of orderBy() is preferred.
*
* Note that this method must be called by reference as well:
*
*
* @code
* $fields =& $query->getOrderBy();
* @endcode
*
*
* @return
* A reference to the expression array structure.
*/
public function &getOrderBy() {
return $this->order;
}
/**
* Returns a reference to the tables array for this query.
*
......@@ -246,11 +246,11 @@ public function &getOrderBy() {
* use of the join() methods is preferred.
*
* Note that this method must be called by reference as well:
*
*
* @code
* $fields =& $query->getTables();
* @endcode
*
*
* @return
* A reference to the tables array structure.
*/
......@@ -277,13 +277,13 @@ public function getArguments() {
$args += $expression['arguments'];
}
}
return $args;
}
public function execute() {
drupal_alter('query', $this);
$this->where->compile($this->connection);
$this->having->compile($this->connection);
$args = $this->where->arguments() + $this->having->arguments();
......@@ -297,7 +297,7 @@ public function execute() {
$args += $expression['arguments'];
}
}
if (!empty($this->range)) {
return $this->connection->queryRange((string)$this, $args, $this->range['start'], $this->range['length'], $this->queryOptions);
}
......@@ -376,15 +376,15 @@ public function addField($table_alias, $field, $alias = NULL) {
*/
public function addExpression($expression, $alias = NULL, $arguments = array()) {
static $alaises = array();
if (empty($alias)) {
$alias = 'expression';
}
if (empty($aliases[$alias])) {
$aliases[$alias] = 1;
}
if (!empty($this->expressions[$alias])) {
$alias = $alias . '_' . $aliases[$alias]++;
}
......@@ -588,7 +588,7 @@ public function range($start = NULL, $length = NULL) {
public function groupBy($field) {
$this->group[] = $field;
}
/**
* Get the equivalent COUNT query of this query as a new query object.
*
......@@ -599,24 +599,24 @@ public function countQuery() {
// Shallow-clone this query. We don't want to duplicate any of the
// referenced objects, so a shallow query is all we need.
$count = clone($this);
// Zero-out existing fields and expressions.
$fields =& $count->getFields();
$fields = array();
$expressions =& $count->getExpressions();
$expressions = array();
// Ordering a count query is a waste of cycles, and breaks on some
// databases anyway.
$orders = &$count->getOrderBy();
$orders = array();
// COUNT() is an expression, so we add that back in.
$count->addExpression('COUNT(*)');
return $count;
}
public function __toString() {
// SELECT
......@@ -681,12 +681,12 @@ public function __toString() {
return $query;
}
public function __clone() {
// On cloning, also clone the conditional objects. However, we do not
// want to clone the database connection object as that would duplicate the
// connection itself.
$this->where = clone($this->where);
$this->having = clone($this->having);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment