Skip to content

Commit

Permalink
a query
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Burks committed Apr 24, 2010
1 parent f486f61 commit fc31c9e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
75 changes: 47 additions & 28 deletions objc/NuMongoDB.m
Expand Up @@ -19,6 +19,7 @@ @interface NuBSON : NSObject
@public
bson b;
}

- (NuBSON *) initWithBSON:(bson) bb;
@end

Expand Down Expand Up @@ -57,8 +58,9 @@ - (bson) current
return cursor->current;
}

- (NuBSON *) currentBSON {
return [[[NuBSON alloc] initWithBSON:cursor->current] autorelease];
- (NuBSON *) currentBSON
{
return [[[NuBSON alloc] initWithBSON:cursor->current] autorelease];
}

- (void) dealloc
Expand All @@ -69,8 +71,6 @@ - (void) dealloc

@end



@implementation NuBSON

- (NuBSON *) initWithBSON:(bson) bb
Expand Down Expand Up @@ -121,21 +121,28 @@ void add_object_to_bson_buffer(bson_buffer *bb, id key, id object)
}
}

- (NuBSON *) initWithObject:(id) object
- (NuBSON *) initWithObject:(id) object name:(id) name
{
bson b;
bson_buffer bb;
bson_buffer_init(& bb );

bson_append_new_oid(&bb, "_id" );

add_object_to_bson_buffer(&bb, @"top", object);
if (name == nil) {
bson_append_new_oid(&bb, "_id" );
add_object_to_bson_buffer(&bb, @"top", object);
} else
add_object_to_bson_buffer(&bb,name, object);

bson_from_buffer(&b, &bb);

return [self initWithBSON:b];
}

- (NuBSON *) initWithObject:(id) object
{
return [self initWithObject:object name:nil];
}

void dump_bson_iterator(bson_iterator it, const char *indent)
{
bson_iterator it2;
Expand Down Expand Up @@ -198,7 +205,7 @@ void add_bson_to_object(bson_iterator it, id object)

NSString *key = [[[NSString alloc] initWithCString:bson_iterator_key(&it) encoding:NSUTF8StringEncoding] autorelease];

id value = nil;
id value = nil;

char hex_oid[25];

Expand Down Expand Up @@ -232,15 +239,17 @@ void add_bson_to_object(bson_iterator it, id object)
fprintf(stderr, "(type %d)\n", bson_iterator_type(&it));
break;
}
if (value) {
if ([object isKindOfClass:[NSDictionary class]]) {
[object setObject:value forKey:key];
} else if ([object isKindOfClass:[NSArray class]]) {
[object addObject:value];
} else {
NSLog(@"we don't know how to add to %@", object);
}
}
if (value) {
if ([object isKindOfClass:[NSDictionary class]]) {
[object setObject:value forKey:key];
}
else if ([object isKindOfClass:[NSArray class]]) {
[object addObject:value];
}
else {
NSLog(@"we don't know how to add to %@", object);
}
}
}
}

Expand Down Expand Up @@ -279,18 +288,28 @@ - (BOOL) connect
return YES;
}

- (NuMongoDBCursor *) find
- (NuMongoDBCursor *) find:(id) query
{
const char *col = "c.simple";
const char *ns = "test.c.simple";
bson b;

mongo_cursor *cursor = mongo_find(conn, ns, bson_empty(&b), 0, 0, 0, 0 );
return [[[NuMongoDBCursor alloc] initWithCursor:cursor] autorelease];
if (query) {
NuBSON *queryBSON = [[[NuBSON alloc] initWithObject:[query objectForKey:@"$where"] name:@"$where"] autorelease];
[queryBSON dump];

mongo_cursor *cursor = mongo_find(conn, ns, &(queryBSON->b), 0, 0, 0, 0 );
return [[[NuMongoDBCursor alloc] initWithCursor:cursor] autorelease];
}
else {
bson b;
mongo_cursor *cursor = mongo_find(conn, ns, bson_empty(&b), 0, 0, 0, 0 );
return [[[NuMongoDBCursor alloc] initWithCursor:cursor] autorelease];
}
}

- (void) insert:(NuBSON *) bson {
mongo_insert(conn, ns, &(bson->b));
- (void) insert:(NuBSON *) bson
{
mongo_insert(conn, ns, &(bson->b));
}

- (BOOL) resetDatabase
Expand Down Expand Up @@ -350,20 +369,20 @@ - (void) loadDB
NuBSON *bson = [[[NuBSON alloc] initWithObject:object] autorelease];
[bson dump];

[self insert:bson];
[self insert:bson];
}

- (void) readDB
{
// read them out of the database
NuMongoDBCursor *cursor = [self find];
NuMongoDBCursor *cursor = [self find:nil];

while ([cursor next]) {
NuBSON *bson = [[[NuBSON alloc] initWithBSON:[cursor current]] autorelease];
[bson dump];

id object = [bson objectValue];
NSLog(@"%@", object);
id object = [bson objectValue];
NSLog(@"%@", object);
}
}

Expand Down
4 changes: 3 additions & 1 deletion run.nu
Expand Up @@ -21,10 +21,12 @@
(set bson ((NuBSON alloc) initWithObject:object))
(mongo insert:bson)))

(set cursor (mongo find))
(set cursor (mongo find:(dict $where:"this.top.i < 4")))

(while (cursor next)
(set bson (cursor currentBSON))
(set object (bson objectValue))
(puts (object description)))

(puts "ok")

0 comments on commit fc31c9e

Please sign in to comment.