Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added methods for dropping a database and getting collection names.

  • Loading branch information...
commit ac21990b09b81e5aebbb1ead5d9f61f694fef041 1 parent 58ac3d6
Tim Burks authored
26  objc/NuMongoDB.m
@@ -68,7 +68,6 @@ - (NSMutableArray *) arrayValueWithLimit:(int) limit
68 68
 
69 69
 @end
70 70
 
71  
-
72 71
 @implementation NuMongoDB
73 72
 
74 73
 - (int) connectWithOptions:(NSDictionary *) options
@@ -181,6 +180,10 @@ - (id) runCommand:(id) command inDatabase:(NSString *) database
181 180
     return result ? [[[[NuBSON alloc] initWithBSON:bsonResult] autorelease] dictionaryValue] : nil;
182 181
 }
183 182
 
  183
+- (BOOL) dropDatabase:(NSString *) database {
  184
+	return mongo_cmd_drop_db(conn, [database cStringUsingEncoding:NSUTF8StringEncoding]);
  185
+}
  186
+
184 187
 - (BOOL) dropCollection:(NSString *) collection inDatabase:(NSString *) database
185 188
 {
186 189
     return mongo_cmd_drop_collection(conn,
@@ -189,6 +192,27 @@ - (BOOL) dropCollection:(NSString *) collection inDatabase:(NSString *) database
189 192
         NULL);
190 193
 }
191 194
 
  195
+- (id) getCollectionNamesInDatabase:(NSString *) database
  196
+{
  197
+	NSArray *names = [self findArray:nil inCollection:[database stringByAppendingString:@".system.namespaces"]];
  198
+	NSMutableArray *result = [NSMutableArray array];
  199
+	for (int i = 0; i < [names count]; i++) {
  200
+		id name = [[[names objectAtIndex:i] objectForKey:@"name"] 
  201
+					stringByReplacingOccurrencesOfString:[database stringByAppendingString:@"."]
  202
+					withString:@""];
  203
+		NSRange match = [name rangeOfString:@".$_id_"];
  204
+		if (match.location != NSNotFound) {
  205
+			continue;
  206
+		}
  207
+		match = [name rangeOfString:@"system.indexes"];
  208
+		if (match.location != NSNotFound) {
  209
+			continue;
  210
+		}
  211
+		[result addObject:name];
  212
+	}
  213
+	return result;
  214
+}
  215
+
192 216
 - (BOOL) ensureCollection:(NSString *) collection hasIndex:(NSObject *) key withOptions:(int) options
193 217
 {
194 218
     bson output;
23  test/test_access.nu
@@ -147,6 +147,29 @@
147 147
         
148 148
         (mongo dropCollection:collection inDatabase:database))
149 149
      
  150
+     (- testCollections is
  151
+        (set database "sample")
  152
+        (set mongo (NuMongoDB new))
  153
+        (set connected (mongo connectWithOptions:connection))
  154
+        (assert_equal 0 connected)
  155
+        (unless (eq connected 0)
  156
+                (puts "could not connect to database. Is mongod running?")
  157
+                (return))
  158
+        (mongo dropDatabase:database)
  159
+        (set collectionNames (array "a" "b" "c" "d"))
  160
+        (collectionNames each:
  161
+             (do (collectionName)
  162
+                 (mongo insertObject:(dict x:0) intoCollection:(+ database "." collectionName))))
  163
+        (set actualCollectionNames (mongo getCollectionNamesInDatabase:database))
  164
+        (collectionNames each:
  165
+             (do (collectionName)
  166
+                 (assert_true (actualCollectionNames containsObject:collectionName))))
  167
+        (mongo dropDatabase:database)
  168
+        (set actualCollectionNames (mongo getCollectionNamesInDatabase:database))
  169
+        (collectionNames each:
  170
+             (do (collectionName)
  171
+                 (assert_false (actualCollectionNames containsObject:collectionName)))))
  172
+     
150 173
      (- testUpdate is
151 174
         (set database "test")
152 175
         (set collection "update")
14  test/test_bson.nu
@@ -8,7 +8,13 @@
8 8
 (class TestBSON is NuTestCase
9 9
      
10 10
      (- testRoundtrip is
11  
-        (set x (dict a:"a" b:"bee" c:123 d:(NSDate date) e:123.456 f:(dict a:(array 1 2 3))))
  11
+        (set x (dict a:"a"
  12
+                     b:"bee"
  13
+                     c:123
  14
+                     d:(NSDate date)
  15
+                     e:123.456
  16
+                     f:(dict a:(array 1 2 3))
  17
+                     g:(NSData dataWithContentsOfFile:"mongoleaf.png")))
12 18
         (set data (x BSONRepresentation))
13 19
         (set y (data BSONValue))
14 20
         (assert_equal (x a:) (y a:))
@@ -20,15 +26,15 @@
20 26
              0.001)
21 27
         (assert_equal (x e:) (y e:))
22 28
         (assert_equal (x f:) (y f:))
  29
+        (assert_equal (x g:) (y g:))
23 30
         
24 31
         (set data (y BSONRepresentation))
25 32
         (set z (data BSONValue))
  33
+        
26 34
         (assert_equal (y a:) (z a:))
27 35
         (assert_equal (y b:) (z b:))
28 36
         (assert_equal (y c:) (z c:))
29 37
         (assert_equal (y d:) (z d:))
30 38
         (assert_equal (y e:) (z e:))
31 39
         (assert_equal (y f:) (z f:))
32  
-        
33  
-        
34  
-        ))
  40
+        (assert_equal (y g:) (z g:))))

0 notes on commit ac21990

Please sign in to comment.
Something went wrong with that request. Please try again.