Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
17.86% covered (danger)
17.86%
5 / 28
15.38% covered (danger)
15.38%
2 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
MySQLPlatform
17.86% covered (danger)
17.86%
5 / 28
15.38% covered (danger)
15.38%
2 / 13
241.70
0.00% covered (danger)
0.00%
0 / 1
 getIdentifierQuoteChar
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 buildStringCast
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildIntegerCast
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 normalizeJoinType
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
5.02
 useIndexClause
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 ignoreIndexClause
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 deleteJoinSqlText
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 isTransactableQuery
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 buildExcludedValue
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 lockSQLText
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 lockIsFreeSQLText
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 unlockSQLText
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 makeLockName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
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 */
20namespace Wikimedia\Rdbms\Platform;
21
22use Wikimedia\Rdbms\DBLanguageError;
23use Wikimedia\Rdbms\Query;
24
25/**
26 * @since 1.39
27 * @see ISQLPlatform
28 */
29class MySQLPlatform extends SQLPlatform {
30    protected function getIdentifierQuoteChar() {
31        return '`';
32    }
33
34    public function buildStringCast( $field ) {
35        return "CAST( $field AS BINARY )";
36    }
37
38    /**
39     * @param string $field Field or column to cast
40     * @return string
41     */
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
57    /**
58     * @param string $index
59     * @return string
60     */
61    public function useIndexClause( $index ) {
62        return "FORCE INDEX (" . $this->indexName( $index ) . ")";
63    }
64
65    /**
66     * @param string $index
67     * @return string
68     */
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}