Commits

vadimtsushko  committed 4cf4fdd

Queries

  • Participants
  • Parent commits b049363

Comments (0)

Files changed (10)

File examples/simple.dart

 #import("../lib/mongo.dart");
+#import("../lib/bson/bson.dart");
 #import("dart:builtin");
 main(){
   Db db = new Db("mongo-dart-test");
-  db.open().then((connOpened){
+  MCollection coll;
+  db.open().chain((c){
     print('connection open');
-    MCollection coll = db.collection("simple_data");
+    coll = db.collection("simple_data");
     coll.remove();
     for (var n = 0; n<1000; n++){  
-      coll.insert({"my_field":n});
-    }
-      
-    coll.findOne({"my_field": 17}).then((val){
-      print("Filtered by value: $val");     
-    });
-      
-    coll.find({"my_field": {"\$gt": 985}}).each((v)=>print(v)).then((dummy){    
+      coll.insert({"my_field":n,"str_field":"str_$n"});
+    }      
+    return coll.findOne({"my_field": 17});
+  }).chain((val){
+      print("Filtered by {'my_field': 17} $val");
+      print("Filtered {'my_field': {'\$gt': 995}}:");
+      return coll.find({'my_field': {'\$gt': 995}}).each((v)=>print(v));
+  }).chain((val){
+      print("Filtered by {'str_field': {'\$regex': new BsonRegexp('^str_(5|7|8)17\$')}");
+      return coll.find({'str_field': {'\$regex': new BsonRegexp('^str_(5|7|8)17\$')}}).each((v)=>print(v));        
+  }).then((dummy){    
       db.close();        
-    });
   });
-
 }

File examples/types.dart

       'binary':new Binary.from([0x23,0x24,0x25]),
       'int':42,
       'float':33.3333,
-//    'regexp':/foobar/i,
+      'regexp': new BsonRegexp(".?dim"),
 //    'regexp2':/foobar2/,
       'boolean':true,
       'where':new BsonCode('this.x == 3'),
-//    'dbref':new BSON.DBRef(collection.collectionName, new BSON.ObjectID()),
       'null':null
     });
     return collection.findOne();
   }).then((v){    
     print(v);  
     db.close();  
-  }); 
-  
+  });  
 }  

File lib/bson/binary.dart

        stringBytes.add(bytes[offset-1]);
     }
     return new String.fromCharCodes(stringBytes);
-  }  
+  }
+  writeCString(List<int> charCodes){
+    bytes.setRange(offset,charCodes.length,charCodes);
+    offset += charCodes.length;
+    writeByte(0);    
+ }
 
   int byteLength() => bytes.length+4+1;
   bool atEnd() => offset == bytes.length;

File lib/bson/bson.dart

 #source("bson_null.dart");
 #source("bson_boolean.dart");
 #source("bson_date.dart");
+#source("bson_regexp.dart");
 
 

File lib/bson/bson_regexp.dart

+class BsonRegexp extends BsonObject{
+  String pattern;
+  String options;
+  bool multiLine;
+  bool caseInsensitive;
+  bool verbose;
+  bool dotAll;
+  bool unicodeMode;
+  bool localeDependent;
+  BsonRegexp(this.pattern,[this.multiLine,this.caseInsensitive,this.verbose,this.dotAll,this.unicodeMode,this.localeDependent]):options="";
+  get value()=>this;
+  int get typeByte() => BSON.BSON_DATA_REGEXP;  
+  byteLength()=>pattern.length+1+options.length+1;
+  unpackValue(Binary buffer){
+    pattern = buffer.readCString();
+    options = buffer.readCString();     
+  }   
+  createOptionsString(){
+    options = "";  
+  }
+  toString()=>"BsonRegexp('$pattern',options:'$options')";
+  packValue(Binary buffer){
+     createOptionsString();
+     buffer.writeCString(pattern.charCodes());
+     buffer.writeCString(options.charCodes());     
+  }  
+}

File lib/bson/bson_string.dart

   get value()=>this;
   int get typeByte() => BSON.BSON_DATA_CODE;
   BsonCode(String data):super(data);
-  String toString()=>"BsonCode($data)";  
+  String toString()=>"BsonCode('$data')";  
 }
 class BsonCString extends BsonObject{
   String data;

File lib/bson/bson_type.dart

   if (value === true || value === false){
     return new BsonBoolean(value);
   }
+  if (value is BsonRegexp){
+    return value;
+  }  
   throw new Exception("Not implemented for $value");           
 }  
 
       return new BsonDate(null);
     case BSON.BSON_DATA_CODE:
       return new BsonCode(null);
+    case BSON.BSON_DATA_REGEXP:
+      return new BsonRegexp(null);      
     default:
       throw new Exception("Not implemented for BSON TYPE $typeByte");           
   }  

File lib/map_proxy.dart

+class _MapProxy<K,V> implements Map<K,V>{
+  Map map;
+  _MapProxy(): map = {};
+  bool containsValue(V value)=>map.containsValue(value);
+  
+  bool containsKey(K key)=>map.containsKey(key);
+  
+  V operator [](K key)=>map[key];
+  
+  void operator []=(K key, V value){
+   map[key]=value;
+  } 
+
+  V putIfAbsent(K key, V ifAbsent())=>map.putIfAbsent(key, ifAbsent);
+  
+  V remove(K key)=>map.remove(key);
+  
+  void clear()=>map.clear();
+
+  void forEach(void f(K key, V value))=>map.forEach(f);
+  
+  Collection<K> getKeys()=>map.getKeys();
+  
+  Collection<V> getValues()=>map.getValues();
+  
+  int get length() => map.length;
+
+  bool isEmpty()=>map.isEmpty();
+  
+}

File lib/mongo.dart

 #source("db.dart");
 #source("mcollection.dart");
 #source("cursor.dart");
-#source("utils.dart");
+#source("utils.dart");
+#source("map_proxy.dart");
+#source("selector_builder.dart");

File lib/selector_builder.dart

+SelectorBuilder sel(){
+  return new SelectorBuilder();
+}
+class SelectorBuilder<K,V> extends _MapProxy<K,V>{  
+  SelectorBuilder eq(String fieldName, var value){
+    map["query"][fieldName] = value;
+    return this;
+  }
+  SelectorBuilder ne(String fieldName, var value){
+    map["query"][fieldName] = {"\$ne":value};
+    return this;
+  }
+  SelectorBuilder gt(String fieldName, var value){
+    map["query"][fieldName] = {"\$gt":value};
+    return this;
+  }
+  SelectorBuilder lt(String fieldName, var value){
+    map["query"][fieldName] = {"\$lt":value};
+    return this;
+  }
+  SelectorBuilder gte(String fieldName, var value){
+    map["query"][fieldName] = {"\$gte":value};
+    return this;
+  }
+  SelectorBuilder lte(String fieldName, var value){    
+    map["query"][fieldName] = {"\$lte":value};
+    return this;
+  }
+  SelectorBuilder all(String fieldName, List values){
+    map["query"][fieldName] = {"\$all":values};
+    return this;
+  }
+  SelectorBuilder exists(String fieldName){
+    map["query"][fieldName] = {"\$exists":true};
+    return this;    
+  }
+  SelectorBuilder notExists(String fieldName){
+    map["query"][fieldName] = {"\$exists":false};
+    return this;    
+  }
+  SelectorBuilder mod(String fieldName, int value){
+    map["query"][fieldName] = {"\$mod":[value,0]};
+    return this;    
+  }  
+}