Skip to content

Commit 8cf6ae8

Browse files
committed
fix output column table alias bug
1 parent 3fbe1f7 commit 8cf6ae8

File tree

9 files changed

+39
-28
lines changed

9 files changed

+39
-28
lines changed

src/FluentCommand/Query/DeleteBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public TBuilder Table(
108108
/// </returns>
109109
public TBuilder Output(
110110
IEnumerable<string> columnNames,
111-
string tableAlias = "DELETED")
111+
string tableAlias = null)
112112
{
113113
if (columnNames is null)
114114
throw new ArgumentNullException(nameof(columnNames));
@@ -130,7 +130,7 @@ public TBuilder Output(
130130
/// </returns>
131131
public TBuilder Output(
132132
string columnName,
133-
string tableAlias = "DELETED",
133+
string tableAlias = null,
134134
string columnAlias = null)
135135
{
136136
var outputClause = new ColumnExpression(columnName, tableAlias, columnAlias);
@@ -152,7 +152,7 @@ public TBuilder Output(
152152
/// </returns>
153153
public TBuilder OutputIf(
154154
string columnName,
155-
string tableAlias = "DELETED",
155+
string tableAlias = null,
156156
string columnAlias = null,
157157
Func<string, bool> condition = null)
158158
{

src/FluentCommand/Query/DeleteEntityBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public DeleteEntityBuilder(
4444
/// </returns>
4545
public DeleteEntityBuilder<TEntity> Output<TValue>(
4646
Expression<Func<TEntity, TValue>> property,
47-
string tableAlias = "DELETED",
47+
string tableAlias = null,
4848
string columnAlias = null)
4949
{
5050
var propertyAccessor = _typeAccessor.FindProperty(property);
@@ -64,7 +64,7 @@ public DeleteEntityBuilder<TEntity> Output<TValue>(
6464
/// </returns>
6565
public DeleteEntityBuilder<TEntity> OutputIf<TValue>(
6666
Expression<Func<TEntity, TValue>> property,
67-
string tableAlias = "DELETED",
67+
string tableAlias = null,
6868
string columnAlias = null,
6969
Func<string, bool> condition = null)
7070
{

src/FluentCommand/Query/Generators/SqlServerGenerator.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,11 @@ public virtual string BuildInsert(InsertStatement insertStatement)
116116

117117
if (insertStatement.OutputExpressions?.Count > 0)
118118
{
119+
119120
insertBuilder
120121
.AppendLine()
121122
.Append("OUTPUT ")
122-
.AppendJoin(", ", insertStatement.OutputExpressions.Select(ColumnExpression));
123+
.AppendJoin(", ", insertStatement.OutputExpressions.Select(c => ColumnExpression(c, "INSERTED")));
123124
}
124125

125126
insertBuilder
@@ -163,7 +164,7 @@ public virtual string BuildUpdate(UpdateStatement updateStatement)
163164
updateBuilder
164165
.AppendLine()
165166
.Append("OUTPUT ")
166-
.AppendJoin(", ", updateStatement.OutputExpressions.Select(ColumnExpression));
167+
.AppendJoin(", ", updateStatement.OutputExpressions.Select(c => ColumnExpression(c, "INSERTED")));
167168
}
168169

169170
if (updateStatement.FromExpressions?.Count > 0)
@@ -224,7 +225,7 @@ public virtual string BuildDelete(DeleteStatement deleteStatement)
224225
deleteBuilder
225226
.AppendLine()
226227
.Append("OUTPUT ")
227-
.AppendJoin(", ", deleteStatement.OutputExpressions.Select(ColumnExpression));
228+
.AppendJoin(", ", deleteStatement.OutputExpressions.Select(c => ColumnExpression(c, "DELETED")));
228229
}
229230

230231
if (deleteStatement.FromExpressions?.Count > 0)
@@ -538,4 +539,14 @@ public virtual string ParseIdentifier(string name)
538539
return name;
539540
}
540541

542+
543+
private string ColumnExpression(ColumnExpression columnExpression, string tableAlias)
544+
{
545+
var column = columnExpression with
546+
{
547+
TableAlias = columnExpression.TableAlias ?? tableAlias
548+
};
549+
550+
return ColumnExpression(column);
551+
}
541552
}

src/FluentCommand/Query/InsertBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public TBuilder ValueIf<TValue>(
167167
/// </returns>
168168
public TBuilder Output(
169169
IEnumerable<string> columnNames,
170-
string tableAlias = "INSERTED")
170+
string tableAlias = null)
171171
{
172172
if (columnNames is null)
173173
throw new ArgumentNullException(nameof(columnNames));
@@ -189,7 +189,7 @@ public TBuilder Output(
189189
/// </returns>
190190
public TBuilder Output(
191191
string columnName,
192-
string tableAlias = "INSERTED",
192+
string tableAlias = null,
193193
string columnAlias = null)
194194
{
195195
var outputClause = new ColumnExpression(columnName, tableAlias, columnAlias);
@@ -211,7 +211,7 @@ public TBuilder Output(
211211
/// </returns>
212212
public TBuilder OutputIf(
213213
string columnName,
214-
string tableAlias = "INSERTED",
214+
string tableAlias = null,
215215
string columnAlias = null,
216216
Func<string, bool> condition = null)
217217
{

src/FluentCommand/Query/InsertEntityBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public InsertEntityBuilder<TEntity> Values(
108108
/// </returns>
109109
public InsertEntityBuilder<TEntity> Output<TValue>(
110110
Expression<Func<TEntity, TValue>> property,
111-
string tableAlias = "INSERTED",
111+
string tableAlias = null,
112112
string columnAlias = null)
113113
{
114114
var propertyAccessor = _typeAccessor.FindProperty(property);
@@ -128,7 +128,7 @@ public InsertEntityBuilder<TEntity> Output<TValue>(
128128
/// </returns>
129129
public InsertEntityBuilder<TEntity> OutputIf<TValue>(
130130
Expression<Func<TEntity, TValue>> property,
131-
string tableAlias = "INSERTED",
131+
string tableAlias = null,
132132
string columnAlias = null,
133133
Func<string, bool> condition = null)
134134
{

src/FluentCommand/Query/UpdateBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public TBuilder ValueIf<TValue>(
179179
/// </returns>
180180
public TBuilder Output(
181181
IEnumerable<string> columnNames,
182-
string tableAlias = "INSERTED")
182+
string tableAlias = null)
183183
{
184184
if (columnNames is null)
185185
throw new ArgumentNullException(nameof(columnNames));
@@ -201,7 +201,7 @@ public TBuilder Output(
201201
/// </returns>
202202
public TBuilder Output(
203203
string columnName,
204-
string tableAlias = "INSERTED",
204+
string tableAlias = null,
205205
string columnAlias = null)
206206
{
207207
var outputClause = new ColumnExpression(columnName, tableAlias, columnAlias);
@@ -223,7 +223,7 @@ public TBuilder Output(
223223
/// </returns>
224224
public TBuilder OutputIf(
225225
string columnName,
226-
string tableAlias = "INSERTED",
226+
string tableAlias = null,
227227
string columnAlias = null,
228228
Func<string, bool> condition = null)
229229
{

src/FluentCommand/Query/UpdateEntityBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public UpdateEntityBuilder<TEntity> Values(
113113
/// </returns>
114114
public UpdateEntityBuilder<TEntity> Output<TValue>(
115115
Expression<Func<TEntity, TValue>> property,
116-
string tableAlias = "INSERTED",
116+
string tableAlias = null,
117117
string columnAlias = null)
118118
{
119119
var propertyAccessor = _typeAccessor.FindProperty(property);
@@ -133,7 +133,7 @@ public UpdateEntityBuilder<TEntity> Output<TValue>(
133133
/// </returns>
134134
public UpdateEntityBuilder<TEntity> OutputIf<TValue>(
135135
Expression<Func<TEntity, TValue>> property,
136-
string tableAlias = "INSERTED",
136+
string tableAlias = null,
137137
string columnAlias = null,
138138
Func<string, bool> condition = null)
139139
{

test/FluentCommand.PostgreSQL.Tests/DataQueryTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public async System.Threading.Tasks.Task SqlInsertValueQuery()
207207
.Value(p => p.DisplayName, "Last, First")
208208
.Value(p => p.FirstName, "First")
209209
.Value(p => p.LastName, "Last")
210-
.Output(p => p.Id, tableAlias: "")
210+
.Output(p => p.Id)
211211
.Tag()
212212
)
213213
.QueryValueAsync<Guid>();
@@ -237,7 +237,7 @@ public async System.Threading.Tasks.Task SqlInsertEntityQuery()
237237
.Sql(builder => builder
238238
.Insert<User>()
239239
.Values(user)
240-
.Output(p => p.Id, tableAlias: "")
240+
.Output(p => p.Id)
241241
.Tag()
242242
)
243243
.QueryValueAsync<Guid>();
@@ -267,7 +267,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery()
267267
.Sql(builder => builder
268268
.Insert<User>()
269269
.Values(user)
270-
.Output(p => p.Id, tableAlias: "")
270+
.Output(p => p.Id)
271271
.Tag()
272272
)
273273
.QueryValueAsync<Guid>();
@@ -289,7 +289,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery()
289289
.Sql(builder => builder
290290
.Update<User>()
291291
.Value(p => p.DisplayName, "Updated")
292-
.Output(p => p.Id, tableAlias: "")
292+
.Output(p => p.Id)
293293
.Where(p => p.Id, id)
294294
.Tag()
295295
)
@@ -300,7 +300,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery()
300300
var deleteId = await session
301301
.Sql(builder => builder
302302
.Delete<User>()
303-
.Output(p => p.Id, tableAlias: "")
303+
.Output(p => p.Id)
304304
.Where(p => p.Id, id)
305305
.Tag()
306306
)

test/FluentCommand.SQLite.Tests/DataQueryTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public async System.Threading.Tasks.Task SqlInsertValueQuery()
207207
.Value(p => p.DisplayName, "Last, First")
208208
.Value(p => p.FirstName, "First")
209209
.Value(p => p.LastName, "Last")
210-
.Output(p => p.Id, tableAlias: "")
210+
.Output(p => p.Id)
211211
.Tag()
212212
)
213213
.QueryValueAsync<Guid>();
@@ -237,7 +237,7 @@ public async System.Threading.Tasks.Task SqlInsertEntityQuery()
237237
.Sql(builder => builder
238238
.Insert<User>()
239239
.Values(user)
240-
.Output(p => p.Id, tableAlias: "")
240+
.Output(p => p.Id)
241241
.Tag()
242242
)
243243
.QueryValueAsync<Guid>();
@@ -267,7 +267,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery()
267267
.Sql(builder => builder
268268
.Insert<User>()
269269
.Values(user)
270-
.Output(p => p.Id, tableAlias: "")
270+
.Output(p => p.Id)
271271
.Tag()
272272
)
273273
.QueryValueAsync<Guid>();
@@ -289,7 +289,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery()
289289
.Sql(builder => builder
290290
.Update<User>()
291291
.Value(p => p.DisplayName, "Updated")
292-
.Output(p => p.Id, tableAlias: "")
292+
.Output(p => p.Id)
293293
.Where(p => p.Id, id)
294294
.Tag()
295295
)
@@ -300,7 +300,7 @@ public async System.Threading.Tasks.Task SqlInsertUpdateDeleteEntityQuery()
300300
var deleteId = await session
301301
.Sql(builder => builder
302302
.Delete<User>()
303-
.Output(p => p.Id, tableAlias: "")
303+
.Output(p => p.Id)
304304
.Where(p => p.Id, id)
305305
.Tag()
306306
)

0 commit comments

Comments
 (0)