-
Notifications
You must be signed in to change notification settings - Fork 36
/
FUCoreDataStore.m
324 lines (274 loc) · 7.23 KB
/
FUCoreDataStore.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
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
//
// FUCoreDataStore.m
// Ingredients
//
// Created by Alex Gordon on 22/04/2011.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "FUCoreDataStore.h"
#import "IGKDocRecordManagedObject.h"
@implementation NSManagedObjectContext (FUCoreData)
- (FUCoreDataStore *)fffffffuuuuuuuuuuuu
{
return [[FUCoreDataStore alloc] initWithManagedObjectContext:self];
}
@end
@implementation FUCoreDataStore
@synthesize database;
- (id)initWithManagedObjectContext:(NSManagedObjectContext *)ctx
{
if (self = [super init])
{
NSPersistentStoreCoordinator *pc = [ctx persistentStoreCoordinator];
NSURL *databaseURL = [pc URLForPersistentStore:[[pc persistentStores] igk_firstObject]];
database = [FMDatabase databaseWithPath:[databaseURL path]];
[database setShouldCacheStatements:YES];
[database open];
context = ctx;
if (!database)
return nil;
relationshipToEntityMap = [[NSMutableDictionary alloc] init];
NSManagedObjectModel *model = [pc managedObjectModel];
for (NSEntityDescription *ent in [model entities])
{
NSDictionary *mapping = [ent relationshipsByName];
for (NSString *relName in mapping)
{
NSRelationshipDescription *rel = [mapping valueForKey:relName];
[relationshipToEntityMap setValue:[[rel destinationEntity] name] forKey:relName];
}
}
}
return self;
}
- (void)finalize
{
[database close];
[super finalize];
}
- (id)emptyMagicObject
{
FUCoreDataMagicObject *obj = [[FUCoreDataMagicObject alloc] init];
obj.store = self;
obj.scalarKeyMap = scalarKeyMap;
obj.relationshipToEntityMap = relationshipToEntityMap;
return obj;
}
- (id)magicObjectForEntity:(NSString *)normalEntityName rowid:(id)rowid
{
NSString *coredataizedEntityName = [FUCoreDataMagicObject coredataizeKey:normalEntityName];
FMResultSet *resultSet = [database executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@ WHERE ZROWID=?", coredataizedEntityName], rowid];
id mobj = nil;
if ([resultSet next])
{
mobj = [self magicObjectForResultSet:resultSet];
}
[resultSet close];
return mobj;
}
- (id)magicObjectForResultSet:(FMResultSet *)resultSet
{
NSDictionary *resultDict = [resultSet resultDict];
FUCoreDataMagicObject *obj = [self emptyMagicObject];
obj.results = resultDict;
return obj;
}
- (NSArray *)magicObjectsForResultSet:(FMResultSet *)resultSet
{
NSMutableArray *mobjs = [[NSMutableArray alloc] init];
while ([resultSet next])
{
id mobj = [self magicObjectForResultSet:resultSet];
if (mobj)
[mobjs addObject:mobj];
}
return mobjs;
}
@end
/*
@interface _NSScalarObjectID : NSObject
{
}
- (long long)_referenceData64;
- (id)_retainedURIString;
- (void)dealloc;
- (id)initWithPK64:(long long)arg1;
@end
*/
@implementation FUCoreDataMagicObject
@synthesize store;
@synthesize scalarKeyMap;
@synthesize relationshipToEntityMap;
@synthesize results;
@synthesize managedObject;
/*- (BOOL)isKindOfEntityNamed:(NSString *)entityName
{
NSManagedObjectContext *ctx = [self managedObjectContext];
return [[self entity] isKindOfEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:ctx]];
}*/
- (BOOL)hasKey:(NSString *)key
{
return [self valueForKey:key] != nil;
}
- (id)valueForSoftKey:(NSString *)key
{
return [self valueForKey:key];
}
- (id)resolveManagedObject
{
//Class f_NSScalarObjectID = NSClassFromString(@"_NSScalarObjectID");
//id objid = [[f_NSScalarObjectID alloc] initWithPK64:[[results objectForKey:@"Z_PK"] longLongValue]];
// So the problem is we NEED to find some way of resolving as managed objects.
// OR we need to be able to convert the whole of the reading side of the app to managed objects
// Either way, it's difficult
NSLog(@"TODO: IMPLEMENT RESOLVING MANAGED OBJECTS");
exit(1);
return managedObject;
}
- (id)valueForKey:(id)key
{
// Is this a releationship?
NSString *mappedEntity = [relationshipToEntityMap objectForKey:key];
id result = [results objectForKey:[self coredataizeKeyInMagicObject:key]];
if (mappedEntity)
{
// Follow the relationship
return [store magicObjectForEntity:mappedEntity rowid:result];
}
// If there's no result, fall back to core data
if (result == nil)
{
NSLog(@"Oops no result for key '%@'", key);
return [[self resolveManagedObject] valueForSoftKey:key];
}
// Otherwise just return the result
return result;
}
- (NSString *)coredataizeKeyInMagicObject:(NSString *)normalKey
{
return [scalarKeyMap objectForKey:normalKey] ?: [[self class] coredataizeKey:normalKey];
}
+ (NSString *)coredataizeKey:(NSString *)normalKey
{
// Uppercase
normalKey = [normalKey uppercaseString];
// Prepend a Z
normalKey = [@"Z" stringByAppendingString:normalKey];
return normalKey;
}
#define CALLINPLACE(proxy, ...) return [[proxy class] instanceMethodForSelector:_cmd](self, _cmd, ## __VA_ARGS__)
- (NSString *)pageTitle:(IGKHTMLDisplayTypeMask)mask
{
CALLINPLACE(IGKDocRecordManagedObject, mask);
}
- (NSMutableArray *)getSuperclasses
{
CALLINPLACE(IGKDocRecordManagedObject);
}
- (NSMutableArray *)getSubclasses
{
CALLINPLACE(IGKDocRecordManagedObject);
}
- (id)findNearestClassWithName:(NSString *)className
{
CALLINPLACE(IGKDocRecordManagedObject, className);
}
/*
- (NSString *)URLComponentExtension
{
CALLINPLACE(IGKDocRecordManagedObject);
}
- (NSString *)URLComponent
{
CALLINPLACE(IGKDocRecordManagedObject);
}
- (NSURL *)docURL:(IGKHTMLDisplayTypeMask)tocMask
{
CALLINPLACE(IGKDocRecordManagedObject, tocMask);
}
*/
- (NSString *)documentPath
{
CALLINPLACE(IGKDocRecordManagedObject);
}
//- (void)setDocumentPath:(NSString *)docpath
//- (void)awakeFromInsert
- (CHRecordPriority)priorityval
{
CALLINPLACE(IGKDocRecordManagedObject);
}
- (NSString *)xcontainername
{
CALLINPLACE(IGKDocRecordManagedObject);
}
- (IGKDocRecordManagedObject *)xcontainer
{
CALLINPLACE(IGKDocRecordManagedObject);
}
- (NSString *)xsuperclassname
{
CALLINPLACE(IGKDocRecordManagedObject);
}
- (NSString *)xconforms
{
CALLINPLACE(IGKDocRecordManagedObject);
}
- (NSString *)xdocset
{
CALLINPLACE(IGKDocRecordManagedObject);
}
- (NSImage *)normalIcon
{
CALLINPLACE(IGKDocRecordManagedObject);
}
- (NSImage *)selectedIcon
{
CALLINPLACE(IGKDocRecordManagedObject);
}
- (CHSymbolButtonImageMask)iconMask
{
CALLINPLACE(IGKDocRecordManagedObject);
}
- (NSImage *)iconForSelectedState:(BOOL)isSelected
{
CALLINPLACE(IGKDocRecordManagedObject, isSelected);
}
- (NSNumber *)lengthOfContent
{
CALLINPLACE(IGKDocRecordManagedObject);
}
#pragma mark NSManagedObject fallback
- (id)actionForwardee
{
return [self resolveManagedObject];
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
if ([super respondsToSelector:aSelector])
return YES;
if ([[NSManagedObject class] respondsToSelector:aSelector])
return YES;
return NO;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
if ([super respondsToSelector:aSelector])
return [super methodSignatureForSelector:aSelector];
id forwardee = [self actionForwardee];
if ([forwardee respondsToSelector:aSelector])
return [forwardee methodSignatureForSelector:aSelector];
return [super methodSignatureForSelector:aSelector];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
id forwardee = [self actionForwardee];
if ([forwardee respondsToSelector:[anInvocation selector]])
{
[anInvocation invokeWithTarget:forwardee];
}
else
{
[super forwardInvocation:anInvocation];
}
}
@end