METAMODEL-1088: Fixed #106
authorAlberto Rodriguez <arodriguez@stratio.com>
Mon, 30 May 2016 07:37:15 +0000 (09:37 +0200)
committerAlberto Rodriguez <arodriguez@stratio.com>
Mon, 30 May 2016 07:37:15 +0000 (09:37 +0200)
CHANGES.md
mongodb/mongo2/src/main/java/org/apache/metamodel/mongodb/mongo2/MongoDbDataContext.java
mongodb/mongo2/src/main/java/org/apache/metamodel/mongodb/mongo2/MongoDbDataSet.java
mongodb/mongo2/src/test/java/org/apache/metamodel/mongodb/mongo2/MongoDbDataContextTest.java
mongodb/mongo3/src/main/java/org/apache/metamodel/mongodb/mongo3/MongoDbDataContext.java
mongodb/mongo3/src/main/java/org/apache/metamodel/mongodb/mongo3/MongoDbDataSet.java
mongodb/mongo3/src/test/java/org/apache/metamodel/mongodb/mongo3/MongoDbDataContextTest.java

index 33f3a04..52f1323 100644 (file)
@@ -1,3 +1,8 @@
+### Apache MetaModel 4.5.4 (work in progress)
+
+ * [METAMODEL-1088] - Add support for aliases in MongoDB
+
+
 ### Apache MetaModel 4.5.3
 
  * [METAMODEL-235] - Fixed a bug related to handling of null or missing values in ElasticSearch using REST client.
index cfeb836..535c8f8 100644 (file)
@@ -287,7 +287,9 @@ public class MongoDbDataContext extends QueryPostprocessDataContext implements U
                 // "SELECT [columns] FROM [table] WHERE [conditions]"
                 // query.
                 for (SelectItem selectItem : selectItems) {
-                    if (selectItem.getFunction() != null || selectItem.getColumn() == null) {
+                    if (selectItem.getAggregateFunction() != null
+                            || selectItem.getScalarFunction() != null
+                                    || selectItem.getColumn() == null) {
                         allSelectItemsAreColumns = false;
                         break;
                     }
@@ -325,10 +327,29 @@ public class MongoDbDataContext extends QueryPostprocessDataContext implements U
 
                     int firstRow = (query.getFirstRow() == null ? 1 : query.getFirstRow());
                     int maxRows = (query.getMaxRows() == null ? -1 : query.getMaxRows());
+                    boolean thereIsAtLeastOneAlias = false;
 
-                    final DataSet dataSet = materializeMainSchemaTableInternal(table, columns, whereItems, firstRow,
-                            maxRows, false);
-                    return dataSet;
+                    for (SelectItem selectItem : selectItems) {
+                        if (selectItem.getAlias() != null) {
+                            thereIsAtLeastOneAlias = true;
+                            break;
+                        }
+                    }
+
+                    if (thereIsAtLeastOneAlias) {
+                        final SelectItem[] selectItemsAsArray = selectItems.toArray(new SelectItem[selectItems.size()]);
+                        final DataSet dataSet = materializeMainSchemaTableInternal(
+                                table,
+                                selectItemsAsArray,
+                                whereItems,
+                                firstRow,
+                                maxRows, false);
+                        return dataSet;
+                    } else {
+                        final DataSet dataSet = materializeMainSchemaTableInternal(table, columns, whereItems, firstRow,
+                                maxRows, false);
+                        return dataSet;
+                    }
                 }
             }
         }
@@ -339,6 +360,19 @@ public class MongoDbDataContext extends QueryPostprocessDataContext implements U
 
     private DataSet materializeMainSchemaTableInternal(Table table, Column[] columns, List<FilterItem> whereItems,
             int firstRow, int maxRows, boolean queryPostProcessed) {
+        DBCursor cursor = getCursor(table, whereItems, firstRow, maxRows);
+
+        return new MongoDbDataSet(cursor, columns, queryPostProcessed);
+    }
+
+    private DataSet materializeMainSchemaTableInternal(Table table, SelectItem[] selectItems,
+            List<FilterItem> whereItems, int firstRow, int maxRows, boolean queryPostProcessed) {
+        DBCursor cursor = getCursor(table, whereItems, firstRow, maxRows);
+
+        return new MongoDbDataSet(cursor, selectItems, queryPostProcessed);
+    }
+
+    private DBCursor getCursor(Table table, List<FilterItem> whereItems, int firstRow, int maxRows) {
         final DBCollection collection = _mongoDb.getCollection(table.getName());
 
         final DBObject query = createMongoDbQuery(table, whereItems);
@@ -353,8 +387,7 @@ public class MongoDbDataContext extends QueryPostprocessDataContext implements U
             final int skip = firstRow - 1;
             cursor = cursor.skip(skip);
         }
