diff --git a/src/FluentCommand/Query/DeleteBuilder.cs b/src/FluentCommand/Query/DeleteBuilder.cs index 49acde91..9b568f3d 100644 --- a/src/FluentCommand/Query/DeleteBuilder.cs +++ b/src/FluentCommand/Query/DeleteBuilder.cs @@ -108,7 +108,7 @@ public TBuilder Table( /// public TBuilder Output( IEnumerable columnNames, - string tableAlias = "DELETED") + string tableAlias = null) { if (columnNames is null) throw new ArgumentNullException(nameof(columnNames)); @@ -130,7 +130,7 @@ public TBuilder Output( /// public TBuilder Output( string columnName, - string tableAlias = "DELETED", + string tableAlias = null, string columnAlias = null) { var outputClause = new ColumnExpression(columnName, tableAlias, columnAlias); @@ -152,7 +152,7 @@ public TBuilder Output( /// public TBuilder OutputIf( string columnName, - string tableAlias = "DELETED", + string tableAlias = null, string columnAlias = null, Func condition = null) { diff --git a/src/FluentCommand/Query/DeleteEntityBuilder.cs b/src/FluentCommand/Query/DeleteEntityBuilder.cs index 722d13d7..bac795e1 100644 --- a/src/FluentCommand/Query/DeleteEntityBuilder.cs +++ b/src/FluentCommand/Query/DeleteEntityBuilder.cs @@ -44,7 +44,7 @@ public DeleteEntityBuilder( /// public DeleteEntityBuilder Output( Expression> property, - string tableAlias = "DELETED", + string tableAlias = null, string columnAlias = null) { var propertyAccessor = _typeAccessor.FindProperty(property); @@ -64,7 +64,7 @@ public DeleteEntityBuilder Output( /// public DeleteEntityBuilder OutputIf( Expression> property, - string tableAlias = "DELETED", + string tableAlias = null, string columnAlias = null, Func condition = null) { diff --git a/src/FluentCommand/Query/Generators/SqlServerGenerator.cs b/src/FluentCommand/Query/Generators/SqlServerGenerator.cs index d8601bc5..ed3437dc 100644 --- a/src/FluentCommand/Query/Generators/SqlServerGenerator.cs +++ b/src/FluentCommand/Query/Generators/SqlServerGenerator.cs @@ -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 @@ -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) @@ -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) @@ -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); + } } diff --git a/src/FluentCommand/Query/InsertBuilder.cs b/src/FluentCommand/Query/InsertBuilder.cs index 7f1238c5..ddf1d538 100644 --- a/src/FluentCommand/Query/InsertBuilder.cs +++ b/src/FluentCommand/Query/InsertBuilder.cs @@ -167,7 +167,7 @@ public TBuilder ValueIf( /// public TBuilder Output( IEnumerable columnNames, - string tableAlias = "INSERTED") + string tableAlias = null) { if (columnNames is null) throw new ArgumentNullException(nameof(columnNames)); @@ -189,7 +189,7 @@ public TBuilder Output( /// public TBuilder Output( string columnName, - string tableAlias = "INSERTED", + string tableAlias = null, string columnAlias = null) { var outputClause = new ColumnExpression(columnName, tableAlias, columnAlias); @@ -211,7 +211,7 @@ public TBuilder Output( /// public TBuilder OutputIf( string columnName, - string tableAlias = "INSERTED", + string tableAlias = null, string columnAlias = null, Func condition = null) { diff --git a/src/FluentCommand/Query/InsertEntityBuilder.cs b/src/FluentCommand/Query/InsertEntityBuilder.cs index d72d8c3b..bc23bfa8 100644 --- a/src/FluentCommand/Query/InsertEntityBuilder.cs +++ b/src/FluentCommand/Query/InsertEntityBuilder.cs @@ -108,7 +108,7 @@ public InsertEntityBuilder Values( /// public InsertEntityBuilder Output( Expression> property, - string tableAlias = "INSERTED", + string tableAlias = null, string columnAlias = null) { var propertyAccessor = _typeAccessor.FindProperty(property); @@ -128,7 +128,7 @@ public InsertEntityBuilder Output( /// public InsertEntityBuilder OutputIf( Expression> property, - string tableAlias = "INSERTED", + string tableAlias = null, string columnAlias = null, Func condition = null) { diff --git a/src/FluentCommand/Query/UpdateBuilder.cs b/src/FluentCommand/Query/UpdateBuilder.cs index 6cac66fe..b4b02fe9 100644 --- a/src/FluentCommand/Query/UpdateBuilder.cs +++ b/src/FluentCommand/Query/UpdateBuilder.cs @@ -179,7 +179,7 @@ public TBuilder ValueIf( /// public TBuilder Output( IEnumerable columnNames, - string tableAlias = "INSERTED") + string tableAlias = null) { if (columnNames is null) throw new ArgumentNullException(nameof(columnNames)); @@ -201,7 +201,7 @@ public TBuilder Output( /// public TBuilder Output( string columnName, - string tableAlias = "INSERTED", + string tableAlias = null, string columnAlias = null) { var outputClause = new ColumnExpression(columnName, tableAlias, columnAlias); @@ -223,7 +223,7 @@ public TBuilder Output( /// public TBuilder OutputIf( string columnName, - string tableAlias = "INSERTED", + string tableAlias = null, string columnAlias = null, Func condition = null) { diff --git a/src/FluentCommand/Query/UpdateEntityBuilder.cs b/src/FluentCommand/Query/UpdateEntityBuilder.cs index e33257a1..2803ba1d 100644 --- a/src/FluentCommand/Query/UpdateEntityBuilder.cs +++ b/src/FluentCommand/Query/UpdateEntityBuilder.cs @@ -113,7 +113,7 @@ public UpdateEntityBuilder Values( /// public UpdateEntityBuilder Output( Expression> property, - string tableAlias = "INSERTED", + string tableAlias = null, string columnAlias = null) { var propertyAccessor = _typeAccessor.FindProperty(property); @@ -133,7 +133,7 @@ public UpdateEntityBuilder Output( /// public UpdateEntityBuilder OutputIf( Expression> property, - string tableAlias = "INSERTED", + string tableAlias = null, string columnAlias = null, Func condition = null) { diff --git a/test/FluentCommand.PostgreSQL.Tests/DataQueryTests.cs b/test/FluentCommand.PostgreSQL.Tests/DataQueryTests.cs index 112f760f..d3fec021 100644 --- a/test/FluentCommand.PostgreSQL.Tests/DataQueryTests.cs +++ b/test/FluentCommand.PostgreSQL.Tests/DataQueryTests.cs @@ -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(); @@ -237,7 +237,7 @@ public async System.Threading.Tasks.Task SqlInsertEntityQuery() .Sql(builder => builder .Insert() .Values(user) - .Output(p => p.Id, tableAlias: "") + .Output(p => p.Id) .Tag() ) .QueryValueAsync(); @@ -267,7 +267,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery() .Sql(builder => builder .Insert() .Values(user) - .Output(p => p.Id, tableAlias: "") + .Output(p => p.Id) .Tag() ) .QueryValueAsync(); @@ -289,7 +289,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery() .Sql(builder => builder .Update() .Value(p => p.DisplayName, "Updated") - .Output(p => p.Id, tableAlias: "") + .Output(p => p.Id) .Where(p => p.Id, id) .Tag() ) @@ -300,7 +300,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery() var deleteId = await session .Sql(builder => builder .Delete() - .Output(p => p.Id, tableAlias: "") + .Output(p => p.Id) .Where(p => p.Id, id) .Tag() ) diff --git a/test/FluentCommand.SQLite.Tests/DataQueryTests.cs b/test/FluentCommand.SQLite.Tests/DataQueryTests.cs index fc36a28c..b7d47a2c 100644 --- a/test/FluentCommand.SQLite.Tests/DataQueryTests.cs +++ b/test/FluentCommand.SQLite.Tests/DataQueryTests.cs @@ -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(); @@ -237,7 +237,7 @@ public async System.Threading.Tasks.Task SqlInsertEntityQuery() .Sql(builder => builder .Insert() .Values(user) - .Output(p => p.Id, tableAlias: "") + .Output(p => p.Id) .Tag() ) .QueryValueAsync(); @@ -267,7 +267,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery() .Sql(builder => builder .Insert() .Values(user) - .Output(p => p.Id, tableAlias: "") + .Output(p => p.Id) .Tag() ) .QueryValueAsync(); @@ -289,7 +289,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery() .Sql(builder => builder .Update() .Value(p => p.DisplayName, "Updated") - .Output(p => p.Id, tableAlias: "") + .Output(p => p.Id) .Where(p => p.Id, id) .Tag() ) @@ -300,7 +300,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery() var deleteId = await session .Sql(builder => builder .Delete() - .Output(p => p.Id, tableAlias: "") + .Output(p => p.Id) .Where(p => p.Id, id) .Tag() )