forked from francois/mongo_explorer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MECollection.m
101 lines (80 loc) · 2.71 KB
/
MECollection.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// MECollection.m
// Mongo Explorer
//
// Created by François Beausoleil on 10-06-08.
// Copyright 2010 Solutions Technologiques Internationales. All rights reserved.
//
#import "MECollection.h"
#import "MEDatabase.h"
#import "MEConnection.h"
#import "MEConnection-Private.h"
#import "MEDocument.h"
#import "MECursor.h"
#import "MEUtils.h"
#import "MEArray.h"
@implementation MECollection
@synthesize connection, database, fullName, name, array, documentKeys;
-(id)initWithDatabase:(MEDatabase *)aDatabase info:(NSDictionary *)info connection:(MEConnection *)aConnection {
if (![super init]) return nil;
self.connection = aConnection;
self.database = aDatabase;
self.fullName = [info objectForKey:@"name"];
self.name = [[info objectForKey:@"name"] stringByReplacingOccurrencesOfString:[self.database.name stringByAppendingString:@"."] withString:@""];
self.array = [[MEArray alloc] initWithCollection:self];
self.documentKeys = [self buildDocumentKeys];
return self;
}
-(void)dealloc {
self.connection = nil;
self.database = nil;
self.name = nil;
self.array = nil;
self.documentKeys = nil;
[super dealloc];
}
-(NSArray *)reload {
if ([self.connection connect]) return [NSArray array];
NSMutableArray *results = [[NSMutableArray alloc] init];
/* TODO: Reimplement in terms of MECursor and MEDocument */
bson query, fields;
const char* ns = [self.fullName cStringUsingEncoding:NSUTF8StringEncoding];
mongo_cursor *cursor = mongo_find([self.connection mongo_connection], ns, bson_empty(&query), bson_empty(&fields), 0, 0, 0);
while(mongo_cursor_next(cursor)) {
bson_iterator it;
bson_iterator_init(&it, cursor->current.data);
NSDictionary *info = [MEUtils dictionaryFromBsonIterator:&it];
[results addObject:[[[MEDocument alloc] initWithCollection:self info:info connection:self.connection] autorelease]];
}
mongo_cursor_destroy(cursor);
return results;
}
-(NSString *)description {
return self.name;
}
-(NSUInteger)documentsCount {
return [self.connection documentsCountFromCollection:self.name database:[self.database name]];
}
-(MECursor *)find {
return [self find:[NSDictionary dictionary]];
}
-(MECursor *)find:(NSDictionary *)query {
return [[MECursor alloc] initWithCollection:self query:query];
}
-(NSString *)namespace {
return [NSString stringWithFormat:@"%@.%@", self.database.name, self.name];
}
-(NSArray *)buildDocumentKeys {
MECursor *cursor = [self find];
cursor.returnCount = 1;
NSArray *resultSet = [cursor documents];
if ([resultSet count] > 0) {
MEDocument *doc = [resultSet objectAtIndex:0];
NSDictionary *flat = [doc flatView];
[cursor release];
return [flat allKeys];
} else {
return [NSArray array];
}
}
@end