3 namespace Drupal\Core\Database\Query;
5 use Drupal\Core\Database\Database;
6 use Drupal\Core\Database\Connection;
9 * Base class for query builders.
11 * Note that query builders use PHP's magic __toString() method to compile the
12 * query object into a prepared statement.
14 abstract class Query implements PlaceholderInterface {
17 * The connection object on which to run this query.
19 * @var \Drupal\Core\Database\Connection
21 protected $connection;
24 * The target of the connection object.
28 protected $connectionTarget;
31 * The key of the connection object.
35 protected $connectionKey;
38 * The query options to pass on to the connection object.
42 protected $queryOptions;
45 * A unique identifier for this query object.
47 protected $uniqueIdentifier;
50 * The placeholder counter.
54 protected $nextPlaceholder = 0;
57 * An array of comments that can be prepended to a query.
61 protected $comments = [];
64 * Constructs a Query object.
66 * @param \Drupal\Core\Database\Connection $connection
67 * Database connection object.
68 * @param array $options
69 * Array of query options.
71 public function __construct(Connection $connection, $options) {
72 $this->uniqueIdentifier = uniqid('', TRUE);
74 $this->connection = $connection;
75 $this->connectionKey = $this->connection->getKey();
76 $this->connectionTarget = $this->connection->getTarget();
78 $this->queryOptions = $options;
82 * Implements the magic __sleep function to disconnect from the database.
84 public function __sleep() {
85 $keys = get_object_vars($this);
86 unset($keys['connection']);
87 return array_keys($keys);
91 * Implements the magic __wakeup function to reconnect to the database.
93 public function __wakeup() {
94 $this->connection = Database::getConnection($this->connectionTarget, $this->connectionKey);
98 * Implements the magic __clone function.
100 public function __clone() {
101 $this->uniqueIdentifier = uniqid('', TRUE);
105 * Runs the query against the database.
107 * @return \Drupal\Core\Database\StatementInterface|null
108 * A prepared statement, or NULL if the query is not valid.
110 abstract protected function execute();
113 * Implements PHP magic __toString method to convert the query to a string.
115 * The toString operation is how we compile a query object to a prepared
119 * A prepared statement query string for this object.
121 abstract public function __toString();
124 * Returns a unique identifier for this object.
126 public function uniqueIdentifier() {
127 return $this->uniqueIdentifier;
131 * Gets the next placeholder value for this query object.
134 * The next placeholder value.
136 public function nextPlaceholder() {
137 return $this->nextPlaceholder++;
141 * Adds a comment to the query.
143 * By adding a comment to a query, you can more easily find it in your
144 * query log or the list of active queries on an SQL server. This allows
145 * for easier debugging and allows you to more easily find where a query
146 * with a performance problem is being generated.
148 * The comment string will be sanitized to remove * / and other characters
149 * that may terminate the string early so as to avoid SQL injection attacks.
152 * The comment string to be inserted into the query.
156 public function comment($comment) {
157 $this->comments[] = $comment;
162 * Returns a reference to the comments array for the query.
164 * Because this method returns by reference, alter hooks may edit the comments
165 * array directly to make their changes. If just adding comments, however, the
166 * use of comment() is preferred.
168 * Note that this method must be called by reference as well:
170 * $comments =& $query->getComments();
174 * A reference to the comments array structure.
176 public function &getComments() {
177 return $this->comments;