Skip to content

Commit 60a2311

Browse files
CopilotPhenX
andcommitted
Fix Oracle MERGE syntax by removing AS keyword and OUTPUT clause
Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
1 parent 231c24c commit 60a2311

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

src/PhenX.EntityFrameworkCore.BulkInsert.Oracle/OracleDialectBuilder.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,12 @@ public override string BuildMoveDataSql<T>(
4949
throw new InvalidOperationException("Table has no primary key that can be used for conflict detection.");
5050
}
5151

52-
q.AppendLine($"MERGE INTO {target.QuotedTableName} AS {PseudoTableInserted}");
52+
q.AppendLine($"MERGE INTO {target.QuotedTableName} {PseudoTableInserted}");
5353

5454
q.Append("USING (SELECT ");
5555
q.AppendColumns(insertedColumns);
56-
q.Append($" FROM {source}) AS {PseudoTableExcluded} (");
57-
q.AppendColumns(insertedColumns);
58-
q.AppendLine(")");
56+
q.Append($" FROM {source}) {PseudoTableExcluded}");
57+
q.AppendLine();
5958

6059
q.Append("ON ");
6160
q.AppendJoin(" AND ", matchColumns, (b, col) => b.Append($"{PseudoTableInserted}.{col} = {PseudoTableExcluded}.{col}"));
@@ -65,7 +64,20 @@ public override string BuildMoveDataSql<T>(
6564
{
6665
var columns = target.GetColumns(false);
6766

68-
q.AppendLine("WHEN MATCHED THEN UPDATE SET ");
67+
q.Append("WHEN MATCHED");
68+
69+
if (onConflictTyped.RawWhere != null || onConflictTyped.Where != null)
70+
{
71+
if (onConflictTyped is { RawWhere: not null, Where: not null })
72+
{
73+
throw new ArgumentException("Cannot specify both RawWhere and Where in OnConflictOptions.");
74+
}
75+
76+
q.Append(" AND ");
77+
AppendConflictCondition(q, target, context, onConflictTyped);
78+
}
79+
80+
q.AppendLine(" THEN UPDATE SET ");
6981
q.AppendJoin(", ", GetUpdates(context, target, columns, onConflictTyped.Update));
7082
q.AppendLine();
7183
}
@@ -77,13 +89,6 @@ public override string BuildMoveDataSql<T>(
7789
q.Append("VALUES (");
7890
q.AppendJoin(", ", insertedColumns, (b, col) => b.Append($"{PseudoTableExcluded}.{col.QuotedColumName}"));
7991
q.AppendLine(")");
80-
81-
if (returnedColumns.Count != 0)
82-
{
83-
q.Append("OUTPUT ");
84-
q.AppendJoin(", ", returnedColumns, (b, col) => b.Append($"{PseudoTableInserted}.{col.QuotedColumName} AS {col.QuotedColumName}"));
85-
q.AppendLine();
86-
}
8792
}
8893

8994
// No conflict handling
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContainer;
2+
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
3+
4+
using Xunit;
5+
6+
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.Tests.Merge;
7+
8+
[Trait("Category", "Oracle")]
9+
[Collection(TestDbContainerOracleCollection.Name)]
10+
public class MergeTestsOracle(TestDbContainerOracle dbContainer) : MergeTestsBase<TestDbContextOracle>(dbContainer)
11+
{
12+
}

0 commit comments

Comments
 (0)