Merge "refactor Database::makeSelectOptions"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Sat, 2 Feb 2013 00:14:08 +0000 (00:14 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Sat, 2 Feb 2013 00:14:08 +0000 (00:14 +0000)
1  2 
includes/db/Database.php
includes/db/DatabaseIbm_db2.php
includes/db/DatabaseMssql.php
includes/db/DatabasePostgres.php

diff --combined includes/db/Database.php
@@@ -724,7 -724,7 +724,7 @@@ abstract class DatabaseBase implements 
         *    Valid options are: host, user, password, dbname, flags, tablePrefix
         * @return DatabaseBase subclass or null
         */
 -      public final static function factory( $dbType, $p = array() ) {
 +      final public static function factory( $dbType, $p = array() ) {
                $canonicalDBTypes = array(
                        'mysql', 'postgres', 'sqlite', 'oracle', 'mssql', 'ibm_db2'
                );
         * @since 1.20
         * @return bool: Whether connection was closed successfully
         */
 -      protected abstract function closeConnection();
 +      abstract protected function closeConnection();
  
        /**
         * @param $error String: fallback error message, used if none is given by DB
         * @param  $sql String: SQL query.
         * @return ResultWrapper Result object to feed to fetchObject, fetchRow, ...; or false on failure
         */
 -      protected abstract function doQuery( $sql );
 +      abstract protected function doQuery( $sql );
  
        /**
         * Determine whether a query writes to the DB.
                        }
                }
  
-               if ( isset( $options['GROUP BY'] ) ) {
-                       $gb = is_array( $options['GROUP BY'] )
-                               ? implode( ',', $options['GROUP BY'] )
-                               : $options['GROUP BY'];
-                       $preLimitTail .= " GROUP BY {$gb}";
-               }
-               if ( isset( $options['HAVING'] ) ) {
-                       $having = is_array( $options['HAVING'] )
-                               ? $this->makeList( $options['HAVING'], LIST_AND )
-                               : $options['HAVING'];
-                       $preLimitTail .= " HAVING {$having}";
-               }
+               $preLimitTail .= $this->makeGroupByWithHaving( $options );
  
-               if ( isset( $options['ORDER BY'] ) ) {
-                       $ob = is_array( $options['ORDER BY'] )
-                               ? implode( ',', $options['ORDER BY'] )
-                               : $options['ORDER BY'];
-                       $preLimitTail .= " ORDER BY {$ob}";
-               }
+               $preLimitTail .= $this->makeOrderBy( $options );
  
                // if (isset($options['LIMIT'])) {
                //      $tailOpts .= $this->limitResult('', $options['LIMIT'],
                return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
        }
  
+       /**
+        * Returns an optional GROUP BY with an optional HAVING
+        *
+        * @param $options Array: associative array of options
+        * @return string
+        * @see DatabaseBase::select()
+        * @since 1.21
+        */
+       public function makeGroupByWithHaving( $options ) {
+               $sql = '';
+               if ( isset( $options['GROUP BY'] ) ) {
+                       $gb = is_array( $options['GROUP BY'] )
+                               ? implode( ',', $options['GROUP BY'] )
+                               : $options['GROUP BY'];
+                       $sql .= ' GROUP BY ' . $gb;
+               }
+               if ( isset( $options['HAVING'] ) ) {
+                       $having = is_array( $options['HAVING'] )
+                               ? $this->makeList( $options['HAVING'], LIST_AND )
+                               : $options['HAVING'];
+                       $sql .= ' HAVING ' . $having;
+               }
+               return $sql;
+       }
+       /**
+        * Returns an optional ORDER BY
+        *
+        * @param $options Array: associative array of options
+        * @return string
+        * @see DatabaseBase::select()
+        * @since 1.21
+        */
+       public function makeOrderBy( $options ) {
+               if ( isset( $options['ORDER BY'] ) ) {
+                       $ob = is_array( $options['ORDER BY'] )
+                               ? implode( ',', $options['ORDER BY'] )
+                               : $options['ORDER BY'];
+                       return ' ORDER BY ' . $ob;
+               }
+               return '';
+       }
        /**
         * Execute a SELECT query constructed using the various parameters provided.
         * See below for full details of the parameters.
@@@ -143,12 -143,13 +143,12 @@@ class IBM_DB2Result
         * @param $sql String
         * @param $columns Array
         */
 -      public function __construct( $db, $result, $num_rows, $sql, $columns ){
 +      public function __construct( $db, $result, $num_rows, $sql, $columns ) {
                $this->db = $db;
  
 -              if( $result instanceof ResultWrapper ){
 +              if( $result instanceof ResultWrapper ) {
                        $this->result = $result->result;
 -              }
 -              else{
 +              } else {
                        $this->result = $result;
                }
  
         * @return mixed Array on success, false on failure
         * @throws DBUnexpectedError
         */
 -      public function fetchRow(){
 +      public function fetchRow() {
                if ( $this->result
                                && $this->num_rows > 0
                                && $this->current_pos >= 0
                                }
                        }
  
 -                      if ( $this->loadedLines > $this->current_pos ){
 +                      if ( $this->loadedLines > $this->current_pos ) {
                                return $this->resultSet[$this->current_pos++];
                        }
  
         * Free a DB2 result object
         * @throws DBUnexpectedError
         */
 -      public function freeResult(){
 +      public function freeResult() {
                unset( $this->resultSet );
                if ( !@db2_free_result( $this->result ) ) {
                        throw new DBUnexpectedError( $this, "Unable to free DB2 result\n" );
@@@ -419,7 -420,7 +419,7 @@@ class DatabaseIbm_db2 extends DatabaseB
         * Returns the database connection object
         * @return Object
         */
 -      public function getDb(){
 +      public function getDb() {
                return $this->mConn;
        }
  
                        }
                }
  
-               if ( isset( $options['GROUP BY'] ) ) {
-                       $preLimitTail .= " GROUP BY {$options['GROUP BY']}";
-               }
-               if ( isset( $options['HAVING'] ) ) {
-                       $preLimitTail .= " HAVING {$options['HAVING']}";
-               }
-               if ( isset( $options['ORDER BY'] ) ) {
-                       $preLimitTail .= " ORDER BY {$options['ORDER BY']}";
-               }
+               $preLimitTail .= $this->makeGroupByWithHaving( $options );
+               $preLimitTail .= $this->makeOrderBy( $options );
  
                if ( isset( $noKeyOptions['DISTINCT'] )
                        || isset( $noKeyOptions['DISTINCTROW'] ) )
@@@ -102,7 -102,7 +102,7 @@@ class DatabaseMssql extends DatabaseBas
                $ntAuthPassTest = strtolower( $password );
  
                // Decide which auth scenerio to use
 -              if( $ntAuthPassTest == 'ntauth' && $ntAuthUserTest == 'ntauth' ){
 +              if( $ntAuthPassTest == 'ntauth' && $ntAuthUserTest == 'ntauth' ) {
                        // Don't add credentials to $connectionInfo
                } else {
                        $connectionInfo['UID'] = $user;
                $identity = null;
                $tableRaw = preg_replace( '#\[([^\]]*)\]#', '$1', $table ); // strip matching square brackets from table name
                $res = $this->doQuery( "SELECT NAME AS idColumn FROM SYS.IDENTITY_COLUMNS WHERE OBJECT_NAME(OBJECT_ID)='{$tableRaw}'" );
 -              if( $res && $res->numrows() ){
 +              if( $res && $res->numrows() ) {
                        // There is an identity for this table.
                        $identity = array_pop( $res->fetch( SQLSRV_FETCH_ASSOC ) );
                }
                                // iterate through
                                foreach ($a as $k => $v ) {
                                        if ( $k == $identity ) {
 -                                              if( !is_null($v) ){
 +                                              if( !is_null($v) ) {
                                                        // there is a value being passed to us, we need to turn on and off inserted identity
                                                        $sqlPre = "SET IDENTITY_INSERT $table ON;" ;
                                                        $sqlPost = ";SET IDENTITY_INSERT $table OFF;";
                        }
                }
  
-               if ( isset( $options['GROUP BY'] ) ) {
-                       $tailOpts .= " GROUP BY {$options['GROUP BY']}";
-               }
-               if ( isset( $options['HAVING'] ) ) {
-                       $tailOpts .= " HAVING {$options['GROUP BY']}";
-               }
-               if ( isset( $options['ORDER BY'] ) ) {
-                       $tailOpts .= " ORDER BY {$options['ORDER BY']}";
-               }
+               $tailOpts .= $this->makeGroupByWithHaving( $options );
+               $tailOpts .= $this->makeOrderBy( $options );
  
                if ( isset( $noKeyOptions['DISTINCT'] ) && isset( $noKeyOptions['DISTINCTROW'] ) ) {
                        $startOpts .= 'DISTINCT';
         * Get the type of the DBMS, as it appears in $wgDBtype.
         * @return string
         */
 -      function getType(){
 +      function getType() {
                return 'mssql';
        }
  
@@@ -232,14 -232,12 +232,14 @@@ class SavepointPostgres 
        public function __destruct() {
                if ( $this->didbegin ) {
                        $this->dbw->rollback();
 +                      $this->didbegin = false;
                }
        }
  
        public function commit() {
                if ( $this->didbegin ) {
                        $this->dbw->commit();
 +                      $this->didbegin = false;
                }
        }
  
@@@ -1386,23 -1384,9 +1386,9 @@@ SQL
                        }
                }
  
-               if ( isset( $options['GROUP BY'] ) ) {
-                       $gb = is_array( $options['GROUP BY'] )
-                               ? implode( ',', $options['GROUP BY'] )
-                               : $options['GROUP BY'];
-                       $preLimitTail .= " GROUP BY {$gb}";
-               }
-               if ( isset( $options['HAVING'] ) ) {
-                       $preLimitTail .= " HAVING {$options['HAVING']}";
-               }
+               $preLimitTail .= $this->makeGroupByWithHaving( $options );
  
-               if ( isset( $options['ORDER BY'] ) ) {
-                       $ob = is_array( $options['ORDER BY'] )
-                               ? implode( ',', $options['ORDER BY'] )
-                               : $options['ORDER BY'];
-                       $preLimitTail .= " ORDER BY {$ob}";
-               }
+               $preLimitTail .= $this->makeOrderBy( $options );
  
                //if ( isset( $options['LIMIT'] ) ) {
                //      $tailOpts .= $this->limitResult( '', $options['LIMIT'],