Permalink
Browse files

auto-generate _id values for inserted objects, this lets us return an…

…d use them immediately.

also added error message when connection fails
  • Loading branch information...
1 parent 5509a71 commit b873e6b3a04bd63c7724c3bf055b29c846dd2e8d Tim Burks committed Sep 26, 2010
Showing with 22 additions and 6 deletions.
  1. +4 −2 objc/NuBSON.h
  2. +8 −1 objc/NuBSON.m
  3. +2 −2 objc/NuMongoDB.h
  4. +7 −1 objc/NuMongoDB.m
  5. +1 −0 src/mongo.c
View
@@ -48,15 +48,17 @@ limitations under the License.
bson_oid_t oid;
}
+/*! Create a new and unique object ID. */
++ (NuBSONObjectID *) objectID;
+
@end
bson *bson_for_object(id object);
-@interface NSData (NuBSON)
+@interface NSData (NuBSON)
- (NSMutableDictionary *) BSONValue;
@end
@interface NSDictionary (NuBSON)
- (NSData *) BSONRepresentation;
@end
-
View
@@ -6,6 +6,14 @@ - (NuBSON *) initWithBSON:(bson) b;
@end
@implementation NuBSONObjectID
+
++ (NuBSONObjectID *) objectID
+{
+ bson_oid_t oid;
+ bson_oid_gen(&oid);
+ return [[[NuBSONObjectID alloc] initWithObjectIDPointer:&oid] autorelease];
+}
+
- (id) initWithString:(NSString *) s
{
if (self = [super init]) {
@@ -293,7 +301,6 @@ void add_bson_to_object(bson_iterator it, id object)
- (NSMutableDictionary *) dictionaryValue
{
id object = [NSMutableDictionary dictionary];
-
bson_iterator it;
bson_iterator_init(&it, bsonValue.data);
add_bson_to_object(it, object);
View
@@ -75,8 +75,8 @@ limitations under the License.
- (NSMutableArray *) findArray:(id) query inCollection:(NSString *) collection returningFields:(id) fields numberToReturn:(int) nToReturn numberToSkip:(int) nToSkip;
/*! Convenience method that returns search results as a single object. */
- (NSMutableDictionary *) findOne:(id) query inCollection:(NSString *) collection;
-/*! Add an object to a collection. */
-- (void) insertObject:(id) insert intoCollection:(NSString *) collection;
+/*! Add an object to a collection, returning the _id of the new object. */
+- (id) insertObject:(id) insert intoCollection:(NSString *) collection;
/*! Update an object in a collection. insertIfNecessary triggers an "upsert". */
- (void) updateObject:(id) update inCollection:(NSString *) collection withCondition:(id) condition insertIfNecessary:(BOOL) insertIfNecessary updateMultipleEntries:(BOOL) updateMultipleEntries;
/*! Remove an object or objects matching a specified condition. */
View
@@ -88,6 +88,7 @@ - (int) connectWithOptions:(NSDictionary *) options
else {
opts.port = 27017;
}
+ //NSLog(@"connecting to host %s port %d", opts.host, opts.port);
return mongo_connect(conn, &opts);
}
@@ -140,14 +141,19 @@ - (NSMutableDictionary *) findOne:(id) query inCollection:(NSString *) collectio
return result ? [[[[NuBSON alloc] initWithBSON:bsonResult] autorelease] dictionaryValue] : nil;
}
-- (void) insertObject:(id) insert intoCollection:(NSString *) collection
+- (id) insertObject:(id) insert intoCollection:(NSString *) collection
{
+ if (![insert objectForKey:@"_id"]) {
+ [insert setObject:[NuBSONObjectID objectID] forKey:@"_id"];
+ }
bson *b = bson_for_object(insert);
if (b) {
mongo_insert(conn, [collection cStringUsingEncoding:NSUTF8StringEncoding], b);
+ return [insert objectForKey:@"_id"];
}
else {
NSLog(@"incomplete insert: insert must not be nil.");
+ return nil;
}
}
View
@@ -125,6 +125,7 @@ static int mongo_connect_helper( mongo_connection * conn ){
}
if ( connect( conn->sock , (struct sockaddr*)&conn->sa , conn->addressSize ) ){
+ printf("failed to connect %s %d\n", conn->left_opts->host, conn->left_opts->port);
return mongo_conn_fail;
}

0 comments on commit b873e6b

Please sign in to comment.