| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace Wikimedia\Rdbms\Platform; |
| 21 |
|
| 22 |
use Wikimedia\Rdbms\Query; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function buildConcat( $stringList ) { |
| 44 |
return '(' . implode( ') || (', $stringList ) . ')'; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function unionQueries( $sqls, $all, $options = [] ) { |
| 54 |
$glue = $all ? ' UNION ALL ' : ' UNION '; |
| 55 |
|
| 56 |
return implode( $glue, $sqls ); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 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 |
|
| 77 |
|
| 78 |
|
| 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 |
|
| 87 |
return $name; |
| 88 |
} |
| 89 |
|
| 90 |
return parent::tableName( $name, $format ); |
| 91 |
} |
| 92 |
|
| 93 |
protected function makeSelectOptions( array $options ) { |
| 94 |
|
| 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 |
|
| 118 |
|
| 119 |
|
| 120 |
protected function makeUpdateOptionsArray( $options ) { |
| 121 |
$options = parent::makeUpdateOptionsArray( $options ); |
| 122 |
$options = $this->rewriteIgnoreKeyword( $options ); |
| 123 |
|
| 124 |
return $options; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
private function rewriteIgnoreKeyword( $options ) { |
| 132 |
|
| 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 |
|
| 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 |
} |