You are here

class Statement in Drupal driver for SQL Server and SQL Azure 8.2

Same name in this branch
  1. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/Statement.php \Drupal\Driver\Database\sqlsrv\Statement
  2. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/PDO/Statement.php \Drupal\Driver\Database\sqlsrv\PDO\Statement
Same name and namespace in other branches
  1. 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Statement.php \Drupal\Driver\Database\sqlsrv\Statement

Hierarchy

  • class \Drupal\Driver\Database\sqlsrv\PDO\Statement extends \PDOStatement implements \Drupal\Driver\Database\sqlsrv\PDO\Countable

Expanded class hierarchy of Statement

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Statement.php, line 13
Definition of Drupal\Driver\Database\sqlsrv\Statement

Namespace

Drupal\Driver\Database\sqlsrv
View source
class Statement extends \Drupal\Driver\Database\sqlsrv\PDO\Statement implements StatementInterface {

  /**
   * Is rowCount() execution allowed.
   *
   * @var bool
   */
  public $allowRowCount = false;

  /**
   * Reference to the database connection object for this statement.
   *
   * The name $dbh is inherited from \PDOStatement.
   *
   * @var \Drupal\Core\Database\Connection
   */
  public $dbh;
  protected function __construct(Connection $dbh) {
    $this->allowRowCount = true;
    $this->dbh = $dbh;
    $this
      ->setFetchMode(\PDO::FETCH_OBJ);
  }

  /**
   * {@inheritdoc}
   */
  public function execute($args = [], $options = []) {
    if (isset($options['fetch'])) {
      if (is_string($options['fetch'])) {

        // Default to an object. Note: db fields will be added to the object
        // before the constructor is run. If you need to assign fields after
        // the constructor is run, see http://drupal.org/node/315092.
        $this
          ->setFetchMode(PDO::FETCH_CLASS, $options['fetch']);
      }
      else {
        $this
          ->setFetchMode($options['fetch']);
      }
    }
    $logger = $this->dbh
      ->getLogger();
    $query_start = microtime(true);

    // If parameteres have already been binded
    // to the statement and we pass an empty array here
    // we will get a PDO Exception.
    if (empty($args)) {
      $args = null;
    }

    // Execute the query. Bypass parent override
    // and directly call PDOStatement implementation.
    $return = $this
      ->doExecute($args);

    // Bind column types properly.
    $this
      ->fixColumnBindings();
    if (!empty($logger)) {
      $query_end = microtime(true);
      $logger
        ->log($this, $args, $query_end - $query_start);
    }
    return $return;
  }

  /**
   * {@inheritdoc}
   */
  public function doExecute($args = [], $options = []) {
    if (isset($options['fetch'])) {
      if (is_string($options['fetch'])) {

        // \PDO::FETCH_PROPS_LATE tells __construct() to run before properties
        // are added to the object.
        $this
          ->setFetchMode(\PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE, $options['fetch']);
      }
      else {
        $this
          ->setFetchMode($options['fetch']);
      }
    }
    $logger = $this->dbh
      ->getLogger();
    if (!empty($logger)) {
      $query_start = microtime(true);
    }
    $return = parent::execute($args);
    if (!empty($logger)) {
      $query_end = microtime(true);
      $logger
        ->log($this, $args, $query_end - $query_start);
    }
    return $return;
  }

  /**
   * {@inheritdoc}
   */
  public function getQueryString() {
    return $this->queryString;
  }

  /**
   * {@inheritdoc}
   */
  public function fetchCol($index = 0) {
    return $this
      ->fetchAll(\PDO::FETCH_COLUMN, $index);
  }

  /**
   * {@inheritdoc}
   */
  public function fetchAllAssoc($key, $fetch = null) {
    $return = [];
    if (isset($fetch)) {
      if (is_string($fetch)) {
        $this
          ->setFetchMode(\PDO::FETCH_CLASS, $fetch);
      }
      else {
        $this
          ->setFetchMode($fetch);
      }
    }
    foreach ($this as $record) {
      $record_key = is_object($record) ? $record->{$key} : $record[$key];
      $return[$record_key] = $record;
    }
    return $return;
  }

  /**
   * {@inheritdoc}
   */
  public function fetchAllKeyed($key_index = 0, $value_index = 1) {
    $return = [];
    $this
      ->setFetchMode(\PDO::FETCH_NUM);
    foreach ($this as $record) {
      $return[$record[$key_index]] = $record[$value_index];
    }
    return $return;
  }

