Move LIST_ constaints to IDatabase class constants
authorAaron Schulz <aschulz@wikimedia.org>
Mon, 19 Sep 2016 07:28:17 +0000 (00:28 -0700)
committerCatrope <roan.kattouw@gmail.com>
Mon, 19 Sep 2016 07:59:21 +0000 (07:59 +0000)
Leave aliases in Defines.php for backwards compatibility.

Change-Id: I88b45e0943cbfe97d863c2e0a4911fc0f81e5bb5

includes/Defines.php
includes/libs/rdbms/database/DBConnRef.php
includes/libs/rdbms/database/Database.php
includes/libs/rdbms/database/DatabaseMysqlBase.php
includes/libs/rdbms/database/IDatabase.php
includes/libs/rdbms/defines.php

index 077f39a..529dfb3 100644 (file)
 # Obsolete aliases
 define( 'DB_SLAVE', -1 );
 
+/**@{
+ * Obsolete IDatabase::makeList() constants
+ * These are also available as Database class constants
+ */
+define( 'LIST_COMMA', IDatabase::LIST_COMMA );
+define( 'LIST_AND', IDatabase::LIST_AND );
+define( 'LIST_SET', IDatabase::LIST_SET );
+define( 'LIST_NAMES', IDatabase::LIST_NAMES );
+define( 'LIST_OR', IDatabase::LIST_OR );
+/**@}*/
+
 /**@{
  * Virtual namespaces; don't appear in the page database
  */
index 0d9b692..2375678 100644 (file)
@@ -308,7 +308,7 @@ class DBConnRef implements IDatabase {
                return $this->__call( __FUNCTION__, func_get_args() );
        }
 
-       public function makeList( $a, $mode = LIST_COMMA ) {
+       public function makeList( $a, $mode = self::LIST_COMMA ) {
                return $this->__call( __FUNCTION__, func_get_args() );
        }
 
index 1c2c0bd..f9e9296 100644 (file)
@@ -1163,7 +1163,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
                }
                if ( isset( $options['HAVING'] ) ) {
                        $having = is_array( $options['HAVING'] )
-                               ? $this->makeList( $options['HAVING'], LIST_AND )
+                               ? $this->makeList( $options['HAVING'], self::LIST_AND )
                                : $options['HAVING'];
                        $sql .= ' HAVING ' . $having;
                }
@@ -1233,7 +1233,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
 
                if ( !empty( $conds ) ) {
                        if ( is_array( $conds ) ) {
-                               $conds = $this->makeList( $conds, LIST_AND );
+                               $conds = $this->makeList( $conds, self::LIST_AND );
                        }
                        $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex WHERE $conds $preLimitTail";
                } else {
@@ -1467,16 +1467,16 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
        function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
                $table = $this->tableName( $table );
                $opts = $this->makeUpdateOptions( $options );
-               $sql = "UPDATE $opts $table SET " . $this->makeList( $values, LIST_SET );
+               $sql = "UPDATE $opts $table SET " . $this->makeList( $values, self::LIST_SET );
 
                if ( $conds !== [] && $conds !== '*' ) {
-                       $sql .= " WHERE " . $this->makeList( $conds, LIST_AND );
+                       $sql .= " WHERE " . $this->makeList( $conds, self::LIST_AND );
                }
 
                return $this->query( $sql, $fname );
        }
 
