comparison core/lib/Drupal/Core/Database/Statement.php @ 0:4c8ae668cc8c

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