-CFLAGS=-g -O0 -fPIC -std=c99 -Wall -D_GNU_SOURCE
build:
- luarocks --pack-binary-rock CFLAGS='$(CFLAGS)' make
+ luarocks --pack-binary-rock make
-check: build
+build-dbg:
+ luarocks --pack-binary-rock CFLAGS='-g -O0 -fPIC -std=c99 -Wall' make
+
+check: build-dbg
make -C ./test
-check-valgrind: build
+check-valgrind: build-dbg
make RUNCMD="valgrind --tool=memcheck --leak-check=full --error-exitcode=1" check -C ./test
clean:
make -C ./test clean
-.PHONY: build test clean
+.PHONY: build build-dbg check check-valgrind clean
function DB:find(cname, q, ...)
assert(getmetatable(q) == mtBObj, "Query object must be instance of 'luaejdb.B' class `q = luaejdb.B()`")
- local flags = ...
- if (type(flags) ~= "number") then
- flags = 0
- end
+ local sflags = ...
local orBsons = {}
local ors = q:getJoinedORs()
if ors then
for _, o in ipairs(ors) do
- table.insert(orBsons, o.toBSON())
+ table.insert(orBsons, o:toBSON())
end
end
- return self:_find(cname, q:toBSON(), orBsons, q:toHintsBSON(), flags)
+ return self:_find(cname, q:toBSON(), orBsons, q:toHintsBSON(), sflags)
+end
+
+
+function DB:findOne(cname, q, ...)
+
+end
+
+function DB:count(cname, q, sflags, ...)
+ if type(sflags) ~= "string" then
+ sflags = "c"
+ else
+ sflags = "c" .. sflags --append count flag
+ end
+ local _, count, log = self:find(cname, q, sflags, ...)
+ return count, log
end
+
-- ------- EJDB Query -------------
function B:_init(fname, ...)
function B:Or(...)
self._or = self._or or {}
for i, v in ipairs({ ... }) do
- assert(getmetatable(v) == mtBObj, "Each 'or' argument must be instance of 'luaejdb.B' class")
+ assert(getmetatable(v) == mtBObj, "Each 'Or()' argument must be instance of 'luaejdb.B' class")
table.insert(self._or, v)
end
return self
void lua_push_bson_array(lua_State *L, bson_iterator *it) {
bson_type bt;
lua_push_bsontype_table(L, BSON_ARRAY);
- for (int i = 1; (bt = bson_iterator_next(it)) != BSON_EOO; ++i) {
+ int i;
+ for (i = 1; (bt = bson_iterator_next(it)) != BSON_EOO; ++i) {
lua_push_bson_value(L, it);
lua_rawseti(L, -2, i);
}
}
if (array) {
if (key) bson_append_start_array(bs, key);
- for (int i = 1; i <= len; ++i, lua_pop(L, 1)) {
+ int i;
+ for (i = 1; i <= len; ++i, lua_pop(L, 1)) {
lua_rawgeti(L, vpos, i);
bson_numstrn(nbuf, TCNUMBUFSIZ, (int64_t) i);
lua_val_to_bson(L, nbuf, -1, bs, tref);
int ipos = lua_gettop(L);
size_t ilen = lua_objlen(L, ipos);
lua_checkstack(L, 2);
- for (size_t i = 1; i <= ilen; ++i, lua_pop(L, 1)) {
+ size_t i;
+ for (i = 1; i <= ilen; ++i, lua_pop(L, 1)) {
lua_rawgeti(L, ipos, i);
//gettop == 3
if (!lua_istable(L, -1)) continue;
size_t jlen = lua_objlen(L, jpos);
lua_checkstack(L, 3);
bool wrapped = false;
- for (size_t j = 1; j <= jlen; ++j, lua_pop(L, 1)) {
+ size_t j;
+ for (j = 1; j <= jlen; ++j, lua_pop(L, 1)) {
lua_rawgeti(L, jpos, j);
if (j == 1) {
fname = strdup(lua_tostring(L, -1));
}
}
tclistsort(keys);
- for (int i = 0; i < TCLISTNUM(keys); ++i) {
+ int i;
+ for (i = 0; i < TCLISTNUM(keys); ++i) {
int vkeysz = TCLISTVALSIZ(keys, i);
const char *vkey = TCLISTVALPTR(keys, i);
lua_pushlstring(L, vkey, vkeysz);
sources = {"luaejdb.c", "luabson.c"},
libraries = {"tcejdb"} ,
incdirs = {"$(LIBTCEJDB_INCDIR)"},
- libdirs = {"$(LIBTCEJDB_LIBDIR)"}
+ libdirs = {"$(LIBTCEJDB_LIBDIR)"},
+ defines = {"_GNU_SOURCE"}
}
}
}
const char *qbsbuf = luaL_checkstring(L, 3); //Query bson
luaL_checktype(L, 4, LUA_TTABLE); //or joined
const char *hbsbuf = luaL_checkstring(L, 5); //Hints bson
- int qflags = luaL_checkinteger(L, 6);
+ int qflags = 0;
+ if (lua_isstring(L, 6)) {
+ const char *sflags = lua_tostring(L, 6);
+ int i;
+ for (i = 0; sflags[i] != '\0'; ++i) {
+ switch (sflags[i]) {
+ case 'c':
+ qflags |= JBQRYCOUNT;
+ break;
+ case 'l':
+ qflags |= JBQRYLOG;
+ break;
+ }
+ }
+ }
+ size_t i;
bson oqarrstack[8]; //max 8 $or bsons on stack
bson *oqarr = NULL;
bson qbson = {NULL};
size_t orsz = lua_objlen(L, 4);
oqarr = ((orsz <= 8) ? oqarrstack : (bson*) tcmalloc(orsz * sizeof (bson)));
- for (size_t i = 0; i < orsz; ++i) {
+
+ for (i = 0; i < orsz; ++i) {
const void *bsdata;
size_t bsdatasz;
lua_rawgeti(L, 4, i + 1);
finish:
- for (size_t i = 0; i < orsz; ++i) {
+ for (i = 0; i < orsz; ++i) {
bson_destroy(&oqarr[i]);
}
if (oqarr && oqarr != oqarrstack) {
} else if (lua_isstring(L, 2)) {
mode = 0;
const char* om = lua_tostring(L, 2);
- for (int i = 0; om[i] != '\0'; ++i) {
+ int i;
+ for (i = 0; om[i] != '\0'; ++i) {
mode |= JBOREADER;
switch (om[i]) {
case 'w':
assert(err == "Cursor closed")
+local qres, count, log = db:find("mycoll", Q("foo", "bar"), "l")
+assert(type(log) == "string")
+assert(qres ~= nil)
+assert(#qres == 1)
+assert(count == 1)
+assert(log:find("COUNT ONLY: NO"))
+
+local qres, count, log = db:find("mycoll", Q("foo", "bar"), "lc") --COUNT only
+assert(type(log) == "string")
+assert(qres == nil)
+assert(count == 1)
+assert(log:find("COUNT ONLY: YES"))
+
+--db:save("mycoll", { foo = "bar6", k2 = "v2" });
+local count, log = db:count("mycoll", Q():Or(Q("foo", "bar"), Q("foo", "bar6")));
+assert(count == 2)
+assert(log == nil)
+
+local count, log = db:count("mycoll", Q():Or(Q("foo", "bar"), Q("foo", "bar6")), "l");
+assert(count == 2)
+assert(log:find("COUNT ONLY: YES"))
+
+
db:close()
collectgarbage()