forked from ccgus/CocoaScript
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCOSR.m
More file actions
332 lines (223 loc) · 8.6 KB
/
COSR.m
File metadata and controls
332 lines (223 loc) · 8.6 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//
// COSR.m
// Cocoa Script
//
// Created by August Mueller on 6/24/14.
//
//
#import "COSR.h"
#import "COSDatabaseQueue.h"
#import "COSDatabaseAdditions.h"
#import "COSExtras.h"
#import "COScript.h"
#define STRINGIZE(x) #x
#define STRINGIZE2(x) STRINGIZE(x)
#define SQLCode(text) @ STRINGIZE2(text)
static NSString *COSUTTypeNumber = @"R.number";
static NSString *COSUTTypeTable = @"R.table";
@interface COSR ()
@property (strong) NSString *tablePath;
@property (strong) NSString *tableID;
@property (strong) COSDatabaseQueue *q;
@end
@implementation COSR
- (instancetype)init {
self = [super init];
if (self) {
// _modules = [NSMutableDictionary dictionary];
}
return self;
}
- (instancetype)tableWithPath:(NSString*)path {
COSR *me = [COSR new];
[me setTablePath:path];
[me setQ:[self q]];
return me;
}
+ (instancetype)rootWithURL:(NSURL*)fileURL {
COSR *me = [COSR new];
[me setQ:[COSDatabaseQueue databaseQueueWithPath:[fileURL path]]];
[me setupDatabase];
return me;
}
+ (instancetype)rootWithPath:(NSString*)filePath {
return [self rootWithURL:[NSURL fileURLWithPath:filePath]];
}
- (void)setupDatabase {
[_q inDatabase:^(COSDatabase *db) {
if (![db tableExists:@"R"]) {
NSString *const rDB = SQLCode (
create table R (uniqueID text,
name text not null,
data blob,
uti text not null,
parentID text);
);
if (![db executeUpdate:rDB]) {
NSLog(@"error creating R table: %@", [db lastError]);
}
}
}];
}
- (instancetype)subTableWithName:(NSString*)name {
if (_tablePath) {
return [self tableWithPath:[_tablePath stringByAppendingFormat:@".%@", name]];
}
return [self tableWithPath:name];
}
- (instancetype)makeTable:(NSString*)tableName {
COSR *sub = [self subTableWithName:tableName];
// FIXME: make sure to return an existing one if it's already around
[sub setTableID:[NSString stringWithUUID]];
[self setObject:sub forKeyedSubscript:tableName];
return sub;
}
- (id)fsObjectWithKey:(NSString*)key {
debug(@"key: '%@'", key);
NSString *basePath = [[_q path] stringByDeletingLastPathComponent];
// FIXME: is this too fragile?
NSString *subFolder = [_tablePath stringByReplacingOccurrencesOfString:@"." withString:@"/"];
basePath = [basePath stringByAppendingPathComponent:subFolder];
NSString *filePath = [basePath stringByAppendingPathComponent:key];
NSString *lookupPath = filePath;
BOOL found = [[NSFileManager defaultManager] fileExistsAtPath:lookupPath];
if (!found) {
NSArray *extensions = @[@"js", @"coscript"];
for (NSString *ext in extensions) {
lookupPath = [filePath stringByAppendingPathExtension:ext];
debug(@"lookupPath: '%@'", lookupPath);
if ([[NSFileManager defaultManager] fileExistsAtPath:lookupPath]) {
found = YES;
break;
}
}
}
if (found) {
NSError *outErr = nil;
NSString *script = [NSString stringWithContentsOfFile:lookupPath encoding:NSUTF8StringEncoding error:&outErr];
if (!script) {
NSLog(@"Error reading path '%@'", lookupPath);
NSLog(@"%@", outErr);
return nil;
}
NSString *rep = [NSString stringWithFormat:@"R.%@.%@", _tablePath, key];
script = [script stringByReplacingOccurrencesOfString:@"$R" withString:rep];
COScript *cos = [COScript currentCOScript];
id r = [cos executeString:script baseURL:[NSURL fileURLWithPath:lookupPath]];
return r;
}
return nil;
}
- (id)objectForKeyedSubscript:(NSString *)key {
__block id value = nil;
[_q inDatabase:^(COSDatabase *db) {
NSString *query = @"select data, uti, uniqueID from R where name = ? and parentID = ?";
if (!_tableID) {
query = @"select data, uti, uniqueID from R where name = ? and parentID is null";
}
COSResultSet *rs = [db executeQuery:query, key, _tableID];
if ([rs next]) {
CFStringRef uti = (__bridge CFStringRef)[rs stringForColumn:@"uti"];
if (UTTypeConformsTo(uti, kUTTypeText)) {
value = [rs stringForColumn:@"data"];
}
else if ([(__bridge id)uti isEqualToString:COSUTTypeNumber]) {
value = [rs objectForColumnName:@"data"];
assert([value isKindOfClass:[NSNumber class]]);
}
else if ([(__bridge id)uti isEqualToString:COSUTTypeTable]) {
value = [self subTableWithName:key];
[(COSR*)value setTableID:[rs stringForColumn:@"uniqueID"]];
}
else {
value = [rs dataForColumn:@"data"];
}
[rs close];
}
}];
if (!value) {
value = [self fsObjectWithKey:key];
}
if (!value) {
NSLog(@"Could not find value for key '%@' in table %@ / %@", key, _tablePath, _tableID);
}
return value;
}
/*
- (void)_dynamicContextEvaluation:(id)e patternString:(NSString*)s {
debug(@"s: '%@'", s);
debug(@"e: '%@' (%@)", e, NSStringFromClass([e class]));
}
*/
- (void)setObject:(id)theObj forKeyedSubscript:(NSString *)key {
debug(@"%@: '%@'", key, theObj);
__block id obj = theObj;
[_q inDatabase:^(COSDatabase *db) {
NSString *uuid = [NSString stringWithUUID];
NSString *uti = (id)kUTTypeData;
if ([obj isKindOfClass:[NSString class]]) {
uti = (id)kUTTypeUTF8PlainText;
}
else if ([obj isKindOfClass:[NSNumber class]]) {
uti = COSUTTypeNumber;
}
else if ([obj isKindOfClass:[COSR class]]) {
uti = COSUTTypeTable;
uuid = [(COSR*)obj tableID];
assert([(COSR*)obj tableID]);
obj = [NSNull null];
}
NSString *delete = @"delete from R where name = ? and parentID = ?";
if (!_tableID) {
delete = @"delete from R where name = ? and parentID is null";
}
[db executeUpdate:delete, key, _tableID];
if (obj) {
NSString *sql = @"insert into R (uniqueID, name, data, uti, parentID) values (?, ?, ?, ?, ?)";
if (![db executeUpdate:sql, uuid, key, obj, uti, _tableID]) {
NSLog(@"Could not set value '%@' for '%@' in table '%@'", obj, key, _tablePath ? _tablePath : [NSNull null]);
}
}
if ([_tablePath length]) {
assert(_tableID);
}
}];
}
- (NSArray*)subTables {
NSMutableArray *subTables = [NSMutableArray array];
[_q inDatabase:^(COSDatabase *db) {
NSString *query = @"select name from R where uti = ? and parentID = ?";
if (!_tablePath) {
query = @"select name, uniqueID from R where uti = ? and parentID is null";
}
COSResultSet *rs = [db executeQuery:query, COSUTTypeTable, _tableID];
while ([rs next]) {
COSR *sub = [self subTableWithName:[rs stringForColumn:@"name"]];
[sub setTableID:[rs stringForColumn:@"uniqueID"]];
[subTables addObject:sub];
}
}];
return subTables;
}
- (NSArray*)keys {
NSMutableArray *keyNames = [NSMutableArray array];
[_q inDatabase:^(COSDatabase *db) {
NSString *query = @"select name from R where parentID = ?";
if (!_tablePath) {
query = @"select name from R where parentID is null";
}
COSResultSet *rs = [db executeQuery:query, _tableID];
while ([rs next]) {
[keyNames addObject:[rs stringForColumn:@"name"]];
}
}];
return keyNames;
}
- (NSString*)description {
return [[super description] stringByAppendingFormat:@" (%@ id:%@)", _tablePath, _tableID];
}
- (BOOL)respondsToSelector:(SEL)aSelector {
debug(@"-[%@ %@]?", NSStringFromClass([self class]), NSStringFromSelector(aSelector));
return [super respondsToSelector:aSelector];
}
@end