Skip to content

Commit

Permalink
basic numeric comparisons for querying
Browse files Browse the repository at this point in the history
  • Loading branch information
billy committed Oct 21, 2009
1 parent d4f2225 commit d674910
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 37 deletions.
66 changes: 45 additions & 21 deletions part2/ExpressionBuilder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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>
64 changes: 55 additions & 9 deletions part2/ExpressionBuilderTest.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
}


Expand Down
8 changes: 1 addition & 7 deletions part2/MongoDBTest.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -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() );

Expand Down Expand Up @@ -70,10 +70,6 @@

}

<<<<<<< HEAD:part2/MongoDBTest.cfc
=======
-------------------------------------------------------------------------------------*/



function $exploreStringSearchExpression(){
Expand Down Expand Up @@ -144,8 +140,6 @@

}

>>>>>>> 027af5559af71a7aecf8e46492703e2a803df92a:part2/MongoDBTest.cfc

function $findByRegEx(){
coll = mongo.getCollection('blog');
//raw = mongo.getMongo(); //raw jo
Expand Down

0 comments on commit d674910

Please sign in to comment.