X-Git-Url: http://git.ithinksw.org/ITFoundation.git/blobdiff_plain/744612b5d258474e367710b870a5caaed7d11ab9..refs/heads/master:/ITSQLite3Database.m diff --git a/ITSQLite3Database.m b/ITSQLite3Database.m index 1984e6d..66f086c 100644 --- a/ITSQLite3Database.m +++ b/ITSQLite3Database.m @@ -55,6 +55,12 @@ id sqlite3_column_objc_object(sqlite3_stmt *statement, int columnIndex) { 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 { @@ -65,11 +71,13 @@ id sqlite3_column_objc_object(sqlite3_stmt *statement, int columnIndex) { [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)]); } @@ -136,7 +144,9 @@ id sqlite3_column_objc_object(sqlite3_stmt *statement, int columnIndex) { va_list args; va_start(args, query); + [dbLock lock]; BOOL result = [self executeQuery:query va_args:args]; + [dbLock unlock]; va_end(args); return result; @@ -154,7 +164,9 @@ id sqlite3_column_objc_object(sqlite3_stmt *statement, int columnIndex) { 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; @@ -206,10 +218,54 @@ id sqlite3_column_objc_object(sqlite3_stmt *statement, int columnIndex) { 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