-       public function makeList( $a, $mode = LIST_COMMA ) {
+       public function makeList( $a, $mode = self::LIST_COMMA ) {
                if ( !is_array( $a ) ) {
                        throw new DBUnexpectedError( $this, __METHOD__ . ' called with incorrect parameters' );
                }
@@ -1486,9 +1486,9 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
 
                foreach ( $a as $field => $value ) {
                        if ( !$first ) {
-                               if ( $mode == LIST_AND ) {
+                               if ( $mode == self::LIST_AND ) {
                                        $list .= ' AND ';
-                               } elseif ( $mode == LIST_OR ) {
+                               } elseif ( $mode == self::LIST_OR ) {
                                        $list .= ' OR ';
                                } else {
                                        $list .= ',';
@@ -1497,11 +1497,13 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
                                $first = false;
                        }
 
-                       if ( ( $mode == LIST_AND || $mode == LIST_OR ) && is_numeric( $field ) ) {
+                       if ( ( $mode == self::LIST_AND || $mode == self::LIST_OR ) && is_numeric( $field ) ) {
                                $list .= "($value)";
-                       } elseif ( ( $mode == LIST_SET ) && is_numeric( $field ) ) {
+                       } elseif ( $mode == self::LIST_SET && is_numeric( $field ) ) {
                                $list .= "$value";
-                       } elseif ( ( $mode == LIST_AND || $mode == LIST_OR ) && is_array( $value ) ) {
+                       } elseif (
+                               ( $mode == self::LIST_AND || $mode == self::LIST_OR ) && is_array( $value )
+                       ) {
                                // Remove null from array to be handled separately if found
                                $includeNull = false;
                                foreach ( array_keys( $value, null, true ) as $nullKey ) {
@@ -1509,7 +1511,8 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
                                        unset( $value[$nullKey] );
                                }
                                if ( count( $value ) == 0 && !$includeNull ) {
-                                       throw new InvalidArgumentException( __METHOD__ . ": empty input for field $field" );
+                                       throw new InvalidArgumentException(
+                                               __METHOD__ . ": empty input for field $field" );
                                } elseif ( count( $value ) == 0 ) {
                                        // only check if $field is null
                                        $list .= "$field IS NULL";
@@ -1534,17 +1537,19 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
                                        }
                                }
                        } elseif ( $value === null ) {
-                               if ( $mode == LIST_AND || $mode == LIST_OR ) {
+                               if ( $mode == self::LIST_AND || $mode == self::LIST_OR ) {
                                        $list .= "$field IS ";
-                               } elseif ( $mode == LIST_SET ) {
+                               } elseif ( $mode == self::LIST_SET ) {
                                        $list .= "$field = ";
                                }
                                $list .= 'NULL';
                        } else {
-                               if ( $mode == LIST_AND || $mode == LIST_OR || $mode == LIST_SET ) {
+                               if (
+                                       $mode == self::LIST_AND || $mode == self::LIST_OR || $mode == self::LIST_SET
+                               ) {
                                        $list .= "$field = ";
                                }
-                               $list .= $mode == LIST_NAMES ? $value : $this->addQuotes( $value );
+                               $list .= $mode == self::LIST_NAMES ? $value : $this->addQuotes( $value );
                        }
                }
 
@@ -1558,12 +1563,12 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
                        if ( count( $sub ) ) {
                                $conds[] = $this->makeList(
                                        [ $baseKey => $base, $subKey => array_keys( $sub ) ],
-                                       LIST_AND );
+                                       self::LIST_AND );
                        }
                }
 
                if ( $conds ) {
-                       return $this->makeList( $conds, LIST_OR );
+                       return $this->makeList( $conds, self::LIST_OR );
                } else {
                        // Nothing to search for...
                        return false;
@@ -1884,7 +1889,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
                                                $tableClause .= ' ' . $ignore;
                                        }
                                }
-                               $on = $this->makeList( (array)$conds, LIST_AND );
+                               $on = $this->makeList( (array)$conds, self::LIST_AND );
                                if ( $on != '' ) {
                                        $tableClause .= ' ON (' . $on . ')';
                                }
@@ -2153,10 +2158,10 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
                                        foreach ( $index as $column ) {
                                                $rowKey[$column] = $row[$column];
                                        }
-                                       $clauses[] = $this->makeList( $rowKey, LIST_AND );
+                                       $clauses[] = $this->makeList( $rowKey, self::LIST_AND );
                                }
                        }
-                       $where = [ $this->makeList( $clauses, LIST_OR ) ];
+                       $where = [ $this->makeList( $clauses, self::LIST_OR ) ];
                } else {
                        $where = false;
                }
@@ -2198,7 +2203,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
                $joinTable = $this->tableName( $joinTable );
                $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
                if ( $conds != '*' ) {
-                       $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND );
+                       $sql .= 'WHERE ' . $this->makeList( $conds, self::LIST_AND );
                }
                $sql .= ')';
 
@@ -2239,7 +2244,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
 
                if ( $conds != '*' ) {
                        if ( is_array( $conds ) ) {
-                               $conds = $this->makeList( $conds, LIST_AND );
+                               $conds = $this->makeList( $conds, self::LIST_AND );
                        }
                        $sql .= ' WHERE ' . $conds;
                }
@@ -2317,7 +2322,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
 
                if ( $conds != '*' ) {
                        if ( is_array( $conds ) ) {
-                               $conds = $this->makeList( $conds, LIST_AND );
+                               $conds = $this->makeList( $conds, self::LIST_AND );
                        }
                        $sql .= " WHERE $conds";
                }
