summaryrefslogtreecommitdiff
path: root/includes/database/query.inc
diff options
context:
space:
mode:
Diffstat (limited to 'includes/database/query.inc')
-rw-r--r--includes/database/query.inc104
1 files changed, 91 insertions, 13 deletions
diff --git a/includes/database/query.inc b/includes/database/query.inc
index 23b652f9b..c7363f238 100644
--- a/includes/database/query.inc
+++ b/includes/database/query.inc
@@ -142,7 +142,15 @@ interface QueryConditionInterface {
* The query this condition belongs to. If not given, the current query is
* used.
*/
- public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder = NULL);
+ public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder);
+
+ /**
+ * Check whether a condition has been previously compiled.
+ *
+ * @return
+ * TRUE if the condition has been previously compiled.
+ */
+ public function compiled();
}
@@ -239,12 +247,17 @@ interface QueryAlterableInterface {
interface QueryPlaceholderInterface {
/**
+ * Returns a unique identifier for this object.
+ */
+ public function uniqueIdentifier();
+
+ /**
* Returns the next placeholder ID for the query.
*
* @return
* The next available placeholder ID as an integer.
*/
- function nextPlaceholder();
+ public function nextPlaceholder();
}
/**
@@ -284,6 +297,11 @@ abstract class Query implements QueryPlaceholderInterface {
protected $queryOptions;
/**
+ * A unique identifier for this query object.
+ */
+ protected $uniqueIdentifier;
+
+ /**
* The placeholder counter.
*/
protected $nextPlaceholder = 0;
@@ -304,6 +322,8 @@ abstract class Query implements QueryPlaceholderInterface {
* Array of query options.
*/
public function __construct(DatabaseConnection $connection, $options) {
+ $this->uniqueIdentifier = uniqid('', TRUE);
+
$this->connection = $connection;
$this->connectionKey = $this->connection->getKey();
$this->connectionTarget = $this->connection->getTarget();
@@ -321,13 +341,20 @@ abstract class Query implements QueryPlaceholderInterface {
}
/**
- * Implements the magic __wakeup function to reconnect to the database.
+ * Implements the magic __wakeup function to reconnect to the database.
*/
public function __wakeup() {
$this->connection = Database::getConnection($this->connectionTarget, $this->connectionKey);
}
/**
+ * Implements the magic __clone function.
+ */
+ public function __clone() {
+ $this->uniqueIdentifier = uniqid('', TRUE);
+ }
+
+ /**
* Runs the query against the database.
*/
abstract protected function execute();
@@ -344,6 +371,13 @@ abstract class Query implements QueryPlaceholderInterface {
abstract public function __toString();
/**
+ * Returns a unique identifier for this object.
+ */
+ public function uniqueIdentifier() {
+ return $this->uniqueIdentifier;
+ }
+
+ /**
* Gets the next placeholder value for this query object.
*
* @return int
@@ -790,8 +824,15 @@ class DeleteQuery extends Query implements QueryConditionInterface {
/**
* Implements QueryConditionInterface::compile().
*/
- public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder = NULL) {
- return $this->condition->compile($connection, isset($queryPlaceholder) ? $queryPlaceholder : $this);
+ public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder) {
+ return $this->condition->compile($connection, $queryPlaceholder);
+ }
+
+ /**
+ * Implements QueryConditionInterface::compiled().
+ */
+ public function compiled() {
+ return $this->condition->compiled();
}
/**
@@ -864,8 +905,15 @@ class TruncateQuery extends Query {
/**
* Implements QueryConditionInterface::compile().
*/
- public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder = NULL) {
- return $this->condition->compile($connection, isset($queryPlaceholder) ? $queryPlaceholder : $this);
+ public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder) {
+ return $this->condition->compile($connection, $queryPlaceholder);
+ }
+
+ /**
+ * Implements QueryConditionInterface::compiled().
+ */
+ public function compiled() {
+ return $this->condition->compiled();
}
/**
@@ -1025,8 +1073,15 @@ class UpdateQuery extends Query implements QueryConditionInterface {
/**
* Implements QueryConditionInterface::compile().
*/
- public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder = NULL) {
- return $this->condition->compile($connection, isset($queryPlaceholder) ? $queryPlaceholder : $this);
+ public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder) {
+ return $this->condition->compile($connection, $queryPlaceholder);
+ }
+
+ /**
+ * Implements QueryConditionInterface::compiled().
+ */
+ public function compiled() {
+ return $this->condition->compiled();
}
/**
@@ -1510,8 +1565,15 @@ class MergeQuery extends Query implements QueryConditionInterface {
/**
* Implements QueryConditionInterface::compile().
*/
- public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder = NULL) {
- return $this->condition->compile($connection, isset($queryPlaceholder) ? $queryPlaceholder : $this);
+ public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder) {
+ return $this->condition->compile($connection, $queryPlaceholder);
+ }
+
+ /**
+ * Implements QueryConditionInterface::compiled().
+ */
+ public function compiled() {
+ return $this->condition->compiled();
}
/**
@@ -1609,6 +1671,11 @@ class DatabaseCondition implements QueryConditionInterface, Countable {
protected $changed = TRUE;
/**
+ * The identifier of the query placeholder this condition has been compiled against.
+ */
+ protected $queryPlaceholderIdentifier;
+
+ /**
* Constructs a DataBaseCondition object.
*
* @param string $conjunction
@@ -1718,8 +1785,12 @@ class DatabaseCondition implements QueryConditionInterface, Countable {
/**
* Implements QueryConditionInterface::compile().
*/
- public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder = NULL) {
- if ($this->changed) {
+ public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder) {
+ // Re-compile if this condition changed or if we are compiled against a
+ // different query placeholder object.
+ if ($this->changed || isset($this->queryPlaceholderIdentifier) && ($this->queryPlaceholderIdentifier != $queryPlaceholder->uniqueIdentifier())) {
+ $this->queryPlaceholderIdentifier = $queryPlaceholder->uniqueIdentifier();
+
$condition_fragments = array();
$arguments = array();
@@ -1792,6 +1863,13 @@ class DatabaseCondition implements QueryConditionInterface, Countable {
}
/**
+ * Implements QueryConditionInterface::compiled().
+ */
+ public function compiled() {
+ return !$this->changed;
+ }
+
+ /**
* Implements PHP magic __toString method to convert the conditions to string.
*
* @return string