Skip navigation


  1. drupal
    1. 7 drupal/includes/database/sqlite/

Specific SQLite implementation of DatabaseConnection.

See DatabaseConnection_sqlite::PDOPrepare() for reasons why we must prefetch the data instead of using PDOStatement.

See also



Functions & methods

DatabaseStatementInterface::fetchAllAssocReturns the result set as an associative array keyed by the given field.
DatabaseStatementInterface::fetchAllKeyedReturns the entire result set as a single associative array.
DatabaseStatementInterface::fetchAssocFetches the next row and returns it as an associative array.
DatabaseStatementInterface::fetchColReturns an entire single column of a result set as an indexed array.
DatabaseStatementInterface::fetchFieldReturns a single field from the next record of a result set.
DatabaseStatementInterface::getQueryStringGets the query string of this statement.
DatabaseStatementInterface::rowCountReturns the number of rows affected by the last SQL statement.
DatabaseStatementPrefetch::currentReturn the current row formatted according to the current fetch style.
DatabaseStatementPrefetch::throwPDOExceptionThrow a PDO Exception based on the last PDO error.
DatabaseStatement_sqlite::executeExecutes a prepared statement Overrides DatabaseStatementInterface::execute
DatabaseStatement_sqlite::getStatementSQLite specific implementation of getStatement(). Overrides DatabaseStatementPrefetch::getStatement


DatabaseStatementPrefetch::$columnNamesThe list of column names in this result set.
DatabaseStatementPrefetch::$currentKeyThe key of the current row.
DatabaseStatementPrefetch::$currentRowThe current row, retrieved in PDO::FETCH_ASSOC format.
DatabaseStatementPrefetch::$dataMain data store.
DatabaseStatementPrefetch::$dbhReference to the database connection object for this statement.
DatabaseStatementPrefetch::$defaultFetchOptionsHolds supplementary default fetch options.
DatabaseStatementPrefetch::$defaultFetchStyleHolds the default fetch style.
DatabaseStatementPrefetch::$driverOptionsDriver-specific options. Can be used by child classes.
DatabaseStatementPrefetch::$fetchOptionsHolds supplementary current fetch options (which will be used by the next fetch).
DatabaseStatementPrefetch::$fetchStyleHolds the current fetch style (which will be used by the next fetch).
DatabaseStatementPrefetch::$queryStringThe query string.
DatabaseStatementPrefetch::$resultRowCountThe number of rows in this result set.
DatabaseStatementPrefetch::$rowCountThe number of rows affected by the last query.


drupal/includes/database/sqlite/, line 383
Database interface code for SQLite embedded database engine.

View source
class DatabaseStatement_sqlite extends DatabaseStatementPrefetch implements Iterator, DatabaseStatementInterface {

   * SQLite specific implementation of getStatement().
   * The PDO SQLite layer doesn't replace numeric placeholders in queries
   * correctly, and this makes numeric expressions (such as COUNT(*) >= :count)
   * fail. We replace numeric placeholders in the query ourselves to work
   * around this bug.
   * See for more details.
  protected function getStatement($query, &$args = array()) {
    if (count($args)) {
      // Check if $args is a simple numeric array.
      if (range(0, count($args) - 1) === array_keys($args)) {
        // In that case, we have unnamed placeholders.
        $count = 0;
        $new_args = array();
        foreach ($args as $value) {
          if (is_float($value) || is_int($value)) {
            if (is_float($value)) {
              // Force the conversion to float so as not to loose precision
              // in the automatic cast.
              $value = sprintf('%F', $value);
            $query = substr_replace($query, $value, strpos($query, '?'), 1);
          else {
            $placeholder = ':db_statement_placeholder_' . $count++;
            $query = substr_replace($query, $placeholder, strpos($query, '?'), 1);
            $new_args[$placeholder] = $value;
        $args = $new_args;
      else {
        // Else, this is using named placeholders.
        foreach ($args as $placeholder => $value) {
          if (is_float($value) || is_int($value)) {
            if (is_float($value)) {
              // Force the conversion to float so as not to loose precision
              // in the automatic cast.
              $value = sprintf('%F', $value);

            // We will remove this placeholder from the query as PDO throws an
            // exception if the number of placeholders in the query and the
            // arguments does not match.
            // PDO allows placeholders to not be prefixed by a colon. See
            // for
            // more.
            if ($placeholder[0] != ':') {
              $placeholder = ":$placeholder";
            // When replacing the placeholders, make sure we search for the
            // exact placeholder. For example, if searching for
            // ':db_placeholder_1', do not replace ':db_placeholder_11'.
            $query = preg_replace('/' . preg_quote($placeholder) . '\b/', $value, $query);

    return $this->dbh->PDOPrepare($query);

  public function execute($args = array(), $options = array()) {
    try {
      $return = parent::execute($args, $options);
    catch (PDOException $e) {
      if (!empty($e->errorInfo[1]) && $e->errorInfo[1] === 17) {
        // The schema has changed. SQLite specifies that we must resend the query.
        $return = parent::execute($args, $options);
      else {
        // Rethrow the exception.
        throw $e;

    // In some weird cases, SQLite will prefix some column names by the name
    // of the table. We post-process the data, by renaming the column names
    // using the same convention as MySQL and PostgreSQL.
    $rename_columns = array();
    foreach ($this->columnNames as $k => $column) {
      // In some SQLite versions, SELECT DISTINCT(field) will return "(field)"
      // instead of "field".
      if (preg_match("/^\((.*)\)$/", $column, $matches)) {
        $rename_columns[$column] = $matches[1];
        $this->columnNames[$k] = $matches[1];
        $column = $matches[1];

      // Remove "table." prefixes.
      if (preg_match("/^.*\.(.*)$/", $column, $matches)) {
        $rename_columns[$column] = $matches[1];
        $this->columnNames[$k] = $matches[1];
    if ($rename_columns) {
      // DatabaseStatementPrefetch already extracted the first row,
      // put it back into the result set.
      if (isset($this->currentRow)) {
        $this->data[0] = &$this->currentRow;

      // Then rename all the columns across the result set.
      foreach ($this->data as $k => $row) {
        foreach ($rename_columns as $old_column => $new_column) {
          $this->data[$k][$new_column] = $this->data[$k][$old_column];

      // Finally, extract the first row again.
      $this->currentRow = $this->data[0];

    return $return;

Related topics