@@ -2368,7 +2373,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
 
        public function conditional( $cond, $trueVal, $falseVal ) {
                if ( is_array( $cond ) ) {
-                       $cond = $this->makeList( $cond, LIST_AND );
+                       $cond = $this->makeList( $cond, self::LIST_AND );
                }
 
                return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
index 46c6678..b3f1add 100644 (file)
@@ -735,7 +735,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
         * @see https://www.percona.com/doc/percona-toolkit/2.1/pt-heartbeat.html
         */
        protected function getHeartbeatData( array $conds ) {
-               $whereSQL = $this->makeList( $conds, LIST_AND );
+               $whereSQL = $this->makeList( $conds, self::LIST_AND );
                // Use ORDER BY for channel based queries since that field might not be UNIQUE.
                // Note: this would use "TIMESTAMPDIFF(MICROSECOND,ts,UTC_TIMESTAMP(6))" but the
                // percision field is not supported in MySQL <= 5.5.
@@ -1107,7 +1107,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                $sql = "DELETE $delTable FROM $delTable, $joinTable WHERE $delVar=$joinVar ";
 
                if ( $conds != '*' ) {
-                       $sql .= ' AND ' . $this->makeList( $conds, LIST_AND );
+                       $sql .= ' AND ' . $this->makeList( $conds, self::LIST_AND );
                }
 
                return $this->query( $sql, $fname );
@@ -1141,7 +1141,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                        $rowTuples[] = '(' . $this->makeList( $row ) . ')';
                }
                $sql .= implode( ',', $rowTuples );
-               $sql .= " ON DUPLICATE KEY UPDATE " . $this->makeList( $set, LIST_SET );
+               $sql .= " ON DUPLICATE KEY UPDATE " . $this->makeList( $set, self::LIST_SET );
 
                return (bool)$this->query( $sql, $fname );
        }
index 495816f..25e5912 100644 (file)
@@ -63,6 +63,17 @@ interface IDatabase {
        /** @var string Estimate time to apply (scanning, applying) */
        const ESTIMATE_DB_APPLY = 'apply';
 
+       /** @var int Combine list with comma delimeters */
+       const LIST_COMMA = 0;
+       /** @var int Combine list with AND clauses */
+       const LIST_AND = 1;
+       /** @var int Convert map into a SET clause */
+       const LIST_SET = 2;
+       /** @var int Treat as field name and do not apply value escaping */
+       const LIST_NAMES = 3;
+       /** @var int Combine list with OR clauses */
+       const LIST_OR = 4;
+
        /**
         * A string describing the current software version, and possibly
         * other details in a user-friendly way. Will be listed on Special:Version, etc.
@@ -897,18 +908,29 @@ interface IDatabase {
        /**
         * Makes an encoded list of strings from an array
         *
+        * These can be used to make conjunctions or disjunctions on SQL condition strings
+        * derived from an array (see IDatabase::select() $conds documentation).
+        *
+        * Example usage:
+        * @code
+        *     $sql = $db->makeList( [
+        *         'rev_user' => $id,
+        *         $db->makeList( [ 'rev_minor' => 1, 'rev_len' < 500 ], $db::LIST_OR ] )
+        *     ], $db::LIST_AND );
+        * @endcode
+        * This would set $sql to "rev_user = '$id' AND (rev_minor = '1' OR rev_len < '500')"
+        *
         * @param array $a Containing the data
-        * @param int $mode Constant
-        *    - LIST_COMMA: Comma separated, no field names
-        *    - LIST_AND:   ANDed WHERE clause (without the WHERE). See the
-        *      documentation for $conds in IDatabase::select().
-        *    - LIST_OR:    ORed WHERE clause (without the WHERE)
-        *    - LIST_SET:   Comma separated with field names, like a SET clause
-        *    - LIST_NAMES: Comma separated field names
+        * @param int $mode IDatabase class constant:
+        *    - IDatabase::LIST_COMMA: Comma separated, no field names
+        *    - IDatabase::LIST_AND:   ANDed WHERE clause (without the WHERE).
+        *    - IDatabase::LIST_OR:    ORed WHERE clause (without the WHERE)
+        *    - IDatabase::LIST_SET:   Comma separated with field names, like a SET clause
+        *    - IDatabase::LIST_NAMES: Comma separated field names
         * @throws DBError
         * @return string
         */
-       public function makeList( $a, $mode = LIST_COMMA );
+       public function makeList( $a, $mode = self::LIST_COMMA );
 
        /**
         * Build a partial where clause from a 2-d array such as used for LinkBatch.
index 48baa3c..b420ca1 100644 (file)
@@ -22,14 +22,3 @@ define( 'DBO_COMPRESS', 512 );
 define( 'DB_REPLICA', -1 );     # Read from a replica (or only server)
 define( 'DB_MASTER', -2 );    # Write to master (or only server)
 /**@}*/
-
-/**@{
- * Flags for IDatabase::makeList()
- * These are also available as Database class constants
- */
-define( 'LIST_COMMA', 0 );
-define( 'LIST_AND', 1 );
-define( 'LIST_SET', 2 );
-define( 'LIST_NAMES', 3 );
-define( 'LIST_OR', 4 );
-/**@}*/