Cicalese has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/281839

Change subject: added new Lua Cargo query function
......................................................................

added new Lua Cargo query function

Change-Id: Ia31d62deeb9dbe50636092f1aadf166542240f50
---
M CargoLua.library.php
M CargoSQLQuery.php
M cargo.lua
3 files changed, 75 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Cargo 
refs/changes/39/281839/1

diff --git a/CargoLua.library.php b/CargoLua.library.php
index 2c8c95a..f923415 100644
--- a/CargoLua.library.php
+++ b/CargoLua.library.php
@@ -5,6 +5,7 @@
        function register() {
                $lib = array(
                        'get' => array( $this, 'cargoGet' ),
+                       'query' => array( $this, 'cargoQuery' ),
                );
                return $this->getEngine()->registerInterface( __DIR__ . 
'/cargo.lua', $lib, array() );
        }
@@ -21,4 +22,63 @@
                $wgCargoQueryResults = null;
                return array( $val );
        }
+
+       function cargoQuery($tables, $fields, $args) {
+
+               $this->checkType( 'query', 1, $tables, 'string' );
+               $this->checkType( 'query', 2, $fields, 'string' );
+               $this->checkTypeOptional( 'query', 3, $args, 'table', array() );
+
+               if ( isset( $args['where'] ) ) {
+                       $where = $args['where'];
+               } else {
+                       $where = null;
+               }
+               if ( isset( $args['join'] ) ) {
+                       $join = $args['join'];
+               } else {
+                       $join = null;
+               }
+               if ( isset( $args['groupBy'] ) ) {
+                       $groupBy = $args['groupBy'];
+               } else {
+                       $groupBy = null;
+               }
+               if ( isset( $args['having'] ) ) {
+                       $having = $args['having'];
+               } else {
+                       $having = null;
+               }
+               if ( isset( $args['orderBy'] ) ) {
+                       $orderBy = $args['orderBy'];
+               } else {
+                       $orderBy = null;
+               }
+               if ( isset( $args['limit'] ) ) {
+                       $limit = $args['limit'];
+               } else {
+                       $limit = null;
+               }
+
+               $query = CargoSQLQuery::newFromValues( $tables, $fields, 
$where, $join,
+                       $groupBy, $having, $orderBy, $limit);
+               $rows = $query->run();
+
+               $result = array();
+
+               $fieldArray = array_map( 'trim', explode( ',', $fields ) );
+
+               $fieldNameAliases = $query->getFieldNameAliases();
+
+               $rowIndex = 1; // because Lua arrays start at 1
+               foreach ( $rows as $row ) {
+                       $values = array();
+                       foreach ( $fieldArray as $field ) {
+                               $values[$field] = 
$row[$fieldNameAliases[$field]];
+                       }
+                       $result[$rowIndex++] = $values;
+               }
+
+               return array( $result );
+       }
 }
diff --git a/CargoSQLQuery.php b/CargoSQLQuery.php
index 834c82e..c095d80 100644
--- a/CargoSQLQuery.php
+++ b/CargoSQLQuery.php
@@ -19,6 +19,7 @@
        public $mCargoJoinConds;
        public $mJoinConds;
        public $mAliasedFieldNames;
+       public $mFieldNameAliases;
        public $mTableSchemas;
        public $mFieldDescriptions;
        public $mFieldTables;
@@ -153,6 +154,14 @@
        }
 
        /**
+        * Gets a mapping of original field names to their field name aliases
+        * as they appear in the query result
+        */
+       function getFieldNameAliases() {
+               return $this->mFieldNameAliases;
+       }
+
+       /**
         * Gets an array of field names and their aliases from the passed-in
         * SQL fragment.
         */
@@ -179,6 +188,7 @@
                // displayed.
                $blankAliasCount = 0;
                foreach ( $fieldNames as $i => $fieldName ) {
+                       $originalFieldName = $fieldName;
                        $fieldNameParts = CargoUtils::smartSplit( '=', 
$fieldName );
                        if ( count( $fieldNameParts ) == 2 ) {
                                $fieldName = trim( $fieldNameParts[0] );
@@ -204,6 +214,7 @@
                                $alias = "Blank value $blankAliasCount";
                        }
                        $this->mAliasedFieldNames[$alias] = $fieldName;
+                       $this->mFieldNameAliases[$originalFieldName] = $alias;
                }
        }
 
diff --git a/cargo.lua b/cargo.lua
index b06c109..664d948 100644
--- a/cargo.lua
+++ b/cargo.lua
@@ -24,4 +24,8 @@
     return php.get()
 end
 
+function cargo.query(tables, fields, args)
+    return php.query(tables, fields, args)
+end
+
 return cargo

-- 
To view, visit https://gerrit.wikimedia.org/r/281839
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia31d62deeb9dbe50636092f1aadf166542240f50
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Cargo
Gerrit-Branch: master
Gerrit-Owner: Cicalese <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to