-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataStoreManager.m
More file actions
224 lines (174 loc) · 7.46 KB
/
DataStoreManager.m
File metadata and controls
224 lines (174 loc) · 7.46 KB
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
//
// DataStoreManager.m
// DataManager
//
// Created by Adam Chin on 5/31/15.
// Copyright (c) 2015 Adam Chin. All rights reserved.
//
#import "DataStoreManager.h"
#define DATABASE_INFO_TABLE_NAME @"Info"
#define DATABASE_VERSION 1
#define DATABASE_VERSION_NAME @"Version"
@class Users;
@interface DataStoreManager ()
+(void)createLocalDatabase;
@property (nonatomic, strong) Users *user;
@end
@implementation DataStoreManager
//TODO: expand this to receive ds string name
+(sqlite3 *)openDataStore
{
NSString *docsDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"users.db"]];
const char *dbpath = [databasePath UTF8String];
sqlite3 * db = nil;
if (sqlite3_open(dbpath, &db) == SQLITE_OK)
{
//success, return the opened database
return db;
}
else if (db != nil)
{
sqlite3_close(db);
}
return nil;
}
//TODO: expand this to receive ds string name
+(void)createLocalDatabase
{
NSString *docsDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"users.db"]];
if ([[NSFileManager defaultManager] fileExistsAtPath: databasePath ] == NO)
{
//it doesn't, need to create
const char *dbpath = [databasePath UTF8String];
sqlite3 * db = nil;
if (sqlite3_open(dbpath, &db) == SQLITE_OK)
{
//if we successfully opened
if(db != nil)
{
char *errMsg;
// users table Add others as necessary
const char *sql_stmt_data_table = [[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (%@ int, %@ text , %@ text, %@ text, %@ text,%@ text)",
CC_TABLE_NAME,
COL_ROW_ID,
COL_FIRST,
COL_LAST,
COL_EMAIL,
COL_STATUS,
COL_PASSWORD] cStringUsingEncoding:NSASCIIStringEncoding];
//attempt to create the table
sqlite3_exec(db, sql_stmt_data_table, NULL, NULL, &errMsg);
//create a table to hold the version
const char *sql_stmt_version_table = [[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (%@ int)", DATABASE_INFO_TABLE_NAME, DATABASE_VERSION_NAME]cStringUsingEncoding:NSASCIIStringEncoding];
//attempt to create the table∑
sqlite3_exec(db, sql_stmt_version_table, NULL, NULL, &errMsg);
//now insert the version into the right table
const NSString * const insertSQL = [NSString stringWithFormat:@"INSERT INTO %@ (%@) VALUES (?)", DATABASE_INFO_TABLE_NAME, DATABASE_VERSION_NAME];
sqlite3_stmt * statement;
//compile the sql statement
int result = sqlite3_prepare_v2(db, [insertSQL UTF8String], -1, &statement, NULL);
if(result == SQLITE_OK)
{
if(sqlite3_bind_int(statement, 1, DATABASE_VERSION) == SQLITE_OK)
{
//success
if(sqlite3_step(statement) == SQLITE_DONE)
{
}
}
}
sqlite3_close(db);
}
}
}
}
-(instancetype)saveUserToDataStore:(Users *)user
{
self.user = user;
sqlite3 *db = [DataStoreManager openDataStore];
if (user)
{
if(db != nil)
{
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO users (first, last, email, password, status, ID) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\",\"%i\")",
self.user.first,
self.user.last,
self.user.email,
self.user.password,
self.user.status,
(int)self.user.ID];
sqlite3_stmt * statement;
sqlite3_prepare_v2(db, [insertSQL UTF8String], -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"saved user");
}
else
{
NSLog(@"fail");
}
sqlite3_finalize(statement);
}
sqlite3_close(db);
}
return self;
}
+(NSMutableArray *)getAllLocalUsers
{
sqlite3 *db = [DataStoreManager openDataStore];
NSMutableArray *userCollection;
if (db != nil)
{
NSString *selectSQL = [NSString stringWithFormat:@"SELECT ID, first, last, email, password, status FROM users"];
sqlite3_stmt *statement;
int result = sqlite3_prepare_v2(db, [selectSQL UTF8String], -1, &statement, NULL);
if (result == SQLITE_OK)
{
userCollection = [[NSMutableArray alloc] init];
while (sqlite3_step(statement) == SQLITE_ROW)
{
Users *usr = [[Users alloc] init];
int index = 0;
usr.ID = (NSInteger)sqlite3_column_int64(statement, index);
index ++; //advance to next column ->
const char * text = (const char * const)sqlite3_column_text(statement, index);
if (text)
{
usr.first = [NSString stringWithUTF8String: text];
}
index ++; //advance to next column ->
text = (const char * const)sqlite3_column_text(statement, index);
if (text)
{
usr.last = [NSString stringWithUTF8String: text];
}
index ++; //advance to next column ->
text = (const char * const)sqlite3_column_text(statement, index);
if (text)
{
usr.email = [NSString stringWithUTF8String: text];
}
index ++; //advance to next column ->
text = (const char * const)sqlite3_column_text(statement, index);
if (text)
{
usr.password= [NSString stringWithUTF8String: text];
}
index ++; //advance to next column ->
text = (const char * const)sqlite3_column_text(statement, index);
if (text)
{
usr.status= [NSString stringWithUTF8String: text];
}
index ++; //advance to next column ->
[userCollection addObject: usr];
}
}
sqlite3_finalize(statement);
}
sqlite3_close(db);
return userCollection;
}
@end