home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Database / OTC_EOFBetaExamples_V1.0 / EOFramework / AccessLayer / DatabaseLevel.m < prev    next >
Encoding:
Text File  |  1994-07-30  |  4.4 KB  |  163 lines

  1. /*--------------------------------------------------------------------------
  2.  *
  3.  *     You may freely copy, distribute, and reuse the code in this example.
  4.  *     SHL Systemhouse disclaims any warranty of any kind, expressed or  
  5.  *    implied, as to its fitness for any particular use.
  6.  *
  7.  *
  8.  *    DatabaseLevel
  9.  *
  10.  *    Inherits From:        NSObject
  11.  *
  12.  *    Conforms To:        None
  13.  *
  14.  *    Declared In:        DatabaseLevel.h
  15.  *
  16.  *------------------------------------------------------------------------*/
  17. #import "DatabaseLevel.h"
  18. #import "ScrollViewExtensions.h"
  19. #import <eoaccess/eoaccess.h>
  20. #import <appkit/appkit.h>
  21.  
  22.  
  23.  
  24. @implementation DatabaseLevel
  25.  
  26. - connect: sender
  27. {
  28.     id    path;
  29.     
  30.     [messageConsole clear: nil];
  31.     [messageConsole sprintf: "connecting to database...\n"];
  32.  
  33.     [messageConsole sprintf: "obtaining model path...\n"];
  34.     if ((path = [EOModel findPathForModelNamed:
  35.         [NSString stringWithCString:"Authors"]]) == nil)
  36.         {
  37.         NXRunAlertPanel (NULL, "Unable to obtain model path", 
  38.             NULL, NULL, NULL);
  39.         [messageConsole sprintf: "unable to obtain model path\n"];
  40.         return nil;
  41.         }
  42.  
  43.     [modelPath setStringValue: [path cString]];
  44.         
  45.     [messageConsole sprintf: "initializing model...\n"];
  46.     if ((model = [[EOModel alloc] initWithContentsOfFile:path]) == nil)
  47.         {
  48.         NXRunAlertPanel (NULL, "Unable to initialize model", NULL, NULL, NULL);
  49.         [messageConsole sprintf: "unable to initialize model\n"];
  50.         return nil;
  51.         }
  52.     
  53.     [messageConsole sprintf: "creating database...\n"];
  54.     if ((database = [[EODatabase alloc] initWithModel:model]) == nil)
  55.         {
  56.         NXRunAlertPanel (NULL, "Unable to create database", NULL, NULL, NULL);
  57.         [messageConsole sprintf: "unable to create database\n"];
  58.         return nil;
  59.         }
  60.     
  61.     [messageConsole sprintf: "creating context...\n"];
  62.     if ((context = [[EODatabaseContext alloc] 
  63.         initWithDatabase:database]) == nil)
  64.         {
  65.         NXRunAlertPanel (NULL, "Unable to create context", NULL, NULL, NULL);
  66.         [messageConsole sprintf: "unable to create context\n"];
  67.         return nil;
  68.         }
  69.         
  70.     [messageConsole sprintf: "creating channel...\n"];
  71.     if ((channel = [[EODatabaseChannel alloc] 
  72.         initWithDatabaseContext:context]) == nil)
  73.         {
  74.         NXRunAlertPanel (NULL, "Unable to create channel", NULL, NULL, NULL);
  75.         [messageConsole sprintf: "unable to create channel\n"];
  76.         return nil;
  77.         }
  78.  
  79.     [messageConsole sprintf: "opening channel...\n"];
  80.     if ([channel openChannel] == NO)
  81.         {
  82.         NXRunAlertPanel (NULL, "Unable to open channel", NULL, NULL, NULL);
  83.         [messageConsole sprintf: "unable to open channel\n"];
  84.         return nil;
  85.         }
  86.         
  87.     [messageConsole sprintf:"channel %s open\n\n", 
  88.         [channel isOpen] ? "is" : "is not"];
  89.     [connectButton setEnabled: ! [channel isOpen]];
  90.     [selectButton setEnabled: [channel isOpen]];
  91.     return self;
  92. }
  93.  
  94.  
  95. - select: sender
  96. {
  97.     int    count = 0;
  98.     id    entity, qualifier, object;
  99.     
  100.     [resultConsole clear: nil];
  101.     [messageConsole sprintf: "beginning transaction...\n"];
  102.  
  103.     if ([context beginTransaction] == NO)
  104.         {
  105.         NXRunAlertPanel (NULL, "Unable to begin transaction", 
  106.             NULL, NULL, NULL);
  107.         [messageConsole sprintf: "unable to begin transaction\n"];
  108.         return nil;
  109.         }
  110.         
  111.     [messageConsole sprintf: "obtaining entity...\n"];
  112.     if ((entity = [model entityNamed: @"authors"]) == nil)
  113.         {
  114.         NXRunAlertPanel (NULL, "Unable to obtain entity authors", 
  115.             NULL, NULL, NULL);
  116.         [messageConsole sprintf: "unable to obtain entity authors\n"];
  117.         return nil;
  118.         }
  119.         
  120.     [messageConsole sprintf: "obtaining qualifier...\n"];
  121.     if ((qualifier = [entity qualifier]) == nil)
  122.         {
  123.         NXRunAlertPanel (NULL, "Unable to obtain qualifier", 
  124.             NULL, NULL, NULL);
  125.         [messageConsole sprintf: "unable to obtain qualifier\n"];
  126.         return nil;
  127.         }
  128.         
  129.     [messageConsole sprintf: "selecting objects...\n"];
  130.     if ([channel selectObjectsDescribedByQualifier:qualifier
  131.         fetchOrder:nil] == NO)
  132.         {
  133.         NXRunAlertPanel (NULL, "Unable to select objects", 
  134.             NULL, NULL, NULL);
  135.         [messageConsole sprintf: "unable to select objects\n"];
  136.         return nil;
  137.         }
  138.         
  139.     while ((object = [channel fetchWithZone:[(EODatabase*)database zone]]))
  140.         {
  141.         [resultConsole sprintf: "%s\n", 
  142.             [[(NSString*)object description] cString]];
  143.         count++;
  144.         }
  145.         
  146.     [messageConsole sprintf: "\n%d records found\n\n", count];
  147.         
  148.     [messageConsole sprintf: "rolling back transaction...\n"];
  149.     if ([context rollbackTransaction] == NO)
  150.         {
  151.         NXRunAlertPanel (NULL, "Unable to rollback transaction", 
  152.             NULL, NULL, NULL);
  153.         [messageConsole sprintf: "unable to rollback transaction\n"];
  154.         return nil;
  155.         }
  156.         
  157.     [messageConsole sprintf: "done\n\n"];
  158.     return self;
  159. }
  160.  
  161.  
  162. @end
  163.