-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathOracleDialectBuilder.cs
More file actions
115 lines (95 loc) · 3.93 KB
/
OracleDialectBuilder.cs
File metadata and controls
115 lines (95 loc) · 3.93 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
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)
{
IEnumerable<string> matchColumns;
if (onConflictTyped.Match != null)
{
matchColumns = GetColumns(target, onConflictTyped.Match);
}
else if (target.PrimaryKey.Count > 0)
{
matchColumns = target.PrimaryKey.Select(x => x.QuotedColumName);
}
else
{
throw new InvalidOperationException("Table has no primary key that can be used for conflict detection.");
}
q.AppendLine($"MERGE INTO {target.QuotedTableName} AS {PseudoTableInserted}");
q.Append("USING (SELECT ");
q.AppendColumns(insertedColumns);
q.Append($" FROM {source}) AS {PseudoTableExcluded} (");
q.AppendColumns(insertedColumns);
q.AppendLine(")");
q.Append("ON ");
q.AppendJoin(" AND ", matchColumns, (b, col) => b.Append($"{PseudoTableInserted}.{col} = {PseudoTableExcluded}.{col}"));
q.AppendLine();
if (onConflictTyped.Update != null)
{
var columns = target.GetColumns(false);
q.AppendLine("WHEN MATCHED THEN UPDATE SET ");
q.AppendJoin(", ", GetUpdates(context, target, columns, onConflictTyped.Update));
q.AppendLine();
}
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 (returnedColumns.Count != 0)
{
q.Append("OUTPUT ");
q.AppendJoin(", ", returnedColumns, (b, col) => b.Append($"{PseudoTableInserted}.{col.QuotedColumName} AS {col.QuotedColumName}"));
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();
}
}