Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Dapper
提交
7535cb7d
D
Dapper
项目概览
int
/
Dapper
12 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
7535cb7d
编写于
4月 06, 2011
作者:
S
Sam Saffron
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
change it so dapper is db independant
上级
b06e62f3
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
69 addition
and
45 deletion
+69
-45
Program.cs
Program.cs
+4
-4
SqlMapper.cs
SqlMapper.cs
+63
-41
Tests.cs
Tests.cs
+2
-0
未找到文件。
Program.cs
浏览文件 @
7535cb7d
...
...
@@ -51,12 +51,12 @@ static void RunPerformanceTests()
static
void
Main
(
string
[]
args
)
{
//
#if DEBUG
//
RunTests();
//
#else
#if DEBUG
RunTests
();
#else
EnsureDBSetup
();
RunPerformanceTests
();
//
#endif
#endif
Console
.
ReadKey
();
}
...
...
SqlMapper.cs
浏览文件 @
7535cb7d
...
...
@@ -15,40 +15,64 @@ namespace SqlMapper
{
public
static
class
SqlMapper
{
static
ConcurrentDictionary
<
Identity
,
object
>
cachedSerializers
=
new
ConcurrentDictionary
<
Identity
,
object
>();
static
ConcurrentDictionary
<
Type
,
Func
<
object
,
List
<
ParamInfo
>>>
cachedParamReaders
=
new
ConcurrentDictionary
<
Type
,
Func
<
object
,
List
<
ParamInfo
>>>();
static
Dictionary
<
Type
,
DbType
>
typeMap
;
static
SqlMapper
()
{
typeMap
=
new
Dictionary
<
Type
,
SqlDbType
>();
typeMap
[
typeof
(
int
)]
=
SqlDbType
.
Int
;
typeMap
[
typeof
(
int
?)]
=
SqlDbType
.
Int
;
typeMap
[
typeof
(
string
)]
=
SqlDbType
.
NVarChar
;
// weird ... I know see: http://msdn.microsoft.com/en-us/library/ms131092.aspx
typeMap
[
typeof
(
double
)]
=
SqlDbType
.
Float
;
typeMap
[
typeof
(
double
?)]
=
SqlDbType
.
Float
;
typeMap
[
typeof
(
bool
)]
=
SqlDbType
.
Bit
;
typeMap
[
typeof
(
bool
?)]
=
SqlDbType
.
Bit
;
typeMap
[
typeof
(
Guid
)]
=
SqlDbType
.
UniqueIdentifier
;
typeMap
[
typeof
(
Guid
?)]
=
SqlDbType
.
UniqueIdentifier
;
typeMap
[
typeof
(
DateTime
)]
=
SqlDbType
.
DateTime
;
typeMap
[
typeof
(
DateTime
?)]
=
SqlDbType
.
DateTime
;
typeMap
=
new
Dictionary
<
Type
,
DbType
>();
typeMap
[
typeof
(
byte
)]
=
DbType
.
Byte
;
typeMap
[
typeof
(
sbyte
)]
=
DbType
.
SByte
;
typeMap
[
typeof
(
short
)]
=
DbType
.
Int16
;
typeMap
[
typeof
(
ushort
)]
=
DbType
.
UInt16
;
typeMap
[
typeof
(
int
)]
=
DbType
.
Int32
;
typeMap
[
typeof
(
uint
)]
=
DbType
.
UInt32
;
typeMap
[
typeof
(
long
)]
=
DbType
.
Int64
;
typeMap
[
typeof
(
ulong
)]
=
DbType
.
UInt64
;
typeMap
[
typeof
(
float
)]
=
DbType
.
Single
;
typeMap
[
typeof
(
double
)]
=
DbType
.
Double
;
typeMap
[
typeof
(
decimal
)]
=
DbType
.
Decimal
;
typeMap
[
typeof
(
bool
)]
=
DbType
.
Boolean
;
typeMap
[
typeof
(
string
)]
=
DbType
.
String
;
typeMap
[
typeof
(
char
)]
=
DbType
.
StringFixedLength
;
typeMap
[
typeof
(
Guid
)]
=
DbType
.
Guid
;
typeMap
[
typeof
(
DateTime
)]
=
DbType
.
DateTime
;
typeMap
[
typeof
(
DateTimeOffset
)]
=
DbType
.
DateTimeOffset
;
typeMap
[
typeof
(
byte
[])]
=
DbType
.
Binary
;
typeMap
[
typeof
(
byte
?)]
=
DbType
.
Byte
;
typeMap
[
typeof
(
sbyte
?)]
=
DbType
.
SByte
;
typeMap
[
typeof
(
short
?)]
=
DbType
.
Int16
;
typeMap
[
typeof
(
ushort
?)]
=
DbType
.
UInt16
;
typeMap
[
typeof
(
int
?)]
=
DbType
.
Int32
;
typeMap
[
typeof
(
uint
?)]
=
DbType
.
UInt32
;
typeMap
[
typeof
(
long
?)]
=
DbType
.
Int64
;
typeMap
[
typeof
(
ulong
?)]
=
DbType
.
UInt64
;
typeMap
[
typeof
(
float
?)]
=
DbType
.
Single
;
typeMap
[
typeof
(
double
?)]
=
DbType
.
Double
;
typeMap
[
typeof
(
decimal
?)]
=
DbType
.
Decimal
;
typeMap
[
typeof
(
bool
?)]
=
DbType
.
Boolean
;
typeMap
[
typeof
(
char
?)]
=
DbType
.
StringFixedLength
;
typeMap
[
typeof
(
Guid
?)]
=
DbType
.
Guid
;
typeMap
[
typeof
(
DateTime
?)]
=
DbType
.
DateTime
;
typeMap
[
typeof
(
DateTimeOffset
?)]
=
DbType
.
DateTimeOffset
;
}
private
static
Sql
DbType
LookupDbType
(
Type
type
)
private
static
DbType
LookupDbType
(
Type
type
)
{
Sql
DbType
dbType
;
DbType
dbType
;
if
(
typeMap
.
TryGetValue
(
type
,
out
dbType
))
{
return
dbType
;
}
else
{
if
(
typeof
(
IEnumerable
<
int
>).
IsAssignableFrom
(
type
)
||
typeof
(
IEnumerable
<
string
>
).
IsAssignableFrom
(
type
))
if
(
typeof
(
IEnumerable
).
IsAssignableFrom
(
type
))
{
return
SqlDbType
.
Structured
;
// use xml to denote its a list, hacky but will work on any DB
return
DbType
.
Xml
;
}
}
...
...
@@ -62,12 +86,12 @@ private ParamInfo()
{
}
public
static
ParamInfo
Create
(
string
name
,
Sql
DbType
type
,
object
val
)
public
static
ParamInfo
Create
(
string
name
,
DbType
type
,
object
val
)
{
return
new
ParamInfo
{
Name
=
name
,
Type
=
type
,
Val
=
val
};
}
public
Sql
DbType
Type
{
get
;
private
set
;
}
public
DbType
Type
{
get
;
private
set
;
}
public
string
Name
{
get
;
private
set
;
}
public
object
Val
{
get
;
private
set
;
}
}
...
...
@@ -104,10 +128,7 @@ public bool Equals(Identity other)
}
}
static
ConcurrentDictionary
<
Identity
,
object
>
cachedSerializers
=
new
ConcurrentDictionary
<
Identity
,
object
>();
static
ConcurrentDictionary
<
Type
,
Func
<
object
,
List
<
ParamInfo
>>>
cachedParamReaders
=
new
ConcurrentDictionary
<
Type
,
Func
<
object
,
List
<
ParamInfo
>>>();
static
Dictionary
<
Type
,
SqlDbType
>
typeMap
;
/// <summary>
/// Execute parameterized SQL
...
...
@@ -118,7 +139,6 @@ public static int ExecuteMapperCommand(this IDbConnection cnn, string sql, objec
return
ExecuteCommand
(
cnn
,
transaction
,
sql
,
GetParamInfo
(
param
));
}
static
class
DynamicStub
{
public
static
Type
Type
=
typeof
(
DynamicStub
);
...
...
@@ -242,7 +262,7 @@ private static object GetDynamicDeserializer(IDataReader reader)
foreach
(
var
prop
in
type
.
GetProperties
().
OrderBy
(
p
=>
p
.
Name
))
{
// we want to call list.Add(ParamInfo.Create(string name,
Sql
DbType type, object val))
// we want to call list.Add(ParamInfo.Create(string name, DbType type, object val))
il
.
Emit
(
OpCodes
.
Dup
);
// stack is now [list] [list]
...
...
@@ -263,7 +283,7 @@ private static object GetDynamicDeserializer(IDataReader reader)
return
(
Func
<
object
,
List
<
ParamInfo
>>)
dm
.
CreateDelegate
(
typeof
(
Func
<
object
,
List
<
ParamInfo
>>));
}
private
static
IDbCommand
SetupCommand
(
IDbConnection
cnn
,
Sql
Transaction
tranaction
,
string
sql
,
List
<
ParamInfo
>
paramInfo
)
private
static
IDbCommand
SetupCommand
(
IDbConnection
cnn
,
IDb
Transaction
tranaction
,
string
sql
,
List
<
ParamInfo
>
paramInfo
)
{
var
cmd
=
cnn
.
CreateCommand
();
...
...
@@ -273,12 +293,12 @@ private static IDbCommand SetupCommand(IDbConnection cnn, SqlTransaction tranact
{
foreach
(
var
info
in
paramInfo
)
{
var
param
=
new
SqlParameter
(
"@"
+
info
.
Name
,
info
.
Type
);
var
param
=
cmd
.
CreateParameter
(
);
param
.
ParameterName
=
"@"
+
info
.
Name
;
param
.
DbType
=
info
.
Type
;
param
.
Value
=
info
.
Val
??
DBNull
.
Value
;
param
.
Direction
=
ParameterDirection
.
Input
;
if
(
info
.
Type
==
SqlDbType
.
NVarChar
)
if
(
info
.
Type
==
DbType
.
String
)
{
param
.
Size
=
4000
;
if
(
info
.
Val
!=
null
&&
((
string
)
info
.
Val
).
Length
>
4000
)
...
...
@@ -287,7 +307,7 @@ private static IDbCommand SetupCommand(IDbConnection cnn, SqlTransaction tranact
}
}
if
(
info
.
Type
==
SqlDbType
.
Structured
)
if
(
info
.
Type
==
DbType
.
Xml
)
{
// initially we tried TVP, however it performs quite poorly.
...
...
@@ -302,16 +322,18 @@ private static IDbCommand SetupCommand(IDbConnection cnn, SqlTransaction tranact
foreach
(
var
item
in
list
)
{
count
++;
var
sqlParam
=
new
SqlParameter
(
"@"
+
info
.
Name
+
count
,
item
??
DBNull
.
Value
);
var
listParam
=
cmd
.
CreateParameter
();
listParam
.
ParameterName
=
"@"
+
info
.
Name
+
count
;
listParam
.
Value
=
item
??
DBNull
.
Value
;
if
(
isString
)
{
sql
Param
.
Size
=
4000
;
list
Param
.
Size
=
4000
;
if
(
item
!=
null
&&
((
string
)
item
).
Length
>
4000
)
{
sql
Param
.
Size
=
-
1
;
list
Param
.
Size
=
-
1
;
}
}
cmd
.
Parameters
.
Add
(
sql
Param
);
cmd
.
Parameters
.
Add
(
list
Param
);
}
cmd
.
CommandText
=
cmd
.
CommandText
.
Replace
(
"@"
+
info
.
Name
,
...
...
@@ -334,7 +356,7 @@ private static IDbCommand SetupCommand(IDbConnection cnn, SqlTransaction tranact
}
private
static
int
ExecuteCommand
(
IDbConnection
cnn
,
Sql
Transaction
tranaction
,
string
sql
,
List
<
ParamInfo
>
paramInfo
)
private
static
int
ExecuteCommand
(
IDbConnection
cnn
,
IDb
Transaction
tranaction
,
string
sql
,
List
<
ParamInfo
>
paramInfo
)
{
using
(
var
cmd
=
SetupCommand
(
cnn
,
tranaction
,
sql
,
paramInfo
))
{
...
...
@@ -342,7 +364,7 @@ private static int ExecuteCommand(IDbConnection cnn, SqlTransaction tranaction,
}
}
private
static
IDataReader
GetReader
(
IDbConnection
cnn
,
Sql
Transaction
tranaction
,
string
sql
,
List
<
ParamInfo
>
paramInfo
)
private
static
IDataReader
GetReader
(
IDbConnection
cnn
,
IDb
Transaction
tranaction
,
string
sql
,
List
<
ParamInfo
>
paramInfo
)
{
using
(
var
cmd
=
SetupCommand
(
cnn
,
tranaction
,
sql
,
paramInfo
))
{
...
...
Tests.cs
浏览文件 @
7535cb7d
...
...
@@ -146,5 +146,7 @@ public void TestMassiveStrings()
.
IsEqualTo
(
str
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录