-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDumbRecord.m
181 lines (143 loc) · 7.46 KB
/
DumbRecord.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
//
// DumbRecord.m
// DumbRecord
//
// Created by Jerome Poichet on 3/16/10.
// Copyright 2010 Jerome Poichet. All rights reserved.
//
#import "DumbRecord.h"
#import "NSStringAdditions.h"
#include <objc/runtime.h> //objc runtime api’s
@implementation DumbRecord
+ (DRLite *) setup: (NSString *) database
{
return [DumbRecord setup: database withModels: nil];
}
+ (DRLite *) setup: (NSString *) database withModels: (NSArray *)models
{
DRLite *db = [[[DRLite alloc] initWithDatabase: database] autorelease];
if ([models count] == 0) {
return db;
}
// Get current list of tables
NSArray *rows = nil;
NSError *error = nil;
NSMutableArray *tables = [[NSMutableArray alloc] init];
int i, n;
rows = [db query: @"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name" withError: &error];
n = [rows count];
for (i = 0; i < n; i++) {
[tables addObject: [[rows objectAtIndex: i] objectForKey: @"name"]];
}
// Do we have all tables?
n = [models count];
for (i = 0; i < n; i++) {
NSString *model = [models objectAtIndex: i];
NSString *table_name = [[model lowercaseString] plural];
NSString *id_column_name = [[model lowercaseString] stringByAppendingString: @"_id"];
//NSLog(@"%@ %@ %@", model, table_name, id_column_name);
if ([tables containsObject: table_name]) {
// Table exists already, figure out the differences
} else {
// New model, create table
NSMutableDictionary *columns = [[NSMutableDictionary alloc] init];
[columns setObject: @"INTEGER PRIMARY KEY" forKey: id_column_name];
Class class = NSClassFromString(model);
id modelClass = objc_getClass([model cStringUsingEncoding: NSASCIIStringEncoding]);
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(modelClass, &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
NSString *name = [NSString stringWithCString: property_getName(property)
encoding: NSASCIIStringEncoding];
NSString *attr = [NSString stringWithCString: property_getAttributes(property)
encoding: NSASCIIStringEncoding];
NSArray *chunks = [attr componentsSeparatedByString: @","];
//NSLog(@"%@", chunks);
NSString *type = [chunks objectAtIndex: 0];
if ([[type substringWithRange: NSMakeRange(1, 1)] isEqualToString: @"@"]) {
type = [type substringWithRange: NSMakeRange(3, [type length] - 4)];
} else {
// Primitive type
type = [type substringFromIndex: 1];
}
// Figure out if we need uniqueness
BOOL unique = [class shouldColumnBeUnique: name];
if (![name isEqualToString: id_column_name]) {
NSLog(@"%@ %@ %@", name, type, unique ? @"Unique" : @"");
NSDictionary *typeMap = [NSDictionary dictionaryWithObjectsAndKeys:
@"INTEGER", @"i",
@"INTEGER", @"I",
@"FLOAT", @"f",
@"INTEGER", @"l",
@"INTEGER", @"s",
@"INTEGER", @"c",
@"INTEGER", @"NSNumber",
@"TEXT", @"NSString",
@"BLOB", @"NSData",
@"INTEGER", @"NSDate",
nil];
NSString *column;
if ((column = [typeMap objectForKey: type])) {
if (unique) {
column = [column stringByAppendingString: @" UNIQUE"];
}
[columns setObject: column forKey: name];
} else {
// Ignore unknown
NSLog(@"Ignoring column %@ of type %@", name, type);
}
}
}
NSLog(@"%@", columns);
NSString *query = [NSString stringWithFormat: @"CREATE TABLE %@ (", table_name];
NSEnumerator *enumerator = [columns keyEnumerator];
NSString *columnName;
int j = 0, m = [columns count];
while ((columnName = [enumerator nextObject])) {
query = [query stringByAppendingFormat: @"%@ %@", columnName, [columns objectForKey: columnName]];
if (j < m - 1) {
query = [query stringByAppendingString: @", "];
}
j++;
}
[columns release];
query = [query stringByAppendingString: @")"];
NSLog(@"%@", query);
[db query: query withError: &error];
if (error) {
NSLog(@"Failed to create table %@: %@", table_name, error);
} else {
// Retrieve indexes
NSArray *indexesTuples = [class indexes];
m = [indexesTuples count];
for (j = 0; j < m; j++) {
if ([[indexesTuples objectAtIndex: j] isKindOfClass: [NSString class]]) {
NSString *name = [indexesTuples objectAtIndex: j];
NSString *indexName = [name stringByAppendingString: @"Index"];
NSString *indexQuery = [NSString stringWithFormat: @"CREATE INDEX IF NOT EXISTS %@ ON %@ (%@)", indexName, table_name, name];
NSLog(@"%@", indexQuery);
[db query: indexQuery withError: &error];
if (error) {
NSLog(@"Failed to create index %@: %@", indexName, error);
}
} else if ([[indexesTuples objectAtIndex: j] isKindOfClass: [NSArray class]]) {
// Indexing multiple columns
NSArray *tuples = [indexesTuples objectAtIndex: j];
NSString *indexName = [[tuples componentsJoinedByString: @""] stringByAppendingString: @"Index"];
NSString *name = [tuples componentsJoinedByString: @","];
NSString *indexQuery = [NSString stringWithFormat: @"CREATE INDEX IF NOT EXISTS %@ ON %@ (%@)", indexName, table_name, name];
NSLog(@"%@", indexQuery);
[db query: indexQuery withError: &error];
if (error) {
NSLog(@"Failed to create index %@: %@", indexName, error);
}
}
}
}
}
}
[tables release];
return db;
}
@end