"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/lib/Drupal/Core/Database/Query/Query.php" (26 Nov 2020, 4368 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Query.php" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 9.0.8_vs_9.1.0-rc1.

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