PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/sope-gdl1/FrontBase2/FBChannel+Model.m

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