26
26
27
27
@implementation DataWrapper
28
28
29
- @synthesize key, table_name, fields;
30
- NSString *db= @" your_database.sqlite" ;
29
+ @synthesize key, fields;
30
+ NSString *db= @" my_database.sqlite" ;
31
+
32
+ /* override this to change the table name, otherwise the Classname is assumed to be the same as the table */
33
+ +(NSString *) get_table
34
+ {
35
+ return NSStringFromClass ([self class ]);
36
+ }
31
37
32
38
-(void ) load : (NSString *)byid
33
39
{
34
40
NSLog (@" finding %@ " , byid);
35
- self= [[self find: @" id" withValue: (NSString *)byid] objectAtIndex: 0 ];
41
+ self= [[[ self class ] find: @" id" withValue: (NSString *)byid] objectAtIndex: 0 ];
36
42
}
37
43
38
- - (NSMutableArray *) find : (NSString *)field withValue : (NSString *)val
44
+ + (NSMutableArray *) find : (NSString *)field withValue : (NSString *)val
39
45
{
40
- NSLog (@" %@ : finding %@ with value %@ " , self. table_name , field, val);
41
- return [self find: [NSString stringWithFormat: @" select * from %@ where %@ ='%@ '" ,self .table_name ,field,val]];
46
+ NSLog (@" %@ : finding %@ with value %@ " , [ self get_table ] , field, val);
47
+ return [self find: [NSString stringWithFormat: @" select * from %@ where %@ ='%@ '" ,[ self get_table ] ,field,val]];
42
48
}
43
49
44
- - (NSMutableArray *) find
50
+ + (NSMutableArray *) find
45
51
{
46
- return [self find: [NSString stringWithFormat: @" select * from %@ " ,self .table_name ]];
52
+ return [self find: [NSString stringWithFormat: @" select * from %@ " ,[ self get_table ] ]];
47
53
}
48
54
49
- - (NSMutableArray *) find : (NSString *)query
55
+ + (NSMutableArray *) find : (NSString *)query
50
56
{
51
- NSLog (@" Running %@ ->find(%@ )" , self. table_name , query);
57
+ NSLog (@" Running %@ ->find(%@ )" , [ self get_table ] , query);
52
58
53
59
NSMutableArray *results= [[NSMutableArray alloc ] init ];
54
60
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,
@@ -71,7 +77,7 @@ -(NSMutableArray*) find:(NSString*)query
71
77
72
78
while (sqlite3_step (compiledStatement) == SQLITE_ROW)
73
79
{
74
- self= [[[self class ] alloc ] init ];
80
+ DataWrapper * self= [[[self class ] alloc ] init ];
75
81
76
82
char *k;
77
83
k= (char *)sqlite3_column_text (compiledStatement, 0 );
@@ -112,7 +118,7 @@ -(NSMutableArray*) find:(NSString*)query
112
118
113
119
-(void ) save
114
120
{
115
- if (!(self.fields && self. table_name ))
121
+ if (!(self.fields && [[ self class ] get_table ] ))
116
122
NSLog (@" WARNING: NSOBJECT WITHOUT TABLE AND DATA STRUCT!" );
117
123
118
124
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,
@@ -129,8 +135,8 @@ -(void) save
129
135
char *sqlStatement;
130
136
if (self.key != nil )
131
137
{
132
- NSLog (@" Updating %@ record %@ " , self. table_name , self.key );
133
- stmt= [NSString stringWithFormat: @" update %@ set " , self .table_name ];
138
+ NSLog (@" Updating %@ record %@ " , [[ self class ] get_table ] , self.key );
139
+ stmt= [NSString stringWithFormat: @" update %@ set " , [[ self class ] get_table ] ];
134
140
for (int i=0 ; i < [self .fields count ]; i++) {
135
141
stmt= [stmt stringByAppendingString: [self .fields objectAtIndex: i]];
136
142
stmt= [stmt stringByAppendingString: @" =?" ];
@@ -141,8 +147,8 @@ -(void) save
141
147
}
142
148
else
143
149
{
144
- NSLog (@" Inserting new %@ record" , self. table_name );
145
- stmt= [NSString stringWithFormat: @" insert into %@ (" , self .table_name ];
150
+ NSLog (@" Inserting new %@ record" , [[ self class ] get_table ] );
151
+ stmt= [NSString stringWithFormat: @" insert into %@ (" , [[ self class ] get_table ] ];
146
152
for (int i=0 ; i < [self .fields count ]; i++)
147
153
{
148
154
stmt= [stmt stringByAppendingString: [self .fields objectAtIndex: i]];
0 commit comments