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 {
[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)]);
}
va_list args;
va_start(args, query);
+ [dbLock lock];
BOOL result = [self executeQuery:query va_args:args];
+ [dbLock unlock];
va_end(args);
return result;
va_list args;
va_start(args, query);
- id result = [self fetchRow:query va_args:args];
+ [dbLock lock];
+ NSDictionary *result = [self fetchRow:query va_args:args];
+ [dbLock unlock];
va_end(args);
return result;
va_list args;
va_start(args, query);
- id result = [self fetchTable:query va_args:args];
+ [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