annotate core/lib/Drupal/Core/Database/Statement.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Database;
Chris@0 4
Chris@0 5 /**
Chris@0 6 * Default implementation of StatementInterface.
Chris@0 7 *
Chris@0 8 * \PDO allows us to extend the \PDOStatement class to provide additional
Chris@0 9 * functionality beyond that offered by default. We do need extra
Chris@0 10 * functionality. By default, this class is not driver-specific. If a given
Chris@0 11 * driver needs to set a custom statement class, it may do so in its
Chris@0 12 * constructor.
Chris@0 13 *
Chris@0 14 * @see http://php.net/pdostatement
Chris@0 15 */
Chris@0 16 class Statement extends \PDOStatement implements StatementInterface {
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Reference to the database connection object for this statement.
Chris@0 20 *
Chris@0 21 * The name $dbh is inherited from \PDOStatement.
Chris@0 22 *
Chris@0 23 * @var \Drupal\Core\Database\Connection
Chris@0 24 */
Chris@0 25 public $dbh;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Is rowCount() execution allowed.
Chris@0 29 *
Chris@0 30 * @var bool
Chris@0 31 */
Chris@0 32 public $allowRowCount = FALSE;
Chris@0 33
Chris@0 34 protected function __construct(Connection $dbh) {
Chris@0 35 $this->dbh = $dbh;
Chris@0 36 $this->setFetchMode(\PDO::FETCH_OBJ);
Chris@0 37 }
Chris@0 38
Chris@0 39 /**
Chris@0 40 * {@inheritdoc}
Chris@0 41 */
Chris@0 42 public function execute($args = [], $options = []) {
Chris@0 43 if (isset($options['fetch'])) {
Chris@0 44 if (is_string($options['fetch'])) {
Chris@0 45 // \PDO::FETCH_PROPS_LATE tells __construct() to run before properties
Chris@0 46 // are added to the object.
Chris@0 47 $this->setFetchMode(\PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE, $options['fetch']);
Chris@0 48 }
Chris@0 49 else {
Chris@0 50 $this->setFetchMode($options['fetch']);
Chris@0 51 }
Chris@0 52 }
Chris@0 53
Chris@0 54 $logger = $this->dbh->getLogger();
Chris@0 55 if (!empty($logger)) {
Chris@0 56 $query_start = microtime(TRUE);
Chris@0 57 }
Chris@0 58
Chris@0 59 $return = parent::execute($args);
Chris@0 60
Chris@0 61 if (!empty($logger)) {
Chris@0 62 $query_end = microtime(TRUE);
Chris@0 63 $logger->log($this, $args, $query_end - $query_start);
Chris@0 64 }
Chris@0 65
Chris@0 66 return $return;
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * {@inheritdoc}
Chris@0 71 */
Chris@0 72 public function getQueryString() {
Chris@0 73 return $this->queryString;
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * {@inheritdoc}
Chris@0 78 */
Chris@0 79 public function fetchCol($index = 0) {
Chris@0 80 return $this->fetchAll(\PDO::FETCH_COLUMN, $index);
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * {@inheritdoc}
Chris@0 85 */
Chris@0 86 public function fetchAllAssoc($key, $fetch = NULL) {
Chris@0 87 $return = [];
Chris@0 88 if (isset($fetch)) {
Chris@0 89 if (is_string($fetch)) {
Chris@0 90 $this->setFetchMode(\PDO::FETCH_CLASS, $fetch);
Chris@0 91 }
Chris@0 92 else {
Chris@0 93 $this->setFetchMode($fetch);
Chris@0 94 }
Chris@0 95 }
Chris@0 96
Chris@0 97 foreach ($this as $record) {
Chris@0 98 $record_key = is_object($record) ? $record->$key : $record[$key];
Chris@0 99 $return[$record_key] = $record;
Chris@0 100 }
Chris@0 101
Chris@0 102 return $return;
Chris@0 103 }
Chris@0 104
Chris@0 105 /**
Chris@0 106 * {@inheritdoc}
Chris@0 107 */
Chris@0 108 public function fetchAllKeyed($key_index = 0, $value_index = 1) {
Chris@0 109 $return = [];
Chris@0 110 $this->setFetchMode(\PDO::FETCH_NUM);
Chris@0 111 foreach ($this as $record) {
Chris@0 112 $return[$record[$key_index]] = $record[$value_index];
Chris@0 113 }
Chris@0 114 return $return;
Chris@0 115 }
Chris@0 116
Chris@0 117 /**
Chris@0 118 * {@inheritdoc}
Chris@0 119 */
Chris@0 120 public function fetchField($index = 0) {
Chris@0 121 // Call \PDOStatement::fetchColumn to fetch the field.
Chris@0 122 return $this->fetchColumn($index);
Chris@0 123 }
Chris@0 124
Chris@0 125 /**
Chris@0 126 * {@inheritdoc}
Chris@0 127 */
Chris@0 128 public function fetchAssoc() {
Chris@0 129 // Call \PDOStatement::fetch to fetch the row.
Chris@0 130 return $this->fetch(\PDO::FETCH_ASSOC);
Chris@0 131 }
Chris@0 132
Chris@0 133 /**
Chris@0 134 * {@inheritdoc}
Chris@0 135 */
Chris@0 136 public function rowCount() {
Chris@0 137 // SELECT query should not use the method.
Chris@0 138 if ($this->allowRowCount) {
Chris@0 139 return parent::rowCount();
Chris@0 140 }
Chris@0 141 else {
Chris@0 142 throw new RowCountException();
Chris@0 143 }
Chris@0 144 }
Chris@0 145
Chris@0 146 /**
Chris@0 147 * {@inheritdoc}
Chris@0 148 */
Chris@0 149 public function setFetchMode($mode, $a1 = NULL, $a2 = []) {
Chris@0 150 // Call \PDOStatement::setFetchMode to set fetch mode.
Chris@0 151 // \PDOStatement is picky about the number of arguments in some cases so we
Chris@0 152 // need to be pass the exact number of arguments we where given.
Chris@0 153 switch (func_num_args()) {
Chris@0 154 case 1:
Chris@0 155 return parent::setFetchMode($mode);
Chris@0 156 case 2:
Chris@0 157 return parent::setFetchMode($mode, $a1);
Chris@0 158 case 3:
Chris@0 159 default:
Chris@0 160 return parent::setFetchMode($mode, $a1, $a2);
Chris@0 161 }
Chris@0 162 }
Chris@0 163
Chris@0 164 /**
Chris@0 165 * {@inheritdoc}
Chris@0 166 */
Chris@0 167 public function fetchAll($mode = NULL, $column_index = NULL, $constructor_arguments = NULL) {
Chris@0 168 // Call \PDOStatement::fetchAll to fetch all rows.
Chris@0 169 // \PDOStatement is picky about the number of arguments in some cases so we
Chris@0 170 // need to be pass the exact number of arguments we where given.
Chris@0 171 switch (func_num_args()) {
Chris@0 172 case 0:
Chris@0 173 return parent::fetchAll();
Chris@0 174 case 1:
Chris@0 175 return parent::fetchAll($mode);
Chris@0 176 case 2:
Chris@0 177 return parent::fetchAll($mode, $column_index);
Chris@0 178 case 3:
Chris@0 179 default:
Chris@0 180 return parent::fetchAll($mode, $column_index, $constructor_arguments);
Chris@0 181 }
Chris@0 182 }
Chris@0 183
Chris@0 184 }