Search in sources :

Example 51 with BSONObject

use of org.bson.BSONObject in project Mycat-Server by MyCATApache.

the class SequoiaSQLParser method UpData.

private int UpData(SQLUpdateStatement state) {
    SQLTableSource table = state.getTableSource();
    DBCollection coll = this._db.getCollection(table.toString());
    SQLExpr expr = state.getWhere();
    BSONObject query = parserWhere(expr);
    BasicBSONObject set = new BasicBSONObject();
    for (SQLUpdateSetItem col : state.getItems()) {
        set.put(getFieldName2(col.getColumn()), getExpValue(col.getValue()));
    }
    BSONObject mod = new BasicBSONObject("$set", set);
    // coll.updateMulti(query, mod);
    coll.update(query, mod, null);
    // System.out.println("changs count:"+coll.getStats().size());
    return 1;
}
Also used : DBCollection(com.sequoiadb.base.DBCollection) BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 52 with BSONObject

use of org.bson.BSONObject in project Mycat-Server by MyCATApache.

the class SequoiaSQLParser method DeleteDate.

private int DeleteDate(SQLDeleteStatement state) {
    SQLTableSource table = state.getTableSource();
    DBCollection coll = this._db.getCollection(table.toString());
    SQLExpr expr = state.getWhere();
    if (expr == null) {
        throw new RuntimeException("not where of sql");
    }
    BSONObject query = parserWhere(expr);
    // coll.remove(query);
    coll.delete(query);
    return 1;
}
Also used : DBCollection(com.sequoiadb.base.DBCollection) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 53 with BSONObject

use of org.bson.BSONObject in project Mycat-Server by MyCATApache.

the class SequoiaSQLParser method query.

public SequoiaData query() throws SequoiaSQLException {
    if (!(statement instanceof SQLSelectStatement)) {
        // return null;
        throw new IllegalArgumentException("not a query sql statement");
    }
    SequoiaData mongo = new SequoiaData();
    DBCursor c = null;
    SQLSelectStatement selectStmt = (SQLSelectStatement) statement;
    SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
    int icount = 0;
    if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
        BasicBSONObject fields = new BasicBSONObject();
        // 显示的字段
        for (SQLSelectItem item : mysqlSelectQuery.getSelectList()) {
            // System.out.println(item.toString());
            if (!(item.getExpr() instanceof SQLAllColumnExpr)) {
                if (item.getExpr() instanceof SQLAggregateExpr) {
                    SQLAggregateExpr expr = (SQLAggregateExpr) item.getExpr();
                    if (expr.getMethodName().equals("COUNT")) {
                        icount = 1;
                        mongo.setField(getExprFieldName(expr), Types.BIGINT);
                    }
                    fields.put(getExprFieldName(expr), Integer.valueOf(1));
                } else {
                    fields.put(getFieldName(item), Integer.valueOf(1));
                }
            }
        }
        // 表名
        SQLTableSource table = mysqlSelectQuery.getFrom();
        DBCollection coll = this._db.getCollection(table.toString());
        mongo.setTable(table.toString());
        SQLExpr expr = mysqlSelectQuery.getWhere();
        BSONObject query = parserWhere(expr);
        // System.out.println(query);
        SQLSelectGroupByClause groupby = mysqlSelectQuery.getGroupBy();
        BasicBSONObject gbkey = new BasicBSONObject();
        if (groupby != null) {
            for (SQLExpr gbexpr : groupby.getItems()) {
                if (gbexpr instanceof SQLIdentifierExpr) {
                    String name = ((SQLIdentifierExpr) gbexpr).getName();
                    gbkey.put(name, Integer.valueOf(1));
                }
            }
            icount = 2;
        }
        int limitoff = 0;
        int limitnum = 0;
        if (mysqlSelectQuery.getLimit() != null) {
            limitoff = getSQLExprToInt(mysqlSelectQuery.getLimit().getOffset());
            limitnum = getSQLExprToInt(mysqlSelectQuery.getLimit().getRowCount());
        }
        SQLOrderBy orderby = mysqlSelectQuery.getOrderBy();
        BasicBSONObject order = new BasicBSONObject();
        if (orderby != null) {
            for (int i = 0; i < orderby.getItems().size(); i++) {
                SQLSelectOrderByItem orderitem = orderby.getItems().get(i);
                order.put(orderitem.getExpr().toString(), Integer.valueOf(getSQLExprToAsc(orderitem.getType())));
            }
        // c.sort(order);
        // System.out.println(order);
        }
        if (icount == 1) {
            mongo.setCount(coll.getCount(query));
        } else if (icount == 2) {
            BasicBSONObject initial = new BasicBSONObject();
            initial.put("num", 0);
            String reduce = "function (obj, prev) { " + "  prev.num++}";
        // mongo.setGrouyBy(coll.group(gbkey, query, initial, reduce));
        } else {
            if ((limitoff > 0) || (limitnum > 0)) {
                // .skip(limitoff).limit(limitnum);
                c = coll.query(query, fields, order, null, limitoff, limitnum);
            } else {
                c = coll.query(query, fields, order, null, 0, -1);
            }
        }
        mongo.setCursor(c);
    }
    return mongo;
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) BasicBSONObject(org.bson.BasicBSONObject) DBCollection(com.sequoiadb.base.DBCollection) DBCursor(com.sequoiadb.base.DBCursor)

