try {
db.open("test5");
- System.out.println("test ELDB opened");
+ System.out.println("test EJDB opened");
EJDBCollection test = db.getCollection("test");
test.sync();
System.out.println("test collection synced");
- EJDBResultSet rs = test.createQuery(new BasicBSONObject(), null, null, 0).execute();
+ EJDBQuery query = test.createQuery(new BasicBSONObject());
+
+ System.out.println("Objects matched: " + query.count());
+
+ EJDBResultSet rs = query.find();
for (BSONObject r : rs) {
System.out.println(r);
}
-// test.createQuery(new BasicBSONObject("index", new BasicBSONObject("$lt", 5)), null, null, EJDBQuery.JBQRYCOUNT).execute();
+
+ System.out.println(query.findOne());
} finally {
db.close();
this.cname = cname;
}
- // TODO:
protected native boolean txControl(int mode);
public void ensureExists() {
public native void setIndex(String path, int flags);
- public EJDBQuery createQuery(BSONObject query, BSONObject[] qors, BSONObject hints, int flags) {
- return new EJDBQuery(this, query, qors, hints, flags);
+ public EJDBQuery createQuery(BSONObject query) {
+ return new EJDBQuery(this, query, null, null);
+ }
+
+ public EJDBQuery createQuery(BSONObject query, BSONObject[] qors) {
+ return new EJDBQuery(this, query, qors, null);
+ }
+
+ public EJDBQuery createQuery(BSONObject query, BSONObject hints) {
+ return new EJDBQuery(this, query, null, hints);
+ }
+
+ public EJDBQuery createQuery(BSONObject query, BSONObject[] qors, BSONObject hints) {
+ return new EJDBQuery(this, query, qors, hints);
}
public void beginTransaction() {
package org.ejdb.driver;
import org.bson.BSONObject;
+import org.bson.BasicBSONObject;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
/**
* @author Tyutyunkov Vyacheslav (tve@softmotions.com)
public class EJDBQuery {
// Query search mode flags
- public static final int JBQRYCOUNT = 1; /*< Query only count(*) */
+ protected static final int JBQRYCOUNT = 1; /*< Query only count(*) */
private EJDBCollection coll;
private BSONObject query;
- private BSONObject[] qors;
+ private List<BSONObject> qors;
private BSONObject hints;
private int flags;
- EJDBQuery(EJDBCollection coll, BSONObject query, BSONObject[] qors, BSONObject hints, int flags) {
+ EJDBQuery(EJDBCollection coll, BSONObject query, BSONObject[] qors, BSONObject hints) {
this.coll = coll;
this.query = query;
- this.qors = qors;
+ this.qors = new ArrayList<BSONObject>();
+ if (qors != null) {
+ this.qors.addAll(Arrays.asList(qors));
+ }
this.hints = hints;
- this.flags = flags;
+ }
+
+ public BSONObject getQueryObject() {
+ return query;
}
// TODO
- public EJDBResultSet execute() {
- QResult qResult = this.execute(query, qors, hints, flags);
- return qResult != null ? new EJDBResultSet(qResult.rsPointer) : null;
+ public EJDBResultSet find() {
+ return this.execute(hints, 0).getResultSet();
+ }
+
+ public BSONObject findOne() {
+ Map hintsMap = hints != null ? hints.toMap() : new HashMap();
+ hintsMap.put("$max", 1);
+
+ EJDBResultSet rs = this.execute(new BasicBSONObject(hintsMap), 0).getResultSet();
+ BSONObject result = rs.hasNext() ? rs.next() : null;
+ rs.close();
+
+ return result;
+ }
+
+ public int update() {
+ return this.execute(hints, JBQRYCOUNT).getCount();
+ }
+
+ public int count() {
+ return this.execute(hints, JBQRYCOUNT).getCount();
+ }
+
+ protected QResult execute(BSONObject hints, int flags) {
+ BSONObject[] qors = new BSONObject[this.qors.size()];
+ this.qors.toArray(qors);
+
+ return this.execute(query, qors, hints, flags);
}
protected native QResult execute(BSONObject query, BSONObject[] qors, BSONObject hints, int flags);
this.count = count;
this.rsPointer = rsPointer;
}
+
+ public int getCount() {
+ return count;
+ }
+
+ public EJDBResultSet getResultSet() {
+ return new EJDBResultSet(rsPointer);
+ }
}
}