-
Notifications
You must be signed in to change notification settings - Fork 933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a driver to support Microsoft.Data.SqlClient provider #2216
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
40c67e4
Add a driver to support Microsoft.Data.SqlClient provider
hazzik 1d921e3
Address feedback
hazzik dfa08fc
Merge branch 'master' into Microsoft.Data.SqlClient
hazzik 84aabbf
Fix SupportsMultipleOpenReaders & Time & DateTime
hazzik 159366a
Fix tests failing with Microsoft.Data.SqlClient
fredericDelaporte 9aebf9d
Re-enable a test for Microsoft.Data.SqlClient
fredericDelaporte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Data.Common; | ||
using NHibernate.AdoNet; | ||
using NHibernate.Dialect; | ||
using NHibernate.Engine; | ||
using NHibernate.SqlTypes; | ||
using NHibernate.Util; | ||
|
||
namespace NHibernate.Driver | ||
{ | ||
/// <summary> | ||
/// A NHibernate Driver for using the SqlClient DataProvider | ||
/// </summary> | ||
public class MicrosoftDataSqlClientDriver : ReflectionBasedDriver, IEmbeddedBatcherFactoryProvider, IParameterAdjuster | ||
{ | ||
const byte MaxTime = 5; | ||
|
||
private static readonly Action<object, SqlDbType> SetSqlDbType = DelegateHelper.BuildPropertySetter<SqlDbType>(System.Type.GetType("Microsoft.Data.SqlClient.SqlParameter, Microsoft.Data.SqlClient", true), "SqlDbType"); | ||
|
||
private Dialect.Dialect _dialect; | ||
|
||
public MicrosoftDataSqlClientDriver() | ||
: base( | ||
"Microsoft.Data.SqlClient", | ||
"Microsoft.Data.SqlClient.SqlConnection", | ||
"Microsoft.Data.SqlClient.SqlCommand") | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// MsSql requires the use of a Named Prefix in the SQL statement. | ||
/// </summary> | ||
/// <remarks> | ||
/// <see langword="true" /> because MsSql uses "<c>@</c>". | ||
/// </remarks> | ||
public override bool UseNamedPrefixInSql => true; | ||
|
||
/// <summary> | ||
/// MsSql requires the use of a Named Prefix in the Parameter. | ||
/// </summary> | ||
/// <remarks> | ||
/// <see langword="true" /> because MsSql uses "<c>@</c>". | ||
/// </remarks> | ||
public override bool UseNamedPrefixInParameter => true; | ||
|
||
/// <summary> | ||
/// The Named Prefix for parameters. | ||
/// </summary> | ||
/// <value> | ||
/// Sql Server uses <c>"@"</c>. | ||
/// </value> | ||
public override string NamedPrefix => "@"; | ||
|
||
/// <inheritdoc/> | ||
public override bool SupportsMultipleOpenReaders => false; | ||
fredericDelaporte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// <inheritdoc /> | ||
public override bool SupportsMultipleQueries => true; | ||
|
||
/// <summary> | ||
/// With read committed snapshot or lower, SQL Server may have not actually already committed the transaction | ||
/// right after the scope disposal. | ||
/// </summary> | ||
public override bool HasDelayedDistributedTransactionCompletion => true; | ||
|
||
public override bool RequiresTimeSpanForTime => true; | ||
|
||
/// <inheritdoc /> | ||
public override DateTime MinDate => DateTime.MinValue; | ||
|
||
System.Type IEmbeddedBatcherFactoryProvider.BatcherFactoryClass => typeof(GenericBatchingBatcherFactory); | ||
fredericDelaporte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// <inheritdoc /> | ||
public virtual void AdjustParameterForValue(DbParameter parameter, SqlType sqlType, object value) | ||
{ | ||
if (value is string stringVal) | ||
switch (parameter.DbType) | ||
{ | ||
case DbType.AnsiString: | ||
case DbType.AnsiStringFixedLength: | ||
parameter.Size = IsAnsiText(parameter, sqlType) | ||
? MsSql2000Dialect.MaxSizeForAnsiClob | ||
: Math.Max(stringVal.Length, sqlType.LengthDefined ? sqlType.Length : parameter.Size); | ||
break; | ||
case DbType.String: | ||
case DbType.StringFixedLength: | ||
parameter.Size = IsText(parameter, sqlType) | ||
? MsSql2000Dialect.MaxSizeForClob | ||
: Math.Max(stringVal.Length, sqlType.LengthDefined ? sqlType.Length : parameter.Size); | ||
break; | ||
} | ||
} | ||
|
||
public override void Configure(IDictionary<string, string> settings) | ||
{ | ||
base.Configure(settings); | ||
|
||
_dialect = Dialect.Dialect.GetDialect(settings); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void InitializeParameter(DbParameter dbParam, string name, SqlType sqlType) | ||
{ | ||
base.InitializeParameter(dbParam, name, sqlType); | ||
|
||
// Defaults size/precision/scale | ||
switch (dbParam.DbType) | ||
hazzik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
case DbType.AnsiString: | ||
case DbType.AnsiStringFixedLength: | ||
dbParam.Size = IsAnsiText(dbParam, sqlType) | ||
? MsSql2000Dialect.MaxSizeForAnsiClob | ||
: MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString; | ||
break; | ||
case DbType.Binary: | ||
dbParam.Size = IsBlob(dbParam, sqlType) | ||
? MsSql2000Dialect.MaxSizeForBlob | ||
: MsSql2000Dialect.MaxSizeForLengthLimitedBinary; | ||
break; | ||
case DbType.Decimal: | ||
if (_dialect == null) | ||
throw new InvalidOperationException( | ||
"Dialect not available, is this driver used without having been configured?"); | ||
dbParam.Precision = _dialect.DefaultCastPrecision; | ||
dbParam.Scale = _dialect.DefaultCastScale; | ||
break; | ||
case DbType.String: | ||
case DbType.StringFixedLength: | ||
dbParam.Size = IsText(dbParam, sqlType) | ||
? MsSql2000Dialect.MaxSizeForClob | ||
: MsSql2000Dialect.MaxSizeForLengthLimitedString; | ||
break; | ||
case DbType.DateTime2: | ||
dbParam.Size = MsSql2000Dialect.MaxDateTime2; | ||
break; | ||
case DbType.DateTimeOffset: | ||
dbParam.Size = MsSql2000Dialect.MaxDateTimeOffset; | ||
break; | ||
case DbType.Xml: | ||
dbParam.Size = MsSql2005Dialect.MaxSizeForXml; | ||
break; | ||
} | ||
hazzik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
switch (sqlType.DbType) | ||
{ | ||
case DbType.Time: | ||
SetSqlDbType(dbParam, SqlDbType.Time); | ||
dbParam.Size = MaxTime; | ||
break; | ||
case DbType.Date: | ||
SetSqlDbType(dbParam, SqlDbType.Date); | ||
break; | ||
} | ||
|
||
// Do not override the default length for string using data from SqlType, since LIKE expressions needs | ||
// larger columns. https://nhibernate.jira.com/browse/NH-3036 | ||
|
||
if (sqlType.PrecisionDefined) | ||
{ | ||
dbParam.Precision = sqlType.Precision; | ||
dbParam.Scale = sqlType.Scale; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Interprets if a parameter is a Clob (for the purposes of setting its default size) | ||
/// </summary> | ||
/// <param name="dbParam">The parameter</param> | ||
/// <param name="sqlType">The <see cref="SqlType" /> of the parameter</param> | ||
/// <returns>True, if the parameter should be interpreted as a Clob, otherwise False</returns> | ||
protected static bool IsAnsiText(DbParameter dbParam, SqlType sqlType) | ||
{ | ||
return (DbType.AnsiString == dbParam.DbType || DbType.AnsiStringFixedLength == dbParam.DbType) && | ||
sqlType.LengthDefined && sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString; | ||
} | ||
|
||
/// <summary> | ||
/// Interprets if a parameter is a Clob (for the purposes of setting its default size) | ||
/// </summary> | ||
/// <param name="dbParam">The parameter</param> | ||
/// <param name="sqlType">The <see cref="SqlType" /> of the parameter</param> | ||
/// <returns>True, if the parameter should be interpreted as a Clob, otherwise False</returns> | ||
protected static bool IsText(DbParameter dbParam, SqlType sqlType) | ||
{ | ||
return sqlType is StringClobSqlType || | ||
(DbType.String == dbParam.DbType || DbType.StringFixedLength == dbParam.DbType) && | ||
sqlType.LengthDefined && sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedString; | ||
} | ||
|
||
/// <summary> | ||
/// Interprets if a parameter is a Blob (for the purposes of setting its default size) | ||
/// </summary> | ||
/// <param name="dbParam">The parameter</param> | ||
/// <param name="sqlType">The <see cref="SqlType" /> of the parameter</param> | ||
/// <returns>True, if the parameter should be interpreted as a Blob, otherwise False</returns> | ||
protected static bool IsBlob(DbParameter dbParam, SqlType sqlType) | ||
{ | ||
return sqlType is BinaryBlobSqlType || DbType.Binary == dbParam.DbType && sqlType.LengthDefined && | ||
sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedBinary; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override IResultSetsCommand GetResultSetsCommand(ISessionImplementor session) | ||
{ | ||
return new BasicResultSetsCommand(session); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Driver.CreateConnection()
should have been used from the start. It is meant for this. It just creates the connection, not doing anything else with it.