| 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\DBError; |
| 23 | use Wikimedia\Rdbms\IExpression; |
| 24 | use Wikimedia\Rdbms\LikeMatch; |
| 25 | use Wikimedia\Rdbms\RawSQLValue; |
| 26 | use Wikimedia\Rdbms\Subquery; |
| 27 | |
| 28 | // Very long type annotations :( |
| 29 | // phpcs:disable Generic.Files.LineLength |
| 30 | |
| 31 | /** |
| 32 | * Interface for query language. |
| 33 | * Note: This is for simple SQL operations, use query builder classes for building full queries. |
| 34 | * |
| 35 | * Methods of this interface should be only used by rdbms library. |
| 36 | * @since 1.39 |
| 37 | */ |
| 38 | interface ISQLPlatform { |
| 39 | |
| 40 | /** Combine list with comma delimiters */ |
| 41 | public const LIST_COMMA = 0; |
| 42 | /** Combine list with AND clauses */ |
| 43 | public const LIST_AND = 1; |
| 44 | /** Convert map into a SET clause */ |
| 45 | public const LIST_SET = 2; |
| 46 | /** Treat as field name and do not apply value escaping */ |
| 47 | public const LIST_NAMES = 3; |
| 48 | /** Combine list with OR clauses */ |
| 49 | public const LIST_OR = 4; |
| 50 | |
| 51 | /** Unconditional update/delete of whole table */ |
| 52 | public const ALL_ROWS = '*'; |
| 53 | |
| 54 | /** Idiom for "no special flags" */ |
| 55 | public const QUERY_NORMAL = 0; |
| 56 | /** Ignore query errors and return false when they happen */ |
| 57 | public const QUERY_SILENCE_ERRORS = 1; // b/c for 1.32 query() argument; (int)true = 1 |
| 58 | /** Track a TEMPORARY table CREATE as if it was for a permanent table (for testing) */ |
| 59 | public const QUERY_PSEUDO_PERMANENT = 2; |
| 60 | /** Enforce that a query does not make effective writes */ |
| 61 | public const QUERY_REPLICA_ROLE = 4; |
| 62 | /** Ignore the current presence of any DBO_TRX flag */ |
| 63 | public const QUERY_IGNORE_DBO_TRX = 8; |
| 64 | /** Do not try to retry the query if the connection was lost */ |
| 65 | public const QUERY_NO_RETRY = 16; |
| 66 | /** Query is a read-only Data Query Language query */ |
| 67 | public const QUERY_CHANGE_NONE = 32; |
| 68 | /** Query is a Transaction Control Language command (BEGIN, USE, SET, ...) */ |
| 69 | public const QUERY_CHANGE_TRX = 64; |
| 70 | /** Query is a Data Manipulation Language command (INSERT, DELETE, LOCK, ...) */ |
| 71 | public const QUERY_CHANGE_ROWS = 128; |
| 72 | /** Query is a Data Definition Language command */ |
| 73 | public const QUERY_CHANGE_SCHEMA = 256; |
| 74 | /** Query is a command for advisory locks */ |
| 75 | public const QUERY_CHANGE_LOCKS = 512; |
| 76 | |
| 77 | /** |
| 78 | * Special value for ->caller() / $fname parameter used when providing a caller is not |
| 79 | * expected, because we're formatting a subquery that won't be executed directly. |
| 80 | * @since 1.43 |
| 81 | */ |
| 82 | public const CALLER_SUBQUERY = 'subquery'; |
| 83 | |
| 84 | /** |
| 85 | * Special value for ->caller() / $fname parameter used when a caller is not provided. |
| 86 | * @since 1.43 |
| 87 | */ |
| 88 | public const CALLER_UNKNOWN = 'unknown'; |
| 89 | |
| 90 | /** |
| 91 | * @param string|int $field |
| 92 | * @return string |
| 93 | */ |
| 94 | public function bitNot( $field ); |
| 95 | |
| 96 | /** |
| 97 | * @param string|int $fieldLeft |
| 98 | * @param string|int $fieldRight |
| 99 | * @return string |
| 100 | */ |
| 101 | public function bitAnd( $fieldLeft, $fieldRight ); |
| 102 | |
| 103 | /** |
| 104 | * @param string|int $fieldLeft |
| 105 | * @param string|int $fieldRight |
| 106 | * @return string |
| 107 | */ |
| 108 | public function bitOr( $fieldLeft, $fieldRight ); |
| 109 | |
| 110 | /** |
| 111 | * Escape a SQL identifier (e.g. table, column, database) for use in a SQL query |
| 112 | * |
| 113 | * Depending on the database this will either be `backticks` or "double quotes" |
| 114 | * |
| 115 | * @param string $s |
| 116 | * @param-taint $s escapes_sql NOTE: this is subpar, as addIdentifierQuotes isn't always the right type of escaping. |
| 117 | * @return string |
| 118 | * @return-taint none |
| 119 | * @since 1.33 |
| 120 | */ |
| 121 | public function addIdentifierQuotes( $s ); |
| 122 | |
| 123 | /** |
| 124 | * Build a GREATEST function statement comparing columns/values |
| 125 | * |
| 126 | * Integer and float values in $values will not be quoted |
| 127 | * |
| 128 | * If $fields is an array, then each value with a string key is treated as an expression |
| 129 | * (which must be manually quoted); such string keys do not appear in the SQL and are only |
| 130 | * descriptive aliases. |
| 131 | * |
| 132 | * @param string|string[] $fields Name(s) of column(s) with values to compare |
| 133 | * @param string|int|float|string[]|int[]|float[] $values Values to compare |
| 134 | * @return mixed |
| 135 | * @since 1.35 in IDatabase, moved to ISQLPlatform in 1.39 |
| 136 | */ |
| 137 | public function buildGreatest( $fields, $values ); |
| 138 | |
| 139 | /** |
| 140 | * Build a LEAST function statement comparing columns/values |
| 141 | * |
| 142 | * Integer and float values in $values will not be quoted |
| 143 | * |
| 144 | * If $fields is an array, then each value with a string key is treated as an expression |
| 145 | * (which must be manually quoted); such string keys do not appear in the SQL and are only |
| 146 | * descriptive aliases. |
| 147 | * |
| 148 | * @param string|string[] $fields Name(s) of column(s) with values to compare |
| 149 | * @param string|int|float|string[]|int[]|float[] $values Values to compare |
| 150 | * @return mixed |
| 151 | * @since 1.35 in IDatabase, moved to ISQLPlatform in 1.39 |
| 152 | */ |
| 153 | public function buildLeast( $fields, $values ); |
| 154 | |
| 155 | /** |
| 156 | * Build a condition comparing multiple values, for use with indexes that cover |
| 157 | * multiple fields, common when e.g. paging through results or doing batch operations. |
| 158 | * |
| 159 | * For example, you might be displaying a list of people ordered alphabetically by their last |
| 160 | * and first name, split across multiple pages. The first page of the results ended at Jane Doe. |
| 161 | * When building the query for the next page, you would use: |
| 162 | * |
| 163 | * $queryBuilder->where( $db->buildComparison( '>', [ 'last' => 'Doe', 'first' => 'Jane' ] ) ); |
| 164 | * |
| 165 | * This will return people whose last name follows Doe, or whose last name is Doe and first name |
| 166 | * follows Jane. |
| 167 | * |
| 168 | * Note that the order of keys in the associative array $conds is significant, |
| 169 | * and must match the order of fields used by the index. |
| 170 | * |
| 171 | * When comparing a single value, prefer using the expression builder: |
| 172 | * |
| 173 | * $db->expr( 'key', '<=', $val ) |
| 174 | * |
| 175 | * // equivalent to: |
| 176 | * $db->buildComparison( '<=', [ 'key' => $val ] ) |
| 177 | * 'key <= ' . $db->addQuotes( $val ) |
| 178 | * |
| 179 | * @param string $op Comparison operator, one of '>', '>=', '<', '<=' |
| 180 | * @param non-empty-array<string,mixed> $conds Map of field names to their values to use in the comparison |
| 181 | * @return string SQL code |
| 182 | */ |
| 183 | public function buildComparison( string $op, array $conds ): string; |
| 184 | |
| 185 | /** |
| 186 | * Makes an encoded list of strings from an array |
| 187 | * |
| 188 | * These can be used to make conjunctions or disjunctions on SQL condition strings |
| 189 | * derived from an array ({@see IDatabase::select} $conds documentation). |
| 190 | * |
| 191 | * Example usage: |
| 192 | * @code |
| 193 | * $sql = $db->makeList( [ |
| 194 | * 'rev_page' => $id, |
| 195 | * $db->makeList( [ 'rev_minor' => 1, 'rev_len < 500' ], $db::LIST_OR ) |
| 196 | * ], $db::LIST_AND ); |
| 197 | * @endcode |
| 198 | * This would set $sql to "rev_page = '$id' AND (rev_minor = 1 OR rev_len < 500)" |
| 199 | * |
| 200 | * @param array $a Containing the data |
| 201 | * @param-taint $a escapes_sql - Note, this is also special-cased in MediaWikiSecurityCheckPlugin |
| 202 | * @param int $mode IDatabase class constant: |
| 203 | * - IDatabase::LIST_COMMA: Comma separated, no field names |
| 204 | * - IDatabase::LIST_AND: ANDed WHERE clause (without the WHERE). |
| 205 | * - IDatabase::LIST_OR: ORed WHERE clause (without the WHERE) |
| 206 | * - IDatabase::LIST_SET: Comma separated with field names, like a SET clause |
| 207 | * - IDatabase::LIST_NAMES: Comma separated field names |
| 208 | * @param-taint $mode none |
| 209 | * @throws DBError If an error occurs, {@see IDatabase::query} |
| 210 | * @return string |
| 211 | * @return-taint none |
| 212 | */ |
| 213 | public function makeList( array $a, $mode = self::LIST_COMMA ); |
| 214 | |
| 215 | /** |
| 216 | * Build a "OR" condition with pairs from a two-dimensional array. |
| 217 | * |
| 218 | * The associative array should have integer keys relating to the $baseKey field. |
| 219 | * The nested array should have string keys for the $subKey field. The inner |
| 220 | * values are ignored, and are typically boolean true. |
| 221 | * |
| 222 | * Example usage: |
| 223 | * ``` |
| 224 | * $data = [ |
| 225 | * 2 => [ |
| 226 | * 'Foo' => true, |
| 227 | * 'Bar' => true, |
| 228 | * ], |
| 229 | * 3 => [ |
| 230 | * 'Quux' => true, |
| 231 | * ], |
| 232 | * ]; |
| 233 | * // (page_namespace = 2 AND page_title IN ('Foo','Bar')) |
| 234 | * // OR (page_namespace = 3 AND page_title = 'Quux') |
| 235 | * ``` |
| 236 | * @todo This is effectively specific to MediaWiki's LinkBatch. |
| 237 | * Consider deprecating or generalising slightly. |
| 238 | * |
| 239 | * @param array $data Nested array, must be non-empty |
| 240 | * @phan-param non-empty-array $data |
| 241 | * @param string $baseKey Field name to match the base-level keys to (eg 'pl_namespace') |
| 242 | * @param string $subKey Field name to match the sub-level keys to (eg 'pl_title') |
| 243 | * @return string SQL fragment |
| 244 | */ |
| 245 | public function makeWhereFrom2d( $data, $baseKey, $subKey ); |
| 246 | |
| 247 | /** |
| 248 | * Given an array of condition arrays representing an OR list of AND lists, |
| 249 | * for example: |
| 250 | * |
| 251 | * (A=1 AND B=2) OR (A=1 AND B=3) |
| 252 | * |
| 253 | * produce an SQL expression in which the conditions are factored: |
| 254 | * |
| 255 | * (A=1 AND (B=2 OR B=3)) |
| 256 | * |
| 257 | * We also use IN() to simplify further: |
| 258 | * |
| 259 | * (A=1 AND (B IN (2,3)) |
| 260 | * |
| 261 | * More compactly, in boolean algebra notation, a sum of products, e.g. |
| 262 | * AB + AC is factored to produce A(B+C). Factoring proceeds recursively |
| 263 | * to reduce expressions with any number of variables, for example |
| 264 | * AEP + AEQ + AFP + AFQ = A(E(P+Q) + F(P+Q)) |
| 265 | * |
| 266 | * The algorithm is simple and will not necessarily find the shortest |
| 267 | * possible expression. For the best results, fields should be given in a |
| 268 | * consistent order, and the fields with values likely to be shared should |
| 269 | * be leftmost in the associative arrays. |
| 270 | * |
| 271 | * @param array $condsArray An array of associative arrays. The associative |
| 272 | * array keys represent field names, and the values represent the field |
| 273 | * values to compare against. |
| 274 | * @return string SQL expression fragment |
| 275 | */ |
| 276 | public function factorConds( $condsArray ); |
| 277 | |
| 278 | /** |
| 279 | * Build a concatenation list to feed into a SQL query |
| 280 | * @param string[] $stringList Raw SQL expression list; caller is responsible for escaping |
| 281 | * @return string |
| 282 | */ |
| 283 | public function buildConcat( $stringList ); |
| 284 | |
| 285 | /** |
| 286 | * Construct a LIMIT query with optional offset |
| 287 | * |
| 288 | * The SQL should be adjusted so that only the first $limit rows |
| 289 | * are returned. If $offset is provided as well, then the first $offset |
| 290 | * rows should be discarded, and the next $limit rows should be returned. |
| 291 | * If the result of the query is not ordered, then the rows to be returned |
| 292 | * are theoretically arbitrary. |
| 293 | * |
| 294 | * $sql is expected to be a SELECT, if that makes a difference. |
| 295 | * |
| 296 | * @param string $sql SQL query we will append the limit too |
| 297 | * @param int $limit The SQL limit |
| 298 | * @param int|false $offset The SQL offset (default false) |
| 299 | * @return string |
| 300 | * @since 1.34 in IDatabase, moved to ISQLPlatform in 1.39 |
| 301 | */ |
| 302 | public function limitResult( $sql, $limit, $offset = false ); |
| 303 | |
| 304 | /** |
| 305 | * LIKE statement wrapper |
| 306 | * |
| 307 | * This takes a variable-length argument list with parts of pattern to match |
| 308 | * containing either string literals that will be escaped or tokens returned by |
| 309 | * {@link anyChar()} or {@link anyString()}. |
| 310 | * Alternatively, the function could be provided with an array of the aforementioned parameters. |
| 311 | * |
| 312 | * Example: $dbr->buildLike( 'My_page_title/', $dbr->anyString() ) returns |
| 313 | * a LIKE clause that searches for subpages of 'My page title'. |
| 314 | * Alternatively: |
| 315 | * $pattern = [ 'My_page_title/', $dbr->anyString() ]; |
| 316 | * $query .= $dbr->buildLike( $pattern ); |
| 317 | * |
| 318 | * @since 1.16 in IDatabase, moved to ISQLPlatform in 1.39 |
| 319 | * @param array[]|string|LikeMatch $param |
| 320 | * @param-taint $param escapes_sql |
| 321 | * @param string|LikeMatch ...$params |
| 322 | * @param-taint ...$params escapes_sql |
| 323 | * @return string Fully built LIKE statement |
| 324 | * @return-taint none |
| 325 | */ |
| 326 | public function buildLike( $param, ...$params ); |
| 327 | |
| 328 | /** |
| 329 | * Returns a token for {@link buildLike()} that denotes a '_' to be used in a LIKE query |
| 330 | * |
| 331 | * @return LikeMatch |
| 332 | */ |
| 333 | public function anyChar(); |
| 334 | |
| 335 | /** |
| 336 | * Returns a token for {@link buildLike()} that denotes a '%' to be used in a LIKE query |
| 337 | * |
| 338 | * @return LikeMatch |
| 339 | */ |
| 340 | public function anyString(); |
| 341 | |
| 342 | /** |
| 343 | * Determine if the RDBMS supports ORDER BY and LIMIT for separate subqueries within UNION |
| 344 | * |
| 345 | * @return bool |
| 346 | */ |
| 347 | public function unionSupportsOrderAndLimit(); |
| 348 | |
| 349 | /** |
| 350 | * Construct a UNION query |
| 351 | * |
| 352 | * This is used for providing overload point for other DB abstractions |
| 353 | * not compatible with the MySQL syntax. |
| 354 | * |
| 355 | * @internal callers outside of rdbms library should use UnionQueryBuilder instead. |
| 356 | * |
| 357 | * @param array $sqls SQL statements to combine |
| 358 | * @param bool $all Either {@link IDatabase::UNION_ALL} or {@link IDatabase::UNION_DISTINCT} |
| 359 | * @param array $options Query options |
| 360 | * |
| 361 | * @return string SQL fragment |
| 362 | */ |
| 363 | public function unionQueries( $sqls, $all, $options = [] ); |
| 364 | |
| 365 | /** |
| 366 | * Convert a timestamp in one of the formats accepted by {@link ConvertibleTimestamp} |
| 367 | * to the format used for inserting into timestamp fields in this DBMS |
| 368 | * |
| 369 | * The result is unquoted, and needs to be passed through addQuotes() |
| 370 | * before it can be included in raw SQL. |
| 371 | * |
| 372 | * @param string|int $ts |
| 373 | * |
| 374 | * @return string |
| 375 | */ |
| 376 | public function timestamp( $ts = 0 ); |
| 377 | |
| 378 | /** |
| 379 | * Convert a timestamp in one of the formats accepted by ConvertibleTimestamp |
| 380 | * to the format used for inserting into timestamp fields in this DBMS |
| 381 | * |
| 382 | * If NULL is input, it is passed through, allowing NULL values to be inserted |
| 383 | * into timestamp fields. |
| 384 | * |
| 385 | * The result is unquoted, and needs to be passed through addQuotes() |
| 386 | * before it can be included in raw SQL. |
| 387 | * |
| 388 | * @param string|int|null $ts |
| 389 | * |
| 390 | * @return string|null |
| 391 | */ |
| 392 | public function timestampOrNull( $ts = null ); |
| 393 | |
| 394 | /** |
| 395 | * Find out when 'infinity' is. Most DBMSes support this. This is a special |
| 396 | * keyword for timestamps in PostgreSQL, and works with CHAR(14) as well |
| 397 | * because "i" sorts after all numbers. |
| 398 | * |
| 399 | * @return string |
| 400 | */ |
| 401 | public function getInfinity(); |
| 402 | |
| 403 | /** |
| 404 | * Encode an expiry time into the DBMS dependent format |
| 405 | * |
| 406 | * @param string $expiry Timestamp for expiry, or the 'infinity' string |
| 407 | * @return string |
| 408 | */ |
| 409 | public function encodeExpiry( $expiry ); |
| 410 | |
| 411 | /** |
| 412 | * Decode an expiry time into a DBMS independent format |
| 413 | * |
| 414 | * @param string $expiry DB timestamp field value for expiry |
| 415 | * @param int $format TS_* constant, defaults to TS_MW |
| 416 | * @return string |
| 417 | */ |
| 418 | public function decodeExpiry( $expiry, $format = TS_MW ); |
| 419 | |
| 420 | /** |
| 421 | * Returns an SQL expression for a simple conditional |
| 422 | * |
| 423 | * This doesn't need to be overridden unless CASE isn't supported in the RDBMS. |
| 424 | * |
| 425 | * @param string|IExpression|array<string,?scalar|non-empty-array<int,?scalar>>|array<int,string|IExpression> $cond |
| 426 | * SQL condition expression (yields a boolean) |
| 427 | * @param string $caseTrueExpression SQL expression to return when the condition is true |
| 428 | * @param string $caseFalseExpression SQL expression to return when the condition is false |
| 429 | * @return string SQL fragment |
| 430 | */ |
| 431 | public function conditional( $cond, $caseTrueExpression, $caseFalseExpression ); |
| 432 | |
| 433 | /** |
| 434 | * Returns a SQL expression for simple string replacement (e.g. REPLACE() in mysql) |
| 435 | * |
| 436 | * @param string $orig Column to modify |
| 437 | * @param string $old Column to seek |
| 438 | * @param string $new Column to replace with |
| 439 | * @return string |
| 440 | */ |
| 441 | public function strreplace( $orig, $old, $new ); |
| 442 | |
| 443 | /** |
| 444 | * Build a SUBSTRING function |
| 445 | * |
| 446 | * Behavior for non-ASCII values is undefined. |
| 447 | * |
| 448 | * @param string $input Field name |
| 449 | * @param int $startPosition Positive integer |
| 450 | * @param int|null $length Non-negative integer length or null for no limit |
| 451 | * @throws \InvalidArgumentException |
| 452 | * @return string SQL text |
| 453 | * @since 1.31 in IDatabase, moved to ISQLPlatform in 1.39 |
| 454 | */ |
| 455 | public function buildSubString( $input, $startPosition, $length = null ); |
| 456 | |
| 457 | /** |
| 458 | * @param string $field Field or column to cast |
| 459 | * @return string |
| 460 | * @since 1.28 in IDatabase, moved to ISQLPlatform in 1.39 |
| 461 | */ |
| 462 | public function buildStringCast( $field ); |
| 463 | |
| 464 | /** |
| 465 | * @param string $field Field or column to cast |
| 466 | * @return string |
| 467 | * @since 1.31 in IDatabase, moved to ISQLPlatform in 1.39 |
| 468 | */ |
| 469 | public function buildIntegerCast( $field ); |
| 470 | |
| 471 | /** |
| 472 | * Returns true if this database does an implicit order by when the column has an index |
| 473 | * For example: SELECT page_title FROM page LIMIT 1 |
| 474 | * |
| 475 | * @return bool |
| 476 | */ |
| 477 | public function implicitOrderby(); |
| 478 | |
| 479 | /** |
| 480 | * Make certain table names use their own database, schema, and table prefix |
| 481 | * when passed into SQL queries pre-escaped and without a qualified database name |
| 482 | * |
| 483 | * For example, "user" can be converted to "myschema.mydbname.user" for convenience. |
| 484 | * Appearances like `user`, somedb.user, somedb.someschema.user will used literally. |
| 485 | * |
| 486 | * Calling this twice will completely clear any old table aliases. Also, note that |
| 487 | * callers are responsible for making sure the schemas and databases actually exist. |
| 488 | * |
| 489 | * @param array[] $aliases Map of (unqualified name of table => (dbname, schema, prefix) map) |
| 490 | * @since 1.28 in IDatabase, moved to ISQLPlatform in 1.39 |
| 491 | */ |
| 492 | public function setTableAliases( array $aliases ); |
| 493 | |
| 494 | /** |
| 495 | * Convert certain index names to alternative names before querying the DB |
| 496 | * |
| 497 | * Note that this applies to indexes regardless of the table they belong to. |
| 498 | * |
| 499 | * This can be employed when an index was renamed X => Y in code, but the new Y-named |
| 500 | * indexes were not yet built on all DBs. After all the Y-named ones are added by the DBA, |
| 501 | * the aliases can be removed, and then the old X-named indexes dropped. |
| 502 | * |
| 503 | * @param string[] $aliases |
| 504 | * @since 1.31 in IDatabase, moved to ISQLPlatform in 1.39 |
| 505 | */ |
| 506 | public function setIndexAliases( array $aliases ); |
| 507 | |
| 508 | /** |
| 509 | * Return current table aliases. |
| 510 | * |
| 511 | * @internal only to be used inside rdbms library |
| 512 | */ |
| 513 | public function getTableAliases(); |
| 514 | |
| 515 | /** |
| 516 | * Take the same arguments as IDatabase::select() and return the SQL it would use |
| 517 | * |
| 518 | * This can be useful for making UNION queries, where the SQL text of each query |
| 519 | * is needed. In general, however, callers outside of Database classes should just |
| 520 | * use select(). |
| 521 | * |
| 522 | * @see IDatabase::select() |
| 523 | * |
| 524 | * @param string|array $tables Table reference(s), using the unqualified name of tables |
| 525 | * or of the form "information_schema.<identifier>". |
| 526 | * @param-taint $tables exec_sql |
| 527 | * @param string|array $vars Field names |
| 528 | * @param-taint $vars exec_sql |
| 529 | * @param string|IExpression|array<string,?scalar|non-empty-array<int,?scalar>|RawSQLValue>|array<int,string|IExpression> $conds |
| 530 | * Conditions |
| 531 | * @param-taint $conds exec_sql_numkey |
| 532 | * @param string $fname Caller function name @phan-mandatory-param |
| 533 | * @param-taint $fname exec_sql |
| 534 | * @param string|array $options Query options |
| 535 | * @param-taint $options none This is special-cased in MediaWikiSecurityCheckPlugin |
| 536 | * @param string|array $join_conds Join conditions |
| 537 | * @param-taint $join_conds none This is special-cased in MediaWikiSecurityCheckPlugin |
| 538 | * @return string SQL query string |
| 539 | * @return-taint onlysafefor_sql |
| 540 | */ |
| 541 | public function selectSQLText( |
| 542 | $tables, |
| 543 | $vars, |
| 544 | $conds = '', |
| 545 | $fname = __METHOD__, |
| 546 | $options = [], |
| 547 | $join_conds = [] |
| 548 | ); |
| 549 | |
| 550 | /** |
| 551 | * Format a table name ready for use in constructing an SQL query |
| 552 | * |
| 553 | * This does two important things: it quotes the table names to clean them up, |
| 554 | * and it adds a table prefix if only given a table name with no quotes. |
| 555 | * |
| 556 | * All functions of this object which require a table name call this function |
| 557 | * themselves. Pass the canonical name to such functions. This is only needed |
| 558 | * when calling {@link query()} directly. |
| 559 | * |
| 560 | * The provided name should not qualify the database nor the schema, unless the name |
| 561 | * is of the form "information_schema.<identifier>". Unlike information_schema tables, |
| 562 | * regular tables can receive writes and are subject to configuration regarding table |
| 563 | * aliases, virtual domains, and LBFactory sharding. Callers needing to access remote |
| 564 | * databases should use appropriate connection factory methods. |
| 565 | * |
| 566 | * @note This function does not sanitize user input. It is not safe to use |
| 567 | * this function to escape user input. |
| 568 | * @param string $name The unqualified name of a table (no quotes, db, schema, nor table |
| 569 | * prefix), or a table name of the form "information_schema.<unquoted identifier>". |
| 570 | * @param string $format One of: |
| 571 | * quoted - Automatically pass the table name through addIdentifierQuotes() |
| 572 | * so that it can be used in a query. |
| 573 | * raw - Do not add identifier quotes to the table name. |
| 574 | * @return string Qualified table name (includes any applicable prefix or foreign db/schema) |
| 575 | */ |
| 576 | public function tableName( string $name, $format = 'quoted' ); |
| 577 | |
| 578 | /** |
| 579 | * Fetch a number of table names into an associative array |
| 580 | * |
| 581 | * Much like {@link tableName()}, this is only needed when calling |
| 582 | * {@link query()} directly. Prefer calling other methods, |
| 583 | * or using {@link SelectQueryBuilder}. |
| 584 | * |
| 585 | * Theoretical example (which really does not require raw SQL): |
| 586 | * ``` |
| 587 | * [ 'user' => $user, 'watchlist' => $watchlist ] = |
| 588 | * $dbr->tableNames( 'user', 'watchlist' ); |
| 589 | * $sql = "SELECT wl_namespace, wl_title FROM $watchlist, $user |
| 590 | * WHERE wl_user=user_id AND wl_user=$nameWithQuotes"; |
| 591 | * ``` |
| 592 | * |
| 593 | * @param string ...$tables |
| 594 | * @return array |
| 595 | * @deprecated since 1.39; if you must format table names, |
| 596 | * write several calls to {@link tableName} or use {@link tableNamesN} |
| 597 | * instead of calling this function. |
| 598 | */ |
| 599 | public function tableNames( ...$tables ); |
| 600 | |
| 601 | /** |
| 602 | * Fetch a number of table names into a zero-indexed numerical array |
| 603 | * |
| 604 | * Much like {@link tableName()}, this is only needed when calling |
| 605 | * {@link query()} directly. It is slightly more convenient than |
| 606 | * {@link tableNames()}, but you should still prefer calling other |
| 607 | * methods, or using {@link SelectQueryBuilder}. |
| 608 | * |
| 609 | * Theoretical example (which really does not require raw SQL): |
| 610 | * ``` |
| 611 | * [ $user, $watchlist ] = $dbr->tableNamesN( 'user', 'watchlist' ); |
| 612 | * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user |
| 613 | * WHERE wl_user=user_id AND wl_user=$nameWithQuotes"; |
| 614 | * ``` |
| 615 | * |
| 616 | * @param string ...$tables |
| 617 | * @return array |
| 618 | * @deprecated Since 1.43; if you must format table names, |
| 619 | * write several calls to {@link tableName}. |
| 620 | */ |
| 621 | public function tableNamesN( ...$tables ); |
| 622 | |
| 623 | /** |
| 624 | * Build a GROUP_CONCAT or equivalent statement for a query. |
| 625 | * |
| 626 | * This is useful for combining a field for several rows into a single string. |
| 627 | * NULL values will not appear in the output, duplicated values will appear, |
| 628 | * and the resulting delimiter-separated values have no defined sort order. |
| 629 | * Code using the results may need to use the PHP unique() or sort() methods. |
| 630 | * |
| 631 | * @param string $delim Glue to bind the results together |
| 632 | * @param string|array $tables Table reference(s), using the unqualified name of tables |
| 633 | * or of the form "information_schema.<identifier>". {@see select} for details. |
| 634 | * @param string $field Field name |
| 635 | * @param string|IExpression|array<string,?scalar|non-empty-array<int,?scalar>|RawSQLValue>|array<int,string|IExpression> $conds |
| 636 | * Conditions |
| 637 | * @param string|array $join_conds Join conditions |
| 638 | * @return string SQL text |
| 639 | * @since 1.23 |
| 640 | */ |
| 641 | public function buildGroupConcatField( |
| 642 | $delim, $tables, $field, $conds = '', $join_conds = [] |
| 643 | ); |
| 644 | |
| 645 | /** |
| 646 | * Equivalent to IDatabase::selectSQLText() except wraps the result in Subquery |
| 647 | * |
| 648 | * @see IDatabase::selectSQLText() |
| 649 | * |
| 650 | * @param string|array $tables Table reference(s), using the unqualified name of tables |
| 651 | * or of the form "information_schema.<identifier>". {@see select} for details. |
| 652 | * @param string|array $vars Field names |
| 653 | * @param string|array $conds Conditions |
| 654 | * @param string $fname Caller function name @phan-mandatory-param |
| 655 | * @param string|array $options Query options |
| 656 | * @param string|array $join_conds Join conditions |
| 657 | * @return Subquery |
| 658 | * @since 1.31 |
| 659 | */ |
| 660 | public function buildSelectSubquery( |
| 661 | $tables, |
| 662 | $vars, |
| 663 | $conds = '', |
| 664 | $fname = __METHOD__, |
| 665 | $options = [], |
| 666 | $join_conds = [] |
| 667 | ); |
| 668 | |
| 669 | /** |
| 670 | * Build a reference to a column value from the conflicting proposed upsert() row. |
| 671 | * |
| 672 | * The reference comes in the form of an alias, function, or parenthesized SQL expression. |
| 673 | * It can be used in upsert() SET expressions to handle the merging of column values between |
| 674 | * each conflicting pair of existing and proposed rows. Such proposed rows are said to have |
| 675 | * been "excluded" from insertion in favor of updating the existing row. |
| 676 | * |
| 677 | * This is useful for multi-row upserts() since the proposed values cannot just be included |
| 678 | * as literals in the SET expressions. |
| 679 | * |
| 680 | * @see IDatabase::upsert() |
| 681 | * |
| 682 | * @param string $column Column name |
| 683 | * @return string SQL expression returning a scalar |
| 684 | * @since 1.39 |
| 685 | */ |
| 686 | public function buildExcludedValue( $column ); |
| 687 | |
| 688 | /** |
| 689 | * Set schema variables to be used when streaming commands from SQL files or stdin |
| 690 | * |
| 691 | * Variables appear as SQL comments and are substituted by their corresponding values |
| 692 | * |
| 693 | * @param array|null $vars Map of (variable => value) or null to use the defaults |
| 694 | */ |
| 695 | public function setSchemaVars( $vars ); |
| 696 | } |