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
6ae42c29
Commit
6ae42c29
authored
Oct 09, 2012
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add: feature 106; dapper takes control of the connection if needeed
parent
293a8edc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
137 additions
and
26 deletions
+137
-26
SqlMapper.cs
Dapper/SqlMapper.cs
+56
-26
Tests.cs
Tests/Tests.cs
+81
-0
No files found.
Dapper/SqlMapper.cs
View file @
6ae42c29
...
@@ -786,16 +786,28 @@ public static IEnumerable<dynamic> Query(this IDbConnection cnn, string sql, dyn
...
@@ -786,16 +786,28 @@ public static IEnumerable<dynamic> Query(this IDbConnection cnn, string sql, dyn
IDbCommand
cmd
=
null
;
IDbCommand
cmd
=
null
;
IDataReader
reader
=
null
;
IDataReader
reader
=
null
;
bool
wasClosed
=
cnn
.
State
==
ConnectionState
.
Closed
;
try
try
{
{
if
(
wasClosed
)
cnn
.
Open
();
cmd
=
SetupCommand
(
cnn
,
transaction
,
sql
,
info
.
ParamReader
,
(
object
)
param
,
commandTimeout
,
commandType
);
cmd
=
SetupCommand
(
cnn
,
transaction
,
sql
,
info
.
ParamReader
,
(
object
)
param
,
commandTimeout
,
commandType
);
reader
=
cmd
.
ExecuteReader
();
reader
=
cmd
.
ExecuteReader
(
wasClosed
?
CommandBehavior
.
CloseConnection
:
CommandBehavior
.
Default
);
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
// in the connection closing itself
return
new
GridReader
(
cmd
,
reader
,
identity
);
return
new
GridReader
(
cmd
,
reader
,
identity
);
}
}
catch
catch
{
{
if
(
reader
!=
null
)
reader
.
Dispose
();
if
(
reader
!=
null
)
{
if
(!
reader
.
IsClosed
)
try
{
cmd
.
Cancel
();
}
catch
{
/* don't spol the existing exception */
}
reader
.
Dispose
();
}
if
(
cmd
!=
null
)
cmd
.
Dispose
();
if
(
cmd
!=
null
)
cmd
.
Dispose
();
if
(
wasClosed
)
cnn
.
Close
();
throw
;
throw
;
}
}
}
}
...
@@ -808,36 +820,45 @@ private static IEnumerable<T> QueryInternal<T>(this IDbConnection cnn, string sq
...
@@ -808,36 +820,45 @@ private static IEnumerable<T> QueryInternal<T>(this IDbConnection cnn, string sq
var
identity
=
new
Identity
(
sql
,
commandType
,
cnn
,
typeof
(
T
),
param
==
null
?
null
:
param
.
GetType
(),
null
);
var
identity
=
new
Identity
(
sql
,
commandType
,
cnn
,
typeof
(
T
),
param
==
null
?
null
:
param
.
GetType
(),
null
);
var
info
=
GetCacheInfo
(
identity
);
var
info
=
GetCacheInfo
(
identity
);
using
(
var
cmd
=
SetupCommand
(
cnn
,
transaction
,
sql
,
info
.
ParamReader
,
param
,
commandTimeout
,
commandType
))
IDbCommand
cmd
=
null
;
IDataReader
reader
=
null
;
bool
wasClosed
=
cnn
.
State
==
ConnectionState
.
Closed
;
try
{
{
IDataReader
reader
=
null
;
cmd
=
SetupCommand
(
cnn
,
transaction
,
sql
,
info
.
ParamReader
,
param
,
commandTimeout
,
commandType
);
try
if
(
wasClosed
)
cnn
.
Open
();
reader
=
cmd
.
ExecuteReader
(
wasClosed
?
CommandBehavior
.
CloseConnection
:
CommandBehavior
.
Default
);
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
// in the connection closing itself
var
tuple
=
info
.
Deserializer
;
int
hash
=
GetColumnHash
(
reader
);
if
(
tuple
.
Func
==
null
||
tuple
.
Hash
!=
hash
)
{
{
reader
=
cmd
.
ExecuteReader
();
tuple
=
info
.
Deserializer
=
new
DeserializerState
(
hash
,
GetDeserializer
(
typeof
(
T
),
reader
,
0
,
-
1
,
false
));
var
tuple
=
info
.
Deserializer
;
SetQueryCache
(
identity
,
info
);
int
hash
=
GetColumnHash
(
reader
);
}
if
(
tuple
.
Func
==
null
||
tuple
.
Hash
!=
hash
)
{
tuple
=
info
.
Deserializer
=
new
DeserializerState
(
hash
,
GetDeserializer
(
typeof
(
T
),
reader
,
0
,
-
1
,
false
));
SetQueryCache
(
identity
,
info
);
}
var
func
=
tuple
.
Func
;
var
func
=
tuple
.
Func
;
while
(
reader
.
Read
())
while
(
reader
.
Read
())
{
{
yield
return
(
T
)
func
(
reader
);
yield
return
(
T
)
func
(
reader
);
}
}
}
finally
}
finally
{
if
(
reader
!=
null
)
{
{
if
(
reader
!=
null
)
if
(!
reader
.
IsClosed
)
try
{
cmd
.
Cancel
();
}
{
catch
{
/* don't spol the existing exception */
}
if
(!
reader
.
IsClosed
)
try
{
cmd
.
Cancel
();
}
reader
.
Dispose
();
catch
{
/* don't spol the existing exception */
}
reader
.
Dispose
();
}
}
}
if
(
wasClosed
)
cnn
.
Close
();
if
(
cmd
!=
null
)
cmd
.
Dispose
();
}
}
}
}
...
@@ -1671,10 +1692,19 @@ private static IDbCommand SetupCommand(IDbConnection cnn, IDbTransaction transac
...
@@ -1671,10 +1692,19 @@ private static IDbCommand SetupCommand(IDbConnection cnn, IDbTransaction transac
private
static
int
ExecuteCommand
(
IDbConnection
cnn
,
IDbTransaction
transaction
,
string
sql
,
Action
<
IDbCommand
,
object
>
paramReader
,
object
obj
,
int
?
commandTimeout
,
CommandType
?
commandType
)
private
static
int
ExecuteCommand
(
IDbConnection
cnn
,
IDbTransaction
transaction
,
string
sql
,
Action
<
IDbCommand
,
object
>
paramReader
,
object
obj
,
int
?
commandTimeout
,
CommandType
?
commandType
)
{
{
using
(
var
cmd
=
SetupCommand
(
cnn
,
transaction
,
sql
,
paramReader
,
obj
,
commandTimeout
,
commandType
))
IDbCommand
cmd
=
null
;
bool
wasClosed
=
cnn
.
State
==
ConnectionState
.
Closed
;
try
{
{
cmd
=
SetupCommand
(
cnn
,
transaction
,
sql
,
paramReader
,
obj
,
commandTimeout
,
commandType
);
if
(
wasClosed
)
cnn
.
Open
();
return
cmd
.
ExecuteNonQuery
();
return
cmd
.
ExecuteNonQuery
();
}
}
finally
{
if
(
wasClosed
)
cnn
.
Close
();
if
(
cmd
!=
null
)
cmd
.
Dispose
();
}
}
}
private
static
Func
<
IDataReader
,
object
>
GetStructDeserializer
(
Type
type
,
Type
effectiveType
,
int
index
)
private
static
Func
<
IDataReader
,
object
>
GetStructDeserializer
(
Type
type
,
Type
effectiveType
,
int
index
)
...
...
Tests/Tests.cs
View file @
6ae42c29
...
@@ -2015,6 +2015,87 @@ public Issue40_User()
...
@@ -2015,6 +2015,87 @@ public Issue40_User()
public
bool
Active
{
get
;
set
;
}
public
bool
Active
{
get
;
set
;
}
}
}
SqlConnection
GetClosedConnection
()
{
var
conn
=
new
SqlConnection
(
connection
.
ConnectionString
);
if
(
conn
.
State
!=
ConnectionState
.
Closed
)
throw
new
InvalidOperationException
(
"should be closed!"
);
return
conn
;
}
public
void
ExecuteFromClosed
()
{
using
(
var
conn
=
GetClosedConnection
())
{
conn
.
Execute
(
"-- nop"
);
conn
.
State
.
IsEqualTo
(
ConnectionState
.
Closed
);
}
}
public
void
ExecuteInvalidFromClosed
()
{
using
(
var
conn
=
GetClosedConnection
())
{
try
{
conn
.
Execute
(
"nop"
);
false
.
IsEqualTo
(
true
);
// shouldn't have got here
}
catch
{
conn
.
State
.
IsEqualTo
(
ConnectionState
.
Closed
);
}
}
}
public
void
QueryFromClosed
()
{
using
(
var
conn
=
GetClosedConnection
())
{
var
i
=
conn
.
Query
<
int
>(
"select 1"
).
Single
();
conn
.
State
.
IsEqualTo
(
ConnectionState
.
Closed
);
i
.
IsEqualTo
(
1
);
}
}
public
void
QueryInvalidFromClosed
()
{
using
(
var
conn
=
GetClosedConnection
())
{
try
{
conn
.
Query
<
int
>(
"select gibberish"
).
Single
();
false
.
IsEqualTo
(
true
);
// shouldn't have got here
}
catch
{
conn
.
State
.
IsEqualTo
(
ConnectionState
.
Closed
);
}
}
}
public
void
QueryMultipleFromClosed
()
{
using
(
var
conn
=
GetClosedConnection
())
{
using
(
var
multi
=
conn
.
QueryMultiple
(
"select 1; select 'abc';"
))
{
multi
.
Read
<
int
>().
Single
().
IsEqualTo
(
1
);
multi
.
Read
<
string
>().
Single
().
IsEqualTo
(
"abc"
);
}
conn
.
State
.
IsEqualTo
(
ConnectionState
.
Closed
);
}
}
public
void
QueryMultipleInvalidFromClosed
()
{
using
(
var
conn
=
GetClosedConnection
())
{
try
{
conn
.
QueryMultiple
(
"select gibberish"
);
false
.
IsEqualTo
(
true
);
// shouldn't have got here
}
catch
{
conn
.
State
.
IsEqualTo
(
ConnectionState
.
Closed
);
}
}
}
class
TransactedConnection
:
IDbConnection
class
TransactedConnection
:
IDbConnection
{
{
IDbConnection
_conn
;
IDbConnection
_conn
;
...
...
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