Skip to content

Commit 86979eb

Browse files
committed
SqlServer provider: Code from providers older than v13 merged into v13
- Azure version is now based on v13 (MS keeps version 12 but functionality is way ahead even v13, according to MS) - '==null','!=null' checks replaced with 'is null'/'is not null' checks
1 parent 6e0e82e commit 86979eb

18 files changed

Lines changed: 3339 additions & 96 deletions

File tree

Orm/Xtensive.Orm.SqlServer/Orm.Providers.SqlServer/SqlCompiler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected override SqlProvider VisitFreeText(FreeTextProvider provider)
3737
var table = Mapping[index.ReflectedType];
3838
var columns = provider.Header.Columns.Select(column => column.Name).ToList();
3939

40-
if (provider.TopN == null) {
40+
if (provider.TopN is null) {
4141
fromTable = SqlDml.FreeTextTable(table, criteriaBinding.ParameterReference, columns);
4242
bindings = new[] {criteriaBinding};
4343
}
@@ -69,7 +69,7 @@ protected override SqlProvider VisitContainsTable(ContainsTableProvider provider
6969
var columns = provider.Header.Columns.Select(column => column.Name).ToList();
7070

7171
var targetColumnNames = provider.TargetColumns.Select(c => c.Name).ToArray();
72-
if (provider.TopN == null) {
72+
if (provider.TopN is null) {
7373
fromTable = SqlDml.ContainsTable(table, criteriaBinding.ParameterReference, columns, targetColumnNames);
7474
bindings = new[] { criteriaBinding };
7575
}

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/Azure/Driver.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
// Created: 2009.07.07
66

77
using Xtensive.Sql.Info;
8-
using SqlServerConnection = Microsoft.Data.SqlClient.SqlConnection;
98

109
namespace Xtensive.Sql.Drivers.SqlServer.Azure
1110
{
12-
internal class Driver : v12.Driver
11+
internal class Driver : v13.Driver
1312
{
1413
protected override Model.Extractor CreateExtractor()
1514
{

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/Azure/Extractor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Xtensive.Sql.Drivers.SqlServer.Azure
88
{
9-
internal class Extractor : v12.Extractor
9+
internal class Extractor : v13.Extractor
1010
{
1111
// Constructors
1212

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/Azure/ServerInfoProvider.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
// Created by: Dmitri Maximov
55
// Created: 2009.11.09
66

7-
using SqlServerConnection = Microsoft.Data.SqlClient.SqlConnection;
87

98
namespace Xtensive.Sql.Drivers.SqlServer.Azure
109
{
11-
internal class ServerInfoProvider : v12.ServerInfoProvider
10+
internal class ServerInfoProvider : v13.ServerInfoProvider
1211
{
1312
// Constructors
1413

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/Connection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public override void Open()
4040
}
4141
else {
4242
var connectionAccessorEx = Extensions.Get<DbConnectionAccessorExtension>();
43-
if (connectionAccessorEx == null) {
43+
if (connectionAccessorEx is null) {
4444
OpenWithCheckFast(DefaultCheckConnectionQuery);
4545
}
4646
else {
@@ -58,7 +58,7 @@ public override Task OpenAsync(CancellationToken cancellationToken)
5858
}
5959

6060
var connectionAccessorEx = Extensions.Get<DbConnectionAccessorExtension>();
61-
if (connectionAccessorEx == null) {
61+
if (connectionAccessorEx is null) {
6262
return OpenWithCheckFastAsync(DefaultCheckConnectionQuery, cancellationToken);
6363
}
6464
else {
@@ -78,7 +78,7 @@ public override void OpenAndInitialize(string initializationScript)
7878
? DefaultCheckConnectionQuery
7979
: initializationScript;
8080
var connectionAccessorEx = Extensions.Get<DbConnectionAccessorExtension>();
81-
if (connectionAccessorEx == null) {
81+
if (connectionAccessorEx is null) {
8282
OpenWithCheckFast(script);
8383
}
8484
else {
@@ -97,7 +97,7 @@ public override Task OpenAndInitializeAsync(string initializationScript, Cancell
9797
? DefaultCheckConnectionQuery
9898
: initializationScript;
9999
var connectionAccessorEx = Extensions.Get<DbConnectionAccessorExtension>();
100-
return connectionAccessorEx == null
100+
return connectionAccessorEx is null
101101
? OpenWithCheckFastAsync(script, token)
102102
: OpenWithCheckAndNotificationAsync(script, connectionAccessorEx, token);
103103
}

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/Driver.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,26 @@ public override SqlExceptionType GetExceptionType(Exception exception)
2828

2929
public override SqlExceptionInfo GetExceptionInfo(Exception exception)
3030
{
31-
var nativeException = exception as SqlException;
32-
if (nativeException==null)
31+
if (exception is not SqlException nativeException)
3332
return SqlExceptionInfo.Create(SqlExceptionType.Unknown);
3433
int errorCode = nativeException.Number;
35-
string errorMessage = nativeException.Message;
36-
if (errorCode==-2)
37-
return SqlExceptionInfo.Create(SqlExceptionType.OperationTimeout);
38-
if (errorCode==1205)
39-
return SqlExceptionInfo.Create(SqlExceptionType.Deadlock);
34+
switch (errorCode) {
35+
case -2:
36+
return SqlExceptionInfo.Create(SqlExceptionType.OperationTimeout);
37+
case 1205:
38+
return SqlExceptionInfo.Create(SqlExceptionType.Deadlock);
39+
case 229:
40+
case 230:
41+
return SqlExceptionInfo.Create(SqlExceptionType.Unknown);
42+
}
43+
4044
if ((errorCode >= 3950 && errorCode <= 3961) || errorCode==3966 || errorCode==3971)
4145
return SqlExceptionInfo.Create(SqlExceptionType.SerializationFailure);
42-
if (errorCode==229 || errorCode==230)
43-
return SqlExceptionInfo.Create(SqlExceptionType.Unknown);
4446
if (errorCode >= 100 && errorCode <= 499)
4547
return SqlExceptionInfo.Create(SqlExceptionType.SyntaxError);
48+
4649
var info = new SqlExceptionInfo();
47-
if (TryProvideErrorContext(errorCode, errorMessage, info)) {
50+
if (TryProvideErrorContext(errorCode, nativeException.Message, info)) {
4851
info.Lock();
4952
return info;
5053
}

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/DriverFactory.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ namespace Xtensive.Sql.Drivers.SqlServer
2222
/// </summary>
2323
public class DriverFactory : SqlDriverFactory
2424
{
25+
private const int SqlServer2016MajorVersion = 13;
26+
private const int SqlServer2017MajorVersion = 14;
27+
private const int SqlServer2019MajorVersion = 15;
28+
private const int SqlServer2022MajorVersion = 16;
29+
private const int SqlServer2025MajorVersion = 17;
30+
2531
private const string CheckConnectionQuery = "SELECT TOP(0) 0;";
2632
private const string PoolingOffCommand = "pooling = false";
2733

@@ -113,12 +119,9 @@ protected override string BuildConnectionString(UrlInfo url)
113119
builder.Encrypt = url.Secure;
114120

115121
// host, port, database
116-
if (url.Port==0) {
117-
builder.DataSource = url.Host;
118-
}
119-
else {
120-
builder.DataSource = url.Host + "," + url.Port;
121-
}
122+
builder.DataSource = url.Port==0
123+
? url.Host
124+
: url.Host + "," + url.Port;
122125

123126
builder.InitialCatalog = url.Resource ?? string.Empty;
124127

@@ -183,7 +186,7 @@ protected override async Task<SqlDriver> CreateDriverAsync(
183186
: await CreateMessageParserAsync(connection, token).ConfigureAwait(false);
184187

185188
var versionString = isForcedVersion
186-
? isForcedAzure ? "10.0.0.0" : forcedServerVersion
189+
? isForcedAzure ? ForcedAzureVersion : forcedServerVersion
187190
: connection.ServerVersion ?? string.Empty;
188191
var version = new Version(versionString);
189192
var defaultSchema = await GetDefaultSchemaAsync(connection, token: token).ConfigureAwait(false);
@@ -207,15 +210,11 @@ private static SqlDriver CreateDriverInstance(string connectionString, bool isAz
207210
return new Azure.Driver(coreServerInfo, parser, isEnsureAlive);
208211
}
209212

210-
if (version.Major < 9) {
211-
throw new NotSupportedException(Strings.ExSqlServerBelow2005IsNotSupported);
213+
if (version.Major < SqlServer2016MajorVersion) {
214+
throw new NotSupportedException(Strings.ExSqlServerBelow2016IsNotSupported);
212215
}
213216
return version.Major switch {
214-
9 => new v09.Driver(coreServerInfo, parser, isEnsureAlive),
215-
10 => new v10.Driver(coreServerInfo, parser, isEnsureAlive),
216-
11 => new v11.Driver(coreServerInfo, parser, isEnsureAlive),
217-
12 => new v12.Driver(coreServerInfo, parser, isEnsureAlive),
218-
13 => new v13.Driver(coreServerInfo, parser, isEnsureAlive),
217+
SqlServer2016MajorVersion => new v13.Driver(coreServerInfo, parser, isEnsureAlive),
219218
_ => new v13.Driver(coreServerInfo, parser, isEnsureAlive)
220219
};
221220
}

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/SqlServerTypeMapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2012-2021 Xtensive LLC.
1+
// Copyright (C) 2012-2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Denis Krjuchkov
@@ -22,7 +22,7 @@ internal abstract class SqlServerTypeMapper : CustomTypeMapper
2222

2323
public override void BindValue(DbParameter parameter, object value)
2424
{
25-
if (value==null) {
25+
if (value is null) {
2626
parameter.Value = DBNull.Value;
2727
return;
2828
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (C) 2003-2010 Xtensive LLC.
2+
// All rights reserved.
3+
// For conditions of distribution and use, see license.
4+
// Created by: Dmitri Maximov
5+
// Created: 2009.08.12
6+
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using Xtensive.Sql.Model;
11+
12+
namespace Xtensive.Sql.Drivers.SqlServer.v13
13+
{
14+
internal sealed class ColumnResolver
15+
{
16+
public DataTable Table;
17+
private List<ColumnIndexMapping> columnMappings;
18+
19+
private class ColumnIndexMapping
20+
{
21+
public readonly int DbIndex;
22+
public readonly int ModelIndex;
23+
24+
public ColumnIndexMapping(int dbIndex, int modelIndex)
25+
{
26+
DbIndex = dbIndex;
27+
ModelIndex = modelIndex;
28+
}
29+
}
30+
31+
public void RegisterColumnMapping(int dbIndex, int modelIndex)
32+
{
33+
if (columnMappings is null)
34+
columnMappings = new List<ColumnIndexMapping>(1);
35+
36+
columnMappings.Add(new ColumnIndexMapping(dbIndex, modelIndex));
37+
}
38+
39+
public DataTableColumn GetColumn(int dbIndex)
40+
{
41+
int modelIndex = dbIndex-1;
42+
if (Table is View view)
43+
return view.ViewColumns[modelIndex];
44+
45+
var table = (Table)Table;
46+
if (columnMappings is null)
47+
return table.TableColumns[modelIndex];
48+
49+
var mapping = columnMappings.Where(item => item.DbIndex==dbIndex).FirstOrDefault();
50+
if (mapping is not null)
51+
return table.TableColumns[mapping.ModelIndex];
52+
53+
throw new ArgumentOutOfRangeException("dbIndex");
54+
}
55+
56+
public ColumnResolver(DataTable table)
57+
{
58+
Table = table;
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)