-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathITSQLite3Database.m
271 lines (217 loc) · 7.68 KB
/
ITSQLite3Database.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#import "ITSQLite3Database.h"
int sqlite3_bind_objc_object(sqlite3_stmt *statement, int index, id object) {
int retval;
if ([object isKindOfClass:[NSData class]]) {
retval = sqlite3_bind_blob(statement, index, [object bytes], [object length], SQLITE_TRANSIENT);
} else if ([object isKindOfClass:[NSDate class]]) {
retval = sqlite3_bind_double(statement, index, [object timeIntervalSince1970]);
} else if ([object isKindOfClass:[NSNull class]]) {
retval = sqlite3_bind_null(statement, index);
} else if ([object isKindOfClass:[NSNumber class]]) {
if (strcmp([object objCType], @encode(BOOL)) == 0) {
retval = sqlite3_bind_int(statement, index, ([object boolValue] ? 1 : 0));
} else if (strcmp([object objCType], @encode(int)) == 0) {
retval = sqlite3_bind_int64(statement, index, [object longValue]);
} else if (strcmp([object objCType], @encode(long)) == 0) {
retval = sqlite3_bind_int64(statement, index, [object longValue]);
} else if (strcmp([object objCType], @encode(float)) == 0) {
retval = sqlite3_bind_double(statement, index, [object floatValue]);
} else if (strcmp([object objCType], @encode(double)) == 0) {
retval = sqlite3_bind_double(statement, index, [object doubleValue]);
}
}
if (!retval) {
retval = sqlite3_bind_text(statement, index, [[object description] UTF8String], -1, SQLITE_TRANSIENT);
}
return retval;
}
id sqlite3_column_objc_object(sqlite3_stmt *statement, int columnIndex) {
id retval;
switch (sqlite3_column_type(statement, columnIndex)) {
case SQLITE_INTEGER:
retval = [NSNumber numberWithLongLong:sqlite3_column_int64(statement, columnIndex)];
break;
case SQLITE_FLOAT:
retval = [NSNumber numberWithDouble:sqlite3_column_double(statement, columnIndex)];
break;
case SQLITE_TEXT:
retval = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(statement, columnIndex)];
break;
case SQLITE_BLOB:
retval = [NSData dataWithBytes:sqlite3_column_blob(statement, columnIndex) length:sqlite3_column_bytes(statement, columnIndex)];
break;
case SQLITE_NULL:
default:
retval = [NSNull null];
break;
}
return retval;
}
@interface ITSQLite3Database (Internals)
- (BOOL)executeQuery:(NSString *)query va_args:(va_list)args;
- (NSDictionary *)fetchRow:(NSString *)query va_args:(va_list)args;
- (NSArray *)fetchTable:(NSString *)query va_args:(va_list)args;
@end
@implementation ITSQLite3Database
- (id)initWithPath:(NSString *)path {
if (self = [super init]) {
dbPath = [path copy];
if (sqlite3_open([dbPath UTF8String], &db) != SQLITE_OK) {
ITDebugLog(@"%@ sqlite3_open(\"%@\"): %@", ITDebugErrorPrefixForObject(self), dbPath, [NSString stringWithUTF8String:sqlite3_errmsg(db)]);
[self release];
return nil;
}
dbLock = [[NSRecursiveLock alloc] init];
}
return self;
}
- (void)dealloc {
[dbLock release];
if (sqlite3_close(db) != SQLITE_OK) {
ITDebugLog(@"%@ sqlite3_close(0x%x): %@", ITDebugErrorPrefixForObject(self), db, [NSString stringWithUTF8String:sqlite3_errmsg(db)]);
}
[dbPath release];
[super dealloc];
}
- (BOOL)begin {
return [self beginTransaction];
}
- (BOOL)beginTransaction {
return [self executeQuery:@"BEGIN TRANSACTION;"];
}
- (BOOL)commit {
return [self commitTransaction];
}
- (BOOL)commitTransaction {
return [self executeQuery:@"COMMIT TRANSACTION;"];
}
- (BOOL)rollback {
return [self rollbackTransaction];
}
- (BOOL)rollbackTransaction {
return [self executeQuery:@"ROLLBACK TRANSACTION;"];
}
- (BOOL)executeQuery:(NSString *)query va_args:(va_list)args {
sqlite3_stmt *statement;
if (sqlite3_prepare(db, [query UTF8String], -1, &statement, 0) != SQLITE_OK) {
ITDebugLog(@"%@ sqlite3_prepare(0x%x, \"%@\", -1, 0x%x, 0): %@", ITDebugErrorPrefixForObject(self), db, query, statement, [NSString stringWithUTF8String:sqlite3_errmsg(db)]);
return NO;
}
int argi, argc = sqlite3_bind_parameter_count(statement);
for (argi = 0; argi < argc; argi++) {
id arg = va_arg(args, id);
if (!arg) {
[NSException raise:NSInvalidArgumentException format:@"ITSQLite3Database: -executeQuery expected %i arguments, received %i.", argc, argi];
sqlite3_finalize(statement);
return NO;
}
sqlite3_bind_objc_object(statement, argi+1, arg);
}
int stepret = sqlite3_step(statement);
int finalizeret = sqlite3_finalize(statement);
if (!(stepret == SQLITE_DONE || stepret == SQLITE_ROW)) {
ITDebugLog(@"%@ sqlite3_step(0x%x): %@", ITDebugErrorPrefixForObject(self), statement, [NSString stringWithUTF8String:sqlite3_errmsg(db)]);
return NO;
}
return YES;
}
- (BOOL)executeQuery:(NSString *)query, ... {
va_list args;
va_start(args, query);
[dbLock lock];
BOOL result = [self executeQuery:query va_args:args];
[dbLock unlock];
va_end(args);
return result;
}
- (NSDictionary *)fetchRow:(NSString *)query va_args:(va_list)args {
NSArray *table = [self fetchTable:query va_args:args];
if ([table count] >= 1) {
return [table objectAtIndex:0];
}
return nil;
}
- (NSDictionary *)fetchRow:(NSString *)query, ... {
va_list args;
va_start(args, query);
[dbLock lock];
NSDictionary *result = [self fetchRow:query va_args:args];
[dbLock unlock];
va_end(args);
return result;
}
- (NSArray *)fetchTable:(NSString *)query va_args:(va_list)args {
sqlite3_stmt *statement;
if (sqlite3_prepare(db, [query UTF8String], -1, &statement, 0) != SQLITE_OK) {
ITDebugLog(@"%@ sqlite3_prepare(0x%x, \"%@\", -1, 0x%x, 0): %@", ITDebugErrorPrefixForObject(self), db, query, statement, [NSString stringWithUTF8String:sqlite3_errmsg(db)]);
return NO;
}
int argi, argc = sqlite3_bind_parameter_count(statement);
for (argi = 0; argi < argc; argi++) {
id arg = va_arg(args, id);
if (!arg) {
[NSException raise:NSInvalidArgumentException format:@"ITSQLite3Database: -executeQuery expected %i arguments, received %i.", argc, argi];
sqlite3_finalize(statement);
return NO;
}
sqlite3_bind_objc_object(statement, argi+1, arg);
}
NSMutableArray *rowArray = [[NSMutableArray alloc] init];
int stepret;
while ((stepret = sqlite3_step(statement)) == SQLITE_ROW) {
NSMutableDictionary *row = [[NSMutableDictionary alloc] init];
int coli, cols = sqlite3_column_count(statement);
for (coli = 0; coli < cols; coli++) {
[row setObject:sqlite3_column_objc_object(statement, coli) forKey:[NSString stringWithUTF8String:sqlite3_column_name(statement, coli)]];
}
[rowArray addObject:[row autorelease]];
}
int finalizeret = sqlite3_finalize(statement);
if (stepret != SQLITE_DONE) {
ITDebugLog(@"%@ sqlite3_step(0x%x): %@", ITDebugErrorPrefixForObject(self), statement, [NSString stringWithUTF8String:sqlite3_errmsg(db)]);
return NO;
}
return [rowArray autorelease];
}
- (NSArray *)fetchTable:(NSString *)query, ... {
va_list args;
va_start(args, query);
[dbLock lock];
NSArray *result = [self fetchTable:query va_args:args];
[dbLock unlock];
va_end(args);
return result;
}
- (id)fetchRowColumn:(NSString *)query, ... {
va_list args;
va_start(args, query);
[dbLock lock];
NSDictionary *result = [self fetchRow:query va_args:args];
[dbLock unlock];
va_end(args);
if (result) {
if ([[result allKeys] count] >= 1) {
return [result objectForKey:[[result allKeys] objectAtIndex:0]];
}
}
return nil;
}
- (NSArray *)fetchTableColumn:(NSString *)query, ... {
va_list args;
va_start(args, query);
[dbLock lock];
NSArray *result = [self fetchTable:query va_args:args];
[dbLock unlock];
va_end(args);
if (result) {
NSMutableArray *columnArray = [[NSMutableArray alloc] init];
NSEnumerator *enumerator = [result objectEnumerator];
NSDictionary *row;
while (row = (NSDictionary *)[enumerator nextObject]) {
[columnArray addObject:[row objectForKey:[[row allKeys] objectAtIndex:0]]];
}
return [columnArray autorelease];
}
return nil;
}
@end