Debian Codegraph @

includes/libs/rdbms/platform/SqlitePlatform.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20 namespace Wikimedia\Rdbms\Platform;
21
22 use Wikimedia\Rdbms\Query;
23
24 /**
25 * @since 1.38
26 * @see ISQLPlatform
27 */
28 class SqlitePlatform extends SQLPlatform {
29 public function buildGreatest( $fields, $values ) {
30 return $this->buildSuperlative( 'MAX', $fields, $values );
31 }
32
33 public function buildLeast( $fields, $values ) {
34 return $this->buildSuperlative( 'MIN', $fields, $values );
35 }
36
37 /**
38 * Build a concatenation list to feed into a SQL query
39 *
40 * @param string[] $stringList
41 * @return string
42 */
43 public function buildConcat( $stringList ) {
44 return '(' . implode( ') || (', $stringList ) . ')';
45 }
46
47 /**
48 * @param string[] $sqls
49 * @param bool $all Whether to "UNION ALL" or not
50 * @param array $options Query options, will be ignored in Sqlite
51 * @return string
52 */
53 public function unionQueries( $sqls, $all, $options = [] ) {
54 $glue = $all ? ' UNION ALL ' : ' UNION ';
55
56 return implode( $glue, $sqls );
57 }
58
59 /**
60 * @return bool
61 */
62 public function unionSupportsOrderAndLimit() {
63 return false;
64 }
65
66 public function buildSubstring( $input, $startPosition, $length = null ) {
67 $this->assertBuildSubstringParams( $startPosition, $length );
68 $params = [ $input, $startPosition ];
69 if ( $length !== null ) {
70 $params[] = $length;
71 }
72 return 'SUBSTR(' . implode( ',', $params ) . ')';
73 }
74
75 /**
76 * @param string $field Field or column to cast
77 * @return string
78 * @since 1.28
79 */
80 public function buildStringCast( $field ) {
81 return 'CAST ( ' . $field . ' AS TEXT )';
82 }
83
84 public function tableName( string $name, $format = 'quoted' ) {
85 if ( preg_match( '/^sqlite_[a-z_]+$/', $name ) ) {
86 // Such names are reserved for internal SQLite tables
87 return $name;
88 }
89
90 return parent::tableName( $name, $format );
91 }
92
93 protected function makeSelectOptions( array $options ) {
94 // Remove problematic options that the base implementation converts to SQL
95 foreach ( $options as $k => $v ) {
96 if ( is_numeric( $k ) && ( $v === 'FOR UPDATE' || $v === 'LOCK IN SHARE MODE' ) ) {
97 $options[$k] = '';
98 }
99 }
100
101 return parent::makeSelectOptions( $options );
102 }
103
104 public function buildGroupConcatField(
105 $delim, $tables, $field, $conds = '', $join_conds = []
106 ) {
107 $fld = "group_concat($field," . $this->quoter->addQuotes( $delim ) . ')';
108
109 return '(' . $this->selectSQLText( $tables, $fld, $conds, static::CALLER_SUBQUERY, [], $join_conds ) . ')';
110 }
111
112 protected function makeInsertNonConflictingVerbAndOptions() {
113 return [ 'INSERT OR IGNORE INTO', '' ];
114 }
115
116 /**
117 * @param array $options
118 * @return array
119 */
120 protected function makeUpdateOptionsArray( $options ) {
121 $options = parent::makeUpdateOptionsArray( $options );
122 $options = $this->rewriteIgnoreKeyword( $options );
123
124 return $options;
125 }
126
127 /**
128 * @param array $options
129 * @return array
130 */
131 private function rewriteIgnoreKeyword( $options ) {
132 # SQLite uses OR IGNORE not just IGNORE
133 foreach ( $options as $k => $v ) {
134 if ( $v == 'IGNORE' ) {
135 $options[$k] = 'OR IGNORE';
136 }
137 }
138
139 return $options;
140 }
141
142 public function dropTableSqlText( $table ) {
143 // No CASCADE support; https://www.sqlite.org/lang_droptable.html
144 return "DROP TABLE " . $this->tableName( $table );
145 }
146
147 public function isTransactableQuery( Query $sql ) {
148 return parent::isTransactableQuery( $sql ) && !in_array(
149 $sql->getVerb(),
150 [ 'ATTACH', 'PRAGMA' ],
151 true
152 );
153 }
154 }