Query.php 4.2 KB

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