Skip to content

Commit

Permalink
Fixed ambiguous column issue in SQL generated in GetByKey queries wit…
Browse files Browse the repository at this point in the history
…h derived resources.
  • Loading branch information
gmcelhanon committed Nov 6, 2024
1 parent 8f5f6fb commit 6571bd1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,24 +308,21 @@ private Aggregate GetAggregate()
return aggregate;
}

protected QueryBuilder GetSingleItemQueryBuilder()
protected (QueryBuilder queryBuilder, string rootTableAlias) GetSingleItemQueryBuilder()
{
// Get the fully qualified physical table name
Entity aggregateRootEntity = _aggregate.Value.AggregateRoot;
string rootTableAlias = aggregateRootEntity.IsDerived ? "b" : "r";

if (_queryBuilder != null)
{
return _queryBuilder.Clone();
return (_queryBuilder.Clone(), rootTableAlias);
}

var idQueryBuilder = new QueryBuilder(_dialect);

// Get the fully qualified physical table name
Entity aggregateRootEntity = _aggregate.Value.AggregateRoot;

var schemaTableName = $"{aggregateRootEntity.Schema}.{aggregateRootEntity.TableName(_databaseEngine)}";

string rootTableAlias = aggregateRootEntity.IsDerived
? "b"
: "r";

idQueryBuilder
.From(schemaTableName.Alias("r"))
.Select($"{rootTableAlias}.{ColumnNames.AggregateId}");
Expand Down Expand Up @@ -360,7 +357,7 @@ protected QueryBuilder GetSingleItemQueryBuilder()

_queryBuilder = idQueryBuilder;

return idQueryBuilder.Clone();
return (idQueryBuilder.Clone(), rootTableAlias);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<IList<TEntity>> GetByIdsAsync(IList<Guid> ids, CancellationTok
if (SerializationEnabled)
{
// Get the item from serialized form
var singleItemQueryBuilder = GetSingleItemQueryBuilder();
var (singleItemQueryBuilder, rootTableAlias) = GetSingleItemQueryBuilder();

singleItemQueryBuilder.Where("Id", ids[0]);
var singleItemTemplate = singleItemQueryBuilder.BuildTemplate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ string GetWhereClause(OrderedDictionary keyValues)
async Task<ItemData<TEntity>> GetItemData(OrderedDictionary compositeKeyValues, SessionScope scope)
{
// Get the item from serialized form
var singleItemQueryBuilder = GetSingleItemQueryBuilder();
var (singleItemQueryBuilder, rootTableAlias) = GetSingleItemQueryBuilder();

foreach (DictionaryEntry keyValue in compositeKeyValues)
{
singleItemQueryBuilder.Where((string) keyValue.Key, keyValue.Value);
singleItemQueryBuilder.Where($"{rootTableAlias}.{(string) keyValue.Key}", keyValue.Value);
}

var singleItemTemplate = singleItemQueryBuilder.BuildTemplate();
Expand Down

0 comments on commit 6571bd1

Please sign in to comment.