Changeset 317

Show
Ignore:
Timestamp:
08/05/08 10:02:20 (4 months ago)
Author:
wolf
Message:

[NEW] mogenerator: Add preliminary fetch request wrapper support.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cocoa/mogenerator/mogenerator.m

    r314 r317  
    66        ***************************************************************************/ 
    77 
    8 #import <Foundation/Foundation.h> 
    9 #import <CoreData/CoreData.h> 
    10  
    11 #import "MiscMergeTemplate.h" 
    12 #import "MiscMergeCommandBlock.h" 
    13 #import "MiscMergeEngine.h" 
    14 #import "FoundationAdditions.h" 
    15 #import "nsenumerate.h" 
    16 #import "NSString+MiscAdditions.h" 
    17 #import "DDCommandLineInterface.h" 
     8#import "mogenerator.h" 
    189 
    1910NSString        *gCustomBaseClass; 
    2011 
    21 @interface NSEntityDescription (customBaseClass) 
    22 - (BOOL)hasCustomSuperentity; 
    23 - (NSString*)customSuperentity; 
    24 @end 
    2512@implementation NSEntityDescription (customBaseClass) 
    2613- (BOOL)hasCustomSuperentity { 
     
    6249        } 
    6350} 
     51 
     52#pragma mark Fetch Request support 
     53 
     54- (NSDictionary*)fetchRequestTemplates { 
     55        // -[NSManagedObjectModel _fetchRequestTemplatesByName] is a private method, but it's the only way to get 
     56        //      model fetch request templates without knowing their name ahead of time. rdar://problem/4901396 asks for 
     57        //      a public method (-[NSManagedObjectModel fetchRequestTemplatesByName]) that does the same thing. 
     58        //      If that request is fulfilled, this code won't need to be modified thanks to KVC lookup order magic. 
     59    //  UPDATE: 10.5 now has a public -fetchRequestTemplatesByName method. 
     60        NSDictionary *fetchRequests = [[self managedObjectModel] valueForKey:@"fetchRequestTemplatesByName"]; 
     61         
     62        NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:[fetchRequests count]]; 
     63        nsenumerate ([fetchRequests allKeys], NSString, fetchRequestName) { 
     64                NSFetchRequest *fetchRequest = [fetchRequests objectForKey:fetchRequestName]; 
     65                if ([fetchRequest entity] == self) { 
     66                        [result setObject:fetchRequest forKey:fetchRequestName]; 
     67                } 
     68        } 
     69        return result; 
     70} 
     71- (void)_processPredicate:(NSPredicate*)predicate_ bindings:(NSMutableArray*)bindings_ { 
     72        if ([predicate_ isKindOfClass:[NSCompoundPredicate class]]) { 
     73                nsenumerate([(NSCompoundPredicate*)predicate_ subpredicates], NSPredicate, subpredicate) { 
     74                        [self _processPredicate:subpredicate bindings:bindings_]; 
     75                } 
     76        } else { 
     77                assert([[(NSComparisonPredicate*)predicate_ leftExpression] expressionType] == NSKeyPathExpressionType); 
     78                NSExpression *lhs = [(NSComparisonPredicate*)predicate_ leftExpression]; 
     79                NSExpression *rhs = [(NSComparisonPredicate*)predicate_ rightExpression]; 
     80                switch([rhs expressionType]) { 
     81                        case NSConstantValueExpressionType: 
     82                        case NSEvaluatedObjectExpressionType: 
     83                        case NSKeyPathExpressionType: 
     84                        case NSFunctionExpressionType: 
     85                                //      Don't do anything with these. 
     86                                break; 
     87                        case NSVariableExpressionType: { 
     88                                // TODO SHOULD Handle LHS keypaths. 
     89                 
     90                NSString *type = nil; 
     91                 
     92                NSAttributeDescription *attribute = [[self attributesByName] objectForKey:[lhs keyPath]]; 
     93                if (attribute) { 
     94                    type = [attribute objectAttributeType]; 
     95                    type = [type stringByAppendingString:@"*"]; 
     96                } else { 
     97                    //  Probably a relationship 
     98                    assert(0&&"TODO: add relationship support"); 
     99                } 
     100                 
     101                                [bindings_ addObject:[NSDictionary dictionaryWithObjectsAndKeys: 
     102                                      [rhs variable], @"name", 
     103                                      type, @"type", 
     104                                      nil]]; 
     105                        } break; 
     106                        default: 
     107                                assert(0 && "unknown NSExpression type"); 
     108                } 
     109        } 
     110} 
     111- (NSArray*)prettyFetchRequests { 
     112        NSDictionary *fetchRequests = [self fetchRequestTemplates]; 
     113        NSMutableArray *result = [NSMutableArray arrayWithCapacity:[fetchRequests count]]; 
     114        nsenumerate ([fetchRequests allKeys], NSString, fetchRequestName) { 
     115                NSFetchRequest *fetchRequest = [fetchRequests objectForKey:fetchRequestName]; 
     116                NSMutableArray *bindings = [NSMutableArray array]; 
     117                [self _processPredicate:[fetchRequest predicate] bindings:bindings]; 
     118                [result addObject:[NSDictionary dictionaryWithObjectsAndKeys: 
     119                           fetchRequestName, @"name", 
     120                           bindings, @"bindings", 
     121                           nil]]; 
     122        } 
     123        return result; 
     124} 
    64125@end 
    65 @interface NSAttributeDescription (scalarAttributeType) 
    66 - (BOOL)hasScalarAttributeType; 
    67 - (NSString*)scalarAttributeType; 
    68 - (BOOL)hasDefinedAttributeType; 
    69 - (NSString*)objectAttributeType; 
    70 @end 
     126 
    71127@implementation NSAttributeDescription (scalarAttributeType) 
    72128- (BOOL)hasScalarAttributeType { 
     
    123179} 
    124180@end 
    125 @interface NSString (camelCaseString) 
    126 - (NSString*)camelCaseString; 
    127 @end 
     181 
    128182@implementation NSString (camelCaseString) 
    129183- (NSString*)camelCaseString { 
     
    147201        return [[[MiscMergeEngine alloc] initWithTemplate:template] autorelease]; 
    148202} 
    149  
    150 @interface MOGeneratorApp : NSObject <DDCliApplicationDelegate> { 
    151         NSString                                *tempMOMPath; 
    152         NSManagedObjectModel    *model; 
    153         NSString                                *baseClass; 
    154         NSString                                *includem; 
    155         NSString                                *templatePath; 
    156         NSString                                *outputDir; 
    157         NSString                                *machineDir; 
    158         NSString                                *humanDir; 
    159         NSString                                *templateGroup; 
    160         BOOL                                    _help; 
    161         BOOL                                    _version; 
    162 } 
    163  
    164 - (NSString*)appSupportFileNamed:(NSString*)fileName_; 
    165 @end 
    166203 
    167204@implementation MOGeneratorApp 
     
    291328    if (_version) 
    292329    { 
    293         printf("mogenerator 1.10.1. By Jonathan 'Wolf' Rentzsch + friends.\n"); 
     330        printf("mogenerator 1.11. By Jonathan 'Wolf' Rentzsch + friends.\n"); 
    294331        return EXIT_SUCCESS; 
    295332    } 
  • trunk/cocoa/mogenerator/mogenerator.xcodeproj/project.pbxproj

    r283 r317  
    7070                55200F9B0C4A2CA800018A42 /* DDCliParseException.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = DDCliParseException.h; sourceTree = "<group>"; }; 
    7171                55200F9C0C4A2CA800018A42 /* DDCliParseException.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = DDCliParseException.m; sourceTree = "<group>"; }; 
     72                79A586510E48E8B400474C35 /* mogenerator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mogenerator.h; sourceTree = "<group>"; }; 
    7273                79D2BF3E0ACFB25B00F3F141 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = "<absolute>"; }; 
    7374                79D2BF520ACFB51A00F3F141 /* _MiscMergeBreakCommand.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = _MiscMergeBreakCommand.h; path = ../MiscMerge/_MiscMergeBreakCommand.h; sourceTree = "<group>"; }; 
     
    185186                        children = ( 
    186187                                08FB7796FE84155DC02AAC07 /* mogenerator.m */, 
     188                                79A586510E48E8B400474C35 /* mogenerator.h */, 
    187189                                79D2C00E0ACFB81200F3F141 /* nsenumerate.h */, 
    188190                                79D2C0570ACFBCB500F3F141 /* FoundationAdditions.h */, 
  • trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_ChildMO.h

    r313 r317  
    3535 
    3636 
     37 
    3738@end 
  • trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_ChildMO.m

    r313 r317  
    5555         
    5656 
     57 
     58 
    5759@end 
  • trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_HumanMO.h

    r313 r317  
    4747 
    4848 
     49 
     50+ (NSArray*)fetchByHumanName:(NSManagedObjectContext*)moc_ humanName:(NSString*)humanName_; 
     51+ (NSArray*)fetchByHumanName:(NSManagedObjectContext*)moc_ humanName:(NSString*)humanName_ error:(NSError**)error_; 
     52 
    4953@end 
  • trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_HumanMO.m

    r313 r317  
    7575         
    7676 
     77 
     78 
     79+ (NSArray*)fetchByHumanName:(NSManagedObjectContext*)moc_ humanName:(NSString*)humanName_ { 
     80        NSError *error = nil; 
     81        id result = [self fetchByHumanName:moc_ humanName:humanName_ error:&error]; 
     82        if (error) { 
     83                [NSApp presentError:error]; 
     84        } 
     85        return result; 
     86} 
     87+ (NSArray*)fetchByHumanName:(NSManagedObjectContext*)moc_ humanName:(NSString*)humanName_ error:(NSError**)error_ { 
     88        NSError *error = nil; 
     89         
     90        NSManagedObjectModel *model = [[moc_ persistentStoreCoordinator] managedObjectModel]; 
     91        NSFetchRequest *fetchRequest = [model fetchRequestFromTemplateWithName:@"byHumanName" 
     92                                                                                                         substitutionVariables:[NSDictionary dictionaryWithObjectsAndKeys: 
     93                                                                                                                 
     94                                                                                                                humanName_, @"humanName", 
     95                                                                                                                 
     96                                                                                                                nil] 
     97                                                                                                         ]; 
     98        NSAssert(fetchRequest, @"Can't find fetch request named \"byHumanName\"."); 
     99         
     100        NSArray *result = [moc_ executeFetchRequest:fetchRequest error:&error]; 
     101        if (error_) *error_ = error; 
     102        return result; 
     103} 
     104 
    77105@end 
  • trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_ParentMO.h

    r313 r317  
    169169 
    170170 
     171 
    171172@end 
  • trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_ParentMO.m

    r313 r317  
    364364         
    365365 
     366 
     367 
    366368@end 
  • trunk/cocoa/mogenerator/mogeneratorTestMule/mogeneratorTestMule.xcodeproj/project.pbxproj

    r228 r317  
    168168                        isa = PBXProject; 
    169169                        buildConfigurationList = 26FC0A880875C7B200E6366F /* Build configuration list for PBXProject "mogeneratorTestMule" */; 
     170                        compatibilityVersion = "Xcode 2.4"; 
    170171                        hasScannedForEncodings = 1; 
    171172                        mainGroup = 29B97314FDCFA39411CA2CEA /* mogeneratorTestMule */; 
  • trunk/cocoa/mogenerator/templates/machine.h.motemplate

    r300 r317  
    4848<$endif$> 
    4949<$endforeach do$> 
     50<$foreach FetchRequest prettyFetchRequests do$> 
     51+ (NSArray*)fetch<$FetchRequest.name.initialCapitalString$>:(NSManagedObjectContext*)moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:(<$Binding.type$>)<$Binding.name$>_<$endforeach do2$>; 
     52+ (NSArray*)fetch<$FetchRequest.name.initialCapitalString$>:(NSManagedObjectContext*)moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:(<$Binding.type$>)<$Binding.name$>_<$endforeach do2$> error:(NSError**)error_; 
     53<$endforeach do$> 
    5054@end 
  • trunk/cocoa/mogenerator/templates/machine.m.motemplate

    r314 r317  
    115115        <$endif$> 
    116116<$endforeach do$> 
     117 
     118<$foreach FetchRequest prettyFetchRequests do$> 
     119+ (NSArray*)fetch<$FetchRequest.name.initialCapitalString$>:(NSManagedObjectContext*)moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:(<$Binding.type$>)<$Binding.name$>_<$endforeach do2$> { 
     120        NSError *error = nil; 
     121        id result = [self fetch<$FetchRequest.name.initialCapitalString$>:moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:<$Binding.name$>_<$endforeach do2$> error:&error]; 
     122        if (error) { 
     123                [NSApp presentError:error]; 
     124        } 
     125        return result; 
     126} 
     127+ (NSArray*)fetch<$FetchRequest.name.initialCapitalString$>:(NSManagedObjectContext*)moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:(<$Binding.type$>)<$Binding.name$>_<$endforeach do2$> error:(NSError**)error_ { 
     128        NSError *error = nil; 
     129         
     130        NSManagedObjectModel *model = [[moc_ persistentStoreCoordinator] managedObjectModel]; 
     131        NSFetchRequest *fetchRequest = [model fetchRequestFromTemplateWithName:@"<$FetchRequest.name$>" 
     132                                                                                                         substitutionVariables:[NSDictionary dictionaryWithObjectsAndKeys: 
     133                                                                                                                <$foreach Binding FetchRequest.bindings do2$> 
     134                                                                                                                <$Binding.name$>_, @"<$Binding.name$>", 
     135                                                                                                                <$endforeach do2$> 
     136                                                                                                                nil] 
     137                                                                                                         ]; 
     138        NSAssert(fetchRequest, @"Can't find fetch request named \"<$FetchRequest.name$>\"."); 
     139         
     140        NSArray *result = [moc_ executeFetchRequest:fetchRequest error:&error]; 
     141        if (error_) *error_ = error; 
     142        return result; 
     143} 
     144<$endforeach do$> 
    117145@end