1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace Wikimedia\Rdbms\Platform;
use Wikimedia\Rdbms\DBLanguageError;
use Wikimedia\Rdbms\Query;
/**
* @since 1.39
* @see ISQLPlatform
*/
class MySQLPlatform extends SQLPlatform {
protected function getIdentifierQuoteChar() {
return '`';
}
public function buildStringCast( $field ) {
return "CAST( $field AS BINARY )";
}
/**
* @param string $field Field or column to cast
* @return string
*/
public function buildIntegerCast( $field ) {
return 'CAST( ' . $field . ' AS SIGNED )';
}
protected function normalizeJoinType( string $joinType ) {
switch ( strtoupper( $joinType ) ) {
case 'STRAIGHT_JOIN':
case 'STRAIGHT JOIN':
return 'STRAIGHT_JOIN';
default:
return parent::normalizeJoinType( $joinType );
}
}
/**
* @param string $index
* @return string
*/
public function useIndexClause( $index ) {
return "FORCE INDEX (" . $this->indexName( $index ) . ")";
}
/**
* @param string $index
* @return string
*/
public function ignoreIndexClause( $index ) {
return "IGNORE INDEX (" . $this->indexName( $index ) . ")";
}
public function deleteJoinSqlText( $delTable, $joinTable, $delVar, $joinVar, $conds ) {
if ( !$conds ) {
throw new DBLanguageError( __METHOD__ . ' called with empty $conds' );
}
$delTable = $this->tableName( $delTable );
$joinTable = $this->tableName( $joinTable );
$sql = "DELETE $delTable FROM $delTable, $joinTable WHERE $delVar=$joinVar ";
if ( $conds != '*' ) {
$sql .= ' AND ' . $this->makeList( $conds, self::LIST_AND );
}
return $sql;
}
public function isTransactableQuery( Query $sql ) {
return parent::isTransactableQuery( $sql ) &&
// TODO: Use query verb
!preg_match( '/^SELECT\s+(GET|RELEASE|IS_FREE)_LOCK\(/', $sql->getSQL() );
}
public function buildExcludedValue( $column ) {
/* @see DatabaseMySQL::upsert() */
// Within "INSERT INTO ON DUPLICATE KEY UPDATE" statements:
// - MySQL>= 8.0.20 supports and prefers "VALUES ... AS".
// - MariaDB >= 10.3.3 supports and prefers VALUE().
// - Both support the old VALUES() function
// https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
// https://mariadb.com/kb/en/insert-on-duplicate-key-update/
return "VALUES($column)";
}
public function lockSQLText( $lockName, $timeout ) {
$encName = $this->quoter->addQuotes( $this->makeLockName( $lockName ) );
// Unlike NOW(), SYSDATE() gets the time at invocation rather than query start.
// The precision argument is silently ignored for MySQL < 5.6 and MariaDB < 5.3.
// https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_sysdate
// https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html
return "SELECT IF(GET_LOCK($encName,$timeout),UNIX_TIMESTAMP(SYSDATE(6)),NULL) AS acquired";
}
public function lockIsFreeSQLText( $lockName ) {
$encName = $this->quoter->addQuotes( $this->makeLockName( $lockName ) );
return "SELECT IS_FREE_LOCK($encName) AS unlocked";
}
public function unlockSQLText( $lockName ) {
$encName = $this->quoter->addQuotes( $this->makeLockName( $lockName ) );
return "SELECT RELEASE_LOCK($encName) AS released";
}
public function makeLockName( $lockName ) {
// https://dev.mysql.com/doc/refman/5.7/en/locking-functions.html#function_get-lock
// MySQL 5.7+ enforces a 64 char length limit.
return ( strlen( $lockName ) > 64 ) ? sha1( $lockName ) : $lockName;
}
}
|