Search in sources :

Example 76 with BasicDBList

use of com.mongodb.BasicDBList in project teiid by teiid.

the class MongoDBSelectVisitor method visit.

@Override
public void visit(Function obj) {
    String functionName = obj.getName();
    if (functionName.indexOf('.') != -1) {
        functionName = functionName.substring(functionName.indexOf('.') + 1);
    }
    if (this.executionFactory.getFunctionModifiers().containsKey(functionName)) {
        List<?> parts = this.executionFactory.getFunctionModifiers().get(functionName).translate(obj);
        if (parts != null) {
            obj = (Function) parts.get(0);
            if (parts.size() > 1) {
                // $NON-NLS-1$
                throw new AssertionError("Not supported");
            }
        }
    }
    BasicDBObject expr = null;
    if (isGeoSpatialFunction(functionName)) {
        try {
            expr = (BasicDBObject) handleGeoSpatialFunction(functionName, obj);
        } catch (TranslatorException e) {
            this.exceptions.add(e);
        }
    } else if (isStringFunction(functionName)) {
        expr = handleStringFunction(functionName, obj);
    } else {
        List<Expression> args = obj.getParameters();
        if (args != null) {
            BasicDBList params = new BasicDBList();
            for (int i = 0; i < args.size(); i++) {
                append(args.get(i));
                Object param = this.onGoingExpression.pop();
                params.add(param);
            }
            expr = new BasicDBObject(obj.getName(), params);
        }
    }
    if (expr != null) {
        // functions over dates do not work if the date is null/missing
        if (obj.getParameters().size() == 1 && Date.class.isAssignableFrom(obj.getParameters().get(0).getType()) && isDateTimeFunction(functionName)) {
            BasicDBList newParams = new BasicDBList();
            newParams.addAll((BasicDBList) expr.values().iterator().next());
            newParams.add(null);
            BasicDBObject nullCheck = new BasicDBObject("$eq", newParams);
            newParams = new BasicDBList();
            newParams.add(nullCheck);
            newParams.add(null);
            newParams.add(expr);
            expr = new BasicDBObject("$cond", newParams);
        }
        this.onGoingExpression.push(expr);
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) TranslatorException(org.teiid.translator.TranslatorException) BasicDBList(com.mongodb.BasicDBList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 77 with BasicDBList

use of com.mongodb.BasicDBList in project teiid by teiid.

the class MongoDBSelectVisitor method visit.

@Override
public void visit(AggregateFunction obj) {
    if (!obj.getParameters().isEmpty()) {
        append(obj.getParameters());
    }
    BasicDBObject expr = null;
    if (obj.getName().equals(AggregateFunction.COUNT)) {
        // this is only true for count(*) case, so we need implicit group id clause
        try {
            Object param = this.onGoingExpression.pop();
            BasicDBList eq = new BasicDBList();
            eq.add(0, param);
            eq.add(1, null);
            BasicDBList values = new BasicDBList();
            // $NON-NLS-1$
            values.add(0, new BasicDBObject("$eq", eq));
            values.add(1, 0);
            values.add(2, 1);
            // $NON-NLS-1$ //$NON-NLS-2$
            expr = new BasicDBObject("$sum", new BasicDBObject("$cond", values));
        } catch (EmptyStackException e) {
            // $NON-NLS-1$
            this.group.put("_id", null);
            // $NON-NLS-1$
            expr = new BasicDBObject("$sum", new Integer(1));
        }
    } else if (obj.getName().equals(AggregateFunction.AVG)) {
        // $NON-NLS-1$
        expr = new BasicDBObject("$avg", this.onGoingExpression.pop());
    } else if (obj.getName().equals(AggregateFunction.SUM)) {
        // $NON-NLS-1$
        expr = new BasicDBObject("$sum", this.onGoingExpression.pop());
    } else if (obj.getName().equals(AggregateFunction.MIN)) {
        // $NON-NLS-1$
        expr = new BasicDBObject("$min", this.onGoingExpression.pop());
    } else if (obj.getName().equals(AggregateFunction.MAX)) {
        // $NON-NLS-1$
        expr = new BasicDBObject("$max", this.onGoingExpression.pop());
    } else {
        this.exceptions.add(new TranslatorException(MongoDBPlugin.Util.gs(MongoDBPlugin.Event.TEIID18005, obj.getName())));
    }
    if (expr != null) {
        this.onGoingExpression.push(expr);
    }
}
Also used : EmptyStackException(java.util.EmptyStackException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) TranslatorException(org.teiid.translator.TranslatorException)

Example 78 with BasicDBList

use of com.mongodb.BasicDBList in project teiid by teiid.

the class TestMongoDBMetadataProcessor method processExampleMetadata.

private MetadataFactory processExampleMetadata(MongoDBMetadataProcessor mp) throws TranslatorException {
    MetadataFactory mf = new MetadataFactory("vdb", 1, "mongodb", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
    MongoDBConnection conn = Mockito.mock(MongoDBConnection.class);
    DBCollection tableDBCollection = Mockito.mock(DBCollection.class);
    DBCollection embeddedDBCollection = Mockito.mock(DBCollection.class);
    DBCollection emptyDBCollection = Mockito.mock(DBCollection.class);
    DBCollection emptyFirstDBCollection = Mockito.mock(DBCollection.class);
    LinkedHashSet<String> tables = new LinkedHashSet<String>();
    tables.add("table");
    tables.add("embedded");
    tables.add("empty");
    tables.add("emptyFirst");
    DB db = Mockito.mock(DB.class);
    BasicDBList array = new BasicDBList();
    array.add("one");
    array.add("two");
    BasicDBObject row = new BasicDBObject();
    row.append("_id", new Integer(1));
    row.append("col2", new Double(2.0));
    row.append("col3", new Long(3L));
    row.append("col5", Boolean.TRUE);
    row.append("col6", new Date(0L));
    row.append("col6", new DBRef(db.getName(), "ns", "one"));
    row.append("col7", array);
    row.append("col8", new Binary("binary".getBytes()));
    BasicDBObject child = new BasicDBObject();
    child.append("col1", "one");
    child.append("col2", "two");
    row.append("child", child);
    BasicDBObject emptyFirstRow = new BasicDBObject();
    emptyFirstRow.append("_id", new ObjectId("5835a598944716c40d2f26ae"));
    emptyFirstRow.append("col2", new Double(2.0));
    emptyFirstRow.append("col3", new Long(3L));
    BasicDBObject embedded = new BasicDBObject();
    embedded.append("col1", 1);
    embedded.append("col2", new byte[0]);
    row.append("embedded", embedded);
    Mockito.stub(db.getCollectionNames()).toReturn(tables);
    Mockito.stub(db.getCollection(Mockito.eq("table"))).toReturn(tableDBCollection);
    Mockito.stub(db.getCollection(Mockito.eq("embedded"))).toReturn(embeddedDBCollection);
    Mockito.stub(db.getCollection(Mockito.eq("empty"))).toReturn(emptyDBCollection);
    Mockito.stub(db.getCollection(Mockito.eq("emptyFirst"))).toReturn(emptyFirstDBCollection);
    BasicDBObject nextRow = new BasicDBObject();
    nextRow.append("_id", new Integer(2));
    nextRow.append("col2", new Double(3.0));
    nextRow.append("col3", "A");
    nextRow.append("col5", Boolean.TRUE);
    nextRow.append("col9", "another");
    DBCursor tableCursor = Mockito.mock(DBCursor.class);
    Mockito.when(tableCursor.numSeen()).thenReturn(1).thenReturn(2);
    Mockito.when(tableCursor.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
    Mockito.when(tableCursor.next()).thenReturn(row).thenReturn(nextRow);
    Mockito.when(tableDBCollection.find()).thenReturn(tableCursor);
    DBCursor embeddedCursor = Mockito.mock(DBCursor.class);
    Mockito.when(embeddedCursor.hasNext()).thenReturn(true).thenReturn(false);
    Mockito.when(embeddedCursor.next()).thenReturn(child);
    Mockito.when(embeddedDBCollection.find()).thenReturn(embeddedCursor);
    DBCursor emptyFirstCursor = Mockito.mock(DBCursor.class);
    Mockito.when(emptyFirstCursor.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
    Mockito.when(emptyFirstCursor.next()).thenReturn(null).thenReturn(emptyFirstRow);
    Mockito.when(emptyFirstDBCollection.find()).thenReturn(emptyFirstCursor);
    DBCursor emptyCursor = Mockito.mock(DBCursor.class);
    Mockito.when(emptyCursor.hasNext()).thenReturn(true).thenReturn(false);
    Mockito.when(emptyCursor.next()).thenReturn(null);
    Mockito.when(emptyDBCollection.find()).thenReturn(emptyCursor);
    Mockito.stub(conn.getDatabase()).toReturn(db);
    mp.process(mf, conn);
    return mf;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ObjectId(org.bson.types.ObjectId) DBRef(com.mongodb.DBRef) Properties(java.util.Properties) Date(java.sql.Date) DBCollection(com.mongodb.DBCollection) BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) MetadataFactory(org.teiid.metadata.MetadataFactory) MongoDBConnection(org.teiid.mongodb.MongoDBConnection) Binary(org.bson.types.Binary) DB(com.mongodb.DB)

Example 79 with BasicDBList

use of com.mongodb.BasicDBList in project teiid by teiid.

the class JoinCriteriaVisitor method buildIfNullBasedProjection.

private BasicDBObject buildIfNullBasedProjection(MongoDocument parent, MongoDocument child) throws TranslatorException {
    BasicDBObject columns = new BasicDBObject();
    Table table = parent.getTable();
    for (Column c : table.getColumns()) {
        if (parent.isMerged() || parent.isEmbeddable()) {
            // $NON-NLS-1$
            columns.append(parent.getQualifiedName(false) + "." + c.getName(), 1);
        } else {
            columns.append(c.getName(), 1);
        }
    }
    BasicDBList exprs = new BasicDBList();
    // $NON-NLS-1$
    exprs.add("$" + child.getQualifiedName(false));
    BasicDBList list = new BasicDBList();
    list.add(new BasicDBObject());
    exprs.add(list);
    // $NON-NLS-1$
    BasicDBObject ifnull = new BasicDBObject("$ifNull", exprs);
    // $NON-NLS-1$
    this.aliasName = "__NN_" + child.getTable().getName();
    columns.append(this.aliasName, ifnull);
    child.setAlias(this.aliasName);
    child.getMergeKey().setAlias(this.aliasName);
    return columns;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column)

Example 80 with BasicDBList

use of com.mongodb.BasicDBList in project ff4j by ff4j.

the class FeatureDBObjectBuilder method addRoles.

/**
 * Chain add to build object.
 *
 * @param value
 *            target value
 * @return
 */
public FeatureDBObjectBuilder addRoles(Set<String> auths) {
    BasicDBList authorizations = new BasicDBList();
    authorizations.addAll(auths);
    builder.add(ROLES, authorizations);
    return this;
}
Also used : BasicDBList(com.mongodb.BasicDBList)

Aggregations

BasicDBList (com.mongodb.BasicDBList)108 BasicDBObject (com.mongodb.BasicDBObject)69 DBObject (com.mongodb.DBObject)50 Test (org.junit.jupiter.api.Test)17 Document (org.springframework.data.mongodb.core.mapping.Document)14 DBCollection (com.mongodb.DBCollection)11 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)11 DBCursor (com.mongodb.DBCursor)8 DBRef (com.mongodb.DBRef)8 List (java.util.List)7 Document (org.bson.Document)7 HashMap (java.util.HashMap)6 ObjectId (org.bson.types.ObjectId)6 MongoClient (com.mongodb.MongoClient)4 Map (java.util.Map)4 NotFoundException (org.graylog2.database.NotFoundException)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 MongoInputSplit (com.mongodb.hadoop.input.MongoInputSplit)3 IOException (java.io.IOException)3