From: umherirrender Date: Sat, 26 Jan 2013 09:43:20 +0000 (+0100) Subject: refactor Database::makeSelectOptions X-Git-Tag: 1.31.0-rc.0~20823^2 X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=commitdiff_plain;h=ad6706f671c48ef0cb0eaa6d594f286acbb4e59c;p=lhc%2Fweb%2Fwiklou.git refactor Database::makeSelectOptions 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 --- diff --git a/includes/db/Database.php b/includes/db/Database.php index 62a3d872b7..a98e37ecdb 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -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. diff --git a/includes/db/DatabaseIbm_db2.php b/includes/db/DatabaseIbm_db2.php index 880d70291a..7bd6a89b00 100644 --- a/includes/db/DatabaseIbm_db2.php +++ b/includes/db/DatabaseIbm_db2.php @@ -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'] ) ) diff --git a/includes/db/DatabaseMssql.php b/includes/db/DatabaseMssql.php index 317ff09020..87032b799c 100644 --- a/includes/db/DatabaseMssql.php +++ b/includes/db/DatabaseMssql.php @@ -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'; diff --git a/includes/db/DatabaseOracle.php b/includes/db/DatabaseOracle.php index 7e3c0caf5c..ed9f324fdc 100644 --- a/includes/db/DatabaseOracle.php +++ b/includes/db/DatabaseOracle.php @@ -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'; diff --git a/includes/db/DatabasePostgres.php b/includes/db/DatabasePostgres.php index 419488e595..67f23075ce 100644 --- a/includes/db/DatabasePostgres.php +++ b/includes/db/DatabasePostgres.php @@ -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'],