From 410272c44d57402dd6076981d970eaca7cd3c178 Mon Sep 17 00:00:00 2001 From: Fedor Yudanov Date: Tue, 2 Apr 2013 19:36:58 +0700 Subject: [PATCH] #50 - advanced tests (test3) --- rbejdb/Makefile | 1 + rbejdb/src/rbbson.c | 6 ++++++ rbejdb/src/rbejdb.c | 29 ++++++++++++++++++++++++++--- rbejdb/test/t2.rb | 20 ++++++++++++++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/rbejdb/Makefile b/rbejdb/Makefile index 8ea0edd..726fdfe 100644 --- a/rbejdb/Makefile +++ b/rbejdb/Makefile @@ -2,6 +2,7 @@ all: build check build: + export CFLAGS=-g ruby extconf.rb make -C ./build make install -C ./build diff --git a/rbejdb/src/rbbson.c b/rbejdb/src/rbbson.c index c23759c..eea849d 100644 --- a/rbejdb/src/rbbson.c +++ b/rbejdb/src/rbbson.c @@ -84,6 +84,12 @@ int iterate_key_values_callback(VALUE key, VALUE val, VALUE bsonWrap) { rb_raise(rb_eRuntimeError, "Cannot convert ruby data object to bson"); } break; + case T_REGEXP: { + VALUE regexp = rb_funcall(val, rb_intern("inspect"), 0); + VALUE source = rb_funcall(val, rb_intern("source"), 0); + bson_append_regex(b, attrName, StringValuePtr(source), StringValuePtr(regexp) + 2 + strlen(StringValuePtr(source))); // "2" for skipping "//" :) + } + break; case T_TRUE: bson_append_bool(b, attrName, 1); break; diff --git a/rbejdb/src/rbejdb.c b/rbejdb/src/rbejdb.c index 022bba0..1bb2b6a 100644 --- a/rbejdb/src/rbejdb.c +++ b/rbejdb/src/rbejdb.c @@ -40,8 +40,17 @@ VALUE ejdbResultsClass; VALUE get_hash_option(VALUE hash, const char* opt) { Check_Type(hash, T_HASH); - VALUE symbol = ID2SYM(rb_intern(opt)); - VALUE res = rb_hash_aref(hash, symbol); + VALUE res = Qnil; + + ID symId = rb_intern(opt); + + if (symId) { + VALUE symbol = ID2SYM(symId); + if (TYPE(symbol) == T_SYMBOL) { + res = rb_hash_aref(hash, symbol); + } + } + return !NIL_P(res) ? res : rb_hash_aref(hash, rb_str_new2(opt)); } @@ -218,6 +227,17 @@ VALUE EJDB_load(VALUE self, VALUE collName, VALUE rboid) { return bson_to_ruby(bs); } + +VALUE prepare_query_hints(VALUE hints) { + VALUE res = rb_hash_new(); + VALUE orderby = get_hash_option(hints, "orderby"); + if (!NIL_P(orderby)) { + rb_hash_aset(res, rb_str_new2("$orderby"), orderby); + } + return res; +} + + VALUE EJDB_find(int argc, VALUE* argv, VALUE self) { VALUE collName; VALUE q; @@ -242,7 +262,10 @@ VALUE EJDB_find(int argc, VALUE* argv, VALUE self) { bson* qbson; ruby_to_bson(q, &qbson, RUBY_TO_BSON_AS_QUERY); - EJQ *ejq = ejdbcreatequery(ejdb, qbson, NULL, 0, NULL); + bson* hintsbson = NULL; + ruby_to_bson(prepare_query_hints(hints), &hintsbson, RUBY_TO_BSON_AS_QUERY); + + EJQ *ejq = ejdbcreatequery(ejdb, qbson, NULL, 0, hintsbson); int count; int qflags = 0; diff --git a/rbejdb/test/t2.rb b/rbejdb/test/t2.rb index f436a62..85bf041 100644 --- a/rbejdb/test/t2.rb +++ b/rbejdb/test/t2.rb @@ -87,4 +87,24 @@ class EJDBTestUnit < Test::Unit::TestCase assert_equal(2, results.to_a.length) end + def test_ejdb3_test_query2 + assert_not_nil $jb + assert $jb.is_open? + + results = $jb.find("parrots", {:name => /(grenny|bounty)/i}, {:orderby => {:name => 1}}) + + assert_not_nil results + assert_equal(2, results.to_a.length) + + results.each_with_index { |rv, index| + if index == 0 + assert_equal("Bounty", rv["name"]) + assert_equal("Cockatoo", rv["type"]) + assert_equal(false, rv["male"]) + assert_equal(15, rv["age"]) + assert_equal($now.inspect, rv["birthdate"].inspect) + assert_equal("sugar cane", rv["likes"].join(",")) + end + } + end end -- 2.7.4