mirror of
https://github.com/gnustep/libs-gdl2.git
synced 2025-02-21 02:20:55 +00:00
* EOAccess/EOSQLExpression.m/.h:
o renamed _useAliases to _flags.useAliases o added _flags.hasOuterJoin o call qualifier -sqlStringForSQLExpression: instead of switching on qualifier class in -sqlStringForArrayOfQualifiers:operation: o asserts o fill _contextStack o added outer joins checks in -_flattenRelPath:entity: (Used in Postgresql) * EOAdaptors/Postgres95/Postgres95SQLExpression.m: o subclass joinExpression to avoid building join expression (it's now build in -tableListWithRootEntity o sublass -tableListWithRootEntity to add joins in it. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gdl2/trunk@18225 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
5f1314ddfc
commit
565696cba9
4 changed files with 385 additions and 48 deletions
16
ChangeLog
16
ChangeLog
|
@ -1,4 +1,4 @@
|
|||
2003-11-26 Manuel Guesdon <mguesdon@orange-concept.com>
|
||||
2003-11-28 Manuel Guesdon <mguesdon@orange-concept.com>
|
||||
|
||||
* EOAccess/EOAttribute.m:
|
||||
o introducing new number types:
|
||||
|
@ -39,6 +39,20 @@
|
|||
* EOAccess/EORelationship.m:
|
||||
o asserts
|
||||
o fix in -isReciprocalToRelationship:
|
||||
* EOAccess/EOSQLExpression.m/.h:
|
||||
o renamed _useAliases to _flags.useAliases
|
||||
o added _flags.hasOuterJoin
|
||||
o call qualifier -sqlStringForSQLExpression:
|
||||
instead of switching on qualifier class
|
||||
in -sqlStringForArrayOfQualifiers:operation:
|
||||
o asserts
|
||||
o fill _contextStack
|
||||
o added outer joins checks in -_flattenRelPath:entity:
|
||||
(Used in Postgresql)
|
||||
* EOAdaptors/Postgres95/Postgres95SQLExpression.m:
|
||||
o subclass joinExpression to avoid building join
|
||||
expression (it's now build in -tableListWithRootEntity
|
||||
o sublass -tableListWithRootEntity to add joins in it.
|
||||
|
||||
2003-10-24 David Ayers <d.ayers@inode.at>
|
||||
|
||||
|
|
|
@ -73,7 +73,11 @@ GDL2ACCESS_EXPORT NSString *EOBindVariableColumnKey;
|
|||
NSMutableArray *_bindings;
|
||||
NSMutableArray *_contextStack;
|
||||
NSString *_statement;
|
||||
BOOL _useAliases;
|
||||
struct {
|
||||
unsigned int useAliases:1;
|
||||
unsigned int hasOuterJoin:1;
|
||||
unsigned int _reserved:30;
|
||||
} _flags;
|
||||
@private
|
||||
int _alias;
|
||||
}
|
||||
|
|
|
@ -108,6 +108,9 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
_contextStack = [NSMutableArray new];
|
||||
[_contextStack addObject: @""];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"added '%@' (t0) in contextStack => %@",
|
||||
@"",_contextStack);
|
||||
|
||||
/*NOT now _listString = [NSMutableString new];
|
||||
_valueListString = [NSMutableString new];
|
||||
_joinClauseString = [NSMutableString new];
|
||||
|
@ -366,7 +369,7 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
|
||||
[entitiesString appendString: externalName];
|
||||
|
||||
if (_useAliases)
|
||||
if (_flags.useAliases)
|
||||
[entitiesString appendFormat: @" %@",
|
||||
[_aliasesByRelationshipPath
|
||||
objectForKey: relationshipPath]];
|
||||
|
@ -410,7 +413,7 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
|
||||
[entitiesString appendString: externalName];
|
||||
|
||||
if (_useAliases)
|
||||
if (_flags.useAliases)
|
||||
{
|
||||
NSString *alias = [_aliasesByRelationshipPath
|
||||
objectForKey: relationshipPath];
|
||||
|
@ -426,6 +429,10 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
i++;
|
||||
}
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression",
|
||||
@"entitiesString=%@",
|
||||
entitiesString);
|
||||
|
||||
EOFLOGObjectFnStop();
|
||||
|
||||
return entitiesString;
|
||||
|
@ -883,6 +890,7 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
// Get relationship joins
|
||||
joins = [rel joins];
|
||||
count = [joins count];
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"joins=%@", joins);
|
||||
|
||||
// Iterate on each join
|
||||
for (i = 0; i < count; i++)
|
||||
|
@ -1290,37 +1298,26 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
|
||||
EOFLOGObjectFnStart();
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"operation=%@ qualifiers=%@",
|
||||
operation, qualifiers);
|
||||
|
||||
count = [qualifiers count];
|
||||
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
NSString *tmpSqlString=nil;
|
||||
|
||||
EOQualifier *qualifier = [qualifiers objectAtIndex: i];
|
||||
EOQualifier<EOQualifierSQLGeneration> *qualifier
|
||||
= [qualifiers objectAtIndex: i];
|
||||
|
||||
// use of isKindOfClass is not very good. Improve it ?
|
||||
if ([qualifier isKindOfClass:[EOKeyValueQualifier class]])
|
||||
tmpSqlString = [self sqlStringForKeyValueQualifier:
|
||||
(EOKeyValueQualifier*)qualifier];
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"qualifier=%@",
|
||||
qualifier);
|
||||
|
||||
else if ([qualifier isKindOfClass:[EOAndQualifier class]])
|
||||
tmpSqlString=[self sqlStringForConjoinedQualifiers:
|
||||
[(EOAndQualifier*)qualifier qualifiers]];
|
||||
tmpSqlString=[qualifier sqlStringForSQLExpression:self];
|
||||
|
||||
else if ([qualifier isKindOfClass:[EOOrQualifier class]])
|
||||
tmpSqlString=[self sqlStringForDisjoinedQualifiers:
|
||||
[(EOOrQualifier*)qualifier qualifiers]];
|
||||
|
||||
else if ([qualifier isKindOfClass:[EONotQualifier class]])
|
||||
tmpSqlString=[self sqlStringForNegatedQualifier:qualifier];
|
||||
|
||||
else if ([qualifier isKindOfClass:[EOKeyComparisonQualifier class]])
|
||||
tmpSqlString=[self sqlStringForKeyComparisonQualifier:(id)qualifier];
|
||||
|
||||
else
|
||||
[NSException raise: NSInternalInconsistencyException
|
||||
format: @"EOSQLExpression: Unknown qualifier class %@",
|
||||
[qualifier class]];
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression",
|
||||
@"qualifier=%@ tmpSqlString=%@",
|
||||
qualifier, tmpSqlString);
|
||||
|
||||
if (tmpSqlString)
|
||||
{
|
||||
|
@ -1332,6 +1329,9 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
|
||||
[sqlString appendString: tmpSqlString];
|
||||
nb++;
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"sqlString=%@",
|
||||
sqlString);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1343,6 +1343,9 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
else if (nb == 0)
|
||||
sqlString = nil;
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"operation=%@ qualifiers=%@ count=%d nb=%d sqlString=%@",
|
||||
operation, qualifiers, count, nb, sqlString);
|
||||
|
||||
EOFLOGObjectFnStop();
|
||||
|
||||
return sqlString;
|
||||
|
@ -1358,6 +1361,8 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
sqlString = [self sqlStringForArrayOfQualifiers: qualifiers
|
||||
operation: @" AND "];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"sqlString=%@", sqlString);
|
||||
|
||||
EOFLOGObjectFnStop();
|
||||
|
||||
return sqlString;
|
||||
|
@ -1373,6 +1378,8 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
sqlString = [self sqlStringForArrayOfQualifiers: qualifiers
|
||||
operation: @" OR "];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"sqlString=%@", sqlString);
|
||||
|
||||
EOFLOGObjectFnStop();
|
||||
|
||||
return sqlString;
|
||||
|
@ -1380,7 +1387,7 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
|
||||
- (NSString *)sqlStringForNegatedQualifier:(EOQualifier *)qualifier
|
||||
{
|
||||
NSString *sqlQual;
|
||||
NSString *sqlQual = nil;
|
||||
|
||||
EOFLOGObjectFnStart();
|
||||
|
||||
|
@ -1388,6 +1395,8 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
if (sqlQual)
|
||||
sqlQual = [NSString stringWithFormat:@"not (%@)", sqlQual];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"sqlQual=%@", sqlQual);
|
||||
|
||||
EOFLOGObjectFnStop();
|
||||
|
||||
return sqlQual;
|
||||
|
@ -1407,6 +1416,8 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
|
||||
EOFLOGObjectFnStart();
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"qualifier=%@", qualifier);
|
||||
|
||||
NSAssert2([qualifier isKindOfClass:[EOKeyValueQualifier class]],
|
||||
@"qualifier is not a EOKeyValueQualifier but a %@: %@",
|
||||
[qualifier class],
|
||||
|
@ -1485,6 +1496,7 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
*/
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"sqlString=%@", sqlString);
|
||||
|
||||
EOFLOGObjectFnStop();
|
||||
|
||||
return sqlString; //return someting like t1.label = 'XXX'
|
||||
|
@ -1576,12 +1588,12 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
|
||||
- (void)setUseAliases: (BOOL)useAliases
|
||||
{
|
||||
_useAliases = useAliases;
|
||||
_flags.useAliases = useAliases;
|
||||
}
|
||||
|
||||
- (BOOL)useAliases
|
||||
{
|
||||
return _useAliases;
|
||||
return _flags.useAliases;
|
||||
}
|
||||
|
||||
- (NSString *)sqlStringForSchemaObjectName: (NSString *)name
|
||||
|
@ -1806,10 +1818,16 @@ NSString *EOBindVariableColumnKey = @"EOBindVariableColumnKey";
|
|||
|
||||
EOFLOGObjectFnStart();
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"anAttribute=%@\nisFlattened=%s\n_definitionArray=%@\n_definitionArray count=%d",
|
||||
anAttribute,
|
||||
([anAttribute isFlattened] ? "YES" : "NO"),
|
||||
[anAttribute _definitionArray],
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"anAttribute=%@",
|
||||
anAttribute);
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"sFlattened=%s",
|
||||
([anAttribute isFlattened] ? "YES" : "NO"));
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"_definitionArray=%@",
|
||||
[anAttribute _definitionArray]);
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"_definitionArray count=%d",
|
||||
[[anAttribute _definitionArray]count]);
|
||||
|
||||
if ([anAttribute isFlattened])
|
||||
|
@ -1888,6 +1906,9 @@ else if([anAttribute isDerived] == YES)
|
|||
{
|
||||
NSString *columnName = [anAttribute columnName];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression",
|
||||
@"columnName=%@", columnName);
|
||||
|
||||
if (!columnName)
|
||||
{
|
||||
NSEmitTODO(); //TODO what to do when there's no column name (definition only like "((firstName || ' ') || lastName)") ?
|
||||
|
@ -1934,7 +1955,11 @@ else if([anAttribute isDerived] == YES)
|
|||
{
|
||||
NSString *sqlString = nil;
|
||||
|
||||
if (!_useAliases)
|
||||
EOFLOGObjectFnStart();
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"path=%@", path);
|
||||
|
||||
if (!_flags.useAliases)
|
||||
{
|
||||
sqlString = [(EOAttribute *)[path lastObject] columnName];
|
||||
|
||||
|
@ -1952,16 +1977,33 @@ else if([anAttribute isDerived] == YES)
|
|||
{
|
||||
for (i = 0; i < (count - 1); i++)
|
||||
{
|
||||
EORelationship* relationship = nil;
|
||||
if (i > 0)
|
||||
[relationshipPathString appendString: @"."];
|
||||
|
||||
[relationshipPathString
|
||||
appendString: [(EORelationship *)[path objectAtIndex:i]
|
||||
name]];
|
||||
relationship = [path objectAtIndex:i];
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"[path objectAtIndex:%d]=%@",
|
||||
i, relationship);
|
||||
|
||||
NSAssert2([relationship isKindOfClass:[EORelationship class]],
|
||||
@"'%@' is not a relationship but a %@",
|
||||
relationship,
|
||||
[relationship class]);
|
||||
|
||||
[relationshipPathString appendString: [relationship name]];
|
||||
}
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"[path lastObject]=%@",
|
||||
[path lastObject]);
|
||||
|
||||
//TODO
|
||||
//call attribute _definitionArray
|
||||
if ([[path lastObject] isDerived])
|
||||
{
|
||||
//call attribute _definitionArray
|
||||
NSEmitTODO(); //TODO
|
||||
[self notImplemented:_cmd];
|
||||
};
|
||||
|
||||
sqlString = [self _aliasForRelatedAttribute: [path lastObject]
|
||||
relationshipPath: relationshipPathString];
|
||||
|
||||
|
@ -1975,6 +2017,8 @@ else if([anAttribute isDerived] == YES)
|
|||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"path=%@ sqlString=%@",
|
||||
path, sqlString);
|
||||
|
||||
EOFLOGObjectFnStop();
|
||||
|
||||
return sqlString;
|
||||
}
|
||||
|
||||
|
@ -2195,10 +2239,13 @@ All relationshipPaths in _aliasesByRelationshipPath are direct paths **/
|
|||
NSMutableString *mutableFlattenRelPath;
|
||||
NSString *alias = nil;
|
||||
NSMutableArray *pathElements;
|
||||
int count;
|
||||
|
||||
int count = 0;
|
||||
int contextStackCurrentIndex = 0;
|
||||
|
||||
EOFLOGObjectFnStart();
|
||||
|
||||
contextStackCurrentIndex = [_contextStack count];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"relationshipPath=%@",
|
||||
relationshipPath);
|
||||
NSAssert(relationshipPath, @"No relationshipPath");
|
||||
|
@ -2223,7 +2270,7 @@ All relationshipPaths in _aliasesByRelationshipPath are direct paths **/
|
|||
|
||||
while (count > 0)
|
||||
{
|
||||
NSString *tmpAlias;
|
||||
NSString *tmpAlias = nil;
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression",@"count=%d flattenRelPath=%@",
|
||||
count,
|
||||
|
@ -2234,14 +2281,22 @@ All relationshipPaths in _aliasesByRelationshipPath are direct paths **/
|
|||
|
||||
if (!tmpAlias)
|
||||
{
|
||||
NSString* tmpRelPath=nil;
|
||||
tmpAlias = [NSString stringWithFormat: @"t%d", _alias++];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"add alias %@ for %@",
|
||||
tmpAlias, mutableFlattenRelPath);
|
||||
|
||||
tmpRelPath = [[mutableFlattenRelPath copy]
|
||||
autorelease]; //immuable key !
|
||||
[_aliasesByRelationshipPath setObject: tmpAlias
|
||||
forKey: [[mutableFlattenRelPath copy]
|
||||
autorelease]]; //immuable key !
|
||||
forKey: tmpRelPath];
|
||||
|
||||
// Insert to ensure logical order (i.e. xx BEFORE xx.yy)
|
||||
[_contextStack insertObject:tmpRelPath
|
||||
atIndex:contextStackCurrentIndex];
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"inserted '%@' (%@) in contextStack => %@",
|
||||
tmpRelPath,tmpAlias,_contextStack);
|
||||
}
|
||||
|
||||
if (!alias)
|
||||
|
@ -2293,7 +2348,6 @@ All relationshipPaths in _aliasesByRelationshipPath are direct paths **/
|
|||
|
||||
// use anyRelationshipNamed: to find hidden relationship too
|
||||
relationship = [entity anyRelationshipNamed: part];
|
||||
|
||||
NSAssert2(relationship,
|
||||
@"no relationship named %@ in entity %@",
|
||||
part,
|
||||
|
@ -2302,6 +2356,11 @@ All relationshipPaths in _aliasesByRelationshipPath are direct paths **/
|
|||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"i=%d part=%@ rel=%@",
|
||||
i, part, relationship);
|
||||
|
||||
// We check if there's outer join has some adaptors may buld things
|
||||
// differently in this case
|
||||
if (!_flags.hasOuterJoin && [relationship joinSemantic]!=EOInnerJoin)
|
||||
_flags.hasOuterJoin=YES;
|
||||
|
||||
if ([relationship isFlattened])
|
||||
{
|
||||
NSString *definition = [relationship definition];
|
||||
|
@ -2344,9 +2403,9 @@ All relationshipPaths in _aliasesByRelationshipPath are direct paths **/
|
|||
relationshipPath: (NSString*)relationshipPath
|
||||
|
||||
{
|
||||
NSString *alias;
|
||||
NSString *relPathAlias;
|
||||
NSString *attributeColumnName;
|
||||
NSString *alias = nil;
|
||||
NSString *relPathAlias = nil;
|
||||
NSString *attributeColumnName = nil;
|
||||
|
||||
EOFLOGObjectFnStart();
|
||||
|
||||
|
@ -2359,6 +2418,11 @@ All relationshipPaths in _aliasesByRelationshipPath are direct paths **/
|
|||
attributeColumnName = [self sqlStringForSchemaObjectName:
|
||||
attributeColumnName]; // ret quoted columnName
|
||||
|
||||
NSAssert1([relPathAlias length]>0,@"no relPathAlias or empty relPathAlias ('%@')",
|
||||
relPathAlias);
|
||||
NSAssert1([attributeColumnName length]>0,@"no attributeColumnName or empty attributeColumnName ('%@')",
|
||||
attributeColumnName);
|
||||
|
||||
alias = [NSString stringWithFormat: @"%@.%@",
|
||||
relPathAlias,
|
||||
attributeColumnName];
|
||||
|
|
|
@ -57,8 +57,15 @@ RCS_ID("$Id$")
|
|||
#include <EOAccess/EOSchemaGeneration.h>
|
||||
|
||||
#include <Postgres95EOAdaptor/Postgres95SQLExpression.h>
|
||||
#include <Postgres95EOAdaptor/Postgres95Adaptor.h>
|
||||
#include <Postgres95EOAdaptor/Postgres95Values.h>
|
||||
|
||||
@interface EOSQLExpression (Privat)
|
||||
//Ayers: Review (Don't rely on privat method)
|
||||
- (NSString*) _aliasForRelatedAttribute: (EOAttribute *)attr
|
||||
relationshipPath: (NSString *)keyPath;
|
||||
|
||||
@end
|
||||
|
||||
@implementation Postgres95SQLExpression
|
||||
|
||||
|
@ -102,7 +109,8 @@ RCS_ID("$Id$")
|
|||
|
||||
formatted = [value sqlString];
|
||||
}
|
||||
else if ([externalType hasPrefix: @"int"])
|
||||
else if ([externalType hasPrefix: @"int"]
|
||||
|| [externalType hasPrefix: @"bigint"])
|
||||
{
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression",
|
||||
@"int case - value=%@ class=%@",
|
||||
|
@ -358,4 +366,251 @@ RCS_ID("$Id$")
|
|||
ASSIGN(_statement, [_statement stringByAppendingString:@" DEFERRABLE INITIALLY DEFERRED"]);
|
||||
}
|
||||
|
||||
/** Build join expression for all used relationships (call this) after all other query parts construction) **/
|
||||
- (void)joinExpression
|
||||
{
|
||||
int contextStackCount=0;
|
||||
|
||||
EOFLOGObjectFnStart();
|
||||
|
||||
contextStackCount=[_contextStack count];
|
||||
if (contextStackCount>1 && _flags.hasOuterJoin)
|
||||
{
|
||||
// No join clause in postgresql, joins are added in -tableList...
|
||||
if (_joinClauseString)
|
||||
DESTROY(_joinClauseString);
|
||||
}
|
||||
else
|
||||
[super joinExpression];
|
||||
|
||||
EOFLOGObjectFnStop();
|
||||
}
|
||||
|
||||
- (NSString *)tableListWithRootEntity: (EOEntity*)entity
|
||||
{
|
||||
int contextStackCount=0;
|
||||
NSString *finalEntitiesString=nil;
|
||||
|
||||
EOFLOGObjectFnStart();
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"entity=%@", entity);
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"_aliasesByRelationshipPath=%@",
|
||||
_aliasesByRelationshipPath);
|
||||
|
||||
|
||||
contextStackCount=[_contextStack count];
|
||||
if (contextStackCount>1 && _flags.hasOuterJoin)
|
||||
{
|
||||
// joins are added here and not in join clause.
|
||||
NSMutableString *entitiesString = [NSMutableString string];
|
||||
NSString *relationshipPath = nil ;
|
||||
EOEntity *currentEntity = nil;
|
||||
int i = 0;
|
||||
int relPathIndex = 0;
|
||||
BOOL useAliases=[self useAliases];
|
||||
|
||||
|
||||
for(relPathIndex=0;relPathIndex<contextStackCount;relPathIndex++)
|
||||
{
|
||||
relationshipPath = [_contextStack objectAtIndex:relPathIndex];
|
||||
currentEntity = entity;
|
||||
|
||||
if ([relationshipPath isEqualToString: @""])
|
||||
{
|
||||
NSString *externalName = [currentEntity externalName];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression",
|
||||
@"entity %p named %@: externalName=%@",
|
||||
currentEntity, [currentEntity name],
|
||||
externalName);
|
||||
|
||||
NSAssert1([externalName length]>0,@"No external name for entity %@",
|
||||
[currentEntity name]);
|
||||
|
||||
[entitiesString appendString: externalName];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"entitiesString=%@", entitiesString);
|
||||
|
||||
if (useAliases)
|
||||
[entitiesString appendFormat: @" %@",
|
||||
[_aliasesByRelationshipPath
|
||||
objectForKey: relationshipPath]];
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"entitiesString=%@", entitiesString);
|
||||
}
|
||||
else
|
||||
{
|
||||
NSEnumerator *defEnum = nil;
|
||||
NSArray *defArray = nil;
|
||||
NSString *relationshipString;
|
||||
NSString *externalName = nil;
|
||||
EORelationship *rel = nil;
|
||||
EOQualifier *auxiliaryQualifier = nil;
|
||||
NSArray *joins = nil;
|
||||
int i, count=0;
|
||||
NSMutableString* joinOn=[NSMutableString string];
|
||||
EOJoinSemantic joinSemantic;
|
||||
NSString* joinOp = nil;
|
||||
|
||||
defArray = [relationshipPath componentsSeparatedByString: @"."];
|
||||
defEnum = [defArray objectEnumerator];
|
||||
|
||||
// Get the relationship for this path (non flattened by design)
|
||||
rel = [entity relationshipForPath: relationshipPath];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"rel=%@", rel);
|
||||
NSAssert2(rel, @"No relationship for path %@ in entity %@",
|
||||
relationshipPath,
|
||||
[entity name]);
|
||||
|
||||
//Get the auxiliary qualifier for this relationship
|
||||
auxiliaryQualifier = [rel auxiliaryQualifier];
|
||||
|
||||
if (auxiliaryQualifier)
|
||||
{
|
||||
NSEmitTODO(); //TODO
|
||||
[self notImplemented:_cmd];
|
||||
}
|
||||
|
||||
while ((relationshipString = [defEnum nextObject]))
|
||||
{
|
||||
// use anyRelationshipNamed: to find hidden relationship too
|
||||
EORelationship *relationship=[currentEntity
|
||||
anyRelationshipNamed: relationshipString];
|
||||
|
||||
NSAssert2(relationship,@"No relationship named %@ in entity %@",
|
||||
relationshipString,
|
||||
[currentEntity name]);
|
||||
|
||||
NSAssert2(currentEntity,@"No destination entity. Entity %@ relationship = %@",
|
||||
[currentEntity name],
|
||||
relationship);
|
||||
|
||||
currentEntity = [relationship destinationEntity];
|
||||
}
|
||||
|
||||
externalName = [currentEntity externalName];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression",
|
||||
@"entity %p named %@: externalName=%@",
|
||||
currentEntity, [currentEntity name],
|
||||
externalName);
|
||||
|
||||
NSAssert1([externalName length]>0,@"No external name for entity %@",
|
||||
[currentEntity name]);
|
||||
|
||||
joinSemantic = [rel joinSemantic];
|
||||
switch (joinSemantic)
|
||||
{
|
||||
case EOInnerJoin:
|
||||
joinOp = @"INNER JOIN";
|
||||
break;
|
||||
case EOLeftOuterJoin:
|
||||
joinOp = @"LEFT OUTER JOIN";
|
||||
break;
|
||||
case EORightOuterJoin:
|
||||
joinOp = @"RIGHT OUTER JOIN";
|
||||
break;
|
||||
case EOFullOuterJoin:
|
||||
joinOp = @"FULL OUTER JOIN";
|
||||
break;
|
||||
}
|
||||
|
||||
// Get relationship joins
|
||||
joins = [rel joins];
|
||||
count = [joins count];
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"joins=%@", joins);
|
||||
|
||||
// Iterate on each join
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
NSString *sourceRelationshipPath = nil;
|
||||
NSArray *sourceRelationshipPathArray;
|
||||
//Get the join
|
||||
EOJoin *join=[joins objectAtIndex:i];
|
||||
// Get source and destination attributes
|
||||
EOAttribute *sourceAttribute = [join sourceAttribute];
|
||||
EOAttribute *destinationAttribute = [join destinationAttribute];
|
||||
NSString *sourceAttributeAlias = nil;
|
||||
NSString *destinationAttributeAlias = nil;
|
||||
|
||||
// Build the source relationshipPath
|
||||
sourceRelationshipPathArray =
|
||||
[relationshipPath componentsSeparatedByString: @"."];
|
||||
sourceRelationshipPathArray =
|
||||
[sourceRelationshipPathArray
|
||||
subarrayWithRange:
|
||||
NSMakeRange(0,[sourceRelationshipPathArray count] - 1)];
|
||||
sourceRelationshipPath = [sourceRelationshipPathArray
|
||||
componentsJoinedByString: @"."];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"sourceRelationshipPath=%@", sourceRelationshipPath);
|
||||
|
||||
// Get the alias for sourceAttribute
|
||||
sourceAttributeAlias = [self
|
||||
_aliasForRelatedAttribute:
|
||||
sourceAttribute
|
||||
relationshipPath:
|
||||
sourceRelationshipPath];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"sourceAttributeAlias=%@", sourceAttributeAlias);
|
||||
|
||||
// Get the alias for destinationAttribute
|
||||
destinationAttributeAlias =
|
||||
[self _aliasForRelatedAttribute: destinationAttribute
|
||||
relationshipPath: relationshipPath];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"addJoin=%@ %d %@",
|
||||
sourceAttributeAlias,
|
||||
(int)joinSemantic,
|
||||
destinationAttributeAlias);
|
||||
|
||||
|
||||
if (i>0)
|
||||
[joinOn appendString:@", "];
|
||||
joinOn = [NSString stringWithFormat: @"%@ = %@",
|
||||
sourceAttributeAlias,
|
||||
destinationAttributeAlias];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"joinOn=%@", joinOn);
|
||||
}
|
||||
|
||||
[entitiesString appendFormat:@" %@ %@",
|
||||
joinOp,
|
||||
externalName];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"entitiesString=%@", entitiesString);
|
||||
|
||||
if (useAliases)
|
||||
{
|
||||
NSString *alias = [_aliasesByRelationshipPath
|
||||
objectForKey: relationshipPath];
|
||||
|
||||
[entitiesString appendFormat: @" %@",alias];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression",
|
||||
@"appending alias %@ in entitiesString",
|
||||
alias);
|
||||
}
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"entitiesString=%@", entitiesString);
|
||||
|
||||
[entitiesString appendFormat:@" on (%@) ",joinOn];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression", @"entitiesString=%@", entitiesString);
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
finalEntitiesString=entitiesString;
|
||||
}
|
||||
else
|
||||
finalEntitiesString=[super tableListWithRootEntity:entity];
|
||||
|
||||
EOFLOGObjectLevelArgs(@"EOSQLExpression",
|
||||
@"finalEntitiesString=%@",
|
||||
finalEntitiesString);
|
||||
|
||||
EOFLOGObjectFnStop();
|
||||
|
||||
return finalEntitiesString;
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
Loading…
Reference in a new issue