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.
52 protected $nextPlaceholder = 0;
55 * An array of comments that can be prepended to a query.
59 protected $comments = [];
62 * Constructs a Query object.
64 * @param \Drupal\Core\Database\Connection $connection
65 * Database connection object.
66 * @param array $options
67 * Array of query options.
69 public function __construct(Connection $connection, $options) {
70 $this->uniqueIdentifier = uniqid('', TRUE);
72 $this->connection = $connection;
73 $this->connectionKey = $this->connection->getKey();
74 $this->connectionTarget = $this->connection->getTarget();
76 $this->queryOptions = $options;
80 * Implements the magic __sleep function to disconnect from the database.
82 public function __sleep() {
83 $keys = get_object_vars($this);
84 unset($keys['connection']);
85 return array_keys($keys);
89 * Implements the magic __wakeup function to reconnect to the database.
91 public function __wakeup() {
92 $this->connection = Database::getConnection($this->connectionTarget, $this->connectionKey);
96 * Implements the magic __clone function.
98 public function __clone() {
99 $this->uniqueIdentifier = uniqid('', TRUE);
103 * Runs the query against the database.
105 * @return \Drupal\Core\Database\StatementInterface|null
106 * A prepared statement, or NULL if the query is not valid.
108 abstract protected function execute();
111 * Implements PHP magic __toString method to convert the query to a string.
113 * The toString operation is how we compile a query object to a prepared
117 * A prepared statement query string for this object.
119 abstract public function __toString();
122 * Returns a unique identifier for this object.
124 public function uniqueIdentifier() {
125 return $this->uniqueIdentifier;
129 * Gets the next placeholder value for this query object.
132 * The next placeholder value.
134 public function nextPlaceholder() {
135 return $this->nextPlaceholder++;
139 * Adds a comment to the query.
141 * By adding a comment to a query, you can more easily find it in your
142 * query log or the list of active queries on an SQL server. This allows
143 * for easier debugging and allows you to more easily find where a query
144 * with a performance problem is being generated.
146 * The comment string will be sanitized to remove * / and other characters
147 * that may terminate the string early so as to avoid SQL injection attacks.
150 * The comment string to be inserted into the query.
154 public function comment($comment) {
155 $this->comments[] = $comment;
160 * Returns a reference to the comments array for the query.
162 * Because this method returns by reference, alter hooks may edit the comments
163 * array directly to make their changes. If just adding comments, however, the
164 * use of comment() is preferred.
166 * Note that this method must be called by reference as well:
168 * $comments =& $query->getComments();
172 * A reference to the comments array structure.
174 public function &getComments() {
175 return $this->comments;