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
fa06c306
Commit
fa06c306
authored
May 02, 2015
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Workaround for
https://github.com/dotnet/corefx/issues/1613
parent
dc6b077f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
10 deletions
+35
-10
SqlMapper.cs
Dapper NET40/SqlMapper.cs
+34
-7
Tests.cs
Tests/Tests.cs
+1
-3
No files found.
Dapper NET40/SqlMapper.cs
View file @
fa06c306
...
@@ -345,7 +345,7 @@ object ITypeHandler.Parse(Type destinationType, object value)
...
@@ -345,7 +345,7 @@ object ITypeHandler.Parse(Type destinationType, object value)
void
ITypeHandler
.
SetValue
(
IDbDataParameter
parameter
,
object
value
)
void
ITypeHandler
.
SetValue
(
IDbDataParameter
parameter
,
object
value
)
{
{
parameter
.
Value
=
((
object
)
value
)
??
DBNull
.
Value
;
parameter
.
Value
=
SanitizeParameterValue
(
value
)
;
if
(
parameter
is
System
.
Data
.
SqlClient
.
SqlParameter
)
if
(
parameter
is
System
.
Data
.
SqlClient
.
SqlParameter
)
{
{
((
System
.
Data
.
SqlClient
.
SqlParameter
)
parameter
).
UdtTypeName
=
udtTypeName
;
((
System
.
Data
.
SqlClient
.
SqlParameter
)
parameter
).
UdtTypeName
=
udtTypeName
;
...
@@ -2745,7 +2745,7 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
...
@@ -2745,7 +2745,7 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
if
(
FeatureSupport
.
Get
(
command
.
Connection
).
Arrays
)
if
(
FeatureSupport
.
Get
(
command
.
Connection
).
Arrays
)
{
{
var
arrayParm
=
command
.
CreateParameter
();
var
arrayParm
=
command
.
CreateParameter
();
arrayParm
.
Value
=
value
??
DBNull
.
Value
;
arrayParm
.
Value
=
SanitizeParameterValue
(
value
)
;
arrayParm
.
ParameterName
=
namePrefix
;
arrayParm
.
ParameterName
=
namePrefix
;
command
.
Parameters
.
Add
(
arrayParm
);
command
.
Parameters
.
Add
(
arrayParm
);
}
}
...
@@ -2775,7 +2775,7 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
...
@@ -2775,7 +2775,7 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
}
}
else
else
{
{
listParam
.
Value
=
item
??
DBNull
.
Value
;
listParam
.
Value
=
SanitizeParameterValue
(
item
)
;
command
.
Parameters
.
Add
(
listParam
);
command
.
Parameters
.
Add
(
listParam
);
}
}
}
}
...
@@ -2832,7 +2832,34 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
...
@@ -2832,7 +2832,34 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
}
}
}
}
internal
static
object
SanitizeParameterValue
(
object
value
)
{
if
(
value
==
null
)
return
DBNull
.
Value
;
if
(
value
is
Enum
)
{
TypeCode
typeCode
;
if
(
value
is
IConvertible
)
{
typeCode
=
((
IConvertible
)
value
).
GetTypeCode
();
}
else
{
typeCode
=
TypeExtensions
.
GetTypeCode
(
Enum
.
GetUnderlyingType
(
value
.
GetType
()));
}
switch
(
typeCode
)
{
case
TypeCode
.
Byte
:
return
(
byte
)
value
;
case
TypeCode
.
SByte
:
return
(
sbyte
)
value
;
case
TypeCode
.
Int16
:
return
(
short
)
value
;
case
TypeCode
.
Int32
:
return
(
int
)
value
;
case
TypeCode
.
Int64
:
return
(
long
)
value
;
case
TypeCode
.
UInt16
:
return
(
ushort
)
value
;
case
TypeCode
.
UInt32
:
return
(
uint
)
value
;
case
TypeCode
.
UInt64
:
return
(
ulong
)
value
;
}
}
return
value
;
}
private
static
IEnumerable
<
PropertyInfo
>
FilterParameters
(
IEnumerable
<
PropertyInfo
>
parameters
,
string
sql
)
private
static
IEnumerable
<
PropertyInfo
>
FilterParameters
(
IEnumerable
<
PropertyInfo
>
parameters
,
string
sql
)
{
{
return
parameters
.
Where
(
p
=>
Regex
.
IsMatch
(
sql
,
@"[?@:]"
+
p
.
Name
+
"([^a-z0-9_]+|$)"
,
RegexOptions
.
IgnoreCase
|
RegexOptions
.
Multiline
|
RegexOptions
.
CultureInvariant
));
return
parameters
.
Where
(
p
=>
Regex
.
IsMatch
(
sql
,
@"[?@:]"
+
p
.
Name
+
"([^a-z0-9_]+|$)"
,
RegexOptions
.
IgnoreCase
|
RegexOptions
.
Multiline
|
RegexOptions
.
CultureInvariant
));
...
@@ -4758,7 +4785,7 @@ protected void AddParameters(IDbCommand command, SqlMapper.Identity identity)
...
@@ -4758,7 +4785,7 @@ protected void AddParameters(IDbCommand command, SqlMapper.Identity identity)
p
.
Direction
=
param
.
ParameterDirection
;
p
.
Direction
=
param
.
ParameterDirection
;
if
(
handler
==
null
)
if
(
handler
==
null
)
{
{
p
.
Value
=
val
??
DBNull
.
Value
;
p
.
Value
=
SqlMapper
.
SanitizeParameterValue
(
val
)
;
if
(
dbType
!=
null
&&
p
.
DbType
!=
dbType
)
if
(
dbType
!=
null
&&
p
.
DbType
!=
dbType
)
{
{
p
.
DbType
=
dbType
.
Value
;
p
.
DbType
=
dbType
.
Value
;
...
@@ -5073,7 +5100,7 @@ void SqlMapper.ICustomQueryParameter.AddParameter(IDbCommand command, string nam
...
@@ -5073,7 +5100,7 @@ void SqlMapper.ICustomQueryParameter.AddParameter(IDbCommand command, string nam
}
}
internal
static
void
Set
(
IDbDataParameter
parameter
,
DataTable
table
,
string
typeName
)
internal
static
void
Set
(
IDbDataParameter
parameter
,
DataTable
table
,
string
typeName
)
{
{
parameter
.
Value
=
(
object
)
table
??
DBNull
.
Value
;
parameter
.
Value
=
SqlMapper
.
SanitizeParameterValue
(
table
)
;
if
(
string
.
IsNullOrEmpty
(
typeName
)
&&
table
!=
null
)
if
(
string
.
IsNullOrEmpty
(
typeName
)
&&
table
!=
null
)
{
{
typeName
=
SqlMapper
.
GetTypeName
(
table
);
typeName
=
SqlMapper
.
GetTypeName
(
table
);
...
@@ -5135,7 +5162,7 @@ public void AddParameter(IDbCommand command, string name)
...
@@ -5135,7 +5162,7 @@ public void AddParameter(IDbCommand command, string name)
}
}
var
param
=
command
.
CreateParameter
();
var
param
=
command
.
CreateParameter
();
param
.
ParameterName
=
name
;
param
.
ParameterName
=
name
;
param
.
Value
=
(
object
)
Value
??
DBNull
.
Value
;
param
.
Value
=
SqlMapper
.
SanitizeParameterValue
(
Value
)
;
if
(
Length
==
-
1
&&
Value
!=
null
&&
Value
.
Length
<=
DefaultLength
)
if
(
Length
==
-
1
&&
Value
!=
null
&&
Value
.
Length
<=
DefaultLength
)
{
{
param
.
Size
=
DefaultLength
;
param
.
Size
=
DefaultLength
;
...
...
Tests/Tests.cs
View file @
fa06c306
...
@@ -3208,9 +3208,7 @@ public void LiteralReplacementEnumAndString()
...
@@ -3208,9 +3208,7 @@ public void LiteralReplacementEnumAndString()
y
.
Equals
(
123.45
M
);
y
.
Equals
(
123.45
M
);
z
.
Equals
(
AnotherEnum
.
A
);
z
.
Equals
(
AnotherEnum
.
A
);
}
}
#if DNXCORE50
[
FrameworkFail
(
"https://github.com/dotnet/corefx/issues/1613"
)]
#endif
public
void
LiteralReplacementDynamicEnumAndString
()
public
void
LiteralReplacementDynamicEnumAndString
()
{
{
var
args
=
new
DynamicParameters
();
var
args
=
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