InsertQuery_sqlite

  1. drupal
    1. 8
    2. 7

SQLite specific implementation of InsertQuery.

We ignore all the default fields and use the clever SQLite syntax: INSERT INTO table DEFAULT VALUES for degenerated "default only" queries.

Hierarchy

Properties

NameDescription
InsertQuery::$defaultFieldsAn array of fields that should be set to their database-defined defaults.
InsertQuery::$fromQueryA SelectQuery object to fetch the rows that should be inserted.
InsertQuery::$insertFieldsAn array of fields on which to insert.
InsertQuery::$insertValuesA nested array of values to insert.
InsertQuery::$tableThe table on which to insert.
Query::$commentsAn array of comments that can be prepended to a query.
Query::$connectionThe connection object on which to run this query.
Query::$connectionKeyThe key of the connection object.
Query::$connectionTargetThe target of the connection object.
Query::$queryOptionsThe query options to pass on to the connection object.

Functions & methods

NameDescription
InsertQuery::fieldsAdds a set of field->value pairs to be inserted.
InsertQuery::fromSets the fromQuery on this InsertQuery object.
InsertQuery::preExecutePreprocesses and validates the query.
InsertQuery::useDefaultsSpecifies fields for which the database defaults should be used.
InsertQuery::valuesAdds another set of values to the query to be inserted.
InsertQuery::__constructConstructs an InsertQuery object. Overrides Query::__construct
InsertQuery_sqlite::executeExecutes the insert query. Overrides InsertQuery::execute
InsertQuery_sqlite::__toStringImplements PHP magic __toString method to convert the query to a string. Overrides InsertQuery::__toString
Query::commentAdds a comment to the query.
Query::getCommentsReturns a reference to the comments array for the query.
Query::nextPlaceholderGets the next placeholder value for this query object. Overrides QueryPlaceholderInterface::nextPlaceholder
Query::uniqueIdentifierReturns a unique identifier for this object. Overrides QueryPlaceholderInterface::uniqueIdentifier
Query::__cloneImplements the magic __clone function.
Query::__sleepImplements the magic __sleep function to disconnect from the database.
Query::__wakeupImplements the magic __wakeup function to reconnect to the database.

includes/database/sqlite/query.inc, line 20

View source
<?php
class InsertQuery_sqlite extends InsertQuery {

  public function execute() {
    if (!$this->preExecute()) {
      return NULL;
    }
    if (count($this->insertFields)) {
      return parent::execute();
    }
    else {
      return $this->connection->query('INSERT INTO {' . $this->table . '} DEFAULT VALUES', array(), $this->queryOptions);
    }
  }

  public function __toString() {
    // Create a sanitized comment string to prepend to the query.
    $comments = $this->connection->makeComment($this->comments);

    // Produce as many generic placeholders as necessary.
    $placeholders = array_fill(0, count($this->insertFields), '?');

    // If we're selecting from a SelectQuery, finish building the query and
    // pass it back, as any remaining options are irrelevant.
    if (!empty($this->fromQuery)) {
      return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->insertFields) . ') ' . $this->fromQuery;
    }

    return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->insertFields) . ') VALUES (' . implode(', ', $placeholders) . ')';
  }

}
?>

Related topics