4 Copyright (C) 1999 MDlink online service center GmbH and Helge Hess
6 Author: Helge Hess (helge@mdlink.de)
8 This file is part of the FB Adaptor Library
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.
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.
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.
25 // $Id: FBChannel+Model.m 1 2004-08-20 10:38:46Z znek $
28 #import <GDLAccess/EOAccess.h>
30 @interface EORelationship(Private)
31 - (void)addJoin:(EOJoin *)_join;
34 @implementation FrontBaseChannel(ModelFetching)
36 - (NSArray *)_attributesForTableName:(NSString *)_tableName {
37 NSArray *attrForTableName = nil;
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;
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]];
57 if (![self evaluateExpression:selectExpression]) {
58 fprintf(stderr, "Couldn`t evaluate expression %s\n",
59 [selectExpression cString]);
62 resultDescription = [self describeResults];
63 columnNameKey = [(EOAttribute *)[resultDescription objectAtIndex:0] name];
64 externalTypeKey = [(EOAttribute *)[resultDescription objectAtIndex:1] name];
65 attributes = [NSMutableArray arrayWithCapacity:16];
67 while ((row = [self fetchAttributes:resultDescription withZone:NULL])) {
68 EOAttribute *attribute = nil;
69 NSString *columnName = nil;
70 NSString *externalType = nil;
71 NSString *attrName = nil;
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];
81 [attribute setName:attrName];
82 [attribute setColumnName:columnName];
83 [attribute loadValueClassAndTypeFromFrontBaseType:fbType];
84 [attribute setExternalType:externalType];
86 [attributes addObject:attribute];
87 RELEASE(attribute); attribute = nil;
90 // fetch external types
92 for (cnt = 0; cnt < [attributes count]; cnt++) {
93 EOAttribute *attribute = nil;
94 NSString *externalType = nil;
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];
104 attrForTableName = attributes;
105 [self->_attributesForTableName setObject:attrForTableName forKey:_tableName];
107 return attrForTableName;
110 - (NSArray *)_primaryKeysNamesForTableName:(NSString *)_tableName {
111 NSArray *pkNameForTable = nil;
113 if (_tableName == nil)
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;
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]];
134 if (![self evaluateExpression:selectExpression]) {
135 fprintf(stderr, "Couldn`t evaluate expression %s\n",
136 [selectExpression cString]);
139 resultDescription = [self describeResults];
140 columnNameKey = [(EOAttribute *)[resultDescription objectAtIndex:0]
142 primaryKeys = [NSMutableArray arrayWithCapacity:4];
144 while ((row = [self fetchAttributes:resultDescription withZone:NULL]))
145 [primaryKeys addObject:[row objectForKey:columnNameKey]];
147 pkNameForTable = primaryKeys;
148 [self->_primaryKeysNamesForTableName setObject:pkNameForTable
151 return pkNameForTable;
154 - (NSArray *)_foreignKeysForTableName:(NSString *)_tableName {
155 return [NSArray array];
158 - (EOModel *)describeModelWithTableNames:(NSArray *)_tableNames {
159 NSMutableArray *buildRelShips = nil;
160 EOModel *model = nil;
164 buildRelShips = [NSMutableArray arrayWithCapacity:64];
165 model = [[EOModel alloc] init];
166 tc = [_tableNames count];
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;
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];
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];
202 for (cnt2 = 0; cnt2 < ac; cnt2++) {
203 EOAttribute *attribute = [attributes objectAtIndex:cnt2];
204 NSString *columnName = [attribute columnName];
206 [entity addAttribute:attribute];
207 [classProperties addObject:attribute];
209 if ([pkeys containsObject:columnName])
210 [primaryKeyAttributes addObject:attribute];
212 [entity setClassProperties:classProperties];
213 [entity setPrimaryKeyAttributes:primaryKeyAttributes];
215 for (cnt2 = 0; cnt2 < fkc; cnt2++) {
216 NSDictionary *fkey = nil;
217 NSMutableArray *classProperties = nil;
221 EORelationship *rel = nil;
223 NSString *relName = nil;
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];
233 AUTORELEASE((id)join);
235 if ([pkeys containsObject:sa])
236 relName = [@"to" stringByAppendingString:[dt _sybModelMakeClassName]];
238 relName = [@"to" stringByAppendingString:
239 [[sa _sybModelMakeInstanceVarName]
240 _sybStringWithCapitalizedFirstChar]];
241 if ([relName hasSuffix:@"Id"]) {
242 int cLength = [relName cStringLength];
244 relName = [relName substringToIndex:cLength - 2];
247 if ([relNamesUsed objectForKey:relName]) {
248 int useCount = [[relNamesUsed objectForKey:relName] intValue];
250 [relNamesUsed setObject:[NSNumber numberWithInt:(useCount++)]
252 relName = [NSString stringWithFormat:@"%s%d",
253 [relName cString], useCount];
256 [relNamesUsed setObject:[NSNumber numberWithInt:0] forKey:relName];
258 [rel setName:relName];
259 //[rel setDestinationEntity:(EOEntity *)[dt _sybModelMakeClassName]];
262 [(id)join setSourceAttribute:
263 (EOAttribute *)[sa _sybModelMakeInstanceVarName]];
264 [(id)join setDestinationAttribute:
265 (EOAttribute *)[da _sybModelMakeInstanceVarName]];
268 [entity addRelationship:rel];
269 [classProperties addObjectsFromArray:[entity classProperties]];
270 [classProperties addObject:rel];
271 [entity setClassProperties:classProperties];
272 [buildRelShips addObject:rel];
275 [entity setAttributesUsedForLocking:[[entity attributes] copy]];
278 [buildRelShips makeObjectsPerformSelector:@selector(replaceStringsWithObjects)];
280 [model setAdaptorName:@"FrontBase2"];
281 [model setAdaptorClassName:@"FrontBase2Adaptor"];
282 [model setConnectionDictionary:[[adaptorContext adaptor] connectionDictionary]];
286 - (NSArray *)describeTableNames {
287 NSMutableArray *tableNames = nil;
288 NSArray *resultDescription = nil;
289 NSString *attributeName = nil;
290 NSDictionary *row = nil;
291 NSString *selectExpression = nil;
293 selectExpression = @"SELECT T1.\"TABLE_NAME\" FROM "
294 @"INFORMATION_SCHEMA.TABLES T1";
296 if (![self evaluateExpression:selectExpression]) {
297 fprintf(stderr, "Couldn`t evaluate expression %s\n",
298 [selectExpression cString]);
301 resultDescription = [self describeResults];
302 attributeName = [(EOAttribute *)[resultDescription objectAtIndex:0] name];
303 tableNames = [NSMutableArray arrayWithCapacity:16];
305 while ((row = [self fetchAttributes:resultDescription withZone:NULL]))
306 [tableNames addObject:[row objectForKey:attributeName]];
313 void __link_FBChannelModel() {
314 // used to force linking of object file
315 __link_FBChannelModel();