Skip to content
Browse files

basic numeric comparisons for querying

  • Loading branch information...
1 parent d4f2225 commit d6749108b40a7e433488b1958b3124e3901987ff billy committed Oct 21, 2009
Showing with 101 additions and 37 deletions.
  1. +45 −21 part2/ExpressionBuilder.cfc
  2. +55 −9 part2/ExpressionBuilderTest.cfc
  3. +1 −7 part2/MongoDBTest.cfc
View
66 part2/ExpressionBuilder.cfc
@@ -14,19 +14,14 @@
eq('field','value'). //numeric
lt('field','value'). //numeric
gt('field','value'). //numeric
- gte('field','value'). //numeric
- lte('field','value'). //numeric
- in('field','value'). //array
- nin('field','value'). //array
- mod('field','value'). //numeric
- size('field','value'). //numeric
-
+ gte('field','value'). //numeric
+ lte('field','value'). //numeric
+ in('field','value'). //array
+ nin('field','value'). //array
+ mod('field','value'). //numeric
+ size('field','value'). //numeric
search('title,author,date', limit, start);
-
-
-
-
search(keys=[keys_to_return],limit=num,start=num);
@@ -64,16 +59,6 @@ function regex(element, val){
}
-function $gt(element, val){
- //need to address all numeric values!
- var exp = {};
- exp[element] = javacast('long', val);
- builder.add( '$gt', exp );
- return this;
-}
-
-
-
function builder(){
return builder;
}
@@ -87,5 +72,44 @@ function get(){
}
+//may need at least some exception handling
+function $lt(element,val){
+ return addNumericCriteria(element,val,'$lt');
+}
+
+function $lte(element,val){
+ return addNumericCriteria(element,val,'$lte');
+}
+
+function $gt(element,val){
+ return addNumericCriteria(element,val,'$gt');
+}
+
+
+function $gte(element,val){
+ return addNumericCriteria(element,val,'$gte');
+}
+
</cfscript>
+
+<!---
+Note to self: Using cffunction here because of the ability/need to cast
+arbitrary numeric data to java without using JavaCast. CFARGUMENT takes care
+of that. CF9 might too, but most folks are still < CF9.
+
+But, this also proved to be a very good refactor.
+ --->
+
+<cffunction name="addNumericCriteria" hint="refactored $expressions for numerics">
+ <cfargument name="element" type="string" hint="The element in the document we're searching"/>
+ <cfargument name="val" type="numeric" hint="The comparative value of the element" />
+ <cfargument name="type" type="string" hint="$gt,$lt,etc. The operators - <><=>= ..." />
+ <cfscript>
+ var exp = {};
+ exp[type] = val;
+ builder.add( element, exp );
+ return this;
+ </cfscript>
+</cffunction>
+
</cfcomponent>
View
64 part2/ExpressionBuilderTest.cfc
@@ -6,28 +6,74 @@
keys = createObject('java', 'com.mongodb.BasicDBObject').init(key_exp);
-function testRegEx(){
- temp = builder.regex( 'TITLE', '.*No.600$');
+function testLTE(){
+ builder.start();
+ temp = builder.$lte( 'TS', 9999999999999999999999);
+ debug(builder.get());
q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ debug(q);
items = coll.find( q, keys );
ia = items.toArray();
debug(items.count());
- debug(items.toArray().toString());
- debug(items.toArray());
- assert( ia.size() );
- assertEquals( ia[1]["TITLE"], "Blog Title No.600" );
- assertEquals( ia[1]["AUTHOR"], "bill_600" );
+ //debug(items.toArray().toString());
+ assertEquals( 1000 , items.count() );
+}
+
+
+function testLT(){
+ builder.start();
+ temp = builder.$lt( 'TS', 9999999999999999999999);
+ debug(builder.get());
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ debug(q);
+ items = coll.find( q, keys );
+ ia = items.toArray();
+ debug(items.count());
+ //debug(items.toArray().toString());
+ assertEquals( 1000 , items.count() );
}
function testGT(){
- temp = builder.$gt( 'TS', 1256148921000);
+ builder.start();
+ temp = builder.$gt( 'TS', 0);
+ debug(builder.get());
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ debug(q);
+ items = coll.find( q, keys );
+ ia = items.toArray();
+ debug(items.count());
+ //debug(items.toArray().toString());
+ assertEquals( 1000 , items.count() );
+}
+
+
+function testGTE(){
+ builder.start();
+ temp = builder.$gte( 'TS', 0);
+ debug(builder.get());
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ debug(q);
+ items = coll.find( q, keys );
+ ia = items.toArray();
+ debug(items.count());
+ //debug(items.toArray().toString());
+ assertEquals( 1000 , items.count() );
+}
+
+
+
+function testRegEx(){
+ temp = builder.regex( 'TITLE', '.*No.600$');
q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
items = coll.find( q, keys );
ia = items.toArray();
debug(items.count());
debug(items.toArray().toString());
- fail( 'last test of the day. must.go.home.' );
+ debug(items.toArray());
+ assert( ia.size() );
+ assertEquals( ia[1]["TITLE"], "Blog Title No.600" );
+ assertEquals( ia[1]["AUTHOR"], "bill_600" );
}
View
8 part2/MongoDBTest.cfc
@@ -34,7 +34,7 @@
exp = createObject('java', 'com.mongodb.BasicDBObjectBuilder').start();
exp.add( '$gte', javacast('long',1256148921000 ) );
- exp.add( '$lte', javacast('long',1256148921000 ) );
+ //exp.add( '$lte', javacast('long',1256148921000 ) );
debug( exp.get() );
@@ -70,10 +70,6 @@
}
-<<<<<<< HEAD:part2/MongoDBTest.cfc
-=======
- -------------------------------------------------------------------------------------*/
-
function $exploreStringSearchExpression(){
@@ -144,8 +140,6 @@
}
->>>>>>> 027af5559af71a7aecf8e46492703e2a803df92a:part2/MongoDBTest.cfc
-
function $findByRegEx(){
coll = mongo.getCollection('blog');
//raw = mongo.getMongo(); //raw jo

0 comments on commit d674910

Please sign in to comment.
Something went wrong with that request. Please try again.