From 2a00a42f38b40ac0247b861055158ceb41601390 Mon Sep 17 00:00:00 2001 From: adam Date: Wed, 13 Mar 2013 17:56:13 +0700 Subject: [PATCH] #51 --- luaejdb/ejdb.lua | 11 +++++++---- luaejdb/luabson.c | 5 +++-- luaejdb/test/t1.lua | 3 +-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/luaejdb/ejdb.lua b/luaejdb/ejdb.lua index 81bc0c7..601ee7e 100644 --- a/luaejdb/ejdb.lua +++ b/luaejdb/ejdb.lua @@ -157,7 +157,7 @@ function B:_setop(op, val, ...) if op == nil then replace = true end - val = self:_toOpVal(val) + val = self:_toOpVal(op, val) local olist = self._omap[self._field] if not olist then olist = { self._field } @@ -173,8 +173,12 @@ function B:_setop(op, val, ...) end end -function B:_toOpVal(val) - return val +function B:_toOpVal(op, val) + if op == nil and self._field == "_id" and type(val) == "string" then + return luaejdb.toOID(val) + else + return val + end end function B:_hintOp(op, val, ...) @@ -209,7 +213,6 @@ function B:KeyVal(key, val) return self end - function B:Eq(val) self:_setop(nil, val, nil, true) return self end function B:ElemMatch(val) self:_setop("$elemMatch", val) return self end diff --git a/luaejdb/luabson.c b/luaejdb/luabson.c index 200780a..fc7ccb8 100644 --- a/luaejdb/luabson.c +++ b/luaejdb/luabson.c @@ -24,6 +24,7 @@ void lua_init_bson(lua_State *L) { } //-0/+1 + void lua_push_bsontype_table(lua_State* L, int bsontype) { lua_newtable(L); //table lua_newtable(L); //metatable @@ -143,7 +144,7 @@ int lua_to_bson(lua_State *L) { } lua_pushlstring(L, bson_data(&bs), bson_size(&bs)); //+1 bson data as string bson_destroy(&bs); - if (lua_gettop(L) - argc != 1) { + if (lua_gettop(L) - argc != 1) { return luaL_error(L, "lua_to_bson: Invalid stack size: %d should be: %d", (lua_gettop(L) - argc), 1); } return 1; @@ -283,7 +284,7 @@ static void lua_val_to_bson(lua_State *L, const char *key, int vpos, bson *bs, i } else if (ktype == LUA_TSTRING) { size_t klen = 0; const char *vkey = lua_tolstring(L, -2, &klen); - if (klen == JDBIDKEYNAMEL && !strcmp(JDBIDKEYNAME, vkey)) { //root level OID as string + if (key == NULL && klen == JDBIDKEYNAMEL && !strcmp(JDBIDKEYNAME, vkey)) { //root level OID as string //pack OID as type table lua_push_bsontype_table(L, BSON_OID); //+type table lua_pushvalue(L, -2); //dup oid on stack diff --git a/luaejdb/test/t1.lua b/luaejdb/test/t1.lua index 60a5bd6..f57a979 100644 --- a/luaejdb/test/t1.lua +++ b/luaejdb/test/t1.lua @@ -10,8 +10,7 @@ assert(type(ejdb) == "table") local Q = ejdb.Q local db = ejdb:open("testdb"); -local q = Q("name", "Andy"):F("age"):Gt(20):Lt(40):F("score"):In({11, 22.12333, 1362835380447, db.toNull()}):Max(232); ---local q = Q("name", "Andy"); +local q = Q("name", "Andy"):F("_id"):Eq("510f7fa91ad6270a00000000"):F("age"):Gt(20):Lt(40):F("score"):In({11, 22.12333, 1362835380447, db.toNull()}):Max(232); print(inspect(q._oarr)) print(db:find("mycoll", q)) -- 2.7.4