]> err.no Git - sope/blob - sope-gdl1/FrontBase2/FBChannel+Model.m
added missing inline pathes
[sope] / sope-gdl1 / FrontBase2 / FBChannel+Model.m
1 /* 
2    FBChannel+Model.m
3
4    Copyright (C) 1999 MDlink online service center GmbH and Helge Hess
5
6    Author: Helge Hess (helge@mdlink.de)
7
8    This file is part of the FB Adaptor Library
9
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Library General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Library General Public License for more details.
19
20    You should have received a copy of the GNU Library General Public
21    License along with this library; see the file COPYING.LIB.
22    If not, write to the Free Software Foundation,
23    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25 // $Id: FBChannel+Model.m 1 2004-08-20 10:38:46Z znek $
26
27 #import "common.h"
28 #import <GDLAccess/EOAccess.h>
29
30 @interface EORelationship(Private)
31 - (void)addJoin:(EOJoin *)_join;
32 @end
33
34 @implementation FrontBaseChannel(ModelFetching)
35
36 - (NSArray *)_attributesForTableName:(NSString *)_tableName {
37   NSArray *attrForTableName = nil;
38   
39   if ((attrForTableName =
40        [self->_attributesForTableName objectForKey:_tableName]) == nil) {
41     NSMutableArray      *attributes        = nil;
42     NSArray             *resultDescription = nil;
43     NSString            *selectExpression  = nil;
44     NSString            *columnNameKey     = nil;
45     NSString            *externalTypeKey   = nil;
46     NSDictionary        *row               = nil;
47     unsigned            cnt                = 0;
48
49     selectExpression = [NSString stringWithFormat:
50       @"SELECT C1.\"COLUMN_NAME\", DTD1.\"DATA_TYPE\" FROM INFORMATION_SCHEMA."
51       @"COLUMNS C1, INFORMATION_SCHEMA.TABLES T1, INFORMATION_SCHEMA.DATA_"
52       @"TYPE_DESCRIPTOR DTD1 WHERE T1.\"TABLE_NAME\" = '%s' AND T1.\""
53       @"TABLE_PK\" = DTD1.\"TABLE_OR_DOMAIN_PK\" AND C1.\"TABLE_PK\" = T1."
54       @"\"TABLE_PK\" AND C1.\"COLUMN_PK\" = DTD1.\"COLUMN_NAME_PK\"",
55                                  [[_tableName uppercaseString] cString]];
56     
57     if (![self evaluateExpression:selectExpression]) {
58       fprintf(stderr, "Couldn`t evaluate expression %s\n",
59               [selectExpression cString]);
60       return nil;
61     }
62     resultDescription = [self describeResults];
63     columnNameKey     = [(EOAttribute *)[resultDescription objectAtIndex:0] name];
64     externalTypeKey   = [(EOAttribute *)[resultDescription objectAtIndex:1] name];
65     attributes        = [NSMutableArray arrayWithCapacity:16];
66
67     while ((row = [self fetchAttributes:resultDescription withZone:NULL])) {
68       EOAttribute *attribute    = nil;
69       NSString    *columnName   = nil;
70       NSString    *externalType = nil;
71       NSString    *attrName     = nil;
72       int         fbType        = 0;
73
74       attribute    = [[EOAttribute alloc] init];
75       columnName   = [row objectForKey:columnNameKey];
76       externalType = [row objectForKey:externalTypeKey];
77       attrName     = [columnName _sybModelMakeInstanceVarName];
78       fbType       = [(id)[adaptorContext adaptor]
79                           typeCodeForExternalName:externalType];
80
81       [attribute setName:attrName];
82       [attribute setColumnName:columnName];
83       [attribute loadValueClassAndTypeFromFrontBaseType:fbType];
84       [attribute setExternalType:externalType];
85     
86       [attributes addObject:attribute];
87       RELEASE(attribute); attribute = nil;
88     }
89
90     // fetch external types
91
92     for (cnt = 0; cnt < [attributes count]; cnt++) {
93       EOAttribute *attribute    = nil;
94       NSString    *externalType = nil;
95       int         fbType        = 0;
96
97       attribute    = [attributes objectAtIndex:cnt];
98       externalType = [attribute externalType];
99       fbType       = [(id)[adaptorContext adaptor]
100                           typeCodeForExternalName:externalType];
101       [attribute loadValueClassAndTypeFromFrontBaseType:fbType];
102       [attribute setExternalType:externalType];
103     }
104     attrForTableName = attributes;
105     [self->_attributesForTableName setObject:attrForTableName forKey:_tableName];
106   }
107   return attrForTableName;
108 }
109
110 - (NSArray *)_primaryKeysNamesForTableName:(NSString *)_tableName {
111   NSArray *pkNameForTable = nil;
112
113   if (_tableName == nil)
114     return nil;
115   
116   if ((pkNameForTable =
117        [self->_primaryKeysNamesForTableName objectForKey:_tableName]) == nil) {
118     NSMutableArray *primaryKeys       = nil;
119     NSString       *selectExpression  = nil;
120     NSArray        *resultDescription = nil;
121     NSString       *columnNameKey     = nil;
122     NSDictionary   *row               = nil;
123
124     selectExpression = [NSString stringWithFormat:
125       @"SELECT C1.\"COLUMN_NAME\" FROM INFORMATION_SCHEMA.COLUMNS C1, "
126       @"INFORMATION_SCHEMA.TABLES T1, INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC1, "
127       @"INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU1 WHERE T1.\"TABLE_NAME\" = "
128       @"'%s' AND TC1.\"TABLE_PK\" = T1.\"TABLE_PK\" AND TC1.\"CONSTRAINT"
129       @"_TYPE\" = 'PRIMARY KEY' AND KCU1.\"TABLE_PK\" = T1.\"TABLE_PK\" AND "
130       @"KCU1.\"CONSTRAINT_NAME_PK\" = TC1.\"CONSTRAINT_NAME_PK\" AND C1.\""
131       @"COLUMN_PK\" = KCU1.\"COLUMN_PK\"",
132                                  [[_tableName uppercaseString] cString]];
133   
134     if (![self evaluateExpression:selectExpression]) {
135       fprintf(stderr, "Couldn`t evaluate expression %s\n",
136               [selectExpression cString]);
137       return nil;
138     }
139     resultDescription  = [self describeResults];
140     columnNameKey      = [(EOAttribute *)[resultDescription objectAtIndex:0]
141                                          name];
142     primaryKeys        = [NSMutableArray arrayWithCapacity:4];
143   
144     while ((row = [self fetchAttributes:resultDescription withZone:NULL]))
145       [primaryKeys addObject:[row objectForKey:columnNameKey]];
146
147     pkNameForTable = primaryKeys;
148     [self->_primaryKeysNamesForTableName setObject:pkNameForTable
149                                          forKey:_tableName];
150   }
151   return pkNameForTable;
152 }
153
154 - (NSArray *)_foreignKeysForTableName:(NSString *)_tableName {
155   return [NSArray array];
156 }
157
158 - (EOModel *)describeModelWithTableNames:(NSArray *)_tableNames {
159   NSMutableArray *buildRelShips = nil;
160   EOModel        *model         = nil;
161   int            cnt            = 0;
162   int            tc             = 0;
163
164   buildRelShips = [NSMutableArray arrayWithCapacity:64];
165   model         = [[EOModel alloc] init];
166   tc            = [_tableNames count];
167   AUTORELEASE(model);
168   
169   for (cnt = 0; cnt < tc; cnt++) {
170     NSMutableDictionary *relNamesUsed         = nil;
171     NSMutableArray      *classProperties      = nil;
172     NSMutableArray      *primaryKeyAttributes = nil;
173     NSString            *tableName            = nil;
174     NSArray             *attributes           = nil;
175     NSArray             *pkeys                = nil;
176     NSArray             *fkeys                = nil;
177     EOEntity            *entity               = nil;
178     int                 cnt2                  = 0;
179     int                 ac                    = 0;
180     int                 fkc                   = 0;
181
182     relNamesUsed         = [NSMutableDictionary dictionary];
183     classProperties      = [NSMutableArray array];
184     primaryKeyAttributes = [NSMutableArray array];
185     tableName            = [_tableNames objectAtIndex:cnt];
186     attributes           = [self _attributesForTableName:tableName];
187     pkeys                = [self _primaryKeysNamesForTableName:tableName];
188     fkeys                = [self _foreignKeysForTableName:tableName];
189     entity               = [[EOEntity alloc] init];
190     ac                   = [attributes count];
191     fkc                  = [fkeys count];
192     AUTORELEASE(entity);
193     
194     [entity setName:[tableName _sybModelMakeClassName]];
195     [entity setClassName:
196               [@"EO" stringByAppendingString:[tableName _sybModelMakeClassName]]];
197     [entity setExternalName:tableName];
198     [classProperties addObjectsFromArray:[entity classProperties]];
199     [primaryKeyAttributes addObjectsFromArray:[entity primaryKeyAttributes]];
200     [model addEntity:entity];
201     
202     for (cnt2 = 0; cnt2 < ac; cnt2++) {
203       EOAttribute *attribute  = [attributes objectAtIndex:cnt2];
204       NSString    *columnName = [attribute columnName];
205
206       [entity addAttribute:attribute];
207       [classProperties addObject:attribute];
208
209       if ([pkeys containsObject:columnName])
210         [primaryKeyAttributes addObject:attribute];
211     }
212     [entity setClassProperties:classProperties];
213     [entity setPrimaryKeyAttributes:primaryKeyAttributes];
214     
215     for (cnt2 = 0; cnt2 < fkc; cnt2++) {
216       NSDictionary   *fkey             = nil;
217       NSMutableArray *classProperties  = nil;
218       NSString       *sa               = nil;
219       NSString       *da               = nil;
220       NSString       *dt               = nil;
221       EORelationship *rel              = nil;
222       EOJoin         *join             = nil;
223       NSString       *relName          = nil;
224
225       fkey             = [fkeys objectAtIndex:cnt2];
226       classProperties  = [NSMutableArray array];
227       sa               = [fkey objectForKey:@"sourceAttr"];
228       da               = [fkey objectForKey:@"targetAttr"];
229       dt               = [fkey objectForKey:@"targetTable"];
230       rel              = [[EORelationship alloc] init];
231       join             = [[EOJoin alloc] init];
232       AUTORELEASE(rel);
233       AUTORELEASE((id)join);
234       
235       if ([pkeys containsObject:sa])
236         relName = [@"to" stringByAppendingString:[dt _sybModelMakeClassName]];
237       else {
238         relName = [@"to" stringByAppendingString:
239                     [[sa _sybModelMakeInstanceVarName]
240                          _sybStringWithCapitalizedFirstChar]];
241         if ([relName hasSuffix:@"Id"]) {
242           int cLength = [relName cStringLength];
243
244           relName = [relName substringToIndex:cLength - 2];
245         }
246       }
247       if ([relNamesUsed objectForKey:relName]) {
248         int useCount = [[relNamesUsed objectForKey:relName] intValue];
249         
250         [relNamesUsed setObject:[NSNumber numberWithInt:(useCount++)]
251                       forKey:relName];
252         relName = [NSString stringWithFormat:@"%s%d",
253                               [relName cString], useCount];
254       }
255       else
256         [relNamesUsed setObject:[NSNumber numberWithInt:0] forKey:relName];
257
258       [rel setName:relName];
259       //[rel setDestinationEntity:(EOEntity *)[dt _sybModelMakeClassName]];
260       [rel setToMany:NO];
261
262       [(id)join setSourceAttribute:
263            (EOAttribute *)[sa _sybModelMakeInstanceVarName]];
264       [(id)join setDestinationAttribute:
265            (EOAttribute *)[da _sybModelMakeInstanceVarName]];
266       [rel  addJoin:join];
267       
268       [entity addRelationship:rel];
269       [classProperties addObjectsFromArray:[entity classProperties]];
270       [classProperties addObject:rel];
271       [entity setClassProperties:classProperties];
272       [buildRelShips addObject:rel];
273     }
274
275     [entity setAttributesUsedForLocking:[[entity attributes] copy]];
276   }
277
278   [buildRelShips makeObjectsPerformSelector:@selector(replaceStringsWithObjects)];
279
280   [model setAdaptorName:@"FrontBase2"];
281   [model setAdaptorClassName:@"FrontBase2Adaptor"];
282   [model setConnectionDictionary:[[adaptorContext adaptor] connectionDictionary]];
283   return model;
284 }
285
286 - (NSArray *)describeTableNames {
287   NSMutableArray *tableNames        = nil;
288   NSArray        *resultDescription = nil;
289   NSString       *attributeName     = nil;
290   NSDictionary   *row               = nil;
291   NSString       *selectExpression  = nil;
292
293   selectExpression = @"SELECT T1.\"TABLE_NAME\" FROM "
294                      @"INFORMATION_SCHEMA.TABLES T1";
295
296   if (![self evaluateExpression:selectExpression]) {
297     fprintf(stderr, "Couldn`t evaluate expression %s\n",
298             [selectExpression cString]);
299     return nil;
300   }
301   resultDescription = [self describeResults];
302   attributeName     = [(EOAttribute *)[resultDescription objectAtIndex:0] name];
303   tableNames        = [NSMutableArray arrayWithCapacity:16];
304
305   while ((row = [self fetchAttributes:resultDescription withZone:NULL]))
306     [tableNames addObject:[row objectForKey:attributeName]];
307   
308   return tableNames;
309 }
310
311 @end
312
313 void __link_FBChannelModel() {
314   // used to force linking of object file
315   __link_FBChannelModel();
316 }