Index: library/mongodb/nx-mongo.tcl =================================================================== diff -u -r04b17cf850af721f6ad1760dece06ef78b11da83 -r42ba8471f7620b850b6296f753cbc3079fe5c6cd --- library/mongodb/nx-mongo.tcl (.../nx-mongo.tcl) (revision 04b17cf850af721f6ad1760dece06ef78b11da83) +++ library/mongodb/nx-mongo.tcl (.../nx-mongo.tcl) (revision 42ba8471f7620b850b6296f753cbc3079fe5c6cd) @@ -5,7 +5,7 @@ # package require nx package require nsf::mongo -package provide nx::mongo 0.2 +package provide nx::mongo 0.3 # todo: how to handle multiple connections; currently we have a single, global connection # todo: all references are currently auto-fetched. make this optional @@ -21,17 +21,20 @@ namespace eval ::nx::mongo { ::nx::Object create ::nx::mongo::db { - :property db - :public method connect {{-db test} args} { + :object property db + :public object method connect {{-db test} args} { set :db $db set :mongoConn [::mongo::connect {*}$args] } - :public method count {args} {::mongo::count ${:mongoConn} {*}$args} - :public method index {args} {::mongo::index ${:mongoConn} {*}$args} - :public method insert {args} {::mongo::insert ${:mongoConn} {*}$args} - :public method remove {args} {::mongo::remove ${:mongoConn} {*}$args} - :public method query {args} {::mongo::query ${:mongoConn} {*}$args} - :public method update {args} {::mongo::update ${:mongoConn} {*}$args} + :public object method count {args} {::mongo::count ${:mongoConn} {*}$args} + :public object method index {args} {::mongo::index ${:mongoConn} {*}$args} + :public object method insert {args} {::mongo::insert ${:mongoConn} {*}$args} + :public object method remove {args} {::mongo::remove ${:mongoConn} {*}$args} + :public object method query {args} {::mongo::query ${:mongoConn} {*}$args} + :public object method update {args} {::mongo::update ${:mongoConn} {*}$args} + :public object method "drop collection" {name} {::mongo::run -nocomplain ${:mongoConn} ${:db} [list drop string $name]} + :public object method "drop database" {} {::mongo::run -nocomplain ${:mongoConn} ${:db} [list dropDatabase integer 1]} + :public object method "reset error" {} {::mongo::run -nocomplain ${:mongoConn} ${:db} [list reseterror integer 1]} } ####################################################################### @@ -66,7 +69,7 @@ # mapping. For now, this handles just the array notation. # :method "bson decode" {bsontype value} { - #puts stderr "bson decode of ${:name} /$bsontype/ '$value'" + #puts stderr "bson decode of ${:name} /$bsontype/ '$value'" if {$bsontype eq "array"} { if {![:isMultivalued]} { # We got an array, but the slot is not multivalued. Maybe @@ -87,7 +90,7 @@ #puts stderr "*** we have a reference, class = ${:arg}" # TODO we assume auto_deref set value [:bson deref ${:arg} $value] - puts stderr "*** bson deref ${:arg} ==> $value" + #puts stderr "*** bson deref ${:arg} ==> $value" } else { error "don't know how to decode object with value '$value'; [:serialize]" } @@ -120,7 +123,7 @@ puts stderr "autosave $value to obtain an object_id" $value save } - set _id [$value _id] + set _id [$value cget -_id] set cls [$value info class] return [list object [list \ {$ref} string [$cls mongo_collection] \ @@ -220,7 +223,7 @@ :method "get slot" {att} { set classes [concat [self] [:info mixin classes] [:info heritage]] foreach cls $classes { - set slot [$cls info slot objects $att] + set slot [$cls info slots $att] if {$slot ne ""} { return $slot } @@ -246,10 +249,11 @@ set bson [list] foreach {att op value} $cond { set slot [:get slot $att] + if {$slot eq ""} {error "could not obtain slot for <$att $op $value>"} switch $op { - "=" {lappend bson $att [$slot mongotype] $value} + "=" {lappend bson $att [$slot cget -mongotype] $value} ">" - "<" - "<=" - ">=" - "!=" { - lappend bson $att object [list [:get relop $op] [$slot mongotype] $value] + lappend bson $att object [list [:get relop $op] [$slot cget -mongotype] $value] } "in" - "all" { lappend bson $att object [list [:get relop $op] {*}[$slot bson encode -array $value]] @@ -294,7 +298,7 @@ set objParams [list] foreach {att type value} $tuple { set slot [:get slot $att] - #puts stderr "att $att type $type value $value => '$slot'" + if {$slot eq ""} {error "could not obtain slot for <$att $op $value>"} lappend objParams -$att [$slot bson decode $type $value] } #puts "bson parameter <$tuple> => $objParams" @@ -365,7 +369,7 @@ :public method insert {args} { set p [:new {*}$args] $p save - set _id [$p _id] + set _id [$p cget -_id] $p destroy return $_id } @@ -455,7 +459,7 @@ set :mongo_collection [string tolower [namespace tail [self]]]s } if {![info exists :mongo_db]} { - set :mongo_db [::nx::mongo::db db] + set :mongo_db [::nx::mongo::db cget -db] } set :mongo_ns ${:mongo_db}.${:mongo_collection} #puts stderr "mongo_ns is set to ${:mongo_ns}" @@ -529,9 +533,10 @@ # reference list. The containing object is not automatically # saved for the time being. We could consider an automatic # save or mongo-$pull update operation. - puts "[self] is embedded in ${:__embedded_in}" + #puts "[self] is embedded in ${:__embedded_in}" lassign ${:__embedded_in} parent att set slot [[$parent info class] get slot $att] + if {$slot eq ""} {error "could not obtain slot for <$att $op $value>"} $slot remove $parent [self] #puts stderr [:serialize] puts stderr "[self] must save parent $parent in db" @@ -544,14 +549,15 @@ foreach reference ${:__referenced_in} { lassign $reference parent att set slot [[$parent info class] get slot $att] + if {$slot eq ""} {error "could not obtain slot for <$att $op $value>"} $slot remove $parent [self] puts stderr "[self] must save parent $parent in db" } :destroy } else { - puts "delete a non-embedded entry" + #puts "delete a non-embedded entry" if {[info exists :_id]} { - set mongo_ns [[:info class] mongo_ns] + set mongo_ns [[:info class] cget -mongo_ns] ::nx::mongo::db remove $mongo_ns [list _id oid ${:_id}] } else { error "[self]: object does not contain an _id; it can't be delete from the mongo db." @@ -564,7 +570,7 @@ # otherwise perform an insert # :public method save {} { - set mongo_ns [[:info class] mongo_ns] + set mongo_ns [[:info class] cget -mongo_ns] if {$mongo_ns eq ""} { # We could perform the delegation probably automatically, but # for now we provide an error