MediaWiki master
MySQLPlatform.php
Go to the documentation of this file.
1<?php
21
24
30 protected function getIdentifierQuoteChar() {
31 return '`';
32 }
33
34 public function buildStringCast( $field ) {
35 return "CAST( $field AS BINARY )";
36 }
37
42 public function buildIntegerCast( $field ) {
43 return 'CAST( ' . $field . ' AS SIGNED )';
44 }
45
46 protected function normalizeJoinType( string $joinType ) {
47 switch ( strtoupper( $joinType ) ) {
48 case 'STRAIGHT_JOIN':
49 case 'STRAIGHT JOIN':
50 return 'STRAIGHT_JOIN';
51
52 default:
53 return parent::normalizeJoinType( $joinType );
54 }
55 }
56
61 public function useIndexClause( $index ) {
62 return "FORCE INDEX (" . $this->indexName( $index ) . ")";
63 }
64
69 public function ignoreIndexClause( $index ) {
70 return "IGNORE INDEX (" . $this->indexName( $index ) . ")";
71 }
72
73 public function deleteJoinSqlText( $delTable, $joinTable, $delVar, $joinVar, $conds ) {
74 if ( !$conds ) {
75 throw new DBLanguageError( __METHOD__ . ' called with empty $conds' );
76 }
77
78 $delTable = $this->tableName( $delTable );
79 $joinTable = $this->tableName( $joinTable );
80 $sql = "DELETE $delTable FROM $delTable, $joinTable WHERE $delVar=$joinVar ";
81
82 if ( $conds != '*' ) {
83 $sql .= ' AND ' . $this->makeList( $conds, self::LIST_AND );
84 }
85
86 return $sql;
87 }
88
89 public function isTransactableQuery( Query $sql ) {
90 return parent::isTransactableQuery( $sql ) &&
91 // TODO: Use query verb
92 !preg_match( '/^SELECT\s+(GET|RELEASE|IS_FREE)_LOCK\‍(/', $sql->getSQL() );
93 }
94
95 public function buildExcludedValue( $column ) {
96 /* @see DatabaseMySQL::upsert() */
97 // Within "INSERT INTO ON DUPLICATE KEY UPDATE" statements:
98 // - MySQL>= 8.0.20 supports and prefers "VALUES ... AS".
99 // - MariaDB >= 10.3.3 supports and prefers VALUE().
100 // - Both support the old VALUES() function
101 // https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
102 // https://mariadb.com/kb/en/insert-on-duplicate-key-update/
103 return "VALUES($column)";
104 }
105
106 public function lockSQLText( $lockName, $timeout ) {
107 $encName = $this->quoter->addQuotes( $this->makeLockName( $lockName ) );
108 // Unlike NOW(), SYSDATE() gets the time at invocation rather than query start.
109 // The precision argument is silently ignored for MySQL < 5.6 and MariaDB < 5.3.
110 // https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_sysdate
111 // https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html
112 return "SELECT IF(GET_LOCK($encName,$timeout),UNIX_TIMESTAMP(SYSDATE(6)),NULL) AS acquired";
113 }
114
115 public function lockIsFreeSQLText( $lockName ) {
116 $encName = $this->quoter->addQuotes( $this->makeLockName( $lockName ) );
117 return "SELECT IS_FREE_LOCK($encName) AS unlocked";
118 }
119
120 public function unlockSQLText( $lockName ) {
121 $encName = $this->quoter->addQuotes( $this->makeLockName( $lockName ) );
122 return "SELECT RELEASE_LOCK($encName) AS released";
123 }
124
125 public function makeLockName( $lockName ) {
126 // https://dev.mysql.com/doc/refman/5.7/en/locking-functions.html#function_get-lock
127 // MySQL 5.7+ enforces a 64 char length limit.
128 return ( strlen( $lockName ) > 64 ) ? sha1( $lockName ) : $lockName;
129 }
130}
buildExcludedValue( $column)
Build a reference to a column value from the conflicting proposed upsert() row.
getIdentifierQuoteChar()
Get the character used for identifier quoting.
deleteJoinSqlText( $delTable, $joinTable, $delVar, $joinVar, $conds)
normalizeJoinType(string $joinType)
Validate and normalize a join type.
isTransactableQuery(Query $sql)
Determine whether a SQL statement is sensitive to isolation level.
indexName( $index)
Allows for index remapping in queries where this is not consistent across DBMS.
tableName(string $name, $format='quoted')
Format a table name ready for use in constructing an SQL query.
makeList(array $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
Holds information on Query to be executed.
Definition Query.php:31