Example 54 with BSONObject

use of org.bson.BSONObject in project mongo-java-driver by mongodb.

the class DBObjectCodec method writeValue.

@SuppressWarnings("unchecked")
private void writeValue(final BsonWriter bsonWriter, final EncoderContext encoderContext, @Nullable final Object value) {
    if (value == null) {
        bsonWriter.writeNull();
    } else if (value instanceof DBRef) {
        encodeDBRef(bsonWriter, (DBRef) value, encoderContext);
    } else if (value instanceof Map) {
        encodeMap(bsonWriter, (Map<String, Object>) value, encoderContext);
    } else if (value instanceof Iterable) {
        encodeIterable(bsonWriter, (Iterable) value, encoderContext);
    } else if (value instanceof BSONObject) {
        encodeBsonObject(bsonWriter, (BSONObject) value, encoderContext);
    } else if (value instanceof CodeWScope) {
        encodeCodeWScope(bsonWriter, (CodeWScope) value, encoderContext);
    } else if (value instanceof byte[]) {
        encodeByteArray(bsonWriter, (byte[]) value);
    } else if (value.getClass().isArray()) {
        encodeArray(bsonWriter, value, encoderContext);
    } else if (value instanceof Symbol) {
        bsonWriter.writeSymbol(((Symbol) value).getSymbol());
    } else {
        Codec codec = codecRegistry.get(value.getClass());
        encoderContext.encodeWithChildContext(codec, bsonWriter, value);
    }
}
Also used : OverridableUuidRepresentationCodec(org.bson.codecs.OverridableUuidRepresentationCodec) Codec(org.bson.codecs.Codec) CollectibleCodec(org.bson.codecs.CollectibleCodec) Symbol(org.bson.types.Symbol) BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) HashMap(java.util.HashMap) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) Map(java.util.Map) BsonTypeCodecMap(org.bson.codecs.BsonTypeCodecMap) CodeWScope(org.bson.types.CodeWScope)

Example 55 with BSONObject

use of org.bson.BSONObject in project mongo-java-driver by mongodb.

the class BasicDBObject method canonicalizeBSONObject.

private static BasicDBObject canonicalizeBSONObject(final BSONObject from) {
    BasicDBObject canonicalized = new BasicDBObject();
    TreeSet<String> keysInOrder = new TreeSet<String>(from.keySet());
    for (String key : keysInOrder) {
        Object val = from.get(key);
        canonicalized.put(key, canonicalize(val));
    }
    return canonicalized;
}
Also used : TreeSet(java.util.TreeSet) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject)

Aggregations

BSONObject (org.bson.BSONObject)101 BasicBSONObject (org.bson.BasicBSONObject)49 Test (org.junit.Test)34 BasicDBObject (com.mongodb.BasicDBObject)19 SerializableString (com.fasterxml.jackson.core.SerializableString)14 SerializedString (com.fasterxml.jackson.core.io.SerializedString)14 LinkedHashMap (java.util.LinkedHashMap)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 IOException (java.io.IOException)11 DBObject (com.mongodb.DBObject)10 ArrayList (java.util.ArrayList)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Map (java.util.Map)6 BasicBSONDecoder (org.bson.BasicBSONDecoder)6 BSONDecoder (org.bson.BSONDecoder)5 BSONEncoder (org.bson.BSONEncoder)5 BasicBSONEncoder (org.bson.BasicBSONEncoder)5 LazyBSONObject (org.bson.LazyBSONObject)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 BSONFileSplit (com.mongodb.hadoop.input.BSONFileSplit)4