Yaron Koren has uploaded a new change for review. https://gerrit.wikimedia.org/r/210933
Change subject: Added "having=" parameter (for SQL HAVING clause) to #cargo_query ...................................................................... Added "having=" parameter (for SQL HAVING clause) to #cargo_query Change-Id: Ida18b807b3698fafb9a128d1d5e83b3698e8504c --- M CargoSQLQuery.php M i18n/en.json M i18n/qqq.json M parserfunctions/CargoQuery.php M specials/CargoViewData.php 5 files changed, 36 insertions(+), 15 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Cargo refs/changes/33/210933/1 diff --git a/CargoSQLQuery.php b/CargoSQLQuery.php index 33f745c..ba7bc8c 100644 --- a/CargoSQLQuery.php +++ b/CargoSQLQuery.php @@ -22,6 +22,7 @@ public $mFieldDescriptions; public $mFieldTables; public $mGroupByStr; + public $mHavingStr; public $mOrderByStr; public $mQueryLimit; @@ -30,11 +31,11 @@ * object can be created without any values. */ public static function newFromValues( $tablesStr, $fieldsStr, $whereStr, $joinOnStr, $groupByStr, - $orderByStr, $limitStr ) { + $havingStr, $orderByStr, $limitStr ) { global $wgCargoDefaultQueryLimit, $wgCargoMaxQueryLimit; self::validateValues( $tablesStr, $fieldsStr, $whereStr, $joinOnStr, $groupByStr, - $orderByStr, $limitStr ); + $havingStr, $orderByStr, $limitStr ); $sqlQuery = new CargoSQLQuery(); $sqlQuery->mTablesStr = $tablesStr; @@ -51,6 +52,7 @@ $sqlQuery->mTableSchemas = CargoUtils::getTableSchemas( $sqlQuery->mTableNames ); $sqlQuery->setOrderBy( $orderByStr ); $sqlQuery->mGroupByStr = $groupByStr; + $sqlQuery->mHavingStr = $havingStr; $sqlQuery->setDescriptionsForFields(); $sqlQuery->handleVirtualFields(); $sqlQuery->handleVirtualCoordinateFields(); @@ -70,9 +72,9 @@ * no processing. */ public static function newFromValues2( $tablesStr, $fieldsStr, $whereStr, $joinOnStr, $groupByStr, - $orderByStr, $limitStr ) { + $havingStr, $orderByStr, $limitStr ) { self::validateValues( $tablesStr, $fieldsStr, $whereStr, $joinOnStr, $groupByStr, - $orderByStr, $limitStr ); + $havingStr, $orderByStr, $limitStr ); $sqlQuery = new CargoSQLQuery(); $sqlQuery->mTablesStr = $tablesStr; @@ -82,6 +84,7 @@ $sqlQuery->mWhereStr = $whereStr; $sqlQuery->mJoinOnStr = $joinOnStr; $sqlQuery->mGroupByStr = $groupByStr; + $sqlQuery->mHavingStr = $havingStr; $sqlQuery->mOrderByStr = $orderByStr; $sqlQuery->mQueryLimit = $limitStr; return $sqlQuery; @@ -94,7 +97,7 @@ // some of the parameters need to be checked for these strings, // but we might as well validate all of them. public static function validateValues( $tablesStr, $fieldsStr, $whereStr, $joinOnStr, $groupByStr, - $orderByStr, $limitStr ) { + $havingStr, $orderByStr, $limitStr ) { $regexps = array( '/\bselect\b/i' => 'SELECT', @@ -112,6 +115,7 @@ ( preg_match( $regexp, $whereStr ) && $regexp != '/;/' ) || preg_match( $regexp, $joinOnStr ) || preg_match( $regexp, $groupByStr ) || + preg_match( $regexp, $havingStr ) || preg_match( $regexp, $orderByStr ) || preg_match( $regexp, $limitStr ) ) { throw new MWException( "Error: the string \"$displayString\" cannot be used within #cargo_query." ); @@ -454,15 +458,15 @@ } function handleVirtualFields() { - // The array-field alias can be found in the "where", "join on", - // "fields" or "order by" clauses. Handling depends on which - // clause it is: + // The array-field alias can be found in a number of different + // clauses. Handling depends on which clause it is: // "where" - make sure that "HOLDS" or "HOlDS LIKE" is // specified. If it is, "translate" it, and add the values // table to "tables" and "join on". // "join on" - make sure that "HOLDS" is specified, If it is, // "translate" it, and add the values table to "tables". // "group by" - always "translate" it into the single value. + // "having" - same as "group by". // "fields" - "translate" it, where the translation (i.e. // the true field) depends on whether or not the values // table is included. @@ -574,8 +578,8 @@ } $this->addToCargoJoinConds( $newCargoJoinConds ); - // "group by" - // We handle this before "fields" and "order by" because, + // "group by" and "having" + // We handle these before "fields" and "order by" because, // unlike those two, a virtual field here can affect the // set of tables and fields being included - which will // affect the other two. @@ -607,8 +611,10 @@ if ( $foundMatch1 ) { $this->mGroupByStr = preg_replace( $pattern1, $replacement, $this->mGroupByStr ); + $this->mHavingStr = preg_replace( $pattern1, $replacement, $this->mGroupByStr ); } elseif ( $foundMatch2 ) { $this->mGroupByStr = preg_replace( $pattern2, $replacement, $this->mGroupByStr ); + $this->mHavingStr = preg_replace( $pattern2, $replacement, $this->mGroupByStr ); } } } @@ -879,6 +885,7 @@ $this->mWhereStr = self::addTablePrefixes( $this->mWhereStr ); } $this->mGroupByStr = self::addTablePrefixes( $this->mGroupByStr ); + $this->mHavingStr = self::addTablePrefixes( $this->mHavingStr ); $this->mOrderByStr = self::addTablePrefixes( $this->mOrderByStr ); } @@ -900,6 +907,9 @@ if ( $this->mGroupByStr != '' ) { $selectOptions['GROUP BY'] = $this->mGroupByStr; } + if ( $this->mHavingStr != '' ) { + $selectOptions['HAVING'] = $this->mHavingStr; + } $selectOptions['ORDER BY'] = $this->mOrderByStr; $selectOptions['LIMIT'] = $this->mQueryLimit; diff --git a/i18n/en.json b/i18n/en.json index c3f57f4..f0abb69 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -20,6 +20,7 @@ "cargo-viewdata-where": "Where:", "cargo-viewdata-joinon": "Join on:", "cargo-viewdata-groupby": "Group by:", + "cargo-viewdata-having": "Having:", "cargo-viewdata-orderby": "Order by:", "cargo-viewdata-limit": "Limit:", "cargo-viewdata-format": "Format:", diff --git a/i18n/qqq.json b/i18n/qqq.json index f7b3674..156e3b9 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -22,6 +22,7 @@ "cargo-viewdata-where": "Label in a form to set the equivalent of an SQL 'WHERE' clause.\n{{Identical|Where}}", "cargo-viewdata-joinon": "Label in a form to set the equivalent of an SQL 'JOIN ... ON' clause.", "cargo-viewdata-groupby": "Label in a form to set the equivalent of an SQL 'GROUP BY' clause.", + "cargo-viewdata-groupby": "Label in a form to set the equivalent of an SQL 'HAVING' clause.", "cargo-viewdata-orderby": "Label in a form to set the sort order for query results.", "cargo-viewdata-limit": "Label in a form to set the limit on the number of results of a query.\n{{Identical|Limit}}", "cargo-viewdata-format": "Label in a form to set the display format for query results.\n{{Identical|Format}}", diff --git a/parserfunctions/CargoQuery.php b/parserfunctions/CargoQuery.php index b1dad1d..3b4074e 100644 --- a/parserfunctions/CargoQuery.php +++ b/parserfunctions/CargoQuery.php @@ -24,6 +24,7 @@ $whereStr = null; $joinOnStr = null; $groupByStr = null; + $havingStr = null; $orderByStr = null; $limitStr = null; $format = 'auto'; // default @@ -47,6 +48,8 @@ $joinOnStr = $value; } elseif ( $key == 'group by' ) { $groupByStr = $value; + } elseif ( $key == 'having' ) { + $havingStr = $value; } elseif ( $key == 'order by' ) { $orderByStr = $value; } elseif ( $key == 'limit' ) { @@ -61,7 +64,7 @@ try { $sqlQuery = CargoSQLQuery::newFromValues( $tablesStr, $fieldsStr, $whereStr, $joinOnStr, - $groupByStr, $orderByStr, $limitStr ); + $groupByStr, $havingStr, $orderByStr, $limitStr ); } catch ( Exception $e ) { return CargoUtils::formatError( $e->getMessage() ); } @@ -89,7 +92,7 @@ // fields in the query, making the first 'Query // necessary. There has to be some better way, though. $sqlQuery = CargoSQLQuery::newFromValues2( $tablesStr, $fieldsStr, $whereStr, $joinOnStr, - $groupByStr, $orderByStr, $limitStr ); + $groupByStr, $havingStr, $orderByStr, $limitStr ); $text = $formatter->queryAndDisplay( array( $sqlQuery ), $displayParams ); return $parser->insertStripItem( $text, $parser->mStripState ); } diff --git a/specials/CargoViewData.php b/specials/CargoViewData.php index 1d4b6e4..cf31cbd 100644 --- a/specials/CargoViewData.php +++ b/specials/CargoViewData.php @@ -60,6 +60,7 @@ $text .= self::displayInputRow( wfMessage( 'cargo-viewdata-where' )->text(), 'where', 60 ); $text .= self::displayInputRow( wfMessage( 'cargo-viewdata-joinon' )->text(), 'join_on', 40 ); $text .= self::displayInputRow( wfMessage( 'cargo-viewdata-groupby' )->text(), 'group_by', 20 ); + $text .= self::displayInputRow( wfMessage( 'cargo-viewdata-having' )->text(), 'having', 20 ); $text .= self::displayInputRow( wfMessage( 'cargo-viewdata-orderby' )->text(), 'order_by', 20 ); $text .= self::displayInputRow( wfMessage( 'cargo-viewdata-limit' )->text(), 'limit', 3 ); $formatLabel = wfMessage( 'cargo-viewdata-format' )->text(); @@ -104,11 +105,12 @@ $whereStr = $req->getVal( 'where' ); $joinOnStr = $req->getVal( 'join_on' ); $groupByStr = $req->getVal( 'group_by' ); + $havingStr = $req->getVal( 'having' ); $orderByStr = $req->getVal( 'order_by' ); $limitStr = null; $this->sqlQuery = CargoSQLQuery::newFromValues( $tablesStr, $fieldsStr, $whereStr, $joinOnStr, - $groupByStr, $orderByStr, $limitStr ); + $groupByStr, $havingStr, $orderByStr, $limitStr ); $formatStr = $req->getVal( 'format' ); $this->format = $formatStr; @@ -119,7 +121,7 @@ $queryStringValues = $this->getRequest()->getValues(); foreach ( $queryStringValues as $key => $value ) { if ( !in_array( $key, - array( 'title', 'tables', 'fields', 'join_on', 'order_by', 'group_by', 'format', + array( 'title', 'tables', 'fields', 'join_on', 'order_by', 'group_by', 'having', 'format', 'offset' ) ) ) { $this->displayParams[$key] = $value; } @@ -146,6 +148,9 @@ if ( $this->sqlQuery->mGroupByStr != '' ) { $selectOptions['GROUP BY'] = $this->sqlQuery->mGroupByStr; } + if ( $this->sqlQuery->mHavingStr != '' ) { + $selectOptions['HAVING'] = $this->sqlQuery->mHavingStr; + } // "order by" is handled elsewhere, in getOrderFields(). // // Field aliases need to have quotes placed around them @@ -171,7 +176,8 @@ function linkParameters() { $possibleParams = array( - 'tables', 'fields', 'where', 'join_on', 'order_by', 'group_by', 'format' ); + 'tables', 'fields', 'where', 'join_on', 'order_by', 'group_by', 'having', 'format' + ); $linkParams = array(); $req = $this->getRequest(); foreach ( $possibleParams as $possibleParam ) { -- To view, visit https://gerrit.wikimedia.org/r/210933 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ida18b807b3698fafb9a128d1d5e83b3698e8504c Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Cargo Gerrit-Branch: master Gerrit-Owner: Yaron Koren <yaro...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits