refactor Database::makeSelectOptions
authorumherirrender <umherirrender_de.wp@web.de>
Sat, 26 Jan 2013 09:43:20 +0000 (10:43 +0100)
committerumherirrender <umherirrender_de.wp@web.de>
Sat, 26 Jan 2013 09:43:20 +0000 (10:43 +0100)
Created new method for creating the group by/having and order by sql,
this avoids code duplication in each database with a overridden
makeSelectOptions.

This fix the group by/having/order by for DB2/MSSQL to support array
conditions. Fixed the having for Postgres for array conditions.

Change-Id: I0fbcb3e8e68d995d93be0bf3d0a347788a39194a

includes/db/Database.php
includes/db/DatabaseIbm_db2.php
includes/db/DatabaseMssql.php
includes/db/DatabaseOracle.php
includes/db/DatabasePostgres.php

index 62a3d87..a98e37e 100644 (file)
@@ -1180,26 +1180,9 @@ abstract class DatabaseBase implements DatabaseType {
                        }
                }
 
-               if ( isset( $options['GROUP BY'] ) ) {
-                       $gb = is_array( $options['GROUP BY'] )
-                               ? implode( ',', $options['GROUP BY'] )
-                               : $options['GROUP BY'];
-                       $preLimitTail .= " GROUP BY {$gb}";
-               }
+               $preLimitTail .= $this->makeGroupByWithHaving( $options );
 
-               if ( isset( $options['HAVING'] ) ) {
-                       $having = is_array( $options['HAVING'] )
-                               ? $this->makeList( $options['HAVING'], LIST_AND )
-                               : $options['HAVING'];
-                       $preLimitTail .= " HAVING {$having}";
-               }
-
-               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'],
@@ -1261,6 +1244,49 @@ abstract class DatabaseBase implements DatabaseType {
                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.
index 880d702..7bd6a89 100644 (file)
@@ -1380,15 +1380,9 @@ class DatabaseIbm_db2 extends DatabaseBase {
                        }
                }
 
-               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'] ) )
index 317ff09..87032b7 100644 (file)
@@ -908,15 +908,9 @@ class DatabaseMssql extends DatabaseBase {
                        }
                }
 
-               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';
index 7e3c0ca..ed9f324 100644 (file)
@@ -1166,26 +1166,9 @@ class DatabaseOracle extends DatabaseBase {
                        }
                }
 
-               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( $noKeyOptions['FOR UPDATE'] ) ) {
                        $postLimitTail .= ' FOR UPDATE';
index 419488e..67f2307 100644 (file)
@@ -1384,23 +1384,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'],