|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data; |
| 4 | +using System.Data.Common; |
| 5 | +using NHibernate.AdoNet; |
| 6 | +using NHibernate.Dialect; |
| 7 | +using NHibernate.Engine; |
| 8 | +using NHibernate.SqlTypes; |
| 9 | +using NHibernate.Util; |
| 10 | + |
| 11 | +namespace NHibernate.Driver |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// A NHibernate Driver for using the SqlClient DataProvider |
| 15 | + /// </summary> |
| 16 | + public class MicrosoftDataSqlClientDriver : ReflectionBasedDriver, IEmbeddedBatcherFactoryProvider, IParameterAdjuster |
| 17 | + { |
| 18 | + const byte MaxTime = 5; |
| 19 | + |
| 20 | + private static readonly Action<object, SqlDbType> SetSqlDbType = DelegateHelper.BuildPropertySetter<SqlDbType>(System.Type.GetType("Microsoft.Data.SqlClient.SqlParameter, Microsoft.Data.SqlClient", true), "SqlDbType"); |
| 21 | + |
| 22 | + private Dialect.Dialect _dialect; |
| 23 | + |
| 24 | + public MicrosoftDataSqlClientDriver() |
| 25 | + : base( |
| 26 | + "Microsoft.Data.SqlClient", |
| 27 | + "Microsoft.Data.SqlClient.SqlConnection", |
| 28 | + "Microsoft.Data.SqlClient.SqlCommand") |
| 29 | + { |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// MsSql requires the use of a Named Prefix in the SQL statement. |
| 34 | + /// </summary> |
| 35 | + /// <remarks> |
| 36 | + /// <see langword="true" /> because MsSql uses "<c>@</c>". |
| 37 | + /// </remarks> |
| 38 | + public override bool UseNamedPrefixInSql => true; |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// MsSql requires the use of a Named Prefix in the Parameter. |
| 42 | + /// </summary> |
| 43 | + /// <remarks> |
| 44 | + /// <see langword="true" /> because MsSql uses "<c>@</c>". |
| 45 | + /// </remarks> |
| 46 | + public override bool UseNamedPrefixInParameter => true; |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// The Named Prefix for parameters. |
| 50 | + /// </summary> |
| 51 | + /// <value> |
| 52 | + /// Sql Server uses <c>"@"</c>. |
| 53 | + /// </value> |
| 54 | + public override string NamedPrefix => "@"; |
| 55 | + |
| 56 | + /// <inheritdoc/> |
| 57 | + public override bool SupportsMultipleOpenReaders => false; |
| 58 | + |
| 59 | + /// <inheritdoc /> |
| 60 | + public override bool SupportsMultipleQueries => true; |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// With read committed snapshot or lower, SQL Server may have not actually already committed the transaction |
| 64 | + /// right after the scope disposal. |
| 65 | + /// </summary> |
| 66 | + public override bool HasDelayedDistributedTransactionCompletion => true; |
| 67 | + |
| 68 | + public override bool RequiresTimeSpanForTime => true; |
| 69 | + |
| 70 | + /// <inheritdoc /> |
| 71 | + public override DateTime MinDate => DateTime.MinValue; |
| 72 | + |
| 73 | + System.Type IEmbeddedBatcherFactoryProvider.BatcherFactoryClass => typeof(GenericBatchingBatcherFactory); |
| 74 | + |
| 75 | + /// <inheritdoc /> |
| 76 | + public virtual void AdjustParameterForValue(DbParameter parameter, SqlType sqlType, object value) |
| 77 | + { |
| 78 | + if (value is string stringVal) |
| 79 | + switch (parameter.DbType) |
| 80 | + { |
| 81 | + case DbType.AnsiString: |
| 82 | + case DbType.AnsiStringFixedLength: |
| 83 | + parameter.Size = IsAnsiText(parameter, sqlType) |
| 84 | + ? MsSql2000Dialect.MaxSizeForAnsiClob |
| 85 | + : Math.Max(stringVal.Length, sqlType.LengthDefined ? sqlType.Length : parameter.Size); |
| 86 | + break; |
| 87 | + case DbType.String: |
| 88 | + case DbType.StringFixedLength: |
| 89 | + parameter.Size = IsText(parameter, sqlType) |
| 90 | + ? MsSql2000Dialect.MaxSizeForClob |
| 91 | + : Math.Max(stringVal.Length, sqlType.LengthDefined ? sqlType.Length : parameter.Size); |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public override void Configure(IDictionary<string, string> settings) |
| 97 | + { |
| 98 | + base.Configure(settings); |
| 99 | + |
| 100 | + _dialect = Dialect.Dialect.GetDialect(settings); |
| 101 | + } |
| 102 | + |
| 103 | + /// <inheritdoc /> |
| 104 | + protected override void InitializeParameter(DbParameter dbParam, string name, SqlType sqlType) |
| 105 | + { |
| 106 | + base.InitializeParameter(dbParam, name, sqlType); |
| 107 | + |
| 108 | + // Defaults size/precision/scale |
| 109 | + switch (dbParam.DbType) |
| 110 | + { |
| 111 | + case DbType.AnsiString: |
| 112 | + case DbType.AnsiStringFixedLength: |
| 113 | + dbParam.Size = IsAnsiText(dbParam, sqlType) |
| 114 | + ? MsSql2000Dialect.MaxSizeForAnsiClob |
| 115 | + : MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString; |
| 116 | + break; |
| 117 | + case DbType.Binary: |
| 118 | + dbParam.Size = IsBlob(dbParam, sqlType) |
| 119 | + ? MsSql2000Dialect.MaxSizeForBlob |
| 120 | + : MsSql2000Dialect.MaxSizeForLengthLimitedBinary; |
| 121 | + break; |
| 122 | + case DbType.Decimal: |
| 123 | + if (_dialect == null) |
| 124 | + throw new InvalidOperationException( |
| 125 | + "Dialect not available, is this driver used without having been configured?"); |
| 126 | + dbParam.Precision = _dialect.DefaultCastPrecision; |
| 127 | + dbParam.Scale = _dialect.DefaultCastScale; |
| 128 | + break; |
| 129 | + case DbType.String: |
| 130 | + case DbType.StringFixedLength: |
| 131 | + dbParam.Size = IsText(dbParam, sqlType) |
| 132 | + ? MsSql2000Dialect.MaxSizeForClob |
| 133 | + : MsSql2000Dialect.MaxSizeForLengthLimitedString; |
| 134 | + break; |
| 135 | + case DbType.DateTime2: |
| 136 | + dbParam.Size = MsSql2000Dialect.MaxDateTime2; |
| 137 | + break; |
| 138 | + case DbType.DateTimeOffset: |
| 139 | + dbParam.Size = MsSql2000Dialect.MaxDateTimeOffset; |
| 140 | + break; |
| 141 | + case DbType.Xml: |
| 142 | + dbParam.Size = MsSql2005Dialect.MaxSizeForXml; |
| 143 | + break; |
| 144 | + } |
| 145 | + switch (sqlType.DbType) |
| 146 | + { |
| 147 | + case DbType.Time: |
| 148 | + SetSqlDbType(dbParam, SqlDbType.Time); |
| 149 | + dbParam.Size = MaxTime; |
| 150 | + break; |
| 151 | + case DbType.Date: |
| 152 | + SetSqlDbType(dbParam, SqlDbType.Date); |
| 153 | + break; |
| 154 | + } |
| 155 | + |
| 156 | + // Do not override the default length for string using data from SqlType, since LIKE expressions needs |
| 157 | + // larger columns. https://nhibernate.jira.com/browse/NH-3036 |
| 158 | + |
| 159 | + if (sqlType.PrecisionDefined) |
| 160 | + { |
| 161 | + dbParam.Precision = sqlType.Precision; |
| 162 | + dbParam.Scale = sqlType.Scale; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary> |
| 167 | + /// Interprets if a parameter is a Clob (for the purposes of setting its default size) |
| 168 | + /// </summary> |
| 169 | + /// <param name="dbParam">The parameter</param> |
| 170 | + /// <param name="sqlType">The <see cref="SqlType" /> of the parameter</param> |
| 171 | + /// <returns>True, if the parameter should be interpreted as a Clob, otherwise False</returns> |
| 172 | + protected static bool IsAnsiText(DbParameter dbParam, SqlType sqlType) |
| 173 | + { |
| 174 | + return (DbType.AnsiString == dbParam.DbType || DbType.AnsiStringFixedLength == dbParam.DbType) && |
| 175 | + sqlType.LengthDefined && sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString; |
| 176 | + } |
| 177 | + |
| 178 | + /// <summary> |
| 179 | + /// Interprets if a parameter is a Clob (for the purposes of setting its default size) |
| 180 | + /// </summary> |
| 181 | + /// <param name="dbParam">The parameter</param> |
| 182 | + /// <param name="sqlType">The <see cref="SqlType" /> of the parameter</param> |
| 183 | + /// <returns>True, if the parameter should be interpreted as a Clob, otherwise False</returns> |
| 184 | + protected static bool IsText(DbParameter dbParam, SqlType sqlType) |
| 185 | + { |
| 186 | + return sqlType is StringClobSqlType || |
| 187 | + (DbType.String == dbParam.DbType || DbType.StringFixedLength == dbParam.DbType) && |
| 188 | + sqlType.LengthDefined && sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedString; |
| 189 | + } |
| 190 | + |
| 191 | + /// <summary> |
| 192 | + /// Interprets if a parameter is a Blob (for the purposes of setting its default size) |
| 193 | + /// </summary> |
| 194 | + /// <param name="dbParam">The parameter</param> |
| 195 | + /// <param name="sqlType">The <see cref="SqlType" /> of the parameter</param> |
| 196 | + /// <returns>True, if the parameter should be interpreted as a Blob, otherwise False</returns> |
| 197 | + protected static bool IsBlob(DbParameter dbParam, SqlType sqlType) |
| 198 | + { |
| 199 | + return sqlType is BinaryBlobSqlType || DbType.Binary == dbParam.DbType && sqlType.LengthDefined && |
| 200 | + sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedBinary; |
| 201 | + } |
| 202 | + |
| 203 | + /// <inheritdoc /> |
| 204 | + public override IResultSetsCommand GetResultSetsCommand(ISessionImplementor session) |
| 205 | + { |
| 206 | + return new BasicResultSetsCommand(session); |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments