return bson_to_ruby(rbbson->bsonval);
}
-VALUE bson_to_ruby_ensure_destroy_wrapper(VALUE bsonWrap, VALUE exception) {
+VALUE bson_to_ruby_ensure_del_wrapper(VALUE bsonWrap, VALUE exception) {
RBBSON* rbbson;
Data_Get_Struct(bsonWrap, RBBSON, rbbson);
if (rbbson->bsonval) {
- bson_destroy(rbbson->bsonval);
+ bson_del(rbbson->bsonval);
}
rbbson->bsonval = NULL;
return Qnil;
}
-VALUE bson_to_ruby_ensure_destroy(bson* bsonval) {
+VALUE bson_to_ruby_ensure_del(bson* bsonval) {
VALUE bsonWrap = createBsonWrap(bsonval);
- return rb_ensure(bson_to_ruby_wrapper, bsonWrap, bson_to_ruby_ensure_destroy_wrapper, bsonWrap);
+ return rb_ensure(bson_to_ruby_wrapper, bsonWrap, bson_to_ruby_ensure_del_wrapper, bsonWrap);
}
VALUE bson_oid_to_ruby(const bson_oid_t* oid) {
bson_oid_t oid;
if (!ejdbsavebson2(coll, bsonval, &oid, merge)) {
- bson_destroy(bsonval);
+ bson_del(bsonval);
raise_ejdb_error(ejdb);
}
- bson_destroy(bsonval);
+ bson_del(bsonval);
VALUE roid = bson_oid_to_ruby(&oid);
rb_ary_push(oids, roid);
bson_oid_t oid = ruby_to_bson_oid(rboid);
bson *bs = ejdbloadbson(coll, &oid);
- return bs ? bson_to_ruby_ensure_destroy(bs) : nil_or_raise_ejdb_error(ejdb);
+ return bs ? bson_to_ruby_ensure_del(bs) : nil_or_raise_ejdb_error(ejdb);
}
/*
void EJDB_remove_query_internal(RBEJDB_QUERY* rbquery) {
if (rbquery->qbson) {
- bson_destroy(rbquery->qbson);
+ bson_del(rbquery->qbson);
rbquery->qbson = NULL;
}
if (rbquery->hintsbson) {
- bson_destroy(rbquery->hintsbson);
+ bson_del(rbquery->hintsbson);
rbquery->hintsbson = NULL;
}
if (rbquery->orarrbson) {
bson* orqbson;
ruby_to_bson(orq, &orqbson, RUBY_TO_BSON_AS_QUERY);
bson_copy(rbquery->orarrbson + (i++), orqbson);
- bson_destroy(orqbson);
+ bson_del(orqbson);
rbquery->orarrlng++;
}
return EJDB_find(4, findargs, self);
}
+/**
+ * call-seq:
+ * ejdb.command(cmd) -> Hash
+ *
+ * Execute the ejdb database command.
+ *
+ * Supported commands:
+ *
+ * 1) Exports database collections data. See ejdbexport() method.
+ *
+ * :export => {
+ * :path => string, //Exports database collections data
+ * :cnames => [string array]|nil, //List of collection names to export
+ * :mode => int|nil //Values: null|`JBJSONEXPORT` See ejdbexport() method
+ * }
+ *
+ * Command response:
+ * {
+ * "log" => string, //Diagnostic log about executing this command
+ * "error" => string|nil, //ejdb error message
+ * "errorCode" => int|0, //ejdb error code
+ * }
+ *
+ * 2) Imports previously exported collections data into ejdb.
+ *
+ * :import => {
+ * :path => string //The directory path in which data resides
+ * :cnames => [string array]|nil, //List of collection names to import
+ * :mode => int|nil //Values: null|`JBIMPORTUPDATE`|`JBIMPORTREPLACE` See ejdbimport() method
+ * }
+ *
+ * Command response:
+ * {
+ * "log" => string, //Diagnostic log about executing this command
+ * "error" => string|nil, //ejdb error message
+ * "errorCode" => int|0, //ejdb error code
+ * }
+ *
+ * - +cmd+ (Hash or Object) - command spec.
+ *
+ * Returns allocated command response object as hash.
+ */
+VALUE EJDB_command(VALUE self, VALUE command) {
+ EJDB* ejdb = getEJDB(self);
+
+ bson* bsoncmd;
+ ruby_to_bson(command, &bsoncmd, 0);
+
+ bson* res = ejdbcommand(ejdb, bsoncmd);
+ bson_del(bsoncmd);
+
+ if (!res) {
+ raise_ejdb_error(ejdb);
+ }
+
+ return bson_to_ruby_ensure_del(res);
+}
VALUE EJDB_set_index_internal(VALUE self, VALUE collName, VALUE fpath, int flags) {
rb_define_method(ejdbClass, "update", RUBY_METHOD_FUNC(EJDB_update), -1);
rb_define_method(ejdbClass, "remove", RUBY_METHOD_FUNC(EJDB_remove), 2);
+ rb_define_method(ejdbClass, "command", RUBY_METHOD_FUNC(EJDB_command), 1);
+
rb_define_method(ejdbClass, "drop_collection", RUBY_METHOD_FUNC(EJDB_drop_collection), -1);
rb_define_method(ejdbClass, "ensure_collection", RUBY_METHOD_FUNC(EJDB_ensure_collection), -1);
end
- def test_ejdbh_different_hacks
+ def test_ejdbh_command
+ assert_not_nil $jb
+ assert $jb.open?
+
+ cmdret = $jb.command(:ping => {})
+
+ assert_not_nil cmdret
+ assert_equal("pong", cmdret["log"])
+
+ puts __method__.inspect + " has passed successfull"
+ end
+
+
+ def test_ejdbi_different_hacks
assert_nil $jb.load("monsters", "111")
assert_nil $jb.load("parrots", "111")
assert_nil $jb.load("parrots", "")
puts __method__.inspect + " has passed successfull"
end
- def test_ejdbi_internal_rbejdb_classes
+
+ def test_ejdbj_internal_rbejdb_classes
assert_raise(RuntimeError) {
EJDB.new
}
puts __method__.inspect + " has passed successfull"
end
- def test_ejdbj_close
+
+ def test_ejdbk_close
assert_not_nil $jb
assert $jb.open?