-
Notifications
You must be signed in to change notification settings - Fork 2
/
MLSQLiteDatabase.m
288 lines (253 loc) · 10.2 KB
/
MLSQLiteDatabase.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
281
282
283
284
285
286
287
288
/*
Copyright (c) 2013 Max Lungarella <[email protected]>
Created on 24/08/2013.
This file is part of AmiKo for OSX.
AmiKo for OSX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------ */
#import "MLSQLiteDatabase.h"
#import "MLUtilities.h"
@implementation MLSQLiteDatabase
{
// Instance variable declarations go here
sqlite3 *database;
dispatch_queue_t dbQueue;
}
/** Class functions
*/
#pragma mark class functions
+ (void) createEditableCopyOfDatabaseIfNeeded:(NSString *)dbName
{
// Create NSFileManager object to check the status of the database and to copy it if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Get documents directory
NSString *documentsDir = [MLUtilities documentsDirectory];
// Returns a new string made by appending to the receiver a given string
NSString *writableDBPath = [documentsDir stringByAppendingPathComponent:dbName];
// Check if the database has already been created at specified path
BOOL success = [fileManager fileExistsAtPath:writableDBPath];
// If the file exists, do nothing...
if (success)
return;
NSError *error;
// Get the path to the database in the application package
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbName];
// Copy the database from the package to the users filesystem
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success)
NSLog(@"%s Failed to create writable database file with message '%@'.", __FUNCTION__, [error localizedDescription]);
}
/** Instance functions
*/
#pragma mark public methods
- (void) dealloc
{
}
- (id) initReadOnlyWithPath:(NSString *)path
{
return [self initReadOnlyWithPath:path andQueue:nil];
}
- (id) initReadOnlyWithPath:(NSString *)path andQueue:(dispatch_queue_t)queue
{
if (self = [super init]) {
// Setup database object
sqlite3 *dbConnection;
// Open database from users filesystem
NSFileManager *fileMgr = [[NSFileManager alloc] init];
NSArray *content = [fileMgr contentsOfDirectoryAtPath:path error:nil];
for (NSString *p in content) {
NSLog(@"%@\n", p);
}
int rc;
rc = sqlite3_open_v2([path UTF8String], &dbConnection, SQLITE_OPEN_READONLY, NULL);
if (rc != SQLITE_OK) {
NSLog(@"%s Unable to open database!", __FUNCTION__);
return nil;
}
database = dbConnection;
// Force using disk for temp storage to reduce memory footprint
rc = sqlite3_exec(database, "PRAGMA temp_store=1", nil, nil, nil);
if (rc != SQLITE_OK)
NSLog(@"%s:%i, rc %d", __FUNCTION__, __LINE__, rc);
dbQueue = queue;
}
return self;
}
- (id) initReadWriteWithPath:(NSString *)path
{
if (self = [super init]) {
// Setup database object
sqlite3 *dbConnection;
// Open database from user's filesystem
NSFileManager *fileMgr = [[NSFileManager alloc] init];
NSArray *content = [fileMgr contentsOfDirectoryAtPath:path error:nil];
for (NSString *p in content) {
NSLog(@"%@\n", p);
}
// Let's open it in read write mode
if (sqlite3_open([path UTF8String], &dbConnection) != SQLITE_OK) {
NSLog(@"%s Unable to open read/write database!", __FUNCTION__);
return nil;
}
database = dbConnection;
// Force using disk for temp storage to reduce memory footprint
sqlite3_exec(database, "PRAGMA temp_store=1", nil, nil, nil);
dbQueue = nil;
}
return self;
}
- (BOOL) createWithPath:(NSString *)path
andTable:(NSString *)table
andColumns:(NSString *)columns
{
NSFileManager *fileMgr = [[NSFileManager alloc] init];
if (![fileMgr fileExistsAtPath:path]) {
// Setup database object
sqlite3 *dbConnection;
// Database does not exist yet. Let's open and create an empty table
if (sqlite3_open([path UTF8String], &dbConnection) == SQLITE_OK) {
NSString *queryStr = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ %@", table, columns];
if (sqlite3_exec(dbConnection, [queryStr UTF8String], NULL, NULL, NULL) == SQLITE_OK) {
NSLog(@"%@ table created successfully...", table);
database = dbConnection;
}
else {
NSLog(@"Failed to create table %@ for database with path %@", table, path);
return FALSE;
}
}
else {
NSLog(@"Failed to create database with path %@", path);
return FALSE;
}
}
return TRUE;
}
- (NSInteger) numberRecordsForTable:(NSString *)table
{
NSInteger numTableRecords = -1;
sqlite3_stmt *sqlClause = nil;
NSString *sqlStatement = [NSString stringWithFormat: @"SELECT COUNT(*) FROM %@", table];
const char *sql = [sqlStatement UTF8String];
if (sqlite3_prepare_v2(database, sql, -1, &sqlClause, NULL) == SQLITE_OK) {
while(sqlite3_step(sqlClause) == SQLITE_ROW) {
numTableRecords = sqlite3_column_int(sqlClause, 0);
}
}
else {
NSLog(@"%s Could not prepare statement: %s", __FUNCTION__, sqlite3_errmsg(database));
}
return numTableRecords;
}
- (NSArray *) performQuery:(NSString *)query
{
if (dbQueue != nil && [NSThread isMainThread]) {
__block NSArray *result = nil;
dispatch_sync(dbQueue, ^{
result = [self performQueryInQueue:query];
});
return result;
}
return [self performQueryInQueue:query];
}
- (NSArray *) performQueryInQueue:(NSString *)query
{
sqlite3_stmt *compiledStatement = nil;
// Convert NSString to a C String
const char *sql = [query UTF8String];
int error_code = SQLITE_OK;
// Open database from users filesystem
if ( (error_code=sqlite3_prepare_v2(database, sql, -1, &compiledStatement, nil)) != SQLITE_OK) {
NSLog(@"%s Error with code %d when preparing query!", __FUNCTION__, error_code);
} else {
NSMutableArray *result = [NSMutableArray array];
@autoreleasepool {
// Loop through results and add them to feeds array
while (sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSMutableArray *row = [NSMutableArray array];
for (int i=0; i<sqlite3_column_count(compiledStatement); i++) {
int colType = sqlite3_column_type(compiledStatement, i);
id value;
if (colType == SQLITE_TEXT) {
const char *col = (const char *)sqlite3_column_text(compiledStatement, i);
value = [[NSString alloc] initWithUTF8String:col];
} else if (colType == SQLITE_INTEGER) {
int col = sqlite3_column_int(compiledStatement, i);
value = [NSNumber numberWithInt:col];
} else if (colType == SQLITE_FLOAT) {
double col = sqlite3_column_double(compiledStatement, i);
value = [NSNumber numberWithDouble:col];
} else if (colType == SQLITE_NULL) {
value = [NSNull null];
} else {
NSLog(@"%s Unknown data type.", __FUNCTION__);
}
// Add value to row
[row addObject:value];
value = nil;
}
// Add row to array
[result addObject:row];
}
// Reset statement (not necessary)
sqlite3_reset(compiledStatement);
// Release compiled statement from memory
sqlite3_finalize(compiledStatement);
}
return result;
}
return nil;
}
- (BOOL) insertRowIntoTable:(NSString *)table forColumns:(NSString *)columns andValues:(NSString *)values
{
char *errMsg;
NSString *query = [NSString stringWithFormat:@"insert into %@ %@ values %@", table, columns, values];
if (sqlite3_exec(database, [query UTF8String], NULL, NULL, &errMsg) != SQLITE_OK) {
NSLog(@"Failed to insert record into table %@ with query %@: %s", table, query, errMsg);
return FALSE;
}
return TRUE;
}
- (BOOL) updateRowIntoTable:(NSString *)table forExpressions:(NSString *)expressions andConditions:(NSString *)conditions
{
char *errMsg;
NSString *query = [NSString stringWithFormat:@"update %@ set %@ where %@", table, expressions, conditions];
if (sqlite3_exec(database, [query UTF8String], NULL, NULL, &errMsg) != SQLITE_OK) {
NSLog(@"Failed to replace record into table %@ with query %@: %s", table, query, errMsg);
return FALSE;
}
return TRUE;
}
- (BOOL) deleteRowFromTable:(NSString *)table withRowId:(long)rowId
{
char *errMsg;
NSString *query = [NSString stringWithFormat:@"delete from %@ where rowId=%ld", table, rowId];
if (sqlite3_exec(database, [query UTF8String], NULL, NULL, &errMsg) != SQLITE_OK) {
NSLog(@"Failed to delete record from db: %s", errMsg);
return FALSE;
}
return TRUE;
}
- (BOOL) deleteRowFromTable:(NSString *)table withUId:(NSString *)uId
{
char *errMsg;
NSString *query = [NSString stringWithFormat:@"delete from %@ where uid='%@'", table, uId];
if (sqlite3_exec(database, [query UTF8String], NULL, NULL, &errMsg) != SQLITE_OK) {
NSLog(@"Failed to delete record from db: %s", errMsg);
return FALSE;
}
return TRUE;
}
- (void) close
{
sqlite3_close(database);
}
@end