From 609b1976f3bdeebc2a70804f85972f40a156b228 Mon Sep 17 00:00:00 2001 From: Jeff Lockhart Date: Wed, 23 May 2018 18:41:33 -0600 Subject: [PATCH] Prefix columns with table in SELECT clause When NSDictionaryResultType is used on a fetch request and a selected column is present on a joined table, an "ambiguous column name: columnName" error results. The columns should be prefixed with tableName.columnName to avoid this ambiguity. --- Incremental Store/EncryptedStore.m | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Incremental Store/EncryptedStore.m b/Incremental Store/EncryptedStore.m index aa7a382..7b5a2da 100755 --- a/Incremental Store/EncryptedStore.m +++ b/Incremental Store/EncryptedStore.m @@ -525,7 +525,7 @@ - (id)executeRequest:(NSPersistentStoreRequest *)request // return fetched dictionaries if (type == NSDictionaryResultType && [[fetchRequest propertiesToFetch] count] > 0) { NSArray * propertiesToFetch = [fetchRequest propertiesToFetch]; - NSString * propertiesToFetchString = [self columnsClauseWithProperties:propertiesToFetch]; + NSString * propertiesToFetchString = [self columnsClauseWithProperties:propertiesToFetch fromTable:table]; // TODO: Need a test case to reach here, or remove it entirely // NOTE - this now supports joins but in a limited fashion. It will successfully @@ -3235,11 +3235,11 @@ - (BOOL) maybeAddJoinStatementsForKey: (NSString *) key } -- (NSString *)expressionDescriptionTypeString:(NSExpressionDescription *)expressionDescription { +- (NSString *)expressionDescriptionTypeString:(NSExpressionDescription *)expressionDescription fromTable:(NSString *)table { switch (expressionDescription.expressionResultType) { case NSObjectIDAttributeType: - return @"__objectID"; + return [NSString stringWithFormat:@"%@.__objectID", table]; break; /* NSUndefinedAttributeType @@ -3262,20 +3262,20 @@ - (NSString *)expressionDescriptionTypeString:(NSExpressionDescription *)express } } -- (NSString *)columnsClauseWithProperties:(NSArray *)properties { +- (NSString *)columnsClauseWithProperties:(NSArray *)properties fromTable:(NSString *)table { NSMutableArray *columns = [NSMutableArray arrayWithCapacity:[properties count]]; [properties enumerateObjectsUsingBlock:^(NSPropertyDescription *prop, NSUInteger idx, BOOL *stop) { if ([prop isKindOfClass:[NSRelationshipDescription class]]) { if (![(NSRelationshipDescription *)prop isToMany]) { - [columns addObject:[self foreignKeyColumnForRelationship:(NSRelationshipDescription *)prop]]; + [columns addObject:[NSString stringWithFormat:@"%@.%@", table, [self foreignKeyColumnForRelationship:(NSRelationshipDescription *)prop]]]; } } else if ([prop isKindOfClass:[NSExpressionDescription class]]) { NSExpressionDescription *expression = (NSExpressionDescription*) prop; - NSString *column = [self expressionDescriptionTypeString:expression]; + NSString *column = [self expressionDescriptionTypeString:expression fromTable:table]; [columns addObject:column]; } else { - [columns addObject:[NSString stringWithFormat:@"%@",prop.name]]; + [columns addObject:[NSString stringWithFormat:@"%@.%@", table, prop.name]]; } }];