home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / EODEV.Z / FlatFileDescription.m < prev    next >
Encoding:
Text File  |  1996-08-23  |  3.6 KB  |  124 lines

  1. /*
  2.         Copyright (c) 1996, NeXT Software, Inc.
  3.         All rights reserved.
  4.  
  5.         You may freely copy, distribute and reuse the code in this example.
  6.         NeXT disclaims any warranty of any kind, expressed or implied,
  7.         as to its fitness for any particular use.
  8. */
  9. #import "FlatFileDescription.h"
  10. #import "FlatFileAdaptor.h"
  11. #import "FlatFileContext.h"
  12.  
  13. @interface FlatFileChannel (ModelDescriptionPrivate)
  14.  
  15. - (EOAttribute *)newAttributeWithColumnInfo:(NSDictionary *)info;
  16.  
  17. @end
  18.  
  19. @implementation FlatFileChannel (ModelDescription)
  20.  
  21. - (NSArray *)describeTableNames
  22. {
  23.     NSArray *filenames;
  24.     NSMutableArray *tablenames;
  25.     NSString *filename, *path, *fullPath;
  26.     NSFileManager *fileManager = [NSFileManager defaultManager];
  27.     int i, fileCount;
  28.  
  29.     filenames = [[[[self adaptorContext] adaptor] connectionDictionary] objectForKey:FlatFileFilesKey];
  30.     fileCount = [filenames count];
  31.  
  32.     if (!fileCount)
  33.         return nil;
  34.  
  35.     tablenames = [NSMutableArray arrayWithCapacity:fileCount];
  36.     path = [(FlatFileContext *)[self adaptorContext] path];
  37.  
  38.     for (i = 0; i < fileCount; i++) {
  39.         filename = [filenames objectAtIndex:i];
  40.         fullPath = [path stringByAppendingPathComponent:filename];
  41.         if ([fileManager isReadableFileAtPath:fullPath])
  42.             [tablenames addObject:filename];
  43.     }
  44.  
  45.     return tablenames;
  46. }
  47.  
  48. - (EOModel *)describeModelWithTableNames:(NSArray *)tableNames
  49. {
  50.     EOAdaptor *adaptor;
  51.     EOModel *model = [[EOModel new] autorelease];
  52.     EOEntity *entity;
  53.     NSString *name;
  54.     int i, tableCount = [tableNames count];
  55.  
  56.     adaptor = [[self adaptorContext] adaptor];
  57.     [model setAdaptorName:[adaptor name]];
  58.     [model setConnectionDictionary:[adaptor connectionDictionary]];
  59.  
  60.     for (i = 0; i < tableCount; i++) {
  61.         name = [tableNames objectAtIndex:i];
  62.         entity = [self describeEntityWithTableName:name];
  63.         if (entity) {
  64.             [model addEntity:entity];
  65.         }
  66.     }
  67.     return model;
  68. }
  69.  
  70. - (void)describeStoredProceduresForModel:(EOModel *)model
  71. {
  72.     return;
  73. }
  74.  
  75. - (EOEntity *)describeEntityWithTableName:(NSString *)tableName
  76. {
  77.     EOEntity *entity = nil;
  78.     EOAttribute *attribute;
  79.     NSArray *columnInfos = [(FlatFileContext *)_context columnInfosForTable:tableName];
  80.     NSString *columnName, *path, *fullPath;
  81.     NSDictionary *columnInfo;
  82.     NSEnumerator *enumerator = [columnInfos objectEnumerator];
  83.     
  84.     while (columnInfo = [enumerator nextObject]) {
  85.         columnName = [columnInfo objectForKey:ColumnNameKey];
  86.         attribute = [self newAttributeWithColumnInfo:columnInfo];
  87.         if (attribute) {
  88.             if (!entity) {
  89.                 entity = [EOEntity new];
  90.                 [entity setName:tableName];
  91.                 [entity setExternalName:tableName];
  92.  
  93.                 path = [(FlatFileContext *)_context path];
  94.                 fullPath = [path stringByAppendingPathComponent:tableName];
  95.                 
  96.                 if (![[NSFileManager defaultManager] isWritableFileAtPath:fullPath])
  97.                     [entity setReadOnly:YES];
  98.             }
  99.  
  100.             [entity addAttribute:attribute];
  101.         }
  102.     }
  103.     return entity;
  104. }
  105.  
  106. - (EOAttribute *)newAttributeWithColumnInfo:(NSDictionary *)info
  107. {
  108.     EOAttribute *attribute = [[EOAttribute new] autorelease];
  109.     EOAdaptor *adaptor = [[self adaptorContext] adaptor];
  110.     NSString *name, *type;
  111.  
  112.     name = [info objectForKey:ColumnNameKey];
  113.     type = [info objectForKey:ColumnTypeKey];
  114.     
  115.     [attribute setName:name];
  116.     [attribute setColumnName:name];
  117.     [attribute setExternalType:type];
  118.     [attribute setValueClassName:[[[adaptor class] externalToInternalTypeMap] objectForKey:type]];
  119.  
  120.     return attribute;
  121. }
  122.  
  123. @end
  124.