diff --git a/mongodb/driver/driver.factor b/mongodb/driver/driver.factor index 3c61c8e4f0..426167b08e 100644 --- a/mongodb/driver/driver.factor +++ b/mongodb/driver/driver.factor @@ -52,8 +52,6 @@ SYNTAX: r/ ( token -- mdbregexp ) [ MDB_OID_FIELD swap at ] keep H{ } clone [ set-at ] keep ; -> 0 > [ [ cursor>> ] [ collection>> ] [ requested#>> ] tri ] @@ -115,9 +113,7 @@ USE: tools.continuations MEMO: reserved-namespace? ( name -- ? ) [ "$cmd" = ] [ "system" head? ] bi or ; -PRIVATE> - -MEMO: ensure-collection ( collection -- fq-collection ) +MEMO: check-collection ( collection -- fq-collection ) dup mdb-collection? [ name>> ] when "." split1 over mdb-instance name>> = [ nip ] [ drop ] if @@ -125,8 +121,9 @@ MEMO: ensure-collection ( collection -- fq-collection ) [ [ (ensure-collection) ] keep ] unless [ mdb-instance name>> ] dip "%s.%s" sprintf ; inline +PRIVATE> + : ( collection query -- mdb-query ) - [ ensure-collection ] dip ; inline GENERIC# limit 1 ( mdb-query limit# -- mdb-query ) @@ -163,19 +160,18 @@ GENERIC: explain. ( mdb-query -- ) M: mdb-query-msg explain. t >>explain find nip . ; - GENERIC: find-one ( mdb-query -- result/f ) M: mdb-query-msg find-one 1 >>return# send-query-plain objects>> dup empty? [ drop f ] [ first ] if ; -GENERIC: count ( collection selector -- result ) -M: assoc count - [ "count" H{ } clone [ set-at ] keep ] dip - [ over [ "query" ] dip set-at ] when* +GENERIC: count ( mdb-query -- result ) +M: mdb-query-msg count + [ collection>> "count" H{ } clone [ set-at ] keep ] keep + query>> [ over [ "query" ] dip set-at ] when* [ cmd-collection ] dip find-one [ check-ok ] keep '[ "n" _ at >fixnum ] [ f ] if ; - + : lasterror ( -- error ) cmd-collection H{ { "getlasterror" 1 } } find-one [ "err" ] dip at ; @@ -198,12 +194,12 @@ PRIVATE> GENERIC: save ( collection assoc -- ) M: assoc save - [ ensure-collection ] dip + [ check-collection ] dip send-message-check-error ; GENERIC: save-unsafe ( collection object -- ) M: assoc save-unsafe - [ ensure-collection ] dip + [ check-collection ] dip send-message ; GENERIC: ensure-index ( collection name spec -- ) @@ -224,7 +220,7 @@ M: assoc ensure-index find-one drop ; : ( collection selector object -- update-msg ) - [ ensure-collection ] 2dip ; + [ check-collection ] 2dip ; : >upsert ( mdb-update-msg -- mdb-update-msg ) 1 >>upsert? ; @@ -239,18 +235,21 @@ M: mdb-update-msg update-unsafe GENERIC: delete ( collection selector -- ) M: assoc delete - [ ensure-collection ] dip + [ check-collection ] dip send-message-check-error ; GENERIC: delete-unsafe ( collection selector -- ) M: assoc delete-unsafe - [ ensure-collection ] dip + [ check-collection ] dip send-message ; : load-index-list ( -- index-list ) index-collection H{ } clone find nip ; +: ensure-collection ( name -- ) + check-collection drop ; + : drop-collection ( name -- ) [ cmd-collection ] dip "drop" H{ } clone [ set-at ] keep