forked from francois/mongo_explorer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MECursor.m
87 lines (70 loc) · 2.16 KB
/
MECursor.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
//
// MECursor.m
// Mongo Explorer
//
// Created by François Beausoleil on 10-06-19.
// Copyright 2010 Solutions Technologiques Internationales. All rights reserved.
//
#import "MECursor.h"
#import "MECollection.h"
#import "MEConnection-Private.h"
#import "MEUtils.h"
#import "MEDocument.h"
@implementation MECursor
@synthesize collection, params, skipCount, returnCount, order;
-(id)initWithCollection:(MECollection *)aCollection query:(NSDictionary *)aDict {
if (![super init]) return nil;
self.collection = aCollection;
self.params = aDict;
self.skipCount = 0;
self.returnCount = UINT32_MAX;
return self;
}
-(void)dealloc {
if (cursor) {
mongo_cursor_destroy(cursor);
cursor = NULL;
}
self.collection = nil;
self.params = nil;
[super dealloc];
}
-(MECursor *)skip:(NSUInteger)number {
self.skipCount = number;
return self;
}
-(MECursor *)limit:(NSUInteger)number {
self.returnCount = number;
return self;
}
-(MECursor *)order:(NSDictionary *)ordering {
self.order = ordering;
return self;
}
-(NSArray *)documents {
NSArray *results = [self allObjects];
return results;
}
-(void)ensureCursor {
if (cursor) return;
int nToReturn = self.returnCount < 0 ? self.returnCount : -1 * self.returnCount;
NSLog(@"-[MECursor ensureCursor] (namespace=%@ query=%@ skipCount=%@ returnCount=%d)", self.collection.namespace, self.params, self.skipCount, nToReturn);
cursor = [self.collection.connection cursorForNamespace:self.collection.namespace
query:self.params
fields:nil
skipCount:self.skipCount
returnCount:nToReturn];
}
-(id)nextObject {
[self ensureCursor];
if (mongo_cursor_next(cursor)) {
return [[[MEDocument alloc] initWithCollection:self.collection
info:[MEUtils dictionaryFromBson:&cursor->current]
connection:self.collection.connection] autorelease];
} else {
mongo_cursor_destroy(cursor);
cursor = NULL;
return nil;
}
}
@end