d9b422b58435ba139b5171c530600923dc83fd0e
[yaffs-website] / web / core / lib / Drupal / Core / Database / Driver / sqlite / Statement.php
1 <?php
2
3 namespace Drupal\Core\Database\Driver\sqlite;
4
5 use Drupal\Core\Database\StatementPrefetch;
6 use Drupal\Core\Database\StatementInterface;
7
8 /**
9  * SQLite implementation of \Drupal\Core\Database\Statement.
10  *
11  * The PDO SQLite driver only closes SELECT statements when the PDOStatement
12  * destructor is called and SQLite does not allow data change (INSERT,
13  * UPDATE etc) on a table which has open SELECT statements. This is a
14  * user-space mock of PDOStatement that buffers all the data and doesn't
15  * have those limitations.
16  */
17 class Statement extends StatementPrefetch implements StatementInterface {
18
19   /**
20    * {@inheritdoc}
21    *
22    * The PDO SQLite layer doesn't replace numeric placeholders in queries
23    * correctly, and this makes numeric expressions (such as COUNT(*) >= :count)
24    * fail. We replace numeric placeholders in the query ourselves to work
25    * around this bug.
26    *
27    * See http://bugs.php.net/bug.php?id=45259 for more details.
28    */
29   protected function getStatement($query, &$args = []) {
30     if (is_array($args) && !empty($args)) {
31       // Check if $args is a simple numeric array.
32       if (range(0, count($args) - 1) === array_keys($args)) {
33         // In that case, we have unnamed placeholders.
34         $count = 0;
35         $new_args = [];
36         foreach ($args as $value) {
37           if (is_float($value) || is_int($value)) {
38             if (is_float($value)) {
39               // Force the conversion to float so as not to loose precision
40               // in the automatic cast.
41               $value = sprintf('%F', $value);
42             }
43             $query = substr_replace($query, $value, strpos($query, '?'), 1);
44           }
45           else {
46             $placeholder = ':db_statement_placeholder_' . $count++;
47             $query = substr_replace($query, $placeholder, strpos($query, '?'), 1);
48             $new_args[$placeholder] = $value;
49           }
50         }
51         $args = $new_args;
52       }
53       else {
54         // Else, this is using named placeholders.
55         foreach ($args as $placeholder => $value) {
56           if (is_float($value) || is_int($value)) {
57             if (is_float($value)) {
58               // Force the conversion to float so as not to loose precision
59               // in the automatic cast.
60               $value = sprintf('%F', $value);
61             }
62
63             // We will remove this placeholder from the query as PDO throws an
64             // exception if the number of placeholders in the query and the
65             // arguments does not match.
66             unset($args[$placeholder]);
67             // PDO allows placeholders to not be prefixed by a colon. See
68             // http://marc.info/?l=php-internals&m=111234321827149&w=2 for
69             // more.
70             if ($placeholder[0] != ':') {
71               $placeholder = ":$placeholder";
72             }
73             // When replacing the placeholders, make sure we search for the
74             // exact placeholder. For example, if searching for
75             // ':db_placeholder_1', do not replace ':db_placeholder_11'.
76             $query = preg_replace('/' . preg_quote($placeholder) . '\b/', $value, $query);
77           }
78         }
79       }
80     }
81
82     return $this->pdoConnection->prepare($query);
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function execute($args = [], $options = []) {
89     try {
90       $return = parent::execute($args, $options);
91     }
92     catch (\PDOException $e) {
93       if (!empty($e->errorInfo[1]) && $e->errorInfo[1] === 17) {
94         // The schema has changed. SQLite specifies that we must resend the query.
95         $return = parent::execute($args, $options);
96       }
97       else {
98         // Rethrow the exception.
99         throw $e;
100       }
101     }
102
103     // In some weird cases, SQLite will prefix some column names by the name
104     // of the table. We post-process the data, by renaming the column names
105     // using the same convention as MySQL and PostgreSQL.
106     $rename_columns = [];
107     foreach ($this->columnNames as $k => $column) {
108       // In some SQLite versions, SELECT DISTINCT(field) will return "(field)"
109       // instead of "field".
110       if (preg_match("/^\((.*)\)$/", $column, $matches)) {
111         $rename_columns[$column] = $matches[1];
112         $this->columnNames[$k] = $matches[1];
113         $column = $matches[1];
114       }
115
116       // Remove "table." prefixes.
117       if (preg_match("/^.*\.(.*)$/", $column, $matches)) {
118         $rename_columns[$column] = $matches[1];
119         $this->columnNames[$k] = $matches[1];
120       }
121     }
122     if ($rename_columns) {
123       // DatabaseStatementPrefetch already extracted the first row,
124       // put it back into the result set.
125       if (isset($this->currentRow)) {
126         $this->data[0] = &$this->currentRow;
127       }
128
129       // Then rename all the columns across the result set.
130       foreach ($this->data as $k => $row) {
131         foreach ($rename_columns as $old_column => $new_column) {
132           $this->data[$k][$new_column] = $this->data[$k][$old_column];
133           unset($this->data[$k][$old_column]);
134         }
135       }
136
137       // Finally, extract the first row again.
138       $this->currentRow = $this->data[0];
139       unset($this->data[0]);
140     }
141
142     return $return;
143   }
144
145 }