Skip to content

Commit

Permalink
fix output column table alias bug
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Oct 28, 2023
1 parent 3fbe1f7 commit 8cf6ae8
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/FluentCommand/Query/DeleteBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public TBuilder Table(
/// </returns>
public TBuilder Output(
IEnumerable<string> columnNames,
string tableAlias = "DELETED")
string tableAlias = null)
{
if (columnNames is null)
throw new ArgumentNullException(nameof(columnNames));
Expand All @@ -130,7 +130,7 @@ public TBuilder Output(
/// </returns>
public TBuilder Output(
string columnName,
string tableAlias = "DELETED",
string tableAlias = null,
string columnAlias = null)
{
var outputClause = new ColumnExpression(columnName, tableAlias, columnAlias);
Expand All @@ -152,7 +152,7 @@ public TBuilder Output(
/// </returns>
public TBuilder OutputIf(
string columnName,
string tableAlias = "DELETED",
string tableAlias = null,
string columnAlias = null,
Func<string, bool> condition = null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/FluentCommand/Query/DeleteEntityBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public DeleteEntityBuilder(
/// </returns>
public DeleteEntityBuilder<TEntity> Output<TValue>(
Expression<Func<TEntity, TValue>> property,
string tableAlias = "DELETED",
string tableAlias = null,
string columnAlias = null)
{
var propertyAccessor = _typeAccessor.FindProperty(property);
Expand All @@ -64,7 +64,7 @@ public DeleteEntityBuilder<TEntity> Output<TValue>(
/// </returns>
public DeleteEntityBuilder<TEntity> OutputIf<TValue>(
Expression<Func<TEntity, TValue>> property,
string tableAlias = "DELETED",
string tableAlias = null,
string columnAlias = null,
Func<string, bool> condition = null)
{
Expand Down
17 changes: 14 additions & 3 deletions src/FluentCommand/Query/Generators/SqlServerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ public virtual string BuildInsert(InsertStatement insertStatement)

if (insertStatement.OutputExpressions?.Count > 0)
{

insertBuilder
.AppendLine()
.Append("OUTPUT ")
.AppendJoin(", ", insertStatement.OutputExpressions.Select(ColumnExpression));
.AppendJoin(", ", insertStatement.OutputExpressions.Select(c => ColumnExpression(c, "INSERTED")));
}

insertBuilder
Expand Down Expand Up @@ -163,7 +164,7 @@ public virtual string BuildUpdate(UpdateStatement updateStatement)
updateBuilder
.AppendLine()
.Append("OUTPUT ")
.AppendJoin(", ", updateStatement.OutputExpressions.Select(ColumnExpression));
.AppendJoin(", ", updateStatement.OutputExpressions.Select(c => ColumnExpression(c, "INSERTED")));
}

if (updateStatement.FromExpressions?.Count > 0)
Expand Down Expand Up @@ -224,7 +225,7 @@ public virtual string BuildDelete(DeleteStatement deleteStatement)
deleteBuilder
.AppendLine()
.Append("OUTPUT ")
.AppendJoin(", ", deleteStatement.OutputExpressions.Select(ColumnExpression));
.AppendJoin(", ", deleteStatement.OutputExpressions.Select(c => ColumnExpression(c, "DELETED")));
}

if (deleteStatement.FromExpressions?.Count > 0)
Expand Down Expand Up @@ -538,4 +539,14 @@ public virtual string ParseIdentifier(string name)
return name;
}


private string ColumnExpression(ColumnExpression columnExpression, string tableAlias)
{
var column = columnExpression with
{
TableAlias = columnExpression.TableAlias ?? tableAlias
};

return ColumnExpression(column);
}
}
6 changes: 3 additions & 3 deletions src/FluentCommand/Query/InsertBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public TBuilder ValueIf<TValue>(
/// </returns>
public TBuilder Output(
IEnumerable<string> columnNames,
string tableAlias = "INSERTED")
string tableAlias = null)
{
if (columnNames is null)
throw new ArgumentNullException(nameof(columnNames));
Expand All @@ -189,7 +189,7 @@ public TBuilder Output(
/// </returns>
public TBuilder Output(
string columnName,
string tableAlias = "INSERTED",
string tableAlias = null,
string columnAlias = null)
{
var outputClause = new ColumnExpression(columnName, tableAlias, columnAlias);
Expand All @@ -211,7 +211,7 @@ public TBuilder Output(
/// </returns>
public TBuilder OutputIf(
string columnName,
string tableAlias = "INSERTED",
string tableAlias = null,
string columnAlias = null,
Func<string, bool> condition = null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/FluentCommand/Query/InsertEntityBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public InsertEntityBuilder<TEntity> Values(
/// </returns>
public InsertEntityBuilder<TEntity> Output<TValue>(
Expression<Func<TEntity, TValue>> property,
string tableAlias = "INSERTED",
string tableAlias = null,
string columnAlias = null)
{
var propertyAccessor = _typeAccessor.FindProperty(property);
Expand All @@ -128,7 +128,7 @@ public InsertEntityBuilder<TEntity> Output<TValue>(
/// </returns>
public InsertEntityBuilder<TEntity> OutputIf<TValue>(
Expression<Func<TEntity, TValue>> property,
string tableAlias = "INSERTED",
string tableAlias = null,
string columnAlias = null,
Func<string, bool> condition = null)
{
Expand Down
6 changes: 3 additions & 3 deletions src/FluentCommand/Query/UpdateBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public TBuilder ValueIf<TValue>(
/// </returns>
public TBuilder Output(
IEnumerable<string> columnNames,
string tableAlias = "INSERTED")
string tableAlias = null)
{
if (columnNames is null)
throw new ArgumentNullException(nameof(columnNames));
Expand All @@ -201,7 +201,7 @@ public TBuilder Output(
/// </returns>
public TBuilder Output(
string columnName,
string tableAlias = "INSERTED",
string tableAlias = null,
string columnAlias = null)
{
var outputClause = new ColumnExpression(columnName, tableAlias, columnAlias);
Expand All @@ -223,7 +223,7 @@ public TBuilder Output(
/// </returns>
public TBuilder OutputIf(
string columnName,
string tableAlias = "INSERTED",
string tableAlias = null,
string columnAlias = null,
Func<string, bool> condition = null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/FluentCommand/Query/UpdateEntityBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public UpdateEntityBuilder<TEntity> Values(
/// </returns>
public UpdateEntityBuilder<TEntity> Output<TValue>(
Expression<Func<TEntity, TValue>> property,
string tableAlias = "INSERTED",
string tableAlias = null,
string columnAlias = null)
{
var propertyAccessor = _typeAccessor.FindProperty(property);
Expand All @@ -133,7 +133,7 @@ public UpdateEntityBuilder<TEntity> Output<TValue>(
/// </returns>
public UpdateEntityBuilder<TEntity> OutputIf<TValue>(
Expression<Func<TEntity, TValue>> property,
string tableAlias = "INSERTED",
string tableAlias = null,
string columnAlias = null,
Func<string, bool> condition = null)
{
Expand Down
10 changes: 5 additions & 5 deletions test/FluentCommand.PostgreSQL.Tests/DataQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public async System.Threading.Tasks.Task SqlInsertValueQuery()
.Value(p => p.DisplayName, "Last, First")
.Value(p => p.FirstName, "First")
.Value(p => p.LastName, "Last")
.Output(p => p.Id, tableAlias: "")
.Output(p => p.Id)
.Tag()
)
.QueryValueAsync<Guid>();
Expand Down Expand Up @@ -237,7 +237,7 @@ public async System.Threading.Tasks.Task SqlInsertEntityQuery()
.Sql(builder => builder
.Insert<User>()
.Values(user)
.Output(p => p.Id, tableAlias: "")
.Output(p => p.Id)
.Tag()
)
.QueryValueAsync<Guid>();
Expand Down Expand Up @@ -267,7 +267,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery()
.Sql(builder => builder
.Insert<User>()
.Values(user)
.Output(p => p.Id, tableAlias: "")
.Output(p => p.Id)
.Tag()
)
.QueryValueAsync<Guid>();
Expand All @@ -289,7 +289,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery()
.Sql(builder => builder
.Update<User>()
.Value(p => p.DisplayName, "Updated")
.Output(p => p.Id, tableAlias: "")
.Output(p => p.Id)
.Where(p => p.Id, id)
.Tag()
)
Expand All @@ -300,7 +300,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery()
var deleteId = await session
.Sql(builder => builder
.Delete<User>()
.Output(p => p.Id, tableAlias: "")
.Output(p => p.Id)
.Where(p => p.Id, id)
.Tag()
)
Expand Down
10 changes: 5 additions & 5 deletions test/FluentCommand.SQLite.Tests/DataQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public async System.Threading.Tasks.Task SqlInsertValueQuery()
.Value(p => p.DisplayName, "Last, First")
.Value(p => p.FirstName, "First")
.Value(p => p.LastName, "Last")
.Output(p => p.Id, tableAlias: "")
.Output(p => p.Id)
.Tag()
)
.QueryValueAsync<Guid>();
Expand Down Expand Up @@ -237,7 +237,7 @@ public async System.Threading.Tasks.Task SqlInsertEntityQuery()
.Sql(builder => builder
.Insert<User>()
.Values(user)
.Output(p => p.Id, tableAlias: "")
.Output(p => p.Id)
.Tag()
)
.QueryValueAsync<Guid>();
Expand Down Expand Up @@ -267,7 +267,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery()
.Sql(builder => builder
.Insert<User>()
.Values(user)
.Output(p => p.Id, tableAlias: "")
.Output(p => p.Id)
.Tag()
)
.QueryValueAsync<Guid>();
Expand All @@ -289,7 +289,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery()
.Sql(builder => builder
.Update<User>()
.Value(p => p.DisplayName, "Updated")
.Output(p => p.Id, tableAlias: "")
.Output(p => p.Id)
.Where(p => p.Id, id)
.Tag()
)
Expand All @@ -300,7 +300,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery()
var deleteId = await session
.Sql(builder => builder
.Delete<User>()
.Output(p => p.Id, tableAlias: "")
.Output(p => p.Id)
.Where(p => p.Id, id)
.Tag()
)
Expand Down

0 comments on commit 8cf6ae8

Please sign in to comment.