  /**
   * {@inheritdoc}
   */
  public function fetchField($index = 0) {

    // Call \PDOStatement::fetchColumn to fetch the field.
    return $this
      ->fetchColumn($index);
  }

  /**
   * {@inheritdoc}
   */
  public function fetchAssoc() {

    // Call \PDOStatement::fetch to fetch the row.
    return $this
      ->fetch(\PDO::FETCH_ASSOC);
  }

  /**
   * {@inheritdoc}
   */
  public function rowCount() {

    // SELECT query should not use the method.
    if ($this->allowRowCount) {
      return parent::rowCount();
    }
    else {
      throw new RowCountException();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setFetchMode($mode, $a1 = null, $a2 = []) {

    // Call \PDOStatement::setFetchMode to set fetch mode.
    // \PDOStatement is picky about the number of arguments in some cases so we
    // need to be pass the exact number of arguments we where given.
    switch (func_num_args()) {
      case 1:
        return parent::setFetchMode($mode);
      case 2:
        return parent::setFetchMode($mode, $a1);
      case 3:
      default:
        return parent::setFetchMode($mode, $a1, $a2);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function fetchAll($mode = null, $column_index = null, $constructor_arguments = null) {

    // Call \PDOStatement::fetchAll to fetch all rows.
    // \PDOStatement is picky about the number of arguments in some cases so we
    // need to be pass the exact number of arguments we where given.
    switch (func_num_args()) {
      case 0:
        return parent::fetchAll();
      case 1:
        return parent::fetchAll($mode);
      case 2:
        return parent::fetchAll($mode, $column_index);
      case 3:
      default:
        return parent::fetchAll($mode, $column_index, $constructor_arguments);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Statement::$allowRowCount public property Is rowCount() execution allowed.
Statement::$boundParams protected property
Statement::$cnn protected property Reference to the Connection
Statement::$CONNECTION_FAILED_CODES protected property ODBC Codes for failed communcation link/dropped connections
Statement::$dbh public property Reference to the database connection object for this statement.
Statement::$INTEGRITY_VIOLATION_CONSTRAINT_CODES protected property ODBC Codes for integrity constraint violation errors.
Statement::$options protected property Special PDO options
Statement::$query_signature protected property An MD5 signature for the query
Statement::BindArguments public function Summary of BindArguments
Statement::BindExpressions public function Summary of BindExpressions
Statement::bindParam public function Overrides the default \PDOStatement method to add the named parameter and it's reference to the array of bound parameters - then accesses and returns parent::bindParam method
Statement::bindValue public function Overrides the default \PDOStatement method to add the named parameter and it's value to the array of bound values
Statement::BindValues public function Binds a set of values to a PDO Statement, taking care of properly managing binary data.
Statement::count public function Return the number of rows.
Statement::doExecute public function
Statement::execute public function Execute a statement. Overrides Statement::execute
Statement::fetchAll public function Returns an array containing all of the result set rows. Overrides StatementInterface::fetchAll
Statement::fetchAllAssoc public function Returns the result set as an associative array keyed by the given field. Overrides StatementInterface::fetchAllAssoc
Statement::fetchAllKeyed public function Optimized for common use cases. Overrides Statement::fetchAllKeyed
Statement::fetchAssoc public function Fetches the next row and returns it as an associative array. Overrides StatementInterface::fetchAssoc
Statement::fetchCol public function Returns an entire single column of a result set as an indexed array. Overrides StatementInterface::fetchCol
Statement::fetchField public function Returns a single field from the next record of a result set. Overrides StatementInterface::fetchField
Statement::fixColumnBindings protected function Make sure that SQL Server types are properly binded to PHP types.
Statement::GetBoundParameters public function Get parameters bound to this statement, useful for debugging purposes.
Statement::getColumnMetaCustom protected function Cached version of getColumnMeta().
Statement::getQueryString public function Gets the query string of this statement. Overrides StatementInterface::getQueryString
Statement::RETRY_DELAY constant Delay between retries in seconds.
Statement::RETRY_MAX constant Maximum number a failed query can be retried in case of failure due to integrity constraint violations or dropped connections.
Statement::rowCount public function Returns the number of rows affected by the last SQL statement. Overrides StatementInterface::rowCount
Statement::SetConnection public function Set a reference to the connection.
Statement::setFetchMode public function Sets the default fetch mode for this statement. Overrides StatementInterface::setFetchMode
Statement::__construct protected function
StatementInterface::fetch public function Fetches the next row from a result set. 2
StatementInterface::fetchObject public function Fetches the next row and returns it as an object. 2