Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
Dapper
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
Dapper
Commits
3b215e37
Commit
3b215e37
authored
Nov 26, 2015
by
Nick Craver
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #415 from StackExchange/UseSingleResultSingleRow
Use single-row/single-result hint when possible
parents
65b26d6e
ec3a1953
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
19 additions
and
16 deletions
+19
-16
Assert.cs
Dapper.Tests/Assert.cs
+2
-2
SqlMapper.Async.cs
Dapper/SqlMapper.Async.cs
+6
-6
SqlMapper.cs
Dapper/SqlMapper.cs
+11
-8
No files found.
Dapper.Tests/Assert.cs
View file @
3b215e37
...
...
@@ -4,9 +4,9 @@ namespace Dapper.Tests
{
public
static
class
Assert
{
public
static
void
IsEqualTo
<
T
>(
this
T
obj
,
T
other
)
public
static
void
IsEqualTo
<
T
>(
this
T
expected
,
T
actual
)
{
Xunit
.
Assert
.
Equal
(
obj
,
other
);
Xunit
.
Assert
.
Equal
(
expected
,
actual
);
}
public
static
void
IsSequenceEqualTo
<
T
>(
this
IEnumerable
<
T
>
obj
,
IEnumerable
<
T
>
other
)
...
...
Dapper/SqlMapper.Async.cs
View file @
3b215e37
...
...
@@ -117,7 +117,7 @@ private static async Task<IEnumerable<T>> QueryAsync<T>(this IDbConnection cnn,
try
{
if
(
wasClosed
)
await
((
DbConnection
)
cnn
).
OpenAsync
(
cancel
).
ConfigureAwait
(
false
);
reader
=
await
cmd
.
ExecuteReaderAsync
(
wasClosed
?
CommandBehavior
.
CloseConnection
|
CommandBehavior
.
SequentialAccess
:
CommandBehavior
.
SequentialAccess
,
cancel
).
ConfigureAwait
(
false
);
reader
=
await
cmd
.
ExecuteReaderAsync
(
GetBehavior
(
wasClosed
,
CommandBehavior
.
SequentialAccess
|
CommandBehavior
.
SingleResult
)
,
cancel
).
ConfigureAwait
(
false
);
var
tuple
=
info
.
Deserializer
;
int
hash
=
GetColumnHash
(
reader
);
...
...
@@ -179,7 +179,7 @@ private static async Task<T> QueryFirstOrDefaultAsync<T>(this IDbConnection cnn,
try
{
if
(
wasClosed
)
await
((
DbConnection
)
cnn
).
OpenAsync
(
cancel
).
ConfigureAwait
(
false
);
reader
=
await
cmd
.
ExecuteReaderAsync
(
wasClosed
?
CommandBehavior
.
CloseConnection
|
CommandBehavior
.
SequentialAccess
:
CommandBehavior
.
SequentialAccess
,
cancel
).
ConfigureAwait
(
false
);
reader
=
await
cmd
.
ExecuteReaderAsync
(
GetBehavior
(
wasClosed
,
CommandBehavior
.
SequentialAccess
|
CommandBehavior
.
SingleResult
|
CommandBehavior
.
SingleRow
)
,
cancel
).
ConfigureAwait
(
false
);
T
result
=
default
(
T
);
if
(
await
reader
.
ReadAsync
(
cancel
).
ConfigureAwait
(
false
))
...
...
@@ -547,7 +547,7 @@ private static async Task<int> ExecuteImplAsync(IDbConnection cnn, CommandDefini
{
if
(
wasClosed
)
await
((
DbConnection
)
cnn
).
OpenAsync
(
command
.
CancellationToken
).
ConfigureAwait
(
false
);
using
(
var
cmd
=
(
DbCommand
)
command
.
SetupCommand
(
cnn
,
info
.
ParamReader
))
using
(
var
reader
=
await
cmd
.
ExecuteReaderAsync
(
wasClosed
?
CommandBehavior
.
CloseConnection
|
CommandBehavior
.
SequentialAccess
:
CommandBehavior
.
SequentialAccess
,
command
.
CancellationToken
).
ConfigureAwait
(
false
))
using
(
var
reader
=
await
cmd
.
ExecuteReaderAsync
(
GetBehavior
(
wasClosed
,
CommandBehavior
.
SequentialAccess
|
CommandBehavior
.
SingleResult
)
,
command
.
CancellationToken
).
ConfigureAwait
(
false
))
{
if
(!
command
.
Buffered
)
wasClosed
=
false
;
// handing back open reader; rely on command-behavior
var
results
=
MultiMapImpl
<
TFirst
,
TSecond
,
TThird
,
TFourth
,
TFifth
,
TSixth
,
TSeventh
,
TReturn
>(
null
,
CommandDefinition
.
ForCallback
(
command
.
Parameters
),
map
,
splitOn
,
reader
,
identity
,
true
);
...
...
@@ -594,7 +594,7 @@ private static async Task<IEnumerable<TReturn>> MultiMapAsync<TReturn>(this IDbC
try
{
if
(
wasClosed
)
await
((
DbConnection
)
cnn
).
OpenAsync
().
ConfigureAwait
(
false
);
using
(
var
cmd
=
(
DbCommand
)
command
.
SetupCommand
(
cnn
,
info
.
ParamReader
))
using
(
var
reader
=
await
cmd
.
ExecuteReaderAsync
(
command
.
CancellationToken
).
ConfigureAwait
(
false
))
{
using
(
var
reader
=
await
cmd
.
ExecuteReaderAsync
(
GetBehavior
(
wasClosed
,
CommandBehavior
.
SequentialAccess
|
CommandBehavior
.
SingleResult
),
command
.
CancellationToken
).
ConfigureAwait
(
false
))
{
var
results
=
MultiMapImpl
<
TReturn
>(
null
,
default
(
CommandDefinition
),
types
,
map
,
splitOn
,
reader
,
identity
,
true
);
return
command
.
Buffered
?
results
.
ToList
()
:
results
;
}
...
...
@@ -644,7 +644,7 @@ public static async Task<GridReader> QueryMultipleAsync(this IDbConnection cnn,
{
if
(
wasClosed
)
await
((
DbConnection
)
cnn
).
OpenAsync
(
command
.
CancellationToken
).
ConfigureAwait
(
false
);
cmd
=
(
DbCommand
)
command
.
SetupCommand
(
cnn
,
info
.
ParamReader
);
reader
=
await
cmd
.
ExecuteReaderAsync
(
wasClosed
?
CommandBehavior
.
CloseConnection
|
CommandBehavior
.
SequentialAccess
:
CommandBehavior
.
SequentialAccess
,
command
.
CancellationToken
).
ConfigureAwait
(
false
);
reader
=
await
cmd
.
ExecuteReaderAsync
(
GetBehavior
(
wasClosed
,
CommandBehavior
.
SequentialAccess
)
,
command
.
CancellationToken
).
ConfigureAwait
(
false
);
var
result
=
new
GridReader
(
cmd
,
reader
,
identity
,
command
.
Parameters
as
DynamicParameters
,
command
.
AddToCache
,
command
.
CancellationToken
);
wasClosed
=
false
;
// *if* the connection was closed and we got this far, then we now have a reader
...
...
@@ -721,7 +721,7 @@ private static async Task<IDataReader> ExecuteReaderImplAsync(IDbConnection cnn,
{
cmd
=
(
DbCommand
)
command
.
SetupCommand
(
cnn
,
paramReader
);
if
(
wasClosed
)
await
((
DbConnection
)
cnn
).
OpenAsync
(
command
.
CancellationToken
).
ConfigureAwait
(
false
);
var
reader
=
await
cmd
.
ExecuteReaderAsync
(
wasClosed
?
CommandBehavior
.
CloseConnection
|
CommandBehavior
.
SequentialAccess
:
CommandBehavior
.
SequentialAccess
,
command
.
CancellationToken
).
ConfigureAwait
(
false
);
var
reader
=
await
cmd
.
ExecuteReaderAsync
(
GetBehavior
(
wasClosed
,
CommandBehavior
.
SequentialAccess
)
,
command
.
CancellationToken
).
ConfigureAwait
(
false
);
wasClosed
=
false
;
return
reader
;
}
...
...
Dapper/SqlMapper.cs
View file @
3b215e37
...
...
@@ -697,6 +697,7 @@ public static GridReader QueryMultiple(this IDbConnection cnn, CommandDefinition
{
return
QueryMultipleImpl
(
cnn
,
ref
command
);
}
private
static
GridReader
QueryMultipleImpl
(
this
IDbConnection
cnn
,
ref
CommandDefinition
command
)
{
object
param
=
command
.
Parameters
;
...
...
@@ -710,7 +711,7 @@ private static GridReader QueryMultipleImpl(this IDbConnection cnn, ref CommandD
{
if
(
wasClosed
)
cnn
.
Open
();
cmd
=
command
.
SetupCommand
(
cnn
,
info
.
ParamReader
);
reader
=
cmd
.
ExecuteReader
(
wasClosed
?
CommandBehavior
.
CloseConnection
|
CommandBehavior
.
SequentialAccess
:
CommandBehavior
.
SequentialAccess
);
reader
=
cmd
.
ExecuteReader
(
GetBehavior
(
wasClosed
,
CommandBehavior
.
SequentialAccess
)
);
var
result
=
new
GridReader
(
cmd
,
reader
,
identity
,
command
.
Parameters
as
DynamicParameters
,
command
.
AddToCache
);
cmd
=
null
;
// now owned by result
...
...
@@ -749,7 +750,7 @@ private static IEnumerable<T> QueryImpl<T>(this IDbConnection cnn, CommandDefini
cmd
=
command
.
SetupCommand
(
cnn
,
info
.
ParamReader
);
if
(
wasClosed
)
cnn
.
Open
();
reader
=
cmd
.
ExecuteReader
(
wasClosed
?
CommandBehavior
.
CloseConnection
|
CommandBehavior
.
SequentialAccess
:
CommandBehavior
.
SequentialAccess
);
reader
=
cmd
.
ExecuteReader
(
GetBehavior
(
wasClosed
,
CommandBehavior
.
SequentialAccess
|
CommandBehavior
.
SingleResult
)
);
wasClosed
=
false
;
// *if* the connection was closed and we got this far, then we now have a reader
// with the CloseConnection flag, so the reader will deal with the connection; we
// still need something in the "finally" to ensure that broken SQL still results
...
...
@@ -811,7 +812,7 @@ private static T QueryFirstOrDefaultImpl<T>(this IDbConnection cnn, ref CommandD
cmd
=
command
.
SetupCommand
(
cnn
,
info
.
ParamReader
);
if
(
wasClosed
)
cnn
.
Open
();
reader
=
cmd
.
ExecuteReader
(
wasClosed
?
CommandBehavior
.
CloseConnection
|
CommandBehavior
.
SequentialAccess
:
CommandBehavior
.
SequentialAccess
);
reader
=
cmd
.
ExecuteReader
(
GetBehavior
(
wasClosed
,
CommandBehavior
.
SequentialAccess
|
CommandBehavior
.
SingleResult
|
CommandBehavior
.
SingleRow
)
);
wasClosed
=
false
;
// *if* the connection was closed and we got this far, then we now have a reader
T
result
=
default
(
T
);
...
...
@@ -1061,7 +1062,7 @@ public static IEnumerable<TReturn> Query<TReturn>(this IDbConnection cnn, string
{
ownedCommand
=
command
.
SetupCommand
(
cnn
,
cinfo
.
ParamReader
);
if
(
wasClosed
)
cnn
.
Open
();
ownedReader
=
ownedCommand
.
ExecuteReader
(
wasClosed
?
CommandBehavior
.
CloseConnection
|
CommandBehavior
.
SequentialAccess
:
CommandBehavior
.
SequentialAccess
);
ownedReader
=
ownedCommand
.
ExecuteReader
(
GetBehavior
(
wasClosed
,
CommandBehavior
.
SequentialAccess
|
CommandBehavior
.
SingleResult
)
);
reader
=
ownedReader
;
}
DeserializerState
deserializer
=
default
(
DeserializerState
);
...
...
@@ -1104,7 +1105,10 @@ public static IEnumerable<TReturn> Query<TReturn>(this IDbConnection cnn, string
}
}
}
private
static
CommandBehavior
GetBehavior
(
bool
close
,
CommandBehavior
@default
)
{
return
close
?
(
@default
|
CommandBehavior
.
CloseConnection
)
:
@default
;
}
static
IEnumerable
<
TReturn
>
MultiMapImpl
<
TReturn
>(
this
IDbConnection
cnn
,
CommandDefinition
command
,
Type
[]
types
,
Func
<
object
[],
TReturn
>
map
,
string
splitOn
,
IDataReader
reader
,
Identity
identity
,
bool
finalize
)
{
if
(
types
.
Length
<
1
)
...
...
@@ -1126,7 +1130,7 @@ static IEnumerable<TReturn> MultiMapImpl<TReturn>(this IDbConnection cnn, Comman
{
ownedCommand
=
command
.
SetupCommand
(
cnn
,
cinfo
.
ParamReader
);
if
(
wasClosed
)
cnn
.
Open
();
ownedReader
=
ownedCommand
.
ExecuteReader
();
ownedReader
=
ownedCommand
.
ExecuteReader
(
GetBehavior
(
wasClosed
,
CommandBehavior
.
SequentialAccess
|
CommandBehavior
.
SingleResult
)
);
reader
=
ownedReader
;
}
DeserializerState
deserializer
=
default
(
DeserializerState
);
...
...
@@ -2272,8 +2276,7 @@ private static IDataReader ExecuteReaderImpl(IDbConnection cnn, ref CommandDefin
{
cmd
=
command
.
SetupCommand
(
cnn
,
paramReader
);
if
(
wasClosed
)
cnn
.
Open
();
if
(
wasClosed
)
commandBehavior
|=
CommandBehavior
.
CloseConnection
;
var
reader
=
cmd
.
ExecuteReader
(
commandBehavior
);
var
reader
=
cmd
.
ExecuteReader
(
GetBehavior
(
wasClosed
,
commandBehavior
));
wasClosed
=
false
;
// don't dispose before giving it to them!
disposeCommand
=
false
;
// note: command.FireOutputCallbacks(); would be useless here; parameters come at the **end** of the TDS stream
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment