Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 68da117

Browse files
wwwliciousmythz
authored andcommitted
Updating dapper to release 1.60.1 (#623)
1 parent 47bc396 commit 68da117

9 files changed

+73
-31
lines changed

src/ServiceStack.OrmLite/Dapper/DataTableHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Data;
3+
34
#if !NETSTANDARD1_3
45
namespace ServiceStack.OrmLite.Dapper
56
{

src/ServiceStack.OrmLite/Dapper/SqlDataRecordListTVPParameter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Data;
3+
using System.Linq;
44

55
namespace ServiceStack.OrmLite.Dapper
66
{
@@ -32,7 +32,7 @@ void SqlMapper.ICustomQueryParameter.AddParameter(IDbCommand command, string nam
3232

3333
internal static void Set(IDbDataParameter parameter, IEnumerable<Microsoft.SqlServer.Server.SqlDataRecord> data, string typeName)
3434
{
35-
parameter.Value = (object)data ?? DBNull.Value;
35+
parameter.Value = data != null && data.Any() ? data : null;
3636
if (parameter is System.Data.SqlClient.SqlParameter sqlParam)
3737
{
3838
sqlParam.SqlDbType = SqlDbType.Structured;

src/ServiceStack.OrmLite/Dapper/SqlMapper.Async.cs

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -373,19 +373,49 @@ private static Task<DbDataReader> ExecuteReaderWithFlagsFallbackAsync(DbCommand
373373
return task;
374374
}
375375

376+
/// <summary>
377+
/// Attempts to open a connection asynchronously, with a better error message for unsupported usages.
378+
/// </summary>
379+
private static Task TryOpenAsync(this IDbConnection cnn, CancellationToken cancel)
380+
{
381+
if (cnn is DbConnection dbConn)
382+
{
383+
return dbConn.OpenAsync(cancel);
384+
}
385+
else
386+
{
387+
throw new InvalidOperationException("Async operations require use of a DbConnection or an already-open IDbConnection");
388+
}
389+
}
390+
391+
/// <summary>
392+
/// Attempts setup a <see cref="DbCommand"/> on a <see cref="DbConnection"/>, with a better error message for unsupported usages.
393+
/// </summary>
394+
private static DbCommand TrySetupAsyncCommand(this CommandDefinition command, IDbConnection cnn, Action<IDbCommand, object> paramReader)
395+
{
396+
if (command.SetupCommand(cnn, paramReader) is DbCommand dbCommand)
397+
{
398+
return dbCommand;
399+
}
400+
else
401+
{
402+
throw new InvalidOperationException("Async operations require use of a DbConnection or an IDbConnection where .CreateCommand() returns a DbCommand");
403+
}
404+
}
405+
376406
private static async Task<IEnumerable<T>> QueryAsync<T>(this IDbConnection cnn, Type effectiveType, CommandDefinition command)
377407
{
378408
object param = command.Parameters;
379409
var identity = new Identity(command.CommandText, command.CommandType, cnn, effectiveType, param?.GetType(), null);
380410
var info = GetCacheInfo(identity, param, command.AddToCache);
381411
bool wasClosed = cnn.State == ConnectionState.Closed;
382412
var cancel = command.CancellationToken;
383-
using (var cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader))
413+
using (var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader))
384414
{
385415
DbDataReader reader = null;
386416
try
387417
{
388-
if (wasClosed) await ((DbConnection)cnn).OpenAsync(cancel).ConfigureAwait(false);
418+
if (wasClosed) await cnn.TryOpenAsync(cancel).ConfigureAwait(false);
389419
reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, cancel).ConfigureAwait(false);
390420

391421
var tuple = info.Deserializer;
@@ -444,12 +474,12 @@ private static async Task<T> QueryRowAsync<T>(this IDbConnection cnn, Row row, T
444474
var info = GetCacheInfo(identity, param, command.AddToCache);
445475
bool wasClosed = cnn.State == ConnectionState.Closed;
446476
var cancel = command.CancellationToken;
447-
using (var cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader))
477+
using (var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader))
448478
{
449479
DbDataReader reader = null;
450480
try
451481
{
452-
if (wasClosed) await ((DbConnection)cnn).OpenAsync(cancel).ConfigureAwait(false);
482+
if (wasClosed) await cnn.TryOpenAsync(cancel).ConfigureAwait(false);
453483
reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, (row & Row.Single) != 0
454484
? CommandBehavior.SequentialAccess | CommandBehavior.SingleResult // need to allow multiple rows, to check fail condition
455485
: CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow, cancel).ConfigureAwait(false);
@@ -546,7 +576,7 @@ private static async Task<int> ExecuteMultiImplAsync(IDbConnection cnn, CommandD
546576
bool wasClosed = cnn.State == ConnectionState.Closed;
547577
try
548578
{
549-
if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false);
579+
if (wasClosed) await cnn.TryOpenAsync(command.CancellationToken).ConfigureAwait(false);
550580

551581
CacheInfo info = null;
552582
string masterSql = null;
@@ -562,7 +592,7 @@ private static async Task<int> ExecuteMultiImplAsync(IDbConnection cnn, CommandD
562592
if (isFirst)
563593
{
564594
isFirst = false;
565-
cmd = (DbCommand)command.SetupCommand(cnn, null);
595+
cmd = command.TrySetupAsyncCommand(cnn, null);
566596
masterSql = cmd.CommandText;
567597
var identity = new Identity(command.CommandText, cmd.CommandType, cnn, null, obj.GetType(), null);
568598
info = GetCacheInfo(identity, obj, command.AddToCache);
@@ -577,7 +607,7 @@ private static async Task<int> ExecuteMultiImplAsync(IDbConnection cnn, CommandD
577607
}
578608
else
579609
{
580-
cmd = (DbCommand)command.SetupCommand(cnn, null);
610+
cmd = command.TrySetupAsyncCommand(cnn, null);
581611
}
582612
info.ParamReader(cmd, obj);
583613

@@ -604,7 +634,7 @@ private static async Task<int> ExecuteMultiImplAsync(IDbConnection cnn, CommandD
604634
}
605635
else
606636
{
607-
using (var cmd = (DbCommand)command.SetupCommand(cnn, null))
637+
using (var cmd = command.TrySetupAsyncCommand(cnn, null))
608638
{
609639
foreach (var obj in multiExec)
610640
{
@@ -640,11 +670,11 @@ private static async Task<int> ExecuteImplAsync(IDbConnection cnn, CommandDefini
640670
var identity = new Identity(command.CommandText, command.CommandType, cnn, null, param?.GetType(), null);
641671
var info = GetCacheInfo(identity, param, command.AddToCache);
642672
bool wasClosed = cnn.State == ConnectionState.Closed;
643-
using (var cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader))
673+
using (var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader))
644674
{
645675
try
646676
{
647-
if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false);
677+
if (wasClosed) await cnn.TryOpenAsync(command.CancellationToken).ConfigureAwait(false);
648678
var result = await cmd.ExecuteNonQueryAsync(command.CancellationToken).ConfigureAwait(false);
649679
command.OnCompleted();
650680
return result;
@@ -910,8 +940,8 @@ private static async Task<IEnumerable<TReturn>> MultiMapAsync<TFirst, TSecond, T
910940
bool wasClosed = cnn.State == ConnectionState.Closed;
911941
try
912942
{
913-
if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false);
914-
using (var cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader))
943+
if (wasClosed) await cnn.TryOpenAsync(command.CancellationToken).ConfigureAwait(false);
944+
using (var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader))
915945
using (var reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, command.CancellationToken).ConfigureAwait(false))
916946
{
917947
if (!command.Buffered) wasClosed = false; // handing back open reader; rely on command-behavior
@@ -960,8 +990,8 @@ private static async Task<IEnumerable<TReturn>> MultiMapAsync<TReturn>(this IDbC
960990
bool wasClosed = cnn.State == ConnectionState.Closed;
961991
try
962992
{
963-
if (wasClosed) await ((DbConnection)cnn).OpenAsync().ConfigureAwait(false);
964-
using (var cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader))
993+
if (wasClosed) await cnn.TryOpenAsync(command.CancellationToken).ConfigureAwait(false);
994+
using (var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader))
965995
using (var reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, command.CancellationToken).ConfigureAwait(false))
966996
{
967997
var results = MultiMapImpl(null, default(CommandDefinition), types, map, splitOn, reader, identity, true);
@@ -1015,8 +1045,8 @@ public static async Task<GridReader> QueryMultipleAsync(this IDbConnection cnn,
10151045
bool wasClosed = cnn.State == ConnectionState.Closed;
10161046
try
10171047
{
1018-
if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false);
1019-
cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader);
1048+
if (wasClosed) await cnn.TryOpenAsync(command.CancellationToken).ConfigureAwait(false);
1049+
cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader);
10201050
reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess, command.CancellationToken).ConfigureAwait(false);
10211051

10221052
var result = new GridReader(cmd, reader, identity, command.Parameters as DynamicParameters, command.AddToCache, command.CancellationToken);
@@ -1108,8 +1138,8 @@ private static async Task<IDataReader> ExecuteReaderImplAsync(IDbConnection cnn,
11081138
bool wasClosed = cnn.State == ConnectionState.Closed;
11091139
try
11101140
{
1111-
cmd = (DbCommand)command.SetupCommand(cnn, paramReader);
1112-
if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false);
1141+
cmd = command.TrySetupAsyncCommand(cnn, paramReader);
1142+
if (wasClosed) await cnn.TryOpenAsync(command.CancellationToken).ConfigureAwait(false);
11131143
var reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, commandBehavior, command.CancellationToken).ConfigureAwait(false);
11141144
wasClosed = false;
11151145
return reader;
@@ -1182,8 +1212,8 @@ private static async Task<T> ExecuteScalarImplAsync<T>(IDbConnection cnn, Comman
11821212
object result;
11831213
try
11841214
{
1185-
cmd = (DbCommand)command.SetupCommand(cnn, paramReader);
1186-
if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false);
1215+
cmd = command.TrySetupAsyncCommand(cnn, paramReader);
1216+
if (wasClosed) await cnn.TryOpenAsync(command.CancellationToken).ConfigureAwait(false);
11871217
result = await cmd.ExecuteScalarAsync(command.CancellationToken).ConfigureAwait(false);
11881218
command.OnCompleted();
11891219
}

src/ServiceStack.OrmLite/Dapper/SqlMapper.DapperRowMetaObject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Reflection;
4+
45
namespace ServiceStack.OrmLite.Dapper
56
{
67
public static partial class SqlMapper

src/ServiceStack.OrmLite/Dapper/SqlMapper.GridReader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Data;
4-
using System.Linq;
54
using System.Globalization;
5+
using System.Linq;
6+
67
namespace ServiceStack.OrmLite.Dapper
78
{
89
public static partial class SqlMapper

src/ServiceStack.OrmLite/Dapper/SqlMapper.IDataReader.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,20 @@ public static IEnumerable<T> Parse<T>(this IDataReader reader)
1515
{
1616
if (reader.Read())
1717
{
18-
var deser = GetDeserializer(typeof(T), reader, 0, -1, false);
18+
var effectiveType = typeof(T);
19+
var deser = GetDeserializer(effectiveType, reader, 0, -1, false);
20+
var convertToType = Nullable.GetUnderlyingType(effectiveType) ?? effectiveType;
1921
do
2022
{
21-
yield return (T)deser(reader);
23+
object val = deser(reader);
24+
if (val == null || val is T)
25+
{
26+
yield return (T)val;
27+
}
28+
else
29+
{
30+
yield return (T)Convert.ChangeType(val, convertToType, System.Globalization.CultureInfo.InvariantCulture);
31+
}
2232
} while (reader.Read());
2333
}
2434
}

src/ServiceStack.OrmLite/Dapper/SqlMapper.TypeDeserializerCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
2-
using System.Data;
32
using System.Collections;
43
using System.Collections.Generic;
4+
using System.Data;
55
using System.Text;
66

77
namespace ServiceStack.OrmLite.Dapper

src/ServiceStack.OrmLite/Dapper/SqlMapper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ public static IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TFift
13491349
MultiMap<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, DontMap, TReturn>(cnn, sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType);
13501350

13511351
/// <summary>
1352-
/// Perform a multi-mapping query with 7 input types.
1352+
/// Perform a multi-mapping query with 7 input types. If you need more types -> use Query with Type[] parameter.
13531353
/// This returns a single type, combined from the raw types via <paramref name="map"/>.
13541354
/// </summary>
13551355
/// <typeparam name="TFirst">The first type in the recordset.</typeparam>
@@ -2323,7 +2323,7 @@ public static string Format(object value)
23232323
return sb.Append(')').__ToStringRecycle();
23242324
}
23252325
}
2326-
throw new NotSupportedException(value.GetType().Name);
2326+
throw new NotSupportedException($"The type '{value.GetType().Name}' is not supported for SQL literals.");
23272327
}
23282328
}
23292329
}
@@ -2416,7 +2416,7 @@ internal static Action<IDbCommand, object> CreateParamInfoGenerator(Identity ide
24162416
il.Emit(OpCodes.Ldarg_1); // stack is now [untyped-param]
24172417
if (isStruct)
24182418
{
2419-
il.DeclareLocal(type.MakePointerType());
2419+
il.DeclareLocal(type.MakeByRefType()); // note: ref-local
24202420
il.Emit(OpCodes.Unbox, type); // stack is now [typed-param]
24212421
}
24222422
else

src/ServiceStack.OrmLite/Dapper/TypeExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Reflection;
3-
using System.Collections.Generic;
43

54
namespace ServiceStack.OrmLite.Dapper
65
{

0 commit comments

Comments
 (0)