Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Dapper
提交
c233ea7b
D
Dapper
项目概览
int
/
Dapper
11 个月 前同步成功
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Dapper
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c233ea7b
编写于
4月 26, 2011
作者:
M
mattias@amigarulez.se
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added optional commandTimeout paraneter to query methods
上级
af4cd5e7
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
54 addition
and
59 deletion
+54
-59
Dapper/SqlMapper.cs
Dapper/SqlMapper.cs
+54
-59
未找到文件。
Dapper/SqlMapper.cs
浏览文件 @
c233ea7b
/*
/*
License: http://www.apache.org/licenses/LICENSE-2.0
Home page: http://code.google.com/p/dapper-dot-net/
*/
...
...
@@ -25,8 +25,8 @@ class CacheInfo
public
Action
<
IDbCommand
,
object
>
ParamReader
{
get
;
set
;
}
}
static
ConcurrentDictionary
<
Identity
,
CacheInfo
>
queryCache
=
new
ConcurrentDictionary
<
Identity
,
CacheInfo
>();
static
Dictionary
<
Type
,
DbType
>
typeMap
;
static
readonly
ConcurrentDictionary
<
Identity
,
CacheInfo
>
queryCache
=
new
ConcurrentDictionary
<
Identity
,
CacheInfo
>();
static
readonly
Dictionary
<
Type
,
DbType
>
typeMap
;
static
SqlMapper
()
{
...
...
@@ -74,16 +74,13 @@ private static DbType LookupDbType(Type type)
{
return
dbType
;
}
else
if
(
typeof
(
IEnumerable
).
IsAssignableFrom
(
type
))
{
if
(
typeof
(
IEnumerable
).
IsAssignableFrom
(
type
))
{
// use xml to denote its a list, hacky but will work on any DB
return
DbType
.
Xml
;
}
// use xml to denote its a list, hacky but will work on any DB
return
DbType
.
Xml
;
}
throw
new
NotSupportedException
(
"The type : "
+
type
.
ToString
()
+
" is not supported by dapper"
);
throw
new
NotSupportedException
(
string
.
Format
(
"The type : {0} is not supported by dapper"
,
type
)
);
}
private
class
Identity
:
IEquatable
<
Identity
>
...
...
@@ -108,9 +105,9 @@ internal Identity(string sql, IDbConnection cnn, Type type, Type parametersType,
hashCode
=
hashCode
*
23
+
(
type
==
null
?
0
:
type
.
GetHashCode
());
if
(
otherTypes
!=
null
)
{
for
(
int
i
=
0
;
i
<
otherTypes
.
Length
;
i
++
)
for
each
(
var
t
in
otherTypes
)
{
hashCode
=
hashCode
*
23
+
(
otherTypes
[
i
]
==
null
?
0
:
otherTypes
[
i
]
.
GetHashCode
());
hashCode
=
hashCode
*
23
+
(
t
==
null
?
0
:
t
.
GetHashCode
());
}
}
hashCode
=
hashCode
*
23
+
(
connectionString
==
null
?
0
:
connectionString
.
GetHashCode
());
...
...
@@ -135,7 +132,7 @@ public bool Equals(Identity other)
{
return
other
!=
null
&&
t
his
.
t
ype
==
other
.
type
&&
type
==
other
.
type
&&
sql
==
other
.
sql
&&
connectionString
==
other
.
connectionString
&&
parametersType
==
other
.
parametersType
;
...
...
@@ -146,44 +143,37 @@ public bool Equals(Identity other)
/// Execute parameterized SQL
/// </summary>
/// <returns>Number of rows affected</returns>
public
static
int
Execute
(
this
IDbConnection
cnn
,
string
sql
,
object
param
=
null
,
IDbTransaction
transaction
=
null
)
public
static
int
Execute
(
this
IDbConnection
cnn
,
string
sql
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
int
?
commandTimeout
=
null
)
{
var
identity
=
new
Identity
(
sql
,
cnn
,
null
,
param
==
null
?
null
:
param
.
GetType
());
var
info
=
GetCacheInfo
(
param
,
identity
);
return
ExecuteCommand
(
cnn
,
transaction
,
sql
,
info
.
ParamReader
,
param
);
return
ExecuteCommand
(
cnn
,
transaction
,
sql
,
info
.
ParamReader
,
param
,
commandTimeout
);
}
/// <summary>
/// Return a list of dynamic objects, reader is closed after the call
/// </summary>
public
static
IEnumerable
<
dynamic
>
Query
(
this
IDbConnection
cnn
,
string
sql
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
bool
buffered
=
true
)
public
static
IEnumerable
<
dynamic
>
Query
(
this
IDbConnection
cnn
,
string
sql
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
bool
buffered
=
true
,
int
?
commandTimeout
=
null
)
{
return
Query
<
ExpandoObject
>(
cnn
,
sql
,
param
,
transaction
,
buffered
);
return
Query
<
ExpandoObject
>(
cnn
,
sql
,
param
,
transaction
,
buffered
,
commandTimeout
);
}
public
static
IEnumerable
<
T
>
Query
<
T
>(
this
IDbConnection
cnn
,
string
sql
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
bool
buffered
=
true
)
public
static
IEnumerable
<
T
>
Query
<
T
>(
this
IDbConnection
cnn
,
string
sql
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
bool
buffered
=
true
,
int
?
commandTimeout
=
null
)
{
var
data
=
QueryInternal
<
T
>(
cnn
,
sql
,
param
,
transaction
);
if
(
buffered
)
{
return
data
.
ToList
();
}
else
{
return
data
;
}
var
data
=
QueryInternal
<
T
>(
cnn
,
sql
,
param
,
transaction
,
commandTimeout
);
return
(
buffered
)
?
data
.
ToList
()
:
data
;
}
/// <summary>
/// Return a typed list of objects, reader is closed after the call
/// </summary>
private
static
IEnumerable
<
T
>
QueryInternal
<
T
>(
this
IDbConnection
cnn
,
string
sql
,
object
param
=
null
,
IDbTransaction
transaction
=
null
)
private
static
IEnumerable
<
T
>
QueryInternal
<
T
>(
this
IDbConnection
cnn
,
string
sql
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
int
?
commandTimeout
=
null
)
{
var
identity
=
new
Identity
(
sql
,
cnn
,
typeof
(
T
),
param
==
null
?
null
:
param
.
GetType
());
var
info
=
GetCacheInfo
(
param
,
identity
);
using
(
var
cmd
=
SetupCommand
(
cnn
,
transaction
,
sql
,
info
.
ParamReader
,
param
))
using
(
var
cmd
=
SetupCommand
(
cnn
,
transaction
,
sql
,
info
.
ParamReader
,
param
,
commandTimeout
))
{
using
(
var
reader
=
cmd
.
ExecuteReader
())
{
...
...
@@ -213,35 +203,37 @@ private static IEnumerable<T> QueryInternal<T>(this IDbConnection cnn, string sq
/// <param name="map"></param>
/// <param name="param"></param>
/// <param name="transaction"></param>
/// <param name="buffered"></param>
/// <param name="splitOn">The Field we should split and read the second object from (default: id)</param>
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
/// <returns></returns>
public
static
IEnumerable
<
T
>
Query
<
T
,
U
>(
this
IDbConnection
cnn
,
string
sql
,
Action
<
T
,
U
>
map
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
bool
buffered
=
true
,
string
splitOn
=
"Id"
)
public
static
IEnumerable
<
T
>
Query
<
T
,
U
>(
this
IDbConnection
cnn
,
string
sql
,
Action
<
T
,
U
>
map
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
bool
buffered
=
true
,
string
splitOn
=
"Id"
,
int
?
commandTimeout
=
null
)
{
return
MultiMap
<
T
,
U
,
DontMap
,
DontMap
,
DontMap
>(
cnn
,
sql
,
map
,
param
,
transaction
,
buffered
,
splitOn
);
return
MultiMap
<
T
,
U
,
DontMap
,
DontMap
,
DontMap
>(
cnn
,
sql
,
map
,
param
,
transaction
,
buffered
,
splitOn
,
commandTimeout
);
}
public
static
IEnumerable
<
T
>
Query
<
T
,
U
,
V
>(
this
IDbConnection
cnn
,
string
sql
,
Action
<
T
,
U
,
V
>
map
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
bool
buffered
=
true
,
string
splitOn
=
"Id"
)
public
static
IEnumerable
<
T
>
Query
<
T
,
U
,
V
>(
this
IDbConnection
cnn
,
string
sql
,
Action
<
T
,
U
,
V
>
map
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
bool
buffered
=
true
,
string
splitOn
=
"Id"
,
int
?
commandTimeout
=
null
)
{
return
MultiMap
<
T
,
U
,
V
,
DontMap
,
DontMap
>(
cnn
,
sql
,
map
,
param
,
transaction
,
buffered
,
splitOn
);
return
MultiMap
<
T
,
U
,
V
,
DontMap
,
DontMap
>(
cnn
,
sql
,
map
,
param
,
transaction
,
buffered
,
splitOn
,
commandTimeout
);
}
public
static
IEnumerable
<
T
>
Query
<
T
,
U
,
V
,
Z
>(
this
IDbConnection
cnn
,
string
sql
,
Action
<
T
,
U
,
V
,
Z
>
map
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
bool
buffered
=
true
,
string
splitOn
=
"Id"
)
public
static
IEnumerable
<
T
>
Query
<
T
,
U
,
V
,
Z
>(
this
IDbConnection
cnn
,
string
sql
,
Action
<
T
,
U
,
V
,
Z
>
map
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
bool
buffered
=
true
,
string
splitOn
=
"Id"
,
int
?
commandTimeout
=
null
)
{
return
MultiMap
<
T
,
U
,
V
,
Z
,
DontMap
>(
cnn
,
sql
,
map
,
param
,
transaction
,
buffered
,
splitOn
);
return
MultiMap
<
T
,
U
,
V
,
Z
,
DontMap
>(
cnn
,
sql
,
map
,
param
,
transaction
,
buffered
,
splitOn
,
commandTimeout
);
}
public
static
IEnumerable
<
T
>
Query
<
T
,
U
,
V
,
Z
,
X
>(
this
IDbConnection
cnn
,
string
sql
,
Action
<
T
,
U
,
V
,
Z
,
X
>
map
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
bool
buffered
=
true
,
string
splitOn
=
"Id"
)
public
static
IEnumerable
<
T
>
Query
<
T
,
U
,
V
,
Z
,
X
>(
this
IDbConnection
cnn
,
string
sql
,
Action
<
T
,
U
,
V
,
Z
,
X
>
map
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
bool
buffered
=
true
,
string
splitOn
=
"Id"
,
int
?
commandTimeout
=
null
)
{
return
MultiMap
<
T
,
U
,
V
,
Z
,
X
>(
cnn
,
sql
,
map
,
param
,
transaction
,
buffered
,
splitOn
);
return
MultiMap
<
T
,
U
,
V
,
Z
,
X
>(
cnn
,
sql
,
map
,
param
,
transaction
,
buffered
,
splitOn
,
commandTimeout
);
}
class
DontMap
{}
static
IEnumerable
<
T
>
MultiMap
<
T
,
U
,
V
,
Z
,
X
>(
this
IDbConnection
cnn
,
string
sql
,
object
map
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
bool
buffered
=
true
,
string
splitOn
=
"Id"
)
static
IEnumerable
<
T
>
MultiMap
<
T
,
U
,
V
,
Z
,
X
>(
this
IDbConnection
cnn
,
string
sql
,
object
map
,
object
param
=
null
,
IDbTransaction
transaction
=
null
,
bool
buffered
=
true
,
string
splitOn
=
"Id"
,
int
?
commandTimeout
=
null
)
{
var
identity
=
new
Identity
(
sql
,
cnn
,
typeof
(
T
),
param
==
null
?
null
:
param
.
GetType
(),
otherTypes
:
new
Type
[]
{
typeof
(
T
),
typeof
(
U
),
typeof
(
V
),
typeof
(
Z
),
typeof
(
X
)
});
var
identity
=
new
Identity
(
sql
,
cnn
,
typeof
(
T
),
param
==
null
?
null
:
param
.
GetType
(),
otherTypes
:
new
[]
{
typeof
(
T
),
typeof
(
U
),
typeof
(
V
),
typeof
(
Z
),
typeof
(
X
)
});
var
info
=
GetCacheInfo
(
param
,
identity
);
using
(
var
cmd
=
SetupCommand
(
cnn
,
transaction
,
sql
,
info
.
ParamReader
,
param
))
using
(
var
cmd
=
SetupCommand
(
cnn
,
transaction
,
sql
,
info
.
ParamReader
,
param
,
commandTimeout
))
{
using
(
var
reader
=
cmd
.
ExecuteReader
())
{
...
...
@@ -264,7 +256,7 @@ class DontMap {}
return
pos
;
};
List
<
object
>
otherDeserializer
=
new
List
<
object
>();
var
otherDeserializer
=
new
List
<
object
>();
split
=
nextSplit
();
info
.
Deserializer
=
GetDeserializer
<
T
>(
identity
,
reader
,
0
,
split
);
...
...
@@ -353,10 +345,11 @@ class DontMap {}
}
}
while
(
reader
.
Read
())
{
yield
return
mapIt
(
reader
);
}
if
(
mapIt
!=
null
)
while
(
reader
.
Read
())
{
yield
return
mapIt
(
reader
);
}
}
}
}
...
...
@@ -393,20 +386,20 @@ private static CacheInfo GetCacheInfo(object param, Identity identity)
oDeserializer
=
GetStructDeserializer
<
T
>(
reader
);
}
Func
<
IDataReader
,
T
>
deserializer
=
(
Func
<
IDataReader
,
T
>)
oDeserializer
;
var
deserializer
=
(
Func
<
IDataReader
,
T
>)
oDeserializer
;
return
deserializer
;
}
private
static
object
GetDynamicDeserializer
(
IDataRe
ader
reader
,
int
startBound
=
0
,
int
length
=
-
1
,
bool
returnNullIfFirstMissing
=
false
)
private
static
object
GetDynamicDeserializer
(
IDataRe
cord
reader
,
int
startBound
=
0
,
int
length
=
-
1
,
bool
returnNullIfFirstMissing
=
false
)
{
List
<
string
>
colNames
=
new
List
<
string
>();
var
colNames
=
new
List
<
string
>();
if
(
length
==
-
1
)
{
length
=
reader
.
FieldCount
-
startBound
;
}
for
(
int
i
=
startBound
;
i
<
startBound
+
length
;
i
++)
for
(
var
i
=
startBound
;
i
<
startBound
+
length
;
i
++)
{
colNames
.
Add
(
reader
.
GetName
(
i
));
}
...
...
@@ -415,8 +408,8 @@ private static object GetDynamicDeserializer(IDataReader reader, int startBound
r
=>
{
IDictionary
<
string
,
object
>
row
=
new
ExpandoObject
();
int
i
=
startBound
;
bool
first
=
true
;
var
i
=
startBound
;
var
first
=
true
;
foreach
(
var
colName
in
colNames
)
{
var
tmp
=
r
.
GetValue
(
i
);
...
...
@@ -451,7 +444,7 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
{
count
++;
var
listParam
=
command
.
CreateParameter
();
listParam
.
ParameterName
=
namePrefix
+
count
.
ToString
(
);
listParam
.
ParameterName
=
string
.
Format
(
"{0}{1}"
,
namePrefix
,
count
);
listParam
.
Value
=
item
??
DBNull
.
Value
;
if
(
isString
)
{
...
...
@@ -480,7 +473,7 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
}
private
static
Action
<
IDbCommand
,
object
>
CreateParamInfoGenerator
(
Type
type
)
{
DynamicMethod
dm
=
new
DynamicMethod
(
"ParamInfo"
+
Guid
.
NewGuid
().
ToString
(),
null
,
new
Type
[]
{
typeof
(
IDbCommand
),
typeof
(
object
)
},
type
,
true
);
var
dm
=
new
DynamicMethod
(
string
.
Format
(
"ParamInfo{0}"
,
Guid
.
NewGuid
()),
null
,
new
[]
{
typeof
(
IDbCommand
),
typeof
(
object
)
},
type
,
true
);
var
il
=
dm
.
GetILGenerator
();
...
...
@@ -606,12 +599,14 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
return
(
Action
<
IDbCommand
,
object
>)
dm
.
CreateDelegate
(
typeof
(
Action
<
IDbCommand
,
object
>));
}
private
static
IDbCommand
SetupCommand
(
IDbConnection
cnn
,
IDbTransaction
transaction
,
string
sql
,
Action
<
IDbCommand
,
object
>
paramReader
,
object
obj
)
private
static
IDbCommand
SetupCommand
(
IDbConnection
cnn
,
IDbTransaction
transaction
,
string
sql
,
Action
<
IDbCommand
,
object
>
paramReader
,
object
obj
,
int
?
commandTimeout
)
{
var
cmd
=
cnn
.
CreateCommand
();
cmd
.
Transaction
=
transaction
;
cmd
.
CommandText
=
sql
;
if
(
commandTimeout
.
HasValue
)
cmd
.
CommandTimeout
=
commandTimeout
.
Value
;
if
(
paramReader
!=
null
)
{
paramReader
(
cmd
,
obj
);
...
...
@@ -620,9 +615,9 @@ private static IDbCommand SetupCommand(IDbConnection cnn, IDbTransaction transac
}
private
static
int
ExecuteCommand
(
IDbConnection
cnn
,
IDbTransaction
tranaction
,
string
sql
,
Action
<
IDbCommand
,
object
>
paramReader
,
object
obj
)
private
static
int
ExecuteCommand
(
IDbConnection
cnn
,
IDbTransaction
tranaction
,
string
sql
,
Action
<
IDbCommand
,
object
>
paramReader
,
object
obj
,
int
?
commandTimeout
)
{
using
(
var
cmd
=
SetupCommand
(
cnn
,
tranaction
,
sql
,
paramReader
,
obj
))
using
(
var
cmd
=
SetupCommand
(
cnn
,
tranaction
,
sql
,
paramReader
,
obj
,
commandTimeout
))
{
return
cmd
.
ExecuteNonQuery
();
}
...
...
@@ -646,15 +641,15 @@ private static object GetStructDeserializer<T>(IDataReader reader)
public
static
Func
<
IDataReader
,
T
>
GetClassDeserializer
<
T
>(
IDataReader
reader
,
int
startBound
=
0
,
int
length
=
-
1
,
bool
returnNullIfFirstMissing
=
false
)
{
DynamicMethod
dm
=
new
DynamicMethod
(
"Deserialize"
+
Guid
.
NewGuid
().
ToString
(),
typeof
(
T
),
new
Type
[]
{
typeof
(
IDataReader
)
},
true
);
var
dm
=
new
DynamicMethod
(
string
.
Format
(
"Deserialize{0}"
,
Guid
.
NewGuid
()),
typeof
(
T
),
new
[]
{
typeof
(
IDataReader
)
},
true
);
var
il
=
dm
.
GetILGenerator
();
var
properties
=
typeof
(
T
)
.
GetProperties
(
BindingFlags
.
Public
|
BindingFlags
.
NonPublic
|
BindingFlags
.
Instance
)
.
Select
(
p
=>
new
{
Name
=
p
.
Name
,
{
p
.
Name
,
Setter
=
p
.
DeclaringType
==
typeof
(
T
)
?
p
.
GetSetMethod
(
true
)
:
p
.
DeclaringType
.
GetProperty
(
p
.
Name
).
GetSetMethod
(
true
),
Type
=
p
.
PropertyType
})
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录