Query.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace Drupal\Core\Database\Query;
  3. use Drupal\Core\Database\Database;
  4. use Drupal\Core\Database\Connection;
  5. /**
  6. * Base class for query builders.
  7. *
  8. * Note that query builders use PHP's magic __toString() method to compile the
  9. * query object into a prepared statement.
  10. */
  11. abstract class Query implements PlaceholderInterface {
  12. /**
  13. * The connection object on which to run this query.
  14. *
  15. * @var \Drupal\Core\Database\Connection
  16. */
  17. protected $connection;
  18. /**
  19. * The target of the connection object.
  20. *
  21. * @var string
  22. */
  23. protected $connectionTarget;
  24. /**
  25. * The key of the connection object.
  26. *
  27. * @var string
  28. */
  29. protected $connectionKey;
  30. /**
  31. * The query options to pass on to the connection object.
  32. *
  33. * @var array
  34. */
  35. protected $queryOptions;
  36. /**
  37. * A unique identifier for this query object.
  38. *
  39. * @var string
  40. */
  41. protected $uniqueIdentifier;
  42. /**
  43. * The placeholder counter.
  44. *
  45. * @var int
  46. */
  47. protected $nextPlaceholder = 0;
  48. /**
  49. * An array of comments that can be prepended to a query.
  50. *
  51. * @var array
  52. */
  53. protected $comments = [];
  54. /**
  55. * Constructs a Query object.
  56. *
  57. * @param \Drupal\Core\Database\Connection $connection
  58. * Database connection object.
  59. * @param array $options
  60. * Array of query options.
  61. */
  62. public function __construct(Connection $connection, $options) {
  63. $this->uniqueIdentifier = uniqid('', TRUE);
  64. $this->connection = $connection;
  65. $this->connectionKey = $this->connection->getKey();
  66. $this->connectionTarget = $this->connection->getTarget();
  67. $this->queryOptions = $options;
  68. }
  69. /**
  70. * Implements the magic __sleep function to disconnect from the database.
  71. */
  72. public function __sleep() {
  73. $keys = get_object_vars($this);
  74. unset($keys['connection']);
  75. return array_keys($keys);
  76. }
  77. /**
  78. * Implements the magic __wakeup function to reconnect to the database.
  79. */
  80. public function __wakeup() {
  81. $this->connection = Database::getConnection($this->connectionTarget, $this->connectionKey);
  82. }
  83. /**
  84. * Implements the magic __clone function.
  85. */
  86. public function __clone() {
  87. $this->uniqueIdentifier = uniqid('', TRUE);
  88. }
  89. /**
  90. * Runs the query against the database.
  91. *
  92. * @return \Drupal\Core\Database\StatementInterface|null
  93. * A prepared statement, or NULL if the query is not valid.
  94. */
  95. abstract protected function execute();
  96. /**
  97. * Implements PHP magic __toString method to convert the query to a string.
  98. *
  99. * The toString operation is how we compile a query object to a prepared
  100. * statement.
  101. *
  102. * @return string
  103. * A prepared statement query string for this object.
  104. */
  105. abstract public function __toString();
  106. /**
  107. * Returns a unique identifier for this object.
  108. */
  109. public function uniqueIdentifier() {
  110. return $this->uniqueIdentifier;
  111. }
  112. /**
  113. * Gets the next placeholder value for this query object.
  114. *
  115. * @return int
  116. * The next placeholder value.
  117. */
  118. public function nextPlaceholder() {
  119. return $this->nextPlaceholder++;
  120. }
  121. /**
  122. * Adds a comment to the query.
  123. *
  124. * By adding a comment to a query, you can more easily find it in your
  125. * query log or the list of active queries on an SQL server. This allows
  126. * for easier debugging and allows you to more easily find where a query
  127. * with a performance problem is being generated.
  128. *
  129. * The comment string will be sanitized to remove * / and other characters
  130. * that may terminate the string early so as to avoid SQL injection attacks.
  131. *
  132. * @param $comment
  133. * The comment string to be inserted into the query.
  134. *
  135. * @return $this
  136. */
  137. public function comment($comment) {
  138. $this->comments[] = $comment;
  139. return $this;
  140. }
  141. /**
  142. * Returns a reference to the comments array for the query.
  143. *
  144. * Because this method returns by reference, alter hooks may edit the comments
  145. * array directly to make their changes. If just adding comments, however, the
  146. * use of comment() is preferred.
  147. *
  148. * Note that this method must be called by reference as well:
  149. * @code
  150. * $comments =& $query->getComments();
  151. * @endcode
  152. *
  153. * @return array
  154. * A reference to the comments array structure.
  155. */
  156. public function &getComments() {
  157. return $this->comments;
  158. }
  159. }