-
-        return new MongoDbDataSet(cursor, columns, queryPostProcessed);
+        return cursor;
     }
 
     protected BasicDBObject createMongoDbQuery(Table table, List<FilterItem> whereItems) {
index 8c2a107..ea7cd9e 100644 (file)
@@ -21,6 +21,7 @@ package org.apache.metamodel.mongodb.mongo2;
 import org.apache.metamodel.data.AbstractDataSet;
 import org.apache.metamodel.data.Row;
 import org.apache.metamodel.mongodb.common.MongoDBUtils;
+import org.apache.metamodel.query.SelectItem;
 import org.apache.metamodel.schema.Column;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -45,6 +46,13 @@ final class MongoDbDataSet extends AbstractDataSet {
         _closed = false;
     }
 
+    public MongoDbDataSet(DBCursor cursor, SelectItem[] selectItems, boolean queryPostProcessed) {
+        super(selectItems);
+        _cursor = cursor;
+        _queryPostProcessed = queryPostProcessed;
+        _closed = false;
+    }
+
     public boolean isQueryPostProcessed() {
         return _queryPostProcessed;
     }
index a03585a..da99955 100644 (file)
@@ -632,4 +632,39 @@ public class MongoDbDataContextTest extends MongoDbTestCase {
             ds4.close();
         }
     }
+
+    public void testSelectWithAlias() throws Exception {
+        if (!isConfigured()) {
+            System.err.println(getInvalidConfigurationMessage());
+            return;
+        }
+
+        DBCollection col = db.createCollection(getCollectionName(), new BasicDBObject());
+
+        // delete if already exists
+        {
+            col.drop();
+            col = db.createCollection(getCollectionName(), new BasicDBObject());
+        }
+
+        final BasicDBObject dbRow = new BasicDBObject();
+        dbRow.append("name", new BasicDBObject().append("first", "John").append("last", "Doe"));
+        dbRow.append("gender", "MALE");
+        col.insert(dbRow);
+
+        final MongoDbDataContext dc = new MongoDbDataContext(db, new SimpleTableDef(getCollectionName(), new String[] {
+                "name.first", "name.last", "gender", "addresses", "addresses[0].city", "addresses[0].country",
+                "addresses[5].foobar" }));
+
+        final DataSet ds1 = dc.executeQuery("select gender AS my_gender, name.first AS my_name from my_collection where gender LIKE '%MALE%'");
+        final SelectItem[] selectItems = ds1.getSelectItems();
+        SelectItem firstSelectItem = selectItems[0];
+        SelectItem secondSelectItem = selectItems[1];
+        try {
+            assertNotNull(firstSelectItem.getAlias());
+            assertNotNull(secondSelectItem.getAlias());
+        } finally {
+            ds1.close();
+        }
+    }
 }
index fbc9047..3108964 100644 (file)
@@ -275,7 +275,9 @@ public class MongoDbDataContext extends QueryPostprocessDataContext implements U
                 // "SELECT [columns] FROM [table] WHERE [conditions]"
                 // query.
                 for (SelectItem selectItem : selectItems) {
-                    if (selectItem.getFunction() != null || selectItem.getColumn() == null) {
+                    if (selectItem.getAggregateFunction() != null
+                            || selectItem.getScalarFunction() != null
+                                    || selectItem.getColumn() == null) {
                         allSelectItemsAreColumns = false;
                         break;
                     }
@@ -314,9 +316,29 @@ public class MongoDbDataContext extends QueryPostprocessDataContext implements U
                     int firstRow = (query.getFirstRow() == null ? 1 : query.getFirstRow());
                     int maxRows = (query.getMaxRows() == null ? -1 : query.getMaxRows());
 
-                    final DataSet dataSet = materializeMainSchemaTableInternal(table, columns, whereItems, firstRow,
-                            maxRows, false);
-                    return dataSet;
+                    boolean thereIsAtLeastOneAlias = false;
+
+                    for (SelectItem selectItem : selectItems) {
+                        if (selectItem.getAlias() != null) {
+                            thereIsAtLeastOneAlias = true;
+                            break;
+                        }
+                    }
+
+                    if (thereIsAtLeastOneAlias) {
+                        final SelectItem[] selectItemsAsArray = selectItems.toArray(new SelectItem[selectItems.size()]);
+                        final DataSet dataSet = materializeMainSchemaTableInternal(
+                                table,
+                                selectItemsAsArray,
+                                whereItems,
+                                firstRow,
+                                maxRows, false);
+                        return dataSet;
+                    } else {
+                        final DataSet dataSet = materializeMainSchemaTableInternal(table, columns, whereItems, firstRow,
+                                maxRows, false);
+                        return dataSet;
+                    }
                 }
             }
         }
