3 namespace Drupal\Core\Database\Driver\sqlite;
5 use Drupal\Core\Database\StatementPrefetch;
6 use Drupal\Core\Database\StatementInterface;
9 * SQLite implementation of \Drupal\Core\Database\Statement.
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.
17 class Statement extends StatementPrefetch implements StatementInterface {
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
27 * See http://bugs.php.net/bug.php?id=45259 for more details.
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.
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);
43 $query = substr_replace($query, $value, strpos($query, '?'), 1);
46 $placeholder = ':db_statement_placeholder_' . $count++;
47 $query = substr_replace($query, $placeholder, strpos($query, '?'), 1);
48 $new_args[$placeholder] = $value;
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);
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
70 if ($placeholder[0] != ':') {
71 $placeholder = ":$placeholder";
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);
82 return $this->pdoConnection->prepare($query);
88 public function execute($args = [], $options = []) {
90 $return = parent::execute($args, $options);
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);
98 // Rethrow the exception.
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];
116 // Remove "table." prefixes.
117 if (preg_match("/^.*\.(.*)$/", $column, $matches)) {
118 $rename_columns[$column] = $matches[1];
119 $this->columnNames[$k] = $matches[1];
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;
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]);
137 // Finally, extract the first row again.
138 $this->currentRow = $this->data[0];
139 unset($this->data[0]);