-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataWrapper.m
More file actions
203 lines (167 loc) · 6.79 KB
/
DataWrapper.m
File metadata and controls
203 lines (167 loc) · 6.79 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
//
// DataWrapper.m
//
// Copyright (c) 2011, Andrew Ettinger
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided
// that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list of conditions and the
// following disclaimer.
// Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
// The names of its contributors may not be used to endorse or promote products derived from this software without
// specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#import "DataWrapper.h"
#import <objc/runtime.h>
@implementation DataWrapper
@synthesize key, fields;
NSString *db= @"my_database.sqlite";
/* override this to change the table name, otherwise the Classname is assumed to be the same as the table */
+(NSString *) get_table
{
return NSStringFromClass([self class]);
}
-(void) load:(NSString *)byid
{
NSLog(@"finding %@", byid);
self= [[[self class] find:@"id" withValue:(NSString *)byid] objectAtIndex:0];
}
+(NSMutableArray*) find:(NSString*)field withValue:(NSString*)val
{
NSLog(@"%@: finding %@ with value %@", [self get_table], field, val);
return [self find:[NSString stringWithFormat:@"select * from %@ where %@='%@'",[self get_table],field,val]];
}
+(NSMutableArray*) find
{
return [self find:[NSString stringWithFormat:@"select * from %@",[self get_table]]];
}
+(NSMutableArray*) find:(NSString*)query
{
NSLog(@"Running %@->find(%@)", [self get_table], query);
NSMutableArray *results= [[NSMutableArray alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent: db];
sqlite3 *database;
if(!sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
NSLog(@"WARNING: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
NSString *stmt= query;
const char *sqlStatement = [stmt UTF8String];
sqlite3_stmt *compiledStatement;
if(!sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
NSLog(@"WARNING: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
DataWrapper *self= [[[self class] alloc] init];
char *k;
k= (char *)sqlite3_column_text(compiledStatement, 0);
if (k != nil && strlen(k) > 0)
self.key= [NSString stringWithUTF8String:k];
else {
NSLog(@"WARNING: NO KEY ON FIND");
}
for(int i=0; i < [self.fields count]; i++)
{
char *temp_char;
SEL method;
NSString *tmp;
tmp= [self.fields objectAtIndex:i];
method= NSSelectorFromString(tmp);
temp_char = (char *)sqlite3_column_text(compiledStatement, i+1);
if (temp_char != nil && strlen(temp_char) > 0)
{
[self setValue:[NSString stringWithUTF8String:temp_char] forKey:tmp];
}
else
[self setValue:@"" forKey:tmp];
}
[results addObject:self];
[self release];
}
NSLog(@"Found %d record(s)", [results count]);
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
return results;
}
-(void) save
{
if (!(self.fields && [[self class] get_table]))
NSLog(@"WARNING: NSOBJECT WITHOUT TABLE AND DATA STRUCT!");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent: db];
sqlite3 *database;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
{
NSString *stmt;
char *sqlStatement;
if (self.key != nil)
{
NSLog(@"Updating %@ record %@", [[self class] get_table], self.key);
stmt= [NSString stringWithFormat: @"update %@ set ", [[self class] get_table]];
for (int i=0; i < [self.fields count]; i++) {
stmt= [stmt stringByAppendingString: [self.fields objectAtIndex:i]];
stmt= [stmt stringByAppendingString: @"=?"];
if (i != [self.fields count] - 1)
stmt= [stmt stringByAppendingString:@", "];
}
stmt= [stmt stringByAppendingString:@" where key=?"];
}
else
{
NSLog(@"Inserting new %@ record", [[self class] get_table]);
stmt= [NSString stringWithFormat: @"insert into %@(", [[self class] get_table]];
for (int i=0; i < [self.fields count]; i++)
{
stmt= [stmt stringByAppendingString: [self.fields objectAtIndex:i]];
if (i != [self.fields count] - 1)
stmt= [stmt stringByAppendingString:@", "];
}
stmt= [stmt stringByAppendingString:@") values("];
for (int i=0; i < [self.fields count]; i++)
{
stmt= [stmt stringByAppendingString:@"?"];
if (i != [self.fields count] - 1)
stmt= [stmt stringByAppendingString:@", "];
}
stmt= [stmt stringByAppendingString:@")"];
}
NSLog(@"%@", stmt);
sqlStatement= [stmt UTF8String];
sqlite3_stmt *compiledStatement;
if(!sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
NSLog(@"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
for (int i=0; i < [self.fields count]; i++)
{
SEL method;
NSString *tmp;
tmp= [self.fields objectAtIndex:i];
method= NSSelectorFromString(tmp);
sqlite3_bind_text(compiledStatement, i+1,
[[self performSelector:method] UTF8String], -1,
SQLITE_TRANSIENT);
}
if (self.key != nil)
sqlite3_bind_text(compiledStatement, [self.fields count]+1,
[self.key UTF8String], -1,
SQLITE_TRANSIENT);
if(!sqlite3_step(compiledStatement) == SQLITE_DONE)
NSLog(@"Error: failed to compile statement with message '%s'.", sqlite3_errmsg(database));
sqlite3_finalize(compiledStatement);
if (self.key == nil)
self.key= [NSString stringWithFormat:@"%d", sqlite3_last_insert_rowid(database)];
}
sqlite3_close(database);
}
@end