@@ -327,6 +349,20 @@ public class MongoDbDataContext extends QueryPostprocessDataContext implements U
 
     private DataSet materializeMainSchemaTableInternal(Table table, Column[] columns, List<FilterItem> whereItems,
             int firstRow, int maxRows, boolean queryPostProcessed) {
+        MongoCursor<Document> cursor = getDocumentMongoCursor(table, whereItems, firstRow, maxRows);
+
+        return new MongoDbDataSet(cursor, columns, queryPostProcessed);
+    }
+
+    private DataSet materializeMainSchemaTableInternal(Table table, SelectItem[] selectItems, List<FilterItem> whereItems,
+            int firstRow, int maxRows, boolean queryPostProcessed) {
+        MongoCursor<Document> cursor = getDocumentMongoCursor(table, whereItems, firstRow, maxRows);
+
+        return new MongoDbDataSet(cursor, selectItems, queryPostProcessed);
+    }
+
+    private MongoCursor<Document> getDocumentMongoCursor(Table table, List<FilterItem> whereItems, int firstRow,
+            int maxRows) {
         final MongoCollection<Document> collection = _mongoDb.getCollection(table.getName());
 
         final Document query = createMongoDbQuery(table, whereItems);
@@ -342,9 +378,7 @@ public class MongoDbDataContext extends QueryPostprocessDataContext implements U
             iterable = iterable.skip(skip);
         }
 
-        MongoCursor<Document> cursor = iterable.iterator();
-
-        return new MongoDbDataSet(cursor, columns, queryPostProcessed);
+        return iterable.iterator();
     }
 
     protected Document createMongoDbQuery(Table table, List<FilterItem> whereItems) {
index 7a480d1..19e543f 100644 (file)
@@ -21,6 +21,7 @@ package org.apache.metamodel.mongodb.mongo3;
 import org.apache.metamodel.data.AbstractDataSet;
 import org.apache.metamodel.data.Row;
 import org.apache.metamodel.mongodb.common.MongoDBUtils;
+import org.apache.metamodel.query.SelectItem;
 import org.apache.metamodel.schema.Column;
 import org.bson.Document;
 import org.slf4j.Logger;
@@ -45,6 +46,13 @@ final class MongoDbDataSet extends AbstractDataSet {
         _closed = false;
     }
 
+    public MongoDbDataSet(MongoCursor<Document> cursor, SelectItem[] selectItems, boolean queryPostProcessed) {
+        super(selectItems);
+        _cursor = cursor;
+        _queryPostProcessed = queryPostProcessed;
+        _closed = false;
+    }
+
     public boolean isQueryPostProcessed() {
         return _queryPostProcessed;
     }
index d16e47c..197fae9 100644 (file)
@@ -24,6 +24,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBCollection;
 import com.mongodb.WriteConcern;
 
 import org.apache.metamodel.DataContext;
@@ -610,4 +612,36 @@ public class MongoDbDataContextTest extends MongoDbTestCase {
             ds4.close();
         }
     }
+
+    public void testSelectWithAlias() throws Exception {
+        if (!isConfigured()) {
+            System.err.println(getInvalidConfigurationMessage());
+            return;
+        }
+
+        mongoDb.createCollection(getCollectionName());
+        MongoCollection<Document> col = mongoDb.getCollection(getCollectionName());
+
+        final Document dbRow = new Document();
+        dbRow.append("name", new Document().append("first", "John").append("last", "Doe"));
+        dbRow.append("gender", "MALE");
+        col.insertOne(dbRow);
+
+        final MongoDbDataContext dc = new MongoDbDataContext(mongoDb,
+                new SimpleTableDef(getCollectionName(), new String[] {
+                        "name.first", "name.last", "gender", "addresses", "addresses[0].city", "addresses[0].country",
+                        "addresses[5].foobar" }));
+
+        final DataSet ds1 = dc.executeQuery("select gender as my_gender, name.first as my_name from my_collection where gender LIKE '%MALE%'");
+        final SelectItem[] selectItems = ds1.getSelectItems();
+        SelectItem firstSelectItem = selectItems[0];
+        SelectItem secondSelectItem = selectItems[1];
+        try {
+            assertNotNull(firstSelectItem.getAlias());
+            assertNotNull(secondSelectItem.getAlias());
+
+        } finally {
+            ds1.close();
+        }
+    }
 }