| 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\DBLanguageError; |
| 23 |
use Wikimedia\Rdbms\Query; |
| 24 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class PostgresPlatform extends SQLPlatform { |
| 31 |
|
| 32 |
private $coreSchema; |
| 33 |
|
| 34 |
public function limitResult( $sql, $limit, $offset = false ) { |
| 35 |
return "$sql LIMIT $limit " . ( is_numeric( $offset ) ? " OFFSET {$offset} " : '' ); |
| 36 |
} |
| 37 |
|
| 38 |
public function buildConcat( $stringList ) { |
| 39 |
return implode( ' || ', $stringList ); |
| 40 |
} |
| 41 |
|
| 42 |
public function timestamp( $ts = 0 ) { |
| 43 |
$ct = new ConvertibleTimestamp( $ts ); |
| 44 |
|
| 45 |
return $ct->getTimestamp( TS_POSTGRES ); |
| 46 |
} |
| 47 |
|
| 48 |
public function buildStringCast( $field ) { |
| 49 |
return $field . '::text'; |
| 50 |
} |
| 51 |
|
| 52 |
public function implicitOrderby() { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
public function getCoreSchema(): string { |
| 57 |
return $this->coreSchema; |
| 58 |
} |
| 59 |
|
| 60 |
public function setCoreSchema( string $coreSchema ): void { |
| 61 |
$this->coreSchema = $coreSchema; |
| 62 |
} |
| 63 |
|
| 64 |
public function selectSQLText( |
| 65 |
$tables, $vars, $conds = '', $fname = __METHOD__, $options = [], $join_conds = [] |
| 66 |
) { |
| 67 |
if ( is_string( $options ) ) { |
| 68 |
$options = [ $options ]; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
if ( is_array( $options ) ) { |
| 79 |
$forUpdateKey = array_search( 'FOR UPDATE', $options, true ); |
| 80 |
if ( $forUpdateKey !== false && $join_conds ) { |
| 81 |
unset( $options[$forUpdateKey] ); |
| 82 |
$options['FOR UPDATE'] = []; |
| 83 |
|
| 84 |
$toCheck = $tables; |
| 85 |
reset( $toCheck ); |
| 86 |
while ( $toCheck ) { |
| 87 |
$alias = key( $toCheck ); |
| 88 |
$table = $toCheck[$alias]; |
| 89 |
unset( $toCheck[$alias] ); |
| 90 |
|
| 91 |
if ( !is_string( $alias ) ) { |
| 92 |
|
| 93 |
$alias = $table; |
| 94 |
} |
| 95 |
|
| 96 |
if ( !isset( $join_conds[$alias] ) || |
| 97 |
!preg_match( '/^(?:LEFT|RIGHT|FULL)(?: OUTER)? JOIN$/i', $join_conds[$alias][0] ) |
| 98 |
) { |
| 99 |
if ( is_array( $table ) ) { |
| 100 |
|
| 101 |
$toCheck = array_merge( $toCheck, $table ); |
| 102 |
} else { |
| 103 |
|
| 104 |
$options['FOR UPDATE'][] = $alias; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if ( |
| 111 |
isset( $options['ORDER BY'] ) && |
| 112 |
( $options['ORDER BY'] == 'NULL' || $options['ORDER BY'] == [ 'NULL' ] ) |
| 113 |
) { |
| 114 |
unset( $options['ORDER BY'] ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return parent::selectSQLText( $tables, $vars, $conds, $fname, $options, $join_conds ); |
| 119 |
} |
| 120 |
|
| 121 |
protected function makeSelectOptions( array $options ) { |
| 122 |
$preLimitTail = $postLimitTail = ''; |
| 123 |
$startOpts = ''; |
| 124 |
|
| 125 |
$noKeyOptions = []; |
| 126 |
foreach ( $options as $key => $option ) { |
| 127 |
if ( is_numeric( $key ) ) { |
| 128 |
$noKeyOptions[$option] = true; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
$preLimitTail .= $this->makeGroupByWithHaving( $options ); |
| 133 |
|
| 134 |
$preLimitTail .= $this->makeOrderBy( $options ); |
| 135 |
|
| 136 |
if ( isset( $options['FOR UPDATE'] ) ) { |
| 137 |
$postLimitTail .= ' FOR UPDATE OF ' . implode( |
| 138 |
', ', |
| 139 |
array_map( [ $this, 'addIdentifierQuotes' ], $options['FOR UPDATE'] ) |
| 140 |
); |
| 141 |
} elseif ( isset( $noKeyOptions['FOR UPDATE'] ) ) { |
| 142 |
$postLimitTail .= ' FOR UPDATE'; |
| 143 |
} |
| 144 |
|
| 145 |
if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) { |
| 146 |
$startOpts .= 'DISTINCT'; |
| 147 |
} |
| 148 |
|
| 149 |
return [ $startOpts, $preLimitTail, $postLimitTail ]; |
| 150 |
} |
| 151 |
|
| 152 |
public function getDatabaseAndTableIdentifier( string $table ) { |
| 153 |
$components = $this->qualifiedTableComponents( $table ); |
| 154 |
switch ( count( $components ) ) { |
| 155 |
case 1: |
| 156 |
return [ $this->currentDomain->getDatabase(), $components[0] ]; |
| 157 |
case 2: |
| 158 |
return [ $this->currentDomain->getDatabase(), $components[1] ]; |
| 159 |
case 3: |
| 160 |
return [ $components[0], $components[2] ]; |
| 161 |
default: |
| 162 |
throw new DBLanguageError( 'Too many table components' ); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
protected function relationSchemaQualifier() { |
| 167 |
if ( $this->coreSchema === $this->currentDomain->getSchema() ) { |
| 168 |
|
| 169 |
return ''; |
| 170 |
} |
| 171 |
|
| 172 |
return parent::relationSchemaQualifier(); |
| 173 |
} |
| 174 |
|
| 175 |
public function buildGroupConcatField( |
| 176 |
$delim, $tables, $field, $conds = '', $join_conds = [] |
| 177 |
) { |
| 178 |
$fld = "array_to_string(array_agg($field)," . $this->quoter->addQuotes( $delim ) . ')'; |
| 179 |
|
| 180 |
return '(' . $this->selectSQLText( $tables, $fld, $conds, static::CALLER_SUBQUERY, [], $join_conds ) . ')'; |
| 181 |
} |
| 182 |
|
| 183 |
public function makeInsertLists( array $rows, $aliasPrefix = '', array $typeByColumn = [] ) { |
| 184 |
$firstRow = $rows[0]; |
| 185 |
if ( !is_array( $firstRow ) || !$firstRow ) { |
| 186 |
throw new DBLanguageError( 'Got an empty row list or empty row' ); |
| 187 |
} |
| 188 |
|
| 189 |
$tupleColumns = array_keys( $firstRow ); |
| 190 |
|
| 191 |
$valueTuples = []; |
| 192 |
foreach ( $rows as $row ) { |
| 193 |
$rowColumns = array_keys( $row ); |
| 194 |
|
| 195 |
if ( $rowColumns !== $tupleColumns ) { |
| 196 |
throw new DBLanguageError( |
| 197 |
'Got row columns (' . implode( ', ', $rowColumns ) . ') ' . |
| 198 |
'instead of expected (' . implode( ', ', $tupleColumns ) . ')' |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
$typedRowValues = []; |
| 203 |
foreach ( $row as $column => $value ) { |
| 204 |
$type = $typeByColumn[$column] ?? null; |
| 205 |
if ( $value === null ) { |
| 206 |
$typedRowValues[] = 'NULL'; |
| 207 |
} elseif ( $type !== null ) { |
| 208 |
$typedRowValues[] = $this->quoter->addQuotes( $value ) . '::' . $type; |
| 209 |
} else { |
| 210 |
$typedRowValues[] = $this->quoter->addQuotes( $value ); |
| 211 |
} |
| 212 |
} |
| 213 |
$valueTuples[] = '(' . implode( ',', $typedRowValues ) . ')'; |
| 214 |
} |
| 215 |
|
| 216 |
$magicAliasFields = []; |
| 217 |
foreach ( $tupleColumns as $column ) { |
| 218 |
$magicAliasFields[] = $aliasPrefix . $column; |
| 219 |
} |
| 220 |
|
| 221 |
return [ |
| 222 |
$this->makeList( $tupleColumns, self::LIST_NAMES ), |
| 223 |
implode( ',', $valueTuples ), |
| 224 |
$this->makeList( $magicAliasFields, self::LIST_NAMES ) |
| 225 |
]; |
| 226 |
} |
| 227 |
|
| 228 |
protected function makeInsertNonConflictingVerbAndOptions() { |
| 229 |
return [ 'INSERT INTO', 'ON CONFLICT DO NOTHING' ]; |
| 230 |
} |
| 231 |
|
| 232 |
protected function makeUpdateOptionsArray( $options ) { |
| 233 |
$options = $this->normalizeOptions( $options ); |
| 234 |
|
| 235 |
$options = array_diff( $options, [ 'IGNORE' ] ); |
| 236 |
|
| 237 |
return parent::makeUpdateOptionsArray( $options ); |
| 238 |
} |
| 239 |
|
| 240 |
public function isTransactableQuery( Query $sql ) { |
| 241 |
return parent::isTransactableQuery( $sql ) && |
| 242 |
!preg_match( '/^SELECT\s+pg_(try_|)advisory_\w+\(/', $sql->getSQL() ); |
| 243 |
} |
| 244 |
|
| 245 |
public function lockSQLText( $lockName, $timeout ) { |
| 246 |
|
| 247 |
$key = $this->quoter->addQuotes( $this->bigintFromLockName( $lockName ) ); |
| 248 |
return "SELECT (CASE WHEN pg_try_advisory_lock($key) " . |
| 249 |
"THEN EXTRACT(epoch from clock_timestamp()) " . |
| 250 |
"ELSE NULL " . |
| 251 |
"END) AS acquired"; |
| 252 |
} |
| 253 |
|
| 254 |
public function lockIsFreeSQLText( $lockName ) { |
| 255 |
|
| 256 |
$key = $this->quoter->addQuotes( $this->bigintFromLockName( $lockName ) ); |
| 257 |
return "SELECT (CASE(pg_try_advisory_lock($key)) |
| 258 |
WHEN FALSE THEN FALSE ELSE pg_advisory_unlock($key) END) AS unlocked"; |
| 259 |
} |
| 260 |
|
| 261 |
public function unlockSQLText( $lockName ) { |
| 262 |
|
| 263 |
$key = $this->quoter->addQuotes( $this->bigintFromLockName( $lockName ) ); |
| 264 |
return "SELECT pg_advisory_unlock($key) AS released"; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
private function bigintFromLockName( $lockName ) { |
| 272 |
return \Wikimedia\base_convert( substr( sha1( $lockName ), 0, 15 ), 16, 10 ); |
| 273 |
} |
| 274 |
} |