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
7cdecbc7
Commit
7cdecbc7
authored
May 08, 2014
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
async literal replacement and multi-exec tests
parent
9afb2fca
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
126 additions
and
21 deletions
+126
-21
SqlMapper.cs
Dapper NET40/SqlMapper.cs
+23
-16
SqlMapperAsync.cs
Dapper NET45/SqlMapperAsync.cs
+51
-1
Tests.cs
DapperTests NET45/Tests.cs
+45
-2
Tests.cs
Tests/Tests.cs
+7
-2
No files found.
Dapper NET40/SqlMapper.cs
View file @
7cdecbc7
...
...
@@ -855,9 +855,12 @@ private static int ExecuteImpl(this IDbConnection cnn, ref CommandDefinition com
{
bool
isFirst
=
true
;
int
total
=
0
;
bool
wasClosed
=
cnn
.
State
==
ConnectionState
.
Closed
;
try
{
if
(
wasClosed
)
cnn
.
Open
();
using
(
var
cmd
=
command
.
SetupCommand
(
cnn
,
null
))
{
string
masterSql
=
null
;
foreach
(
var
obj
in
multiExec
)
{
...
...
@@ -877,6 +880,10 @@ private static int ExecuteImpl(this IDbConnection cnn, ref CommandDefinition com
total
+=
cmd
.
ExecuteNonQuery
();
}
}
}
finally
{
if
(
wasClosed
)
cnn
.
Close
();
}
return
total
;
}
...
...
Dapper NET45/SqlMapperAsync.cs
View file @
7cdecbc7
using
System
;
using
System.Collections
;
using
System.Collections.Generic
;
using
System.Data
;
using
System.Data.Common
;
...
...
@@ -57,9 +58,58 @@ public static Task<int> ExecuteAsync(this IDbConnection cnn, string sql, dynamic
/// <summary>
/// Execute a command asynchronously using .NET 4.5 Task.
/// </summary>
public
static
async
Task
<
int
>
ExecuteAsync
(
this
IDbConnection
cnn
,
CommandDefinition
command
)
public
static
Task
<
int
>
ExecuteAsync
(
this
IDbConnection
cnn
,
CommandDefinition
command
)
{
object
param
=
command
.
Parameters
;
IEnumerable
multiExec
=
param
as
IEnumerable
;
if
(
multiExec
!=
null
&&
!(
param
is
string
))
{
return
ExecuteMultiImplAsync
(
cnn
,
command
,
multiExec
);
}
else
{
return
ExecuteImplAsync
(
cnn
,
command
,
param
);
}
}
private
static
async
Task
<
int
>
ExecuteMultiImplAsync
(
IDbConnection
cnn
,
CommandDefinition
command
,
IEnumerable
multiExec
)
{
bool
isFirst
=
true
;
int
total
=
0
;
bool
wasClosed
=
cnn
.
State
==
ConnectionState
.
Closed
;
try
{
if
(
wasClosed
)
await
((
DbConnection
)
cnn
).
OpenAsync
();
using
(
var
cmd
=
(
DbCommand
)
command
.
SetupCommand
(
cnn
,
null
))
{
string
masterSql
=
null
;
CacheInfo
info
=
null
;
foreach
(
var
obj
in
multiExec
)
{
if
(
isFirst
)
{
masterSql
=
cmd
.
CommandText
;
isFirst
=
false
;
var
identity
=
new
Identity
(
command
.
CommandText
,
cmd
.
CommandType
,
cnn
,
null
,
obj
.
GetType
(),
null
);
info
=
GetCacheInfo
(
identity
,
obj
);
}
else
{
cmd
.
CommandText
=
masterSql
;
// because we do magic replaces on "in" etc
cmd
.
Parameters
.
Clear
();
// current code is Add-tastic
}
info
.
ParamReader
(
cmd
,
obj
);
total
+=
await
cmd
.
ExecuteNonQueryAsync
();
}
}
}
finally
{
if
(
wasClosed
)
cnn
.
Close
();
}
return
total
;
}
private
static
async
Task
<
int
>
ExecuteImplAsync
(
IDbConnection
cnn
,
CommandDefinition
command
,
object
param
)
{
var
identity
=
new
Identity
(
command
.
CommandText
,
command
.
CommandType
,
cnn
,
null
,
param
==
null
?
null
:
param
.
GetType
(),
null
);
var
info
=
GetCacheInfo
(
identity
,
param
);
bool
wasClosed
=
cnn
.
State
==
ConnectionState
.
Closed
;
...
...
DapperTests NET45/Tests.cs
View file @
7cdecbc7
...
...
@@ -98,9 +98,9 @@ public void TestMultiMapWithSplitClosedConnAsync()
public
void
TestMultiAsync
()
{
using
(
var
conn
=
Program
.
GetOpenConnection
())
using
(
var
conn
=
Program
.
GetOpenConnection
())
{
using
(
Dapper
.
SqlMapper
.
GridReader
multi
=
conn
.
QueryMultipleAsync
(
"select 1; select 2"
).
Result
)
using
(
Dapper
.
SqlMapper
.
GridReader
multi
=
conn
.
QueryMultipleAsync
(
"select 1; select 2"
).
Result
)
{
multi
.
Read
<
int
>().
Single
().
IsEqualTo
(
1
);
multi
.
Read
<
int
>().
Single
().
IsEqualTo
(
2
);
...
...
@@ -148,6 +148,49 @@ public void ExecuteReaderClosedAsync()
}
}
public
void
LiteralReplacementOpen
()
{
using
(
var
conn
=
Program
.
GetOpenConnection
())
LiteralReplacement
(
conn
);
}
public
void
LiteralReplacementClosed
()
{
using
(
var
conn
=
Program
.
GetClosedConnection
())
LiteralReplacement
(
conn
);
}
private
void
LiteralReplacement
(
IDbConnection
connection
)
{
try
{
connection
.
ExecuteAsync
(
"drop table literal1"
).
Wait
();
}
catch
{
}
connection
.
ExecuteAsync
(
"create table literal1 (id int not null, foo int not null)"
).
Wait
();
connection
.
ExecuteAsync
(
"insert literal1 (id,foo) values ({=id}, @foo)"
,
new
{
id
=
123
,
foo
=
456
}).
Wait
();
var
rows
=
new
[]
{
new
{
id
=
1
,
foo
=
2
},
new
{
id
=
3
,
foo
=
4
}
};
connection
.
ExecuteAsync
(
"insert literal1 (id,foo) values ({=id}, @foo)"
,
rows
).
Wait
();
var
count
=
connection
.
QueryAsync
<
int
>(
"select count(1) from literal1 where id={=foo}"
,
new
{
foo
=
123
}).
Result
.
Single
();
count
.
IsEqualTo
(
1
);
int
sum
=
connection
.
QueryAsync
<
int
>(
"select sum(id) + sum(foo) from literal1"
).
Result
.
Single
();
sum
.
IsEqualTo
(
123
+
456
+
1
+
2
+
3
+
4
);
}
public
void
LiteralReplacementDynamicOpen
()
{
using
(
var
conn
=
Program
.
GetOpenConnection
())
LiteralReplacementDynamic
(
conn
);
}
public
void
LiteralReplacementDynamicClosed
()
{
using
(
var
conn
=
Program
.
GetClosedConnection
())
LiteralReplacementDynamic
(
conn
);
}
private
void
LiteralReplacementDynamic
(
IDbConnection
connection
)
{
var
args
=
new
DynamicParameters
();
args
.
Add
(
"id"
,
123
);
try
{
connection
.
ExecuteAsync
(
"drop table literal2"
).
Wait
();
}
catch
{
}
connection
.
ExecuteAsync
(
"create table literal2 (id int not null)"
).
Wait
();
connection
.
ExecuteAsync
(
"insert literal2 (id) values ({=id})"
,
args
).
Wait
();
args
=
new
DynamicParameters
();
args
.
Add
(
"foo"
,
123
);
var
count
=
connection
.
QueryAsync
<
int
>(
"select count(1) from literal2 where id={=foo}"
,
args
).
Result
.
Single
();
count
.
IsEqualTo
(
1
);
}
class
Product
{
public
int
Id
{
get
;
set
;
}
...
...
Tests/Tests.cs
View file @
7cdecbc7
...
...
@@ -2670,10 +2670,14 @@ public void TestParameterInclusionNotSensitiveToCurrentCulture()
}
public
void
LiteralReplacement
()
{
connection
.
Execute
(
"create table #literal1 (id int not null)"
);
connection
.
Execute
(
"insert #literal1 (id) values ({=id})"
,
new
{
id
=
123
});
connection
.
Execute
(
"create table #literal1 (id int not null, foo int not null)"
);
connection
.
Execute
(
"insert #literal1 (id,foo) values ({=id}, @foo)"
,
new
{
id
=
123
,
foo
=
456
});
var
rows
=
new
[]
{
new
{
id
=
1
,
foo
=
2
},
new
{
id
=
3
,
foo
=
4
}
};
connection
.
Execute
(
"insert #literal1 (id,foo) values ({=id}, @foo)"
,
rows
);
var
count
=
connection
.
Query
<
int
>(
"select count(1) from #literal1 where id={=foo}"
,
new
{
foo
=
123
}).
Single
();
count
.
IsEqualTo
(
1
);
int
sum
=
connection
.
Query
<
int
>(
"select sum(id) + sum(foo) from #literal1"
).
Single
();
sum
.
IsEqualTo
(
123
+
456
+
1
+
2
+
3
+
4
);
}
public
void
LiteralReplacementDynamic
()
{
...
...
@@ -2688,6 +2692,7 @@ public void LiteralReplacementDynamic()
count
.
IsEqualTo
(
1
);
}
public
void
TestProcedureWithTimeParameter
()
{
var
p
=
new
DynamicParameters
();
...
...
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