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
7535cb7d
Commit
7535cb7d
authored
Apr 06, 2011
by
Sam Saffron
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change it so dapper is db independant
parent
b06e62f3
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
45 deletions
+69
-45
Program.cs
Program.cs
+4
-4
SqlMapper.cs
SqlMapper.cs
+63
-41
Tests.cs
Tests.cs
+2
-0
No files found.
Program.cs
View file @
7535cb7d
...
@@ -51,12 +51,12 @@ static void RunPerformanceTests()
...
@@ -51,12 +51,12 @@ static void RunPerformanceTests()
static
void
Main
(
string
[]
args
)
static
void
Main
(
string
[]
args
)
{
{
//
#if DEBUG
#if DEBUG
//
RunTests();
RunTests
();
//
#else
#else
EnsureDBSetup
();
EnsureDBSetup
();
RunPerformanceTests
();
RunPerformanceTests
();
//
#endif
#endif
Console
.
ReadKey
();
Console
.
ReadKey
();
}
}
...
...
SqlMapper.cs
View file @
7535cb7d
...
@@ -15,40 +15,64 @@ namespace SqlMapper
...
@@ -15,40 +15,64 @@ namespace SqlMapper
{
{
public
static
class
SqlMapper
public
static
class
SqlMapper
{
{
static
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
>>>();
typeMap
=
new
Dictionary
<
Type
,
SqlDbType
>();
static
Dictionary
<
Type
,
DbType
>
typeMap
;
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
;
}
private
static
SqlDbType
LookupDbType
(
Type
type
)
static
SqlMapper
(
)
{
{
SqlDbType
dbType
;
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
DbType
LookupDbType
(
Type
type
)
{
DbType
dbType
;
if
(
typeMap
.
TryGetValue
(
type
,
out
dbType
))
if
(
typeMap
.
TryGetValue
(
type
,
out
dbType
))
{
{
return
dbType
;
return
dbType
;
}
}
else
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()
...
@@ -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
};
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
string
Name
{
get
;
private
set
;
}
public
object
Val
{
get
;
private
set
;
}
public
object
Val
{
get
;
private
set
;
}
}
}
...
@@ -104,10 +128,7 @@ public bool Equals(Identity other)
...
@@ -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>
/// <summary>
/// Execute parameterized SQL
/// Execute parameterized SQL
...
@@ -118,7 +139,6 @@ public static int ExecuteMapperCommand(this IDbConnection cnn, string sql, objec
...
@@ -118,7 +139,6 @@ public static int ExecuteMapperCommand(this IDbConnection cnn, string sql, objec
return
ExecuteCommand
(
cnn
,
transaction
,
sql
,
GetParamInfo
(
param
));
return
ExecuteCommand
(
cnn
,
transaction
,
sql
,
GetParamInfo
(
param
));
}
}
static
class
DynamicStub
static
class
DynamicStub
{
{
public
static
Type
Type
=
typeof
(
DynamicStub
);
public
static
Type
Type
=
typeof
(
DynamicStub
);
...
@@ -242,7 +262,7 @@ private static object GetDynamicDeserializer(IDataReader reader)
...
@@ -242,7 +262,7 @@ private static object GetDynamicDeserializer(IDataReader reader)
foreach
(
var
prop
in
type
.
GetProperties
().
OrderBy
(
p
=>
p
.
Name
))
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]
il
.
Emit
(
OpCodes
.
Dup
);
// stack is now [list] [list]
...
@@ -263,7 +283,7 @@ private static object GetDynamicDeserializer(IDataReader reader)
...
@@ -263,7 +283,7 @@ private static object GetDynamicDeserializer(IDataReader reader)
return
(
Func
<
object
,
List
<
ParamInfo
>>)
dm
.
CreateDelegate
(
typeof
(
Func
<
object
,
List
<
ParamInfo
>>));
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
();
var
cmd
=
cnn
.
CreateCommand
();
...
@@ -273,12 +293,12 @@ private static IDbCommand SetupCommand(IDbConnection cnn, SqlTransaction tranact
...
@@ -273,12 +293,12 @@ private static IDbCommand SetupCommand(IDbConnection cnn, SqlTransaction tranact
{
{
foreach
(
var
info
in
paramInfo
)
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
.
Value
=
info
.
Val
??
DBNull
.
Value
;
param
.
Direction
=
ParameterDirection
.
Input
;
param
.
Direction
=
ParameterDirection
.
Input
;
if
(
info
.
Type
==
SqlDbType
.
NVarChar
)
if
(
info
.
Type
==
DbType
.
String
)
{
{
param
.
Size
=
4000
;
param
.
Size
=
4000
;
if
(
info
.
Val
!=
null
&&
((
string
)
info
.
Val
).
Length
>
4000
)
if
(
info
.
Val
!=
null
&&
((
string
)
info
.
Val
).
Length
>
4000
)
...
@@ -287,7 +307,7 @@ private static IDbCommand SetupCommand(IDbConnection cnn, SqlTransaction tranact
...
@@ -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.
// initially we tried TVP, however it performs quite poorly.
...
@@ -302,16 +322,18 @@ private static IDbCommand SetupCommand(IDbConnection cnn, SqlTransaction tranact
...
@@ -302,16 +322,18 @@ private static IDbCommand SetupCommand(IDbConnection cnn, SqlTransaction tranact
foreach
(
var
item
in
list
)
foreach
(
var
item
in
list
)
{
{
count
++;
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
)
if
(
isString
)
{
{
sql
Param
.
Size
=
4000
;
list
Param
.
Size
=
4000
;
if
(
item
!=
null
&&
((
string
)
item
).
Length
>
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
,
cmd
.
CommandText
=
cmd
.
CommandText
.
Replace
(
"@"
+
info
.
Name
,
...
@@ -334,7 +356,7 @@ private static IDbCommand SetupCommand(IDbConnection cnn, SqlTransaction tranact
...
@@ -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
))
using
(
var
cmd
=
SetupCommand
(
cnn
,
tranaction
,
sql
,
paramInfo
))
{
{
...
@@ -342,7 +364,7 @@ private static int ExecuteCommand(IDbConnection cnn, SqlTransaction tranaction,
...
@@ -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
))
using
(
var
cmd
=
SetupCommand
(
cnn
,
tranaction
,
sql
,
paramInfo
))
{
{
...
...
Tests.cs
View file @
7535cb7d
...
@@ -146,5 +146,7 @@ public void TestMassiveStrings()
...
@@ -146,5 +146,7 @@ public void TestMassiveStrings()
.
IsEqualTo
(
str
);
.
IsEqualTo
(
str
);
}
}
}
}
}
}
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