Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
修改mongdbDriver 2.11 -> 3.11 支持mongdb4.0
修改mongdb插入支持多行插入
修改mongdb增删改返回影响行数
  • Loading branch information
linhai123 committed Nov 14, 2019
1 parent 2b6ee93 commit c6b48d8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.4</version>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>org.iq80.leveldb</groupId>
Expand Down
32 changes: 18 additions & 14 deletions src/main/java/io/mycat/backend/jdbc/mongodb/MongoData.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,24 @@ public BasicDBList getGrouyBys() {
else {
return null;
}
}
public void setGrouyBy(DBObject gb) {
this.groupby=gb;
this.type=true;
if (gb instanceof BasicDBList) {
Object gb2=((BasicDBList)gb).get(0);
if (gb2 instanceof DBObject) {
for (String field :((DBObject)gb2).keySet()) {
Object val = ((DBObject)gb2).get(field);
setField(field,getObjectToType(val));
}
}
}
}
}

public void setGrouyBy(DBObject gb) {
this.groupby = gb;
this.type = true;
if (gb instanceof BasicDBList) {
BasicDBList basicDBList = (BasicDBList)gb;
if(!basicDBList.isEmpty()){
Object gb2 = basicDBList.get(0);
if (gb2 instanceof DBObject) {
for (String field : ((DBObject) gb2).keySet()) {
Object val = ((DBObject) gb2).get(field);
setField(field, getObjectToType(val));
}
}
}
}
}

public static int getObjectToType(Object ob){
if (ob instanceof Integer) {
Expand Down
40 changes: 19 additions & 21 deletions src/main/java/io/mycat/backend/jdbc/mongodb/MongoSQLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@
import java.util.List;


import com.mongodb.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
import com.alibaba.druid.sql.ast.SQLStatement;
Expand Down Expand Up @@ -186,15 +181,20 @@ private int InsertData(SQLInsertStatement state) {
throw new RuntimeException("number of values and columns have to match");
}
SQLTableSource table=state.getTableSource();
BasicDBObject o = new BasicDBObject();
int i=0;
for(SQLExpr col : state.getColumns()) {
o.put(getFieldName2(col), getExpValue(state.getValues().getValues().get(i)));
i++;
}
BasicDBObject[] oList = new BasicDBObject[state.getValuesList().size()];
int i = 0;
for(SQLInsertStatement.ValuesClause values : state.getValuesList()){
int j = 0;
BasicDBObject o = new BasicDBObject();
oList[i++] = o ;
for(SQLExpr col : state.getColumns()) {
o.put(getFieldName2(col), getExpValue(values.getValues().get(j++)));
}
}

DBCollection coll =this._db.getCollection(table.toString());
coll.insert(new DBObject[] { o });
return 1;
WriteResult result = coll.insert(oList);
return i; // 这里result.getN 总是返回0 , 所以按插入数据量返回影响行数
}
private int UpData(SQLUpdateStatement state) {
SQLTableSource table=state.getTableSource();
Expand All @@ -208,9 +208,9 @@ private int UpData(SQLUpdateStatement state) {
set.put(getFieldName2(col.getColumn()), getExpValue(col.getValue()));
}
DBObject mod = new BasicDBObject("$set", set);
coll.updateMulti(query, mod);
WriteResult result = coll.updateMulti(query, mod);
//System.out.println("changs count:"+coll.getStats().size());
return 1;
return result.getN();
}
private int DeleteDate(SQLDeleteStatement state) {
SQLTableSource table=state.getTableSource();
Expand All @@ -221,11 +221,9 @@ private int DeleteDate(SQLDeleteStatement state) {
throw new RuntimeException("not where of sql");
}
DBObject query = parserWhere(expr);

coll.remove(query);

return 1;


WriteResult result = coll.remove(query);
return result.getN();
}
private int dropTable(SQLDropTableStatement state) {
for (SQLTableSource table : state.getTableSources()){
Expand Down

0 comments on commit c6b48d8

Please sign in to comment.