-
Notifications
You must be signed in to change notification settings - Fork 9
/
SQLiteInstanceManager.m
280 lines (256 loc) · 8.09 KB
/
SQLiteInstanceManager.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
272
273
274
275
276
277
278
279
280
//
// SQLiteInstanceManager.m
// ----------------------------------------------------------------------
// Part of the SQLite Persistent Objects for Cocoa and Cocoa Touch
//
// (c) 2008 Jeff LaMarche (jeff_Lamarche@mac.com)
// ----------------------------------------------------------------------
// This code may be used without restriction in any software, commercial,
// free, or otherwise. There are no attribution requirements, and no
// requirement that you distribute your changes, although bugfixes and
// enhancements are welcome.
//
// If you do choose to re-distribute the source code, you must retain the
// copyright notice and this license information. I also request that you
// place comments in to identify your changes.
//
// For information on how to use these classes, take a look at the
// included eadme.txt file
// ----------------------------------------------------------------------
#import "SQLiteInstanceManager.h"
#import "NSDate-SQLitePersistence.h"
#ifndef debugLog(...)
#define debugLog(...)
#endif
static SQLiteInstanceManager *sharedSQLiteManager = nil;
#pragma mark Private Method Declarations
@interface SQLiteInstanceManager (private)
- (NSString *)databaseFilepath;
@end
@implementation SQLiteInstanceManager
#pragma mark -
#pragma mark Singleton Methods
+ (id)sharedManager
{
@synchronized(self)
{
if (sharedSQLiteManager == nil)
[[self alloc] init];
}
return sharedSQLiteManager;
}
+ (id)allocWithZone:(NSZone *)zone
{
@synchronized(self) {
if (sharedSQLiteManager == nil)
{
sharedSQLiteManager = [super allocWithZone:zone];
return sharedSQLiteManager;
}
}
return nil;
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (id)retain
{
return self;
}
- (unsigned)retainCount
{
return UINT_MAX; //denotes an object that cannot be released
}
- (void)release
{
// never release
}
- (id)autorelease
{
return self;
}
#pragma mark -
-(sqlite3 *)database
{
static BOOL first = YES;
if (first || database == NULL)
{
first = NO;
if (!sqlite3_open([[self databaseFilepath] UTF8String], &database) == SQLITE_OK)
{
// Even though the open failed, call close to properly clean up resources.
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
sqlite3_close(database);
}
char *errorMsg = 0;
if (sqlite3_exec(database, "PRAGMA encoding = \"UTF-8\"", NULL, NULL, &errorMsg) != SQLITE_OK) {
NSAssert1(0, @"Failed to switch encoding to UTF-8 with message '%s'.", errorMsg);
sqlite3_free(errorMsg);
}
}
return database;
}
- (NSArray *)columnsForQuery:(sqlite3_stmt *)query
{
int columnCount = sqlite3_column_count(query);
if(columnCount <= 0)
return nil;
NSMutableArray *columnNames = [NSMutableArray array];
for(int i = 0; i < columnCount; ++i)
{
const char *name;
name = sqlite3_column_name(query, i);
[columnNames addObject:[NSString stringWithUTF8String:name]];
}
return columnNames;
}
// You have to step through the *query yourself,
- (id)valueForColumn:(unsigned int)colIndex query:(sqlite3_stmt *)query
{
int columnType = sqlite3_column_type(query, colIndex);
switch(columnType)
{
case SQLITE_INTEGER:
return [NSNumber numberWithInt:sqlite3_column_int(query, colIndex)];
break;
case SQLITE_FLOAT:
return [NSNumber numberWithDouble:sqlite3_column_double(query, colIndex)];
break;
case SQLITE_BLOB:
return [NSData dataWithBytes:sqlite3_column_blob(query, colIndex)
length:sqlite3_column_bytes(query, colIndex)];
break;
case SQLITE_NULL:
return [NSNull null];
break;
case SQLITE_TEXT:
return [NSString stringWithUTF8String:(const char *)sqlite3_column_text(query, colIndex)];
break;
default:
// It really shouldn't ever come to this.
break;
}
return nil;
}
- (void)beginTransaction {
[self executeQuery:@"BEGIN"];
}
- (void)commitTransaction {
[self executeQuery:@"COMMIT"];
}
- (void)rollbackTransaction {
[self executeQuery:@"ROLLBACK"];
}
- (NSArray *)executeQuery:(NSString *)sql {
return [self executeQuery:sql substitutions:nil];
}
- (NSArray *)executeQuery:(NSString *)sql substitutions:(NSDictionary *)substitutions {
@synchronized(self) {
sqlite3 *db = [self database];
sqlite3_stmt *stmt;
const char *errMsg;
int err = sqlite3_prepare_v2(db, [sql UTF8String],
[sql lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
&stmt, &errMsg);
if(err != SQLITE_OK || stmt == NULL)
{
NSLog(@"ERROR Couldn't prepare(%@), %@", sql, [NSString stringWithUTF8String:sqlite3_errmsg(db)]);
return nil;
}
else {
debugLog(sql);
}
NSArray *columnNames = [self columnsForQuery:stmt];
for(int i = 1; i <= sqlite3_bind_parameter_count(stmt); ++i)
{
const char *keyCstring = sqlite3_bind_parameter_name(stmt, i);
if(!keyCstring)
continue;
NSString *key = [[NSString stringWithUTF8String:keyCstring] stringByReplacingOccurrencesOfString:@":" withString:@""];
id sub = [substitutions objectForKey:key];
if(!sub)
continue;
if([sub isMemberOfClass:[NSString class]] || [[sub className] isEqualToString:@"NSCFString"])
sqlite3_bind_text(stmt, i, [sub UTF8String], -1, SQLITE_TRANSIENT);
else if([sub isMemberOfClass:[NSData class]])
sqlite3_bind_blob(stmt, i, [sub bytes], [sub length], SQLITE_STATIC); // Not sure if we should make this transient
else if([[sub className] isEqualToString:@"NSCFNumber"])
sqlite3_bind_double(stmt, i, [sub doubleValue]);
else if([sub isMemberOfClass:[NSNull class]])
sqlite3_bind_null(stmt, i);
else if([sub isKindOfClass:[NSDate class]])
sqlite3_bind_text(stmt, i, [[sub sqlColumnRepresentationOfSelf] UTF8String], -1, NULL);
else
NSLog(@"ERROR SQLiteInstanceManager doesn't know how to handle this type of object: %@ class: %@", sub, [sub className]);
}
NSMutableArray *result = [NSMutableArray array];
NSMutableDictionary *columns;
int resultCode = 0;
while((resultCode = sqlite3_step(stmt)) != SQLITE_DONE)
{
if(resultCode == SQLITE_ERROR || err == SQLITE_MISUSE)
{
NSLog(@"ERROR while running \"%@\" %@", sql, [NSString stringWithUTF8String:sqlite3_errmsg(db)]);
break;
}
else if(resultCode == SQLITE_ROW)
{
// construct the dictionary for the row
columns = [NSMutableDictionary dictionary];
int i = 0;
for (NSString *columnName in columnNames)
{
id value = [self valueForColumn:i query:stmt];
if ([NSNull null] != value) {
[columns setObject:value forKey:columnName];
}
++i;
}
[result addObject:columns];
}
}
sqlite3_finalize(stmt);
return result;
}
return nil;
}
#pragma mark -
- (void)dealloc
{
[databaseFilepath release];
[super dealloc];
}
#pragma mark -
#pragma mark Private Methods
- (NSString *)databaseFilepath
{
if (databaseFilepath == nil)
{
NSMutableString *ret = [NSMutableString string];
NSString *appName = [[NSProcessInfo processInfo] processName];
for (int i = 0; i < [appName length]; i++)
{
NSRange range = NSMakeRange(i, 1);
NSString *oneChar = [appName substringWithRange:range];
if (![oneChar isEqualToString:@" "])
[ret appendString:[oneChar lowercaseString]];
}
#if (TARGET_OS_MAC && ! (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR))
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : NSTemporaryDirectory();
NSString *saveDirectory = [basePath stringByAppendingPathComponent:appName];
#else
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];
#endif
NSString *saveFileName = [NSString stringWithFormat:@"%@.sqlite3", ret];
NSString *filepath = [saveDirectory stringByAppendingPathComponent:saveFileName];
databaseFilepath = [filepath retain];
debugLog(databaseFilepath);
if (![[NSFileManager defaultManager] fileExistsAtPath:saveDirectory])
[[NSFileManager defaultManager] createDirectoryAtPath:saveDirectory withIntermediateDirectories:YES attributes:nil error:nil];
}
return databaseFilepath;
}
@end