Permalink
Browse files

merging

  • Loading branch information...
2 parents fa05a70 + 55e861e commit 8d428cb4c76a16f15366720a82f47c784b84a191 bill shelton committed Jun 6, 2010
View
@@ -0,0 +1 @@
+/settings.xml
View
@@ -1,3 +1,4 @@
+<<<<<<< HEAD:.project
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>cfmongodb</name>
@@ -9,3 +10,18 @@
<natures>
</natures>
</projectDescription>
+=======
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>cfmongodb</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ </buildSpec>
+ <natures>
+ <nature>com.adobe.ide.coldfusion.projectNature</nature>
+ <nature>org.cfeclipse.cfml.CFENature</nature>
+ </natures>
+</projectDescription>
+>>>>>>> 55e861e72919aa660874119e751de4e0fd3730b3:.project
View
@@ -1,8 +1,90 @@
-<cfcomponent output="false" hint="Represents a Mongo Collection">
+<cfcomponent extends="MongoBase" output="false" hint="Represents a Mongo Collection">
+
+
+
<!---
Operations on Collections:
--->
-<cffunction name="init" hint="Constructor"></cffunction>
+
+ <cfscript>
+ //if you don't init, you must quit (coding, that is)
+ function init(mongo,dbName,collectionName){
+ variables.mongo = arguments.mongo;
+ variables.db = mongo.getDb(dbName);
+ variables.collection = variables.db.getCollection(collectionName);
+ return this;
+ }
+
+
+ // !!! Find !!!!
+
+ function findOne(field="",value=""){
+ var query = newDBObject(field,value);
+ if(field eq ""){
+ query = newDBObjectFromStruct({});
+ }
+ return collection.findOne(query);
+ }
+
+
+ //when only a string id is available
+ function findById(id){
+ var objId = newIDCriteriaObject(id);
+ return findOne("_id", objId,dbName,collectionName);
+ }
+
+ function findAll(){
+ var sort = {pub_date = -1}; // TODO: figure out wtf this is
+ var sort_spec = newDBObjectFromStruct(sort);
+ return collection.find().sort( sort_spec ).toArray();
+ }
+
+ function count(){
+ return collection.getCount();
+ }
+
+ // !!! Insert and Upsert !!!
+ function insert(structure){
+ var doc = newDBObjectFromStruct(structure);
+ var id = chr(0);
+ collection.insert(doc);
+ id = doc.get("_id");
+ structure._id = id; //add the _id object to the struct
+ return id;
+ }
+
+ function update(structure){
+ var newObject = newDBObjectFromStruct(structure);
+ if(structKeyExists(structure,"_id")){
+ critera = newIDCriteriaObject(structure._id);
+ return collection.update(criteria, newObject, true, false);
+ }else{
+ return collection.insert(newObject);
+ }
+ }
+
+
+
+ // !!! Remove !!!
+
+ function remove(criteria){
+ var doc = newDBObjectFromStruct(criteria);
+ return collection.remove(doc);
+
+ }
+
+ //Note the ObjectId object. This creates an ObjectId from
+ //the string representation of
+
+ function removeById(id){
+ var objId = newIDCriteriaObject(id);
+ return collection.remove(objID); //id's of deleted items
+ }
+
+
+
+ </cfscript>
+
</cfcomponent>
View
@@ -1,5 +1,5 @@
-<cfcomponent output="false">
-<!---
+<cfcomponent extends="MongoBase" output="false">
+<!---
Operations on Databases:
set port
@@ -13,15 +13,20 @@ config = {
server_port = 27017,
db_name = 'default_db'
};
-
+
mongo = createObject('java', 'com.mongodb.Mongo').init( variables.config.server_name , variables.config.server_port );
-
- function getDB(name){
- return mongo.getDB(name);
+
+ function init(dbName){
+ variables.db = mongo.getDb(dbName);
+ return this;
+ }
+
+ function getCollection(collectionName){
+ return createObject("component","Collection").init(mongo,variables.db.getName(),collectionName);
}
</cfscript>
-<!---
+<!---
getAddress public com.mongodb.DBAddress com.mongodb.DBTCP.getAddress()
debugString public java.lang.String com.mongodb.DBTCP.debugString()
View
@@ -0,0 +1,67 @@
+<cfcomponent extends="MongoBase" output="false" implements="IDocument" hint="Default implementation of IDocument and used by the Mongo class to generate a document.">
+
+<cfset this.__props__ = structNew() />
+<cfset variables.mongo = createObject('component', 'MongoDB') />
+
+
+<cffunction name="init" hint="Constructor. Creates an instance of a MongoDocument.">
+ <cfargument name="collection_name" type="string" required="true" hint="The name of the collection to which this document is bound." />
+ <cfargument name="_mongo" type="any" required="false" default="#variables.mongo#" hint="The instance of the Mongo wrapper to which the document is bound." />
+ <cfset variables.mongo.collection(collection_name) />
+ <cfset variables.mongo = _mongo />
+ <cfreturn this />
+</cffunction>
+
+
+<cffunction name="setCollection">
+</cffunction>
+
+
+<cffunction name="set" hint="Sets a property for the Document" returntype="void">
+ <cfargument name="property" type="String" />
+ <cfargument name="value" type="Any" />
+ <cfset structInsert(this.__props__, arguments.property, arguments.value) />
+</cffunction>
+
+
+<cffunction name="get" hint="Fetches the value of the given property. Returns null if not found." returntype="Any">
+ <cfargument name="property" />
+ <cfset var ret_val = javacast('null', '') />
+ <cftry>
+ <cfset ret_val = this.__props__[arguments.property] />
+ <cfcatch type="Expression">
+ <!--- want to return null --->
+ </cfcatch>
+ </cftry>
+ <cfreturn ret_val />
+</cffunction>
+
+<cffunction name="remove" returntype="void" hint="Removes a property form a Document. **NOT IMPLEMENTED**">
+ <cfargument name="property" hint="Removes the given property if it exists." />
+ <cfthrow type="NotImplementedException" message="To Do.">
+</cffunction>
+
+
+
+<cffunction name="save" returntype="String">
+ <cfreturn mongo.put(this.__props__) />
+</cffunction>
+
+
+<cffunction name="delete" returntype="void" hint="Deletes this Document from the Collection">
+ <cfset mongo.deleteById(this.__props__['_id']) />
+</cffunction>
+
+
+<cffunction name="update" hint="Performs in-place update of the the value for 'property'. NOT IMPLEMENTED">
+ <cfargument name="property">
+ <cfargument name="value">
+ <cfthrow type="NotImplementedException" message="To Do.">
+</cffunction>
+
+
+<cffunction name="validate" hint="Should be called before save() to perform any required validation. NOT IMPLEMENTED" returntype="void">
+ <cfthrow type="NotImplementedException" message="To Do.">
+</cffunction>
+
+</cfcomponent>
@@ -1,18 +1,18 @@
-<cfcomponent hint="Creates a Domain Specific Language (DSL) for querying MongoDB collections.">
+<cfcomponent extends="MongoBase">
<cfscript>
-
+
/*---------------------------------------------------------------------
-
- DSL for MongoDB searches:
-
+
+ DSL for MongoDB searches:
+
mongo.expressionBuilder().
-
+
results = mongo.startsWith('name','foo'). //string
endsWith('title','bar'). //string
exists('field','value'). //string
regex('field','value'). //string
eq('field','value'). //numeric
- lt('field','value'). //numeric
+ lt('field','value'). //numeric
gt('field','value'). //numeric
gte('field','value'). //numeric
lte('field','value'). //numeric
@@ -25,15 +25,31 @@
search('title,author,date', limit, start);
search(keys=[keys_to_return],limit=num,start=num);
-
-
-
+
+
+
-------------------------------------------------------------------------------------*/
builder = createObject('java', 'com.mongodb.BasicDBObjectBuilder').start();
pattern = createObject('java', 'java.util.regex.Pattern');
+function builder(){
+ return builder;
+}
+
+function start(){
+ builder.start();
+ return this;
+}
+
+function get(){
+ var g = builder.get();
+ start();//ensure we clear out any previous additions; otherwise, future queries with this same object would be screwy
+ return g;
+}
+
+
function startsWith(element, val){
var regex = val & '.*';
builder.add( element, pattern.compile(regex) );
@@ -61,20 +77,6 @@ function regex(element, val){
}
-function builder(){
- return builder;
-}
-
-function start(){
- builder.start();
- return this;
-}
-
-function get(){
- return builder.get();
-}
-
-
//May need at least some exception handling
function where( js_expression ){
builder.add( '$where', js_expression );
@@ -133,7 +135,7 @@ function $gte(element,val){
<cffunction name="before">
<cfargument name="element" type="string" />
<cfargument name="val" type="date" />
- <cfscript>
+ <cfscript>
var exp = {};
var date = parseDateTime(val);
exp['$lte'] = date;
@@ -146,7 +148,7 @@ function $gte(element,val){
<cffunction name="after">
<cfargument name="element" type="string" />
<cfargument name="val" type="date" />
- <cfscript>
+ <cfscript>
var exp = {};
var date = parseDateTime(val);
exp['$gte'] = date;
@@ -155,7 +157,7 @@ function $gte(element,val){
</cfscript>
</cffunction>
-<!---
+<!---
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.
@@ -167,7 +169,7 @@ But, this also proved to be a very good refactor.
<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>
+ <cfscript>
var exp = {};
exp[type] = val;
builder.add( element, exp );
@@ -179,7 +181,7 @@ But, this also proved to be a very good refactor.
<cfargument name="element" type="string" hint="The array element in the document we're searching"/>
<cfargument name="val" type="array" hint="The value(s) of an element in the array" />
<cfargument name="type" type="string" hint="$in,$nin,etc." />
- <cfscript>
+ <cfscript>
var exp = {};
exp[type] = val;
builder.add( element, exp );
View
@@ -0,0 +1,35 @@
+<cfcomponent>
+<cfscript>
+
+ /* OBJECT HELPER FUNCTIONS */
+ public function newDBObjectFromStruct(Struct data){
+ return createObject("java","com.mongodb.BasicDBObject").init(data);
+ }
+
+ public function newDBObject(String key, any value){
+ return createObject("java","com.mongodb.BasicDBObject").init(key,value);
+ }
+
+ public function newObjectIDFromID(String id){
+ return createObject("java","com.mongodb.ObjectId").init(id);
+ }
+
+ public function newIDCriteriaObject(String id){
+ return newDBObject("_id",newObjectIDFromID(id));
+ }
+
+ public struct function dbObjectToStruct(BasicDBObject){
+ var s = {};
+ s.putAll(BasicDBObject);
+ return s;
+ }
+
+ public function toJavaType(value){
+ if(not isNumeric(value) AND isBoolean(value)) return javacast("boolean",value);
+ if(isNumeric(value) and find(".",value)) return javacast("double",value);
+ if(isNumeric(value)) return javacast("int",value);
+ return value;
+ }
+
+</cfscript>
+</cfcomponent>
Oops, something went wrong.

0 comments on commit 8d428cb

Please sign in to comment.