Skip to content
Browse files

check in prior to branch

  • Loading branch information...
1 parent 9316b4e commit 340bd98938e1b73a4203e3054f3f10aafe4a7c0f billy committed Oct 18, 2009
Showing with 261 additions and 67 deletions.
  1. +17 −0 Blog.cfc
  2. +11 −0 Mixin.cfc
  3. +27 −14 Mongo.cfc
  4. +40 −0 MongoBlogTest.cfc
  5. +57 −52 MongoDB.cfc
  6. +19 −1 MongoDBTest.cfc
  7. +80 −0 MongoMapper.cfc
  8. +10 −0 Person.cfc
View
17 Blog.cfc
@@ -0,0 +1,17 @@
+<cfcomponent output="false">
+<cfscript>
+this.title = '';
+this.body = '';
+this.tags = [];
+this.author = '';
+this.pub_data = now();
+this.comments = [
+ comment= {
+ comment_author = 'Coward',
+ comment_email = '',
+ comment_www = '',
+ comment_text = ''}
+ ];
+
+</cfscript>
+</cfcomponent>
View
11 Mixin.cfc
@@ -0,0 +1,11 @@
+<cfcomponent output="false">
+<cfscript>
+foo = 'bar';
+id = 123;
+
+function bar(){
+ return foo;
+}
+</cfscript>
+
+</cfcomponent>
View
41 Mongo.cfc
@@ -15,19 +15,17 @@ collection = db.getCollection(collection_name);
//------------------------------------------------------------------------//
+function add(key,value,o){
+ //add key value pair to object ... todo
+}
-function put(){
+function put(o){
var doc = createObject('java', 'com.mongodb.BasicDBObject').init();
- //var o = arguments[1];
- if(arguments.size() eq 1){
- doc.putAll(arguments[1]);
- arguments[1]._id = doc.get("_id");
- }
- else {
- doc.put(arguments[1],arguments[2]);
- }
-
- return collection.insert(doc).get("_id");
+ var id = chr(0);
+ doc.putAll(o);
+ id = collection.insert(doc).get("_id");
+ o._id = id;
+ return id;
}//end function
@@ -48,8 +46,16 @@ function find(){
} //end function
+function delete(o){
+ var obj = get("_id", o._id);
+ return collection.remove(obj); //id's of deleted items
+} //end function
+
+
-function delete(field,value){
+
+//old
+function __delete(field,value){
var q = createObject('java', 'com.mongodb.BasicDBObject').init(field,value);
return collection.remove(q); //id's of deleted items
} //end function
@@ -62,8 +68,15 @@ function delete(field,value){
//in the structure.
//update by object id ..._id
-function update(_id,o){
- var obj = get("_id", _id);
+
+//testing if pass in map what it return
+function findSame(o){
+ var new_object = createObject('java', 'com.mongodb.BasicDBObject').init(o);
+ return collection.findOne(new_object);
+}
+
+function update(o){
+ var obj = get("_id", o._id);
var new_object = createObject('java', 'com.mongodb.BasicDBObject').init(o);
return collection.update(obj, new_object, false, false);
View
40 MongoBlogTest.cfc
@@ -0,0 +1,40 @@
+<cfcomponent output="false" extends="mxunit.framework.TestCase">
+<cfscript>
+ mongo = createObject('component', 'MongoDB');
+
+ function testBlog() {
+
+ blog = createObject('component', 'Blog');
+ blog.title = 'Look Ma. No SQL ...';
+ blog.text = 'Rapid development with MongoDB';
+ blog.author = 'bill';
+ /*
+ blog.author = 'billy';
+ blog.title = 'blog test';
+ blog.tags = ['politics','food','satire'];
+ blog.body = '<h1>testing ...</h1>';
+ blog.comments[1].author = 'Foo';
+ blog.comments[1].comment_text = 'whatever';
+ blog.comments[2].author = 'Bar';
+ blog.comments[2].comment_www = 'http://google.com';
+ blog.comments[2].comment_text = 'cool';
+ */
+
+ mongo.put(blog);
+
+ // debug(blog);
+
+ // blog.author = 'ed';
+ // mongo.update(blog);
+
+ // mongo.delete(blog);
+
+ bb = mongo.findOne();
+ debug(bb);
+ }
+
+
+
+
+</cfscript>
+</cfcomponent>
View
109 MongoDB.cfc
@@ -1,75 +1,80 @@
<cfcomponent output="false">
+<!--- Maybe should extend an Abstract Mongo? --->
+
<cfscript>
-variables.server_name = 'localhost';
-variables.server_port = 27017;
-variables.mongo = chr(0);
-variables.db_name = "default_db";
-variables.collection_name = "default_collection";
+//And this is configurable.
+server_name = 'localhost';
+server_port = 27017;
+db_name = 'default_db';
+collection_name = 'default_collection';
-/********************************
- mongo = createObject('java', 'com.mongodb.Mongo').init( "localhost" , 27017 );
- //debug(mongo);
- db = mongo.getDB( "mydb" );
+//maybe this goes in super class
+mongo = createObject('java', 'com.mongodb.Mongo').init( variables.server_name , variables.server_port );
+db = mongo.getDb(db_name);
+collection = db.getCollection(collection_name);
-************************************/
-
-
-function init(){
- if( arguments.size() gte 1 ) setServer(arguments[1]);
- if( arguments.size() gte 2 ) setPort(arguments[2]);
- if( arguments.size() gte 3 ) setDatabase(arguments[3]);
- variables.mongo = createObject('java', 'com.mongodb.Mongo').init( getServer() , getPort() );
- variables.db_name = getDBName();
- return this;
+
+//------------------------------------------------------------------------//
+function add(key,value,o){
+ //add key value pair to object ... todo
}
+function put(o){
+ var doc = createObject('java', 'com.mongodb.BasicDBObject').init();
+ var id = chr(0);
+ doc.putAll(o);
+ id = collection.insert(doc).get("_id");
+ o._id = id; //add the _id object to the struct
+ return id;
+}//end function
-function getDb() {
- if( arguments.size() ) {
- return variables.mongo.getDB( arguments[1] );
- }
- else{
- return variables.mongo.getDB( variables.db_name ); //default name
- }
-}
+function get(field,value){
+ var q = createObject('java', 'com.mongodb.BasicDBObject').init(field,value);
+ var cursor = collection.find(q);
+ return cursor.next();
+} //end function
-/*********************************************
- acessor methods
-***********************************************/
-function setCollection(collection_name) {
- variables.collection_name = arguments.collection_name;
-}
-function getCollection() {
- return variables.collection_name;
+function count(){
+ return collection.getCount();
}
-function getDBName(){
- return variables.db_name;
-}
+function findOne(){
+ return collection.findOne();
+} //
-function setDBName(db_name){
- variables.db_name = arguments.db_name;
-}
+function find(){
-function setServer(server_name){
- variables.server_name = arguments.server_name;
-}
+} //end function
-function setPort(port){
- variables.server_port = port;
-}
+function delete(o){
+ var obj = get("_id", o._id);
+ return collection.remove(obj); //id's of deleted items
+} //end function
-function getPort(){
- return variables.server_port;
-}
-function getServer(){
- return variables.server_name;
+
+
+function update(o){
+ var obj = get("_id", o._id);
+ var new_object = createObject('java', 'com.mongodb.BasicDBObject').init(o);
+ return collection.update(obj, new_object, false, false);
+
+} //end function
+
+
+
+//not tested ...
+function switchCollection(collection_name){
+ collection = db.getCollection(collection_name);
}
</cfscript>
+
+<cffunction name="dump">
+ <cfdump var="#doc#">
+</cffunction>
</cfcomponent>
View
20 MongoDBTest.cfc
@@ -26,6 +26,16 @@
friend=friend
};
+
+function testSerializeJSONThis(){
+ var p = createObject('component','Person');
+ var j = serializeJSON(p);
+ debug(j);
+ mongo = createObject('component','Mongo');
+ id = mongo.put(p);
+ o = mongo.get('_id',id);
+ debug(o);
+}
function $endToEndSyntax(){
mongo = createObject('component','Mongo');
@@ -45,7 +55,15 @@ function $endToEndSyntax(){
debug( mongo.count() );
}
-
+function $testFindSame(){
+ return;
+ mongo = createObject('component','Mongo');
+ //id = mongo.put(person); //name/value or struct
+ person.name = 'ed';
+ newperson = mongo.findSame(person);
+ debug(person);
+ debug(newperson);
+}
function $updatePerson(){
mongo = createObject('component','Mongo');
View
80 MongoMapper.cfc
@@ -0,0 +1,80 @@
+
+<!--- Maybe should extend an Abstract Mongo? --->
+
+<cfscript>
+//And this is configurable.
+server_name = 'localhost';
+server_port = 27017;
+db_name = 'default_db';
+collection_name = 'default_collection';
+
+//maybe this goes in super class
+mongo = createObject('java', 'com.mongodb.Mongo').init( variables.server_name , variables.server_port );
+db = mongo.getDb(db_name);
+collection = db.getCollection(collection_name);
+
+
+//------------------------------------------------------------------------//
+
+function put(){
+ var doc = createObject('java', 'com.mongodb.BasicDBObject').init();
+ doc.putAll( this );
+ return collection.insert(doc).get("_id");
+}//end function
+
+
+
+function get(field,value){
+ var q = createObject('java', 'com.mongodb.BasicDBObject').init(field,value);
+ var cursor = collection.find(q);
+ return cursor.next();
+} //end function
+
+
+function count(){
+ return collection.getCount();
+}
+
+function _find(){
+
+} //end function
+
+
+
+function delete(field,value){
+ var q = createObject('java', 'com.mongodb.BasicDBObject').init(field,value);
+ return collection.remove(q); //id's of deleted items
+} //end function
+
+
+
+//ideally, we would have an object that when called would update itself
+//however, when working with structs, we need to tell MongoDB, which
+//item we want to update, since we are not maintaining the string id
+//in the structure.
+
+//update by object id ..._id
+
+//testing if pass in map what it return
+function findSame(o){
+ var new_object = createObject('java', 'com.mongodb.BasicDBObject').init(o);
+ return collection.findOne(new_object);
+}
+
+function update(_id,o){
+ var obj = get("_id", _id);
+ var new_object = createObject('java', 'com.mongodb.BasicDBObject').init(o);
+ return collection.update(obj, new_object, false, false);
+
+} //end function
+
+
+
+//not tested ...
+function switchCollection(collection_name){
+ collection = db.getCollection(collection_name);
+}
+
+</cfscript>
+
+
View
10 Person.cfc
@@ -0,0 +1,10 @@
+<cfcomponent output="false">
+<cfscript>
+this.name = 'bill';
+this.email = 'bill@bill.com';
+this.address = {
+ street = '123 main',
+ city = 'anytown'
+};
+</cfscript>
+</cfcomponent>

0 comments on commit 340bd98

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