-- ------------ Misc -----------------------
-function strim(s)
+local function strim(s)
local from = s:match("^%s*()")
return from > #s and "" or s:match(".*%S", from)
end
flags = 0
end
local orBsons = {}
- for _, o in ipairs(q:getJoinedORs()) do
- table.insert(orBsons, o.toBSON())
+ local ors = q:getJoinedORs()
+ if ors then
+ for _, o in ipairs(ors) do
+ table.insert(orBsons, o.toBSON())
+ end
end
return self:_find(cname, q:toBSON(), orBsons, q:toHintsBSON(), flags)
end
self._hints = nil -- hints Q
self._omap = {} -- field operations
self._oarr = {} -- resulting array of field operations
+ self._bson = nil -- cached bson
if fname then
self:F(fname, ...)
end
else
table.insert(olist, { op, val })
end
+ self._bson = nil
end
function B:_toOpVal(op, val)
function B:Do(val) return self:_rootOp("$do", val) end
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")
table.insert(self._or, v)
end
function B:toBSON()
- return luaejdb.to_bson(self)
+ if self._bson then
+ return self._bson
+ else
+ self._bson = luaejdb.to_bson(self)
+ return self._bson
+ end
end
luaejdb.B = setmetatable(B, {
#include <lauxlib.h>
#include <math.h>
#include <string.h>
+#include <tcejdb/tcutil.h>
static void lua_push_bson_value(lua_State *L, bson_iterator *it);
static void lua_push_bson_table(lua_State *L, bson_iterator *it);
static void lua_push_bson_array(lua_State *L, bson_iterator *it);
static void lua_to_bson_impl(lua_State *L, int spos, bson *bs);
+static void bson_print_xstr(TCXSTR* xstr, const char *data, int depth);
void lua_init_bson(lua_State *L) {
if (!lua_istable(L, -1)) {
lua_pushcfunction(L, lua_to_bson);
lua_setfield(L, -2, "to_bson");
+ lua_pushcfunction(L, print_bson);
+ lua_setfield(L, -2, "print_bson");
+
+}
+
+int print_bson(lua_State *L) {
+ size_t slen = 0;
+ const void *bsdata = luaL_checklstring(L, lua_gettop(L), &slen);
+ if (slen <= 4 || !bsdata) {
+ return luaL_error(L, "Invalid bson string at argument #1");
+ }
+ TCXSTR *xstr = tcxstrnew();
+ bson_print_xstr(xstr, bsdata, 0);
+ lua_pushstring(L, TCXSTRPTR(xstr));
+ tcxstrdel(xstr);
+ return 1;
}
//-0/+1
lua_val_to_bson(L, NULL, lua_gettop(L), bs, tref);
lua_unref(L, tref);
}
+
+static void bson_print_xstr(TCXSTR* xstr, const char *data, int depth) {
+ bson_iterator i;
+ const char *key;
+ int temp;
+ bson_timestamp_t ts;
+ char oidhex[25];
+ bson scope;
+ bson_iterator_from_buffer(&i, data);
+
+ while (bson_iterator_next(&i)) {
+ bson_type t = bson_iterator_type(&i);
+ if (t == 0)
+ break;
+ key = bson_iterator_key(&i);
+
+ for (temp = 0; temp <= depth; temp++) {
+ tcxstrprintf(xstr, ".");
+ }
+ tcxstrprintf(xstr, "%s(%d)=", key, t);
+ switch (t) {
+ case BSON_DOUBLE:
+ tcxstrprintf(xstr, "%f", bson_iterator_double(&i));
+ break;
+ case BSON_STRING:
+ tcxstrprintf(xstr, "%s", bson_iterator_string(&i));
+ break;
+ case BSON_SYMBOL:
+ tcxstrprintf(xstr, "SYMBOL: %s", bson_iterator_string(&i));
+ break;
+ case BSON_OID:
+ bson_oid_to_string(bson_iterator_oid(&i), oidhex);
+ tcxstrprintf(xstr, "%s", oidhex);
+ break;
+ case BSON_BOOL:
+ tcxstrprintf(xstr, "%s", bson_iterator_bool(&i) ? "true" : "false");
+ break;
+ case BSON_DATE:
+ tcxstrprintf(xstr, "%ld", (long int) bson_iterator_date(&i));
+ break;
+ case BSON_BINDATA:
+ tcxstrprintf(xstr, "BSON_BINDATA");
+ break;
+ case BSON_UNDEFINED:
+ tcxstrprintf(xstr, "BSON_UNDEFINED");
+ break;
+ case BSON_NULL:
+ tcxstrprintf(xstr, "BSON_NULL");
+ break;
+ case BSON_REGEX:
+ tcxstrprintf(xstr, "BSON_REGEX: %s", bson_iterator_regex(&i));
+ break;
+ case BSON_CODE:
+ tcxstrprintf(xstr, "BSON_CODE: %s", bson_iterator_code(&i));
+ break;
+ case BSON_CODEWSCOPE:
+ tcxstrprintf(xstr, "BSON_CODE_W_SCOPE: %s", bson_iterator_code(&i));
+ /* bson_init( &scope ); */ /* review - stepped on by bson_iterator_code_scope? */
+ bson_iterator_code_scope(&i, &scope);
+ tcxstrprintf(xstr, "\n SCOPE: ");
+ tcxstrprintf(xstr, &scope);
+ /* bson_destroy( &scope ); */ /* review - causes free error */
+ break;
+ case BSON_INT:
+ tcxstrprintf(xstr, "%d", bson_iterator_int(&i));
+ break;
+ case BSON_LONG:
+ tcxstrprintf(xstr, "%lld", (uint64_t) bson_iterator_long(&i));
+ break;
+ case BSON_TIMESTAMP:
+ ts = bson_iterator_timestamp(&i);
+ tcxstrprintf(xstr, "i: %d, t: %d", ts.i, ts.t);
+ break;
+ case BSON_OBJECT:
+ case BSON_ARRAY:
+ tcxstrprintf(xstr, "\n");
+ bson_print_xstr(xstr, bson_iterator_value(&i), depth + 1);
+ break;
+ default:
+ fprintf(stderr, "can't print type : %d\n", t);
+ }
+ tcxstrprintf(xstr, "\n");
+ }
+}