1 #import "ITSQLite3Database.h"
3 int sqlite3_bind_objc_object(sqlite3_stmt *statement, int index, id object) {
6 if ([object isKindOfClass:[NSData class]]) {
7 retval = sqlite3_bind_blob(statement, index, [object bytes], [object length], SQLITE_TRANSIENT);
8 } else if ([object isKindOfClass:[NSDate class]]) {
9 retval = sqlite3_bind_double(statement, index, [object timeIntervalSince1970]);
10 } else if ([object isKindOfClass:[NSNull class]]) {
11 retval = sqlite3_bind_null(statement, index);
12 } else if ([object isKindOfClass:[NSNumber class]]) {
13 if (strcmp([object objCType], @encode(BOOL)) == 0) {
14 retval = sqlite3_bind_int(statement, index, ([object boolValue] ? 1 : 0));
15 } else if (strcmp([object objCType], @encode(int)) == 0) {
16 retval = sqlite3_bind_int64(statement, index, [object longValue]);
17 } else if (strcmp([object objCType], @encode(long)) == 0) {
18 retval = sqlite3_bind_int64(statement, index, [object longValue]);
19 } else if (strcmp([object objCType], @encode(float)) == 0) {
20 retval = sqlite3_bind_double(statement, index, [object floatValue]);
21 } else if (strcmp([object objCType], @encode(double)) == 0) {
22 retval = sqlite3_bind_double(statement, index, [object doubleValue]);
27 retval = sqlite3_bind_text(statement, index, [[object description] UTF8String], -1, SQLITE_TRANSIENT);
33 id sqlite3_column_objc_object(sqlite3_stmt *statement, int columnIndex) {
36 switch (sqlite3_column_type(statement, columnIndex)) {
38 retval = [NSNumber numberWithLongLong:sqlite3_column_int64(statement, columnIndex)];
41 retval = [NSNumber numberWithDouble:sqlite3_column_double(statement, columnIndex)];
44 retval = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(statement, columnIndex)];
47 retval = [NSData dataWithBytes:sqlite3_column_blob(statement, columnIndex) length:sqlite3_column_bytes(statement, columnIndex)];
51 retval = [NSNull null];
58 @implementation ITSQLite3Database
60 - (id)initWithPath:(NSString *)path {
61 if (self = [super init]) {
63 if (sqlite3_open([dbPath UTF8String], &db) != SQLITE_OK) {
64 ITDebugLog(@"%@ sqlite3_open(\"%@\"): %@", ITDebugErrorPrefixForObject(self), dbPath, [NSString stringWithUTF8String:sqlite3_errmsg(db)]);
73 if (sqlite3_close(db) != SQLITE_OK) {
74 ITDebugLog(@"%@ sqlite3_close(0x%x): %@", ITDebugErrorPrefixForObject(self), db, [NSString stringWithUTF8String:sqlite3_errmsg(db)]);
81 return [self beginTransaction];
84 - (BOOL)beginTransaction {
85 return [self executeQuery:@"BEGIN TRANSACTION;"];
89 return [self commitTransaction];
92 - (BOOL)commitTransaction {
93 return [self executeQuery:@"COMMIT TRANSACTION;"];
97 return [self rollbackTransaction];
100 - (BOOL)rollbackTransaction {
101 return [self executeQuery:@"ROLLBACK TRANSACTION;"];
104 - (BOOL)executeQuery:(NSString *)query va_args:(va_list)args {
105 sqlite3_stmt *statement;
107 if (sqlite3_prepare(db, [query UTF8String], -1, &statement, 0) != SQLITE_OK) {
108 ITDebugLog(@"%@ sqlite3_prepare(0x%x, \"%@\", -1, 0x%x, 0): %@", ITDebugErrorPrefixForObject(self), db, query, statement, [NSString stringWithUTF8String:sqlite3_errmsg(db)]);
112 int argi, argc = sqlite3_bind_parameter_count(statement);
113 for (argi = 0; argi < argc; argi++) {
114 id arg = va_arg(args, id);
117 [NSException raise:NSInvalidArgumentException format:@"ITSQLite3Database: -executeQuery expected %i arguments, received %i.", argc, argi];
118 sqlite3_finalize(statement);
122 sqlite3_bind_objc_object(statement, argi+1, arg);
125 int stepret = sqlite3_step(statement);
126 int finalizeret = sqlite3_finalize(statement);
127 if (!(stepret == SQLITE_DONE || stepret == SQLITE_ROW)) {
128 ITDebugLog(@"%@ sqlite3_step(0x%x): %@", ITDebugErrorPrefixForObject(self), statement, [NSString stringWithUTF8String:sqlite3_errmsg(db)]);
135 - (BOOL)executeQuery:(NSString *)query, ... {
137 va_start(args, query);
139 BOOL result = [self executeQuery:query va_args:args];
145 - (NSDictionary *)fetchRow:(NSString *)query va_args:(va_list)args {
146 NSArray *table = [self fetchTable:query va_args:args];
147 if ([table count] >= 1) {
148 return [table objectAtIndex:0];
153 - (NSDictionary *)fetchRow:(NSString *)query, ... {
155 va_start(args, query);
157 id result = [self fetchRow:query va_args:args];
163 - (NSArray *)fetchTable:(NSString *)query va_args:(va_list)args {
164 sqlite3_stmt *statement;
166 if (sqlite3_prepare(db, [query UTF8String], -1, &statement, 0) != SQLITE_OK) {
167 ITDebugLog(@"%@ sqlite3_prepare(0x%x, \"%@\", -1, 0x%x, 0): %@", ITDebugErrorPrefixForObject(self), db, query, statement, [NSString stringWithUTF8String:sqlite3_errmsg(db)]);
171 int argi, argc = sqlite3_bind_parameter_count(statement);
172 for (argi = 0; argi < argc; argi++) {
173 id arg = va_arg(args, id);
176 [NSException raise:NSInvalidArgumentException format:@"ITSQLite3Database: -executeQuery expected %i arguments, received %i.", argc, argi];
177 sqlite3_finalize(statement);
181 sqlite3_bind_objc_object(statement, argi+1, arg);
184 NSMutableArray *rowArray = [[NSMutableArray alloc] init];
187 while ((stepret = sqlite3_step(statement)) == SQLITE_ROW) {
188 NSMutableDictionary *row = [[NSMutableDictionary alloc] init];
189 int coli, cols = sqlite3_column_count(statement);
190 for (coli = 0; coli < cols; coli++) {
191 [row setObject:sqlite3_column_objc_object(statement, coli) forKey:[NSString stringWithUTF8String:sqlite3_column_name(statement, coli)]];
193 [rowArray addObject:[row autorelease]];
196 int finalizeret = sqlite3_finalize(statement);
197 if (stepret != SQLITE_DONE) {
198 ITDebugLog(@"%@ sqlite3_step(0x%x): %@", ITDebugErrorPrefixForObject(self), statement, [NSString stringWithUTF8String:sqlite3_errmsg(db)]);
202 return [rowArray autorelease];
205 - (NSArray *)fetchTable:(NSString *)query, ... {
207 va_start(args, query);
209 id result = [self fetchTable:query va_args:args];