-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathOracleDialectBuilder.cs
More file actions
151 lines (128 loc) · 6.46 KB
/
OracleDialectBuilder.cs
File metadata and controls
151 lines (128 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System.Text;
using Microsoft.EntityFrameworkCore;
using PhenX.EntityFrameworkCore.BulkInsert.Dialect;
using PhenX.EntityFrameworkCore.BulkInsert.Metadata;
using PhenX.EntityFrameworkCore.BulkInsert.Options;
namespace PhenX.EntityFrameworkCore.BulkInsert.Oracle;
internal class OracleDialectBuilder : SqlDialectBuilder
{
protected override string OpenDelimiter => "\"";
protected override string CloseDelimiter => "\"";
protected override string ConcatOperator => "||";
protected override bool SupportsMoveRows => false;
public override string CreateTableCopySql(string tempTableName, TableMetadata tableInfo, IReadOnlyList<ColumnMetadata> columns)
{
return CreateTableCopySqlBase(tempTableName, columns);
}
public override string BuildMoveDataSql<T>(
DbContext context,
TableMetadata target,
string source,
IReadOnlyList<ColumnMetadata> insertedColumns,
IReadOnlyList<ColumnMetadata> returnedColumns,
BulkInsertOptions options,
OnConflictOptions? onConflict = null)
{
var q = new StringBuilder();
// Merge handling
if (onConflict is OnConflictOptions<T> onConflictTyped)
{
// Oracle MERGE doesn't support returning entities
if (returnedColumns.Count != 0)
{
throw new NotSupportedException("Oracle MERGE does not support returning entities. Use ExecuteBulkInsertAsync without returning results when using conflict resolution.");
}
IReadOnlyList<string> matchColumns;
if (onConflictTyped.Match != null)
{
matchColumns = GetColumns(target, onConflictTyped.Match).ToList();
}
else if (target.PrimaryKey.Length > 0)
{
matchColumns = target.PrimaryKey.Select(x => x.QuotedColumName).ToList();
}
else
{
throw new InvalidOperationException("Table has no primary key that can be used for conflict detection.");
}
// Validate that all match columns are available in the source subquery
var insertedColumnNames = insertedColumns.Select(c => c.QuotedColumName).ToHashSet();
var missingMatchColumns = matchColumns.Where(c => !insertedColumnNames.Contains(c)).ToList();
if (missingMatchColumns.Count != 0)
{
throw new InvalidOperationException(
$"Oracle MERGE requires match columns to be present in the source data. " +
$"The following match columns are not available: {string.Join(", ", missingMatchColumns)}. " +
$"This can happen when using auto-generated primary key columns for conflict detection. " +
$"Use the 'Match' option to specify non-generated columns for conflict detection, " +
$"or set 'CopyGeneratedColumns = true' if the generated column values are provided.");
}
// Oracle MERGE syntax does NOT use AS for table aliases
q.AppendLine($"MERGE INTO {target.QuotedTableName} {PseudoTableInserted}");
q.Append("USING (SELECT ");
q.AppendColumns(insertedColumns);
// Oracle MERGE syntax does NOT use AS for subquery aliases
q.Append($" FROM {source}) {PseudoTableExcluded}");
q.AppendLine();
// Oracle requires ON clause conditions to be wrapped in parentheses
q.Append("ON (");
q.AppendJoin(" AND ", matchColumns, (b, col) => b.Append($"{PseudoTableInserted}.{col} = {PseudoTableExcluded}.{col}"));
q.AppendLine(")");
// Oracle MERGE syntax: WHEN NOT MATCHED clause for inserts, followed by WHEN MATCHED clause for updates
q.Append("WHEN NOT MATCHED THEN INSERT (");
q.AppendColumns(insertedColumns);
q.AppendLine(")");
q.Append("VALUES (");
q.AppendJoin(", ", insertedColumns, (b, col) => b.Append($"{PseudoTableExcluded}.{col.QuotedColumName}"));
q.AppendLine(")");
if (onConflictTyped.Update != null)
{
q.Append("WHEN MATCHED ");
if (onConflictTyped.RawWhere != null || onConflictTyped.Where != null)
{
if (onConflictTyped is { RawWhere: not null, Where: not null })
{
throw new ArgumentException("Cannot specify both RawWhere and Where in OnConflictOptions.");
}
q.Append("AND ");
AppendConflictCondition(q, target, context, onConflictTyped);
}
q.AppendLine("THEN UPDATE SET ");
// Oracle MERGE: columns in ON clause cannot be updated, so exclude match columns
// Use insertedColumns instead of all columns because the USING subquery only contains insertedColumns
var matchColumnSet = matchColumns.ToHashSet();
var updateableColumns = insertedColumns.Where(c => !matchColumnSet.Contains(c.QuotedColumName)).ToList();
if (updateableColumns.Count == 0)
{
throw new InvalidOperationException(
"Oracle MERGE cannot update any columns because all available columns are used in the ON clause for conflict detection. " +
"Specify different columns in the 'Match' option or use specific columns in the 'Update' expression.");
}
q.AppendJoin(", ", GetUpdates(context, target, updateableColumns, onConflictTyped.Update));
q.AppendLine();
}
}
// No conflict handling
else
{
q.Append($"INSERT INTO {target.QuotedTableName} (");
q.AppendColumns(insertedColumns);
q.AppendLine(")");
q.Append("SELECT ");
q.AppendColumns(insertedColumns);
q.AppendLine();
q.Append($"FROM {source}");
q.AppendLine();
if (returnedColumns.Count != 0)
{
q.Append("RETURNING ");
q.AppendJoin(", ", returnedColumns, (b, col) => b.Append(col.QuotedColumName));
q.Append(" INTO ");
q.AppendJoin(", ", returnedColumns, (b, col) => b.Append($":{col.ColumnName}"));
q.AppendLine();
}
}
q.AppendLine(";");
return q.ToString();
}
}