-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlServerDatabaseAdapter.cs
More file actions
168 lines (136 loc) · 5.48 KB
/
Copy pathSqlServerDatabaseAdapter.cs
File metadata and controls
168 lines (136 loc) · 5.48 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) 2026 David Liebeherr
// Licensed under the MIT License. See LICENSE.md in the project root for more information.
using RentADeveloper.DbConnectionPlus.Converters;
namespace RentADeveloper.DbConnectionPlus.DatabaseAdapters.SqlServer;
/// <summary>
/// The database adapter for SQL Server databases.
/// </summary>
public class SqlServerDatabaseAdapter : IDatabaseAdapter
{
private static readonly Dictionary<Type, string> typeToSqlDataType = new()
{
{ typeof(bool), "bit" },
{ typeof(byte), "tinyint" },
{ typeof(byte[]), "varbinary(max)" },
{ typeof(char), "char(1)" },
{ typeof(DateOnly), "date" },
{ typeof(DateTime), "datetime2" },
{ typeof(DateTimeOffset), "datetimeoffset" },
{ typeof(decimal), "decimal(28,10)" },
{ typeof(double), "float" },
{ typeof(Guid), "uniqueidentifier" },
{ typeof(short), "smallint" },
{ typeof(int), "int" },
{ typeof(long), "bigint" },
{ typeof(object), "sql_variant" },
{ typeof(float), "real" },
{ typeof(string), "nvarchar(max)" },
{ typeof(TimeOnly), "time" },
{ typeof(TimeSpan), "time" },
};
private readonly SqlServerEntityManipulator entityManipulator;
private readonly SqlServerTemporaryTableBuilder temporaryTableBuilder;
/// <summary>
/// Initializes a new instance of the <see cref="SqlServerDatabaseAdapter" /> class.
/// </summary>
public SqlServerDatabaseAdapter()
{
this.temporaryTableBuilder = new(this);
this.entityManipulator = new(this);
}
/// <inheritdoc />
public IEntityManipulator EntityManipulator => this.entityManipulator;
/// <inheritdoc />
public ITemporaryTableBuilder TemporaryTableBuilder => this.temporaryTableBuilder;
/// <inheritdoc />
public void BindParameterValue(DbParameter parameter, object? value)
{
ArgumentNullException.ThrowIfNull(parameter);
switch (value)
{
case DateTime:
parameter.DbType = DbType.DateTime2;
parameter.Value = value;
break;
case Enum enumValue:
parameter.DbType = DbConnectionPlusConfiguration.Instance.EnumSerializationMode switch
{
EnumSerializationMode.Integers => DbType.Int32,
EnumSerializationMode.Strings => DbType.String,
_ => ThrowHelper.ThrowInvalidEnumSerializationModeException<DbType>(
DbConnectionPlusConfiguration.Instance.EnumSerializationMode
),
};
parameter.Value = EnumSerializer.SerializeEnum(
enumValue,
DbConnectionPlusConfiguration.Instance.EnumSerializationMode
);
break;
case byte[]:
parameter.DbType = DbType.Binary;
parameter.Value = value;
break;
default:
parameter.Value = value ?? DBNull.Value;
break;
}
}
/// <inheritdoc />
public string FormatParameterName(string parameterName) => "@" + parameterName;
/// <inheritdoc />
public string GetDataType(Type type, EnumSerializationMode enumSerializationMode)
{
ArgumentNullException.ThrowIfNull(type);
// Unwrap Nullable<T> types:
var effectiveType = Nullable.GetUnderlyingType(type) ?? type;
if (effectiveType.IsEnum)
{
return enumSerializationMode switch
{
EnumSerializationMode.Strings => "nvarchar(200)", // 200 should be enough for most enum names
EnumSerializationMode.Integers => "int",
_ => ThrowHelper.ThrowInvalidEnumSerializationModeException<string>(enumSerializationMode),
};
}
if (!typeToSqlDataType.TryGetValue(effectiveType, out var result))
{
throw new ArgumentOutOfRangeException(
nameof(type),
type,
$"Could not map the type {type} to an SQL Server data type."
);
}
return result;
}
/// <inheritdoc />
public string QuoteIdentifier(string identifier) => "[" + identifier + "]";
/// <inheritdoc />
public string QuoteTemporaryTableName(string tableName, DbConnection connection) => "[#" + tableName + "]";
/// <inheritdoc />
public bool SupportsTemporaryTables(DbConnection connection) => true;
/// <inheritdoc />
public bool WasSqlStatementCancelledByCancellationToken(Exception exception, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(exception);
if (exception is not SqlException sqlException)
{
return false;
}
// Unfortunately SQL Server does not raise a specific error when a statement is being cancelled by the user.
// However, if a cancellation was requested via the specified cancellation token and
// SQL Server raised an error with class 11, number 0 and state 0, then we can be pretty sure the error was
// raised because of the cancellation.
if (!cancellationToken.IsCancellationRequested)
{
return false;
}
foreach (SqlError error in sqlException.Errors)
{
if (error is { Class: 11, Number: 0, State: 0 })
{
return true;
}
}
return false;
}
}