-
Notifications
You must be signed in to change notification settings - Fork 0
/
DRLite.m
255 lines (225 loc) · 10.6 KB
/
DRLite.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// DRLite.m
// DumbRecord
//
// Created by Jerome Poichet on 5/13/09.
// Copyright 2009 Jerome Poichet. All rights reserved.
//
#import "DRLite.h"
static NSError *generate_error(int code, NSString *str)
{
NSDictionary *dict = [NSDictionary dictionaryWithObject:str forKey:NSLocalizedDescriptionKey];
return [NSError errorWithDomain:@"com.frencaze.DumbRecord.ErrorDomain" code:code userInfo:dict];
}
static BOOL __drlite_verbose = false;
@implementation DRLite
@synthesize databasePath = _databasePath;
@synthesize created = _created;
+ (DRLite *) liteWithDatabase: (NSString *) database
{
return [[[DRLite alloc] initWithDatabase: database] autorelease];
}
- (id) initWithDatabase: (NSString *) database
{
if (self = [super init]) {
_databasePath = [database retain];
NSFileManager *fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath: _databasePath] == YES) {
_created = NO;
} else {
_created = YES;
}
if(sqlite3_open([_databasePath UTF8String], &_database) != SQLITE_OK) {
}
}
return self;
}
- (void) dealloc
{
if (_database) {
sqlite3_close(_database);
}
[_databasePath release];
[super dealloc];
}
- (NSArray *) query: (NSString *) query withArguments: (NSArray *) args andError: (NSError **) error
{
NSMutableArray *results = nil;
sqlite3_stmt *stmt;
if (__drlite_verbose) NSLog(@"%@", query);
if (!_database) {
if (error) {
*error = generate_error(100, @"Could not open database");
}
return results;
}
@synchronized(self) {
if(sqlite3_prepare_v2(_database, [query UTF8String], -1, &stmt, NULL) != SQLITE_OK) {
if (error) {
*error = generate_error(sqlite3_errcode(_database), [NSString stringWithCString: sqlite3_errmsg(_database) encoding: NSUTF8StringEncoding]);
}
return results;
}
if (args) {
int i = 1;
for(id arg in args) {
if ([[arg className] isEqualToString: @"NSCFNumber"]) {
NSNumber *aNumber = arg;
if((strcasecmp([aNumber objCType], @encode(int))) == 0) {
if (SQLITE_OK != sqlite3_bind_int(stmt, i, [arg intValue])) {
if (error) {
*error = generate_error(201, [NSString stringWithFormat: @"Could not bind NSNumber argument %d", i]);
}
return results;
}
} else if((strcasecmp([aNumber objCType], @encode(double))) == 0) {
if (SQLITE_OK != sqlite3_bind_double(stmt, i, [arg doubleValue])) {
if (error) {
*error = generate_error(202, [NSString stringWithFormat: @"Could not bind NSNumber argument %d", i]);
}
return results;
}
} else if((strcasecmp([aNumber objCType], @encode(long))) == 0 || (strcasecmp([aNumber objCType], @encode(long long))) == 0) {
if (SQLITE_OK != sqlite3_bind_double(stmt, i, [arg longValue])) {
if (error) {
*error = generate_error(203, [NSString stringWithFormat: @"Could not bind NSNumber argument %d", i]);
}
return results;
}
} else if((strcasecmp([aNumber objCType], @encode(float))) == 0) {
if (SQLITE_OK != sqlite3_bind_double(stmt, i, [arg floatValue])) {
if (error) {
*error = generate_error(204, [NSString stringWithFormat: @"Could not bind NSNumber argument %d", i]);
}
return results;
}
} else {
if (error) {
*error = generate_error(205, [NSString stringWithFormat: @"Could not bind NSNumber argument %d (%s %s %@)", i, [aNumber objCType], @encode(long long), aNumber]);
}
return results;
}
} else if ([[arg className] isEqualToString: @"NSCFString"]) {
if (SQLITE_OK != sqlite3_bind_text(stmt, i, [arg UTF8String], strlen([arg UTF8String]), NULL)) {
if (error) {
*error = generate_error(206, [NSString stringWithFormat: @"Could not bind NSString argument %d", i]);
}
return results;
}
} else if ([[arg className] isEqualToString: @"NSPathStore2"]) {
if (SQLITE_OK != sqlite3_bind_text(stmt, i, [arg UTF8String], strlen([arg UTF8String]), NULL)) {
if (error) {
*error = generate_error(206, [NSString stringWithFormat: @"Could not bind NSString argument %d", i]);
}
return results;
}
} else if ([[arg className] isEqualToString: @"NSCFBoolean"]) {
if (SQLITE_OK != sqlite3_bind_int(stmt, i, [arg intValue])) {
if (error) {
*error = generate_error(206, [NSString stringWithFormat: @"Could not bind BOOL argument %d", i]);
}
return results;
}
} else if ([[arg className] isEqualToString: @"__NSCFDate"]) {
if (SQLITE_OK != sqlite3_bind_double(stmt, i, (long)[arg timeIntervalSince1970])) {
if (error) {
*error = generate_error(207, [NSString stringWithFormat: @"Could not bind NSDate argument %d", i]);
}
return results;
}
} else if ([[arg className] isEqualToString: @"NSCFData"] || [[arg className] isEqualToString: @"NSConcreteData"]) {
if (SQLITE_OK != sqlite3_bind_blob(stmt, i, [arg bytes], [arg length], NULL)) {
if (error) {
*error = generate_error(208, [NSString stringWithFormat: @"Could not bind NSData argument %d", i]);
}
return results;
}
} else if ([[arg className] isEqualToString: @"NSNull"]) {
if (SQLITE_OK != sqlite3_bind_null(stmt, i)) {
if (error) {
*error = generate_error(209, [NSString stringWithFormat: @"Could not bind NSData argument %d", i]);
}
return results;
}
} else {
if (error) {
*error = generate_error(210, [NSString stringWithFormat: @"Could not bind %@ argument %d (value: %@)", [arg className], i, arg]);
}
return results;
}
i++;
}
}
while (SQLITE_ROW == sqlite3_step(stmt)) {
if (!results) {
results = [[NSMutableArray alloc] init];
}
int i, n = sqlite3_column_count(stmt);
NSMutableDictionary *row = [NSMutableDictionary dictionaryWithCapacity: n];
for (i = 0; i < n; i++) {
const char *name = sqlite3_column_name(stmt, i);
int type = sqlite3_column_type(stmt, i);
switch (type) {
case SQLITE_INTEGER:
{
int v = sqlite3_column_int(stmt, i);
[row setObject: [NSNumber numberWithInt: v]
forKey: [NSString stringWithCString: name encoding: NSUTF8StringEncoding]];
break;
}
case SQLITE_FLOAT:
{
double v = sqlite3_column_double(stmt, i);
[row setObject: [NSNumber numberWithFloat: v]
forKey: [NSString stringWithCString: name encoding: NSUTF8StringEncoding]];
break;
}
default:
case SQLITE_TEXT:
{
const char *v = (const char *)sqlite3_column_text(stmt, i);
[row setObject: [NSString stringWithCString:v encoding: NSUTF8StringEncoding]
forKey: [NSString stringWithCString: name encoding: NSUTF8StringEncoding]];
break;
}
case SQLITE_BLOB:
{
const void *v = sqlite3_column_blob(stmt, i);
int len = sqlite3_column_bytes(stmt, i);
[row setObject: [NSData dataWithBytes: v length:len]
forKey: [NSString stringWithCString: name encoding: NSUTF8StringEncoding]];
break;
}
case SQLITE_NULL:
{
[row setObject: [NSNull null]
forKey: [NSString stringWithCString: name encoding: NSUTF8StringEncoding]];
break;
}
}
}
[results addObject: row];
}
if (SQLITE_OK != sqlite3_finalize(stmt)) {
if (error) {
// *error = generate_error(302, @"Could not execute query");
*error = generate_error(sqlite3_errcode(_database), [NSString stringWithCString: sqlite3_errmsg(_database) encoding: NSUTF8StringEncoding]);
}
return [results autorelease];
}
}
return [results autorelease];
}
- (NSArray *)query: (NSString *)query withError: (NSError **)error
{
return [self query: query withArguments: nil andError: error];
}
- (NSNumber *)lastId
{
return [NSNumber numberWithLongLong: sqlite3_last_insert_rowid(_database)];
}
+ (void) setVerbose: (BOOL) verbose
{
__drlite_verbose = verbose;
}
@end