Skip to content

Commit d9d6340

Browse files
Make find: methods static
1 parent 32e9704 commit d9d6340

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

DataWrapper.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,16 @@
2626

2727
@interface DataWrapper : NSObject {
2828
NSString *key;
29-
NSString *table_name;
3029
NSMutableArray *fields;
3130
}
3231
-(void) save;
3332
-(void) load:(NSString *)byid;
34-
-(NSMutableArray*) find; // returns all
35-
-(NSMutableArray*) find: (NSString*)field withValue:(NSString*)val;
36-
-(NSMutableArray*) find: (NSString*)query;
33+
+(NSMutableArray*) find; // returns all
34+
+(NSMutableArray*) find: (NSString*)field withValue:(NSString*)val;
35+
+(NSMutableArray*) find: (NSString*)query;
36+
+(NSString *)get_table;
3737

3838
@property (retain) NSString *key;
39-
@property (retain) NSString *table_name;
4039
@property (retain) NSMutableArray *fields;
4140

4241
@end

DataWrapper.m

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,35 @@
2626

2727
@implementation DataWrapper
2828

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+
}
3137

3238
-(void) load:(NSString *)byid
3339
{
3440
NSLog(@"finding %@", byid);
35-
self= [[self find:@"id" withValue:(NSString *)byid] objectAtIndex:0];
41+
self= [[[self class] find:@"id" withValue:(NSString *)byid] objectAtIndex:0];
3642
}
3743

38-
-(NSMutableArray*) find:(NSString*)field withValue:(NSString*)val
44+
+(NSMutableArray*) find:(NSString*)field withValue:(NSString*)val
3945
{
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]];
4248
}
4349

44-
-(NSMutableArray*) find
50+
+(NSMutableArray*) find
4551
{
46-
return [self find:[NSString stringWithFormat:@"select * from %@",self.table_name]];
52+
return [self find:[NSString stringWithFormat:@"select * from %@",[self get_table]]];
4753
}
4854

49-
-(NSMutableArray*) find:(NSString*)query
55+
+(NSMutableArray*) find:(NSString*)query
5056
{
51-
NSLog(@"Running %@->find(%@)", self.table_name, query);
57+
NSLog(@"Running %@->find(%@)", [self get_table], query);
5258

5359
NSMutableArray *results= [[NSMutableArray alloc] init];
5460
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
@@ -71,7 +77,7 @@ -(NSMutableArray*) find:(NSString*)query
7177

7278
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
7379
{
74-
self= [[[self class] alloc] init];
80+
DataWrapper *self= [[[self class] alloc] init];
7581

7682
char *k;
7783
k= (char *)sqlite3_column_text(compiledStatement, 0);
@@ -112,7 +118,7 @@ -(NSMutableArray*) find:(NSString*)query
112118

113119
-(void) save
114120
{
115-
if (!(self.fields && self.table_name))
121+
if (!(self.fields && [[self class] get_table]))
116122
NSLog(@"WARNING: NSOBJECT WITHOUT TABLE AND DATA STRUCT!");
117123

118124
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
@@ -129,8 +135,8 @@ -(void) save
129135
char *sqlStatement;
130136
if (self.key != nil)
131137
{
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]];
134140
for (int i=0; i < [self.fields count]; i++) {
135141
stmt= [stmt stringByAppendingString: [self.fields objectAtIndex:i]];
136142
stmt= [stmt stringByAppendingString: @"=?"];
@@ -141,8 +147,8 @@ -(void) save
141147
}
142148
else
143149
{
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]];
146152
for (int i=0; i < [self.fields count]; i++)
147153
{
148154
stmt= [stmt stringByAppendingString: [self.fields objectAtIndex:i]];

0 commit comments

Comments
 (0)