bson_date_t ruby_time_to_bson_internal(VALUE time);
+VALUE bson_date_to_ruby(bson_date_t date);
+
VALUE bsonWrapClass = Qnil;
} else {
rb_raise(rb_eRuntimeError, "Cannot convert ruby data object to bson");
}
+ break;
case T_TRUE:
bson_append_bool(b, attrName, 1);
break;
case BSON_INT:
val = INT2NUM(bson_iterator_int(it));
break;
+ case BSON_DATE:
+ val = bson_date_to_ruby(bson_iterator_date(it));
+ break;
case BSON_OBJECT: {
bson subbson;
bson_iterator_subobject(it, &subbson);
case BSON_ARRAY:
val = bson_array_to_ruby(it);
break;
+ case BSON_NULL:
+ val = Qnil;
+ break;
default:
rb_raise(rb_eRuntimeError, "Cannot convert object from bson: %d", t);
}
return res;
}
+VALUE bson_date_to_ruby(bson_date_t date) {
+ return rb_funcall(rb_path2class("Time"), rb_intern("at"), 1, INT2NUM(date * 1000));
+}
+
-VALUE bson_to_ruby(bson* bsonval) {
+VALUE bson_to_ruby(const bson* bsonval) {
VALUE res = rb_hash_new();
bson_iterator it;
return res;
}
-VALUE bson_oid_to_ruby(bson_oid_t* oid) {
+VALUE bson_oid_to_ruby(const bson_oid_t* oid) {
char oidhex[25];
bson_oid_to_string(oid, oidhex);
return rb_str_new2(oidhex);
}
+
+bson_oid_t ruby_to_bson_oid(VALUE rboid) {
+ bson_oid_t oid;
+ bson_oid_from_string(&oid, StringValuePtr(rboid));
+ return oid;
+}
VALUE EJDB_open(VALUE clazz, VALUE path, VALUE mode) {
- Check_SafeStr(path);
+ SafeStringValue(path);
Check_Type(mode, T_FIXNUM);
VALUE ejdbWrap = Data_Wrap_Struct(clazz, NULL, EJDB_free, ruby_xmalloc(sizeof(RBEJDB)));
}
void EJDB_dropCollection(VALUE self, VALUE collName, VALUE prune) {
- Check_SafeStr(collName);
+ SafeStringValue(collName);
EJDB* ejdb = getEJDB(self);
if (!ejdbrmcoll(ejdb, StringValuePtr(collName), RTEST(prune))) {
rb_scan_args(argc, argv, "11", &collName, &copts);
- Check_SafeStr(collName);
+ SafeStringValue(collName);
EJCOLLOPTS jcopts = {NULL};
if (!NIL_P(copts)) {
rb_raise(rb_eRuntimeError, "Error calling EJDB.save(): need to specify collection name");
}
- EJDB* ejdb = getEJDB(self);
-
VALUE collName = argv[0];
Check_Type(collName, T_STRING);
- VALUE oids = rb_ary_new();
+ EJDB* ejdb = getEJDB(self);
EJCOLL *coll = ejdbcreatecoll(ejdb, StringValuePtr(collName), NULL);
if (!coll) {
raise_ejdb_error(ejdb);
}
+ VALUE oids = rb_ary_new();
int i;
for (i = 1; i < argc; i++) {
VALUE rbobj = argv[i];
}
}
+VALUE EJDB_load(VALUE self, VALUE collName, VALUE rboid) {
+ SafeStringValue(collName);
+
+ EJDB* ejdb = getEJDB(self);
+
+ EJCOLL *coll = ejdbgetcoll(ejdb, StringValuePtr(collName));
+ if (!coll) {
+ raise_ejdb_error(ejdb);
+ }
+
+ bson_oid_t oid = ruby_to_bson_oid(rboid);
+
+ bson *bs = ejdbloadbson(coll, &oid);
+ if (!bs) {
+ raise_ejdb_error(ejdb);
+ }
+
+ return bson_to_ruby(bs);
+}
+
VALUE EJDB_find(int argc, VALUE* argv, VALUE self) {
VALUE collName;
VALUE q;
rb_scan_args(argc, argv, "11", &collName, &q);
- Check_SafeStr(collName);
+ SafeStringValue(collName);
q = !NIL_P(q) ? q :rb_hash_new();
EJDB* ejdb = getEJDB(self);
rb_define_method(ejdbClass, "is_open?", RUBY_METHOD_FUNC(EJDB_is_open), 0);
rb_define_method(ejdbClass, "close", RUBY_METHOD_FUNC(EJDB_close), 0);
rb_define_method(ejdbClass, "save", RUBY_METHOD_FUNC(EJDB_save), -1);
+ rb_define_method(ejdbClass, "load", RUBY_METHOD_FUNC(EJDB_load), 2);
rb_define_method(ejdbClass, "find", RUBY_METHOD_FUNC(EJDB_find), -1);
rb_define_method(ejdbClass, "dropCollection", RUBY_METHOD_FUNC(EJDB_dropCollection), 2);