From 976397f6ffbc79aac16d12556f85f67e80b16607 Mon Sep 17 00:00:00 2001 From: adam Date: Sun, 17 Mar 2013 14:54:06 +0700 Subject: [PATCH] #51 --- luaejdb/ejdb.lua | 15 +++++++++++++-- luaejdb/test/t1.lua | 10 +++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/luaejdb/ejdb.lua b/luaejdb/ejdb.lua index e782d74..cb8185e 100644 --- a/luaejdb/ejdb.lua +++ b/luaejdb/ejdb.lua @@ -128,7 +128,7 @@ function DB:save(cname, obj, ...) end function DB:find(cname, q, ...) - assert(getmetatable(q) == mtBObj, "Query object must be instance of 'luaejdb.B' class `q = luaejdb.B()`") + assert(getmetatable(q) == mtBObj, "Query object must be instance of 'luaejdb.Q' class `q = luaejdb.Q()`") local sflags = ... local orBsons = {} local ors = q:getJoinedORs() @@ -142,7 +142,14 @@ end function DB:findOne(cname, q, ...) - + assert(getmetatable(q) == mtBObj, "Query object must be instance of 'luaejdb.Q' class `q = luaejdb.Q()`") + q:Max(1); + local qres, count, log = self:find(cname, q, ...) + if qres ~= nil and #qres > 0 then + return qres:object(1), count, log + else + return nil, count, log + end end function DB:count(cname, q, sflags, ...) @@ -156,6 +163,10 @@ function DB:count(cname, q, sflags, ...) end +function DB:update(cname, q, ...) + return self:count(cname, q, ...) +end + -- ------- EJDB Query ------------- function B:_init(fname, ...) diff --git a/luaejdb/test/t1.lua b/luaejdb/test/t1.lua index 765ea1b..a74cb2b 100644 --- a/luaejdb/test/t1.lua +++ b/luaejdb/test/t1.lua @@ -135,7 +135,7 @@ assert(#qres == 1) qres:close() assert(#qres == 0) -local r,err = pcall(qres.object, qres, 1); +local r, err = pcall(qres.object, qres, 1); assert(err == "Cursor closed") @@ -161,6 +161,14 @@ local count, log = db:count("mycoll", Q():Or(Q("foo", "bar"), Q("foo", "bar6")), assert(count == 2) assert(log:find("COUNT ONLY: YES")) +local vobj, count, log = db:findOne("mycoll", Q():Or(Q("foo", "bar"), Q("foo", "bar6")):OrderBy("foo desc")); +assert(count == 1) +assert(vobj["foo"] == "bar6") + +local vobj, count, log = db:findOne("mycoll", Q():Or(Q("foo", "bar"), Q("foo", "bar6")):OrderBy({ foo = 1 })); +assert(count == 1) +assert(vobj["foo"] == "bar") + db:close() -- 2.7.4