-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSqlServerDialectBuilder.cs
More file actions
140 lines (113 loc) · 4.78 KB
/
SqlServerDialectBuilder.cs
File metadata and controls
140 lines (113 loc) · 4.78 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
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.SqlServer;
internal class SqlServerDialectBuilder : 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);
}
protected override string Trim(string lhs) => $"TRIM({lhs})";
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();
var identityInsert = options.CopyGeneratedColumns && insertedColumns.Any(x => x.IsGenerated);
if (identityInsert)
{
q.AppendLine($"SET IDENTITY_INSERT {target.QuotedTableName} ON;");
}
// 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 ");
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 ");
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(")");
if (returnedColumns.Count != 0)
{
q.Append("OUTPUT ");
q.AppendJoin(", ", returnedColumns, (b, col) => b.Append($"{PseudoTableInserted}.{col.QuotedColumName} AS {col.QuotedColumName}"));
q.AppendLine();
}
q.Append("SELECT ");
q.AppendColumns(insertedColumns);
q.AppendLine();
q.Append($"FROM {source}");
q.AppendLine();
}
q.AppendLine(";");
if (identityInsert)
{
q.AppendLine($"SET IDENTITY_INSERT {target.QuotedTableName} OFF;");
}
return q.ToString();
}
}