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
86476bc2
Commit
86476bc2
authored
May 24, 2013
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(courtesy @mrange) fix dictionary semantics of DapperRow
parent
eba9780e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
6 deletions
+55
-6
SqlMapper.cs
Dapper NET40/SqlMapper.cs
+21
-6
Tests.cs
Tests/Tests.cs
+34
-0
No files found.
Dapper NET40/SqlMapper.cs
View file @
86476bc2
...
...
@@ -1327,7 +1327,7 @@ internal bool FieldExists(string key)
sealed
partial
class
DapperRowMetaObject
:
System
.
Dynamic
.
DynamicMetaObject
{
static
readonly
MethodInfo
getValueMethod
=
typeof
(
IDictionary
<
string
,
object
>).
GetProperty
(
"Item"
).
GetGetMethod
();
static
readonly
MethodInfo
setValueMethod
=
typeof
(
DapperRow
).
GetMethod
(
"SetValue"
);
static
readonly
MethodInfo
setValueMethod
=
typeof
(
DapperRow
).
GetMethod
(
"SetValue"
,
new
Type
[]
{
typeof
(
string
),
typeof
(
object
)
}
);
public
DapperRowMetaObject
(
System
.
Linq
.
Expressions
.
Expression
expression
,
...
...
@@ -1546,8 +1546,7 @@ IEnumerator IEnumerable.GetEnumerator()
void
IDictionary
<
string
,
object
>.
Add
(
string
key
,
object
value
)
{
IDictionary
<
string
,
object
>
dic
=
this
;
dic
[
key
]
=
value
;
SetValue
(
key
,
value
,
true
);
}
bool
IDictionary
<
string
,
object
>.
Remove
(
string
key
)
...
...
@@ -1561,9 +1560,14 @@ IEnumerator IEnumerable.GetEnumerator()
object
IDictionary
<
string
,
object
>.
this
[
string
key
]
{
get
{
object
val
;
TryGetValue
(
key
,
out
val
);
return
val
;
}
set
{
SetValue
(
key
,
value
);
}
set
{
SetValue
(
key
,
value
,
false
);
}
}
public
object
SetValue
(
string
key
,
object
value
)
{
return
SetValue
(
key
,
value
,
false
);
}
private
object
SetValue
(
string
key
,
object
value
,
bool
isAdd
)
{
if
(
key
==
null
)
throw
new
ArgumentNullException
(
"key"
);
int
index
=
table
.
IndexOfName
(
key
);
...
...
@@ -1571,10 +1575,21 @@ public object SetValue(string key, object value)
{
index
=
table
.
AddField
(
key
);
}
if
(
values
.
Length
<=
index
)
{
// we'll assume they're doing lots of things, and
else
if
(
isAdd
&&
index
<
values
.
Length
&&
!(
values
[
index
]
is
DeadValue
))
{
// then semantically, this value already exists
throw
new
ArgumentException
(
"An item with the same key has already been added"
,
"key"
);
}
int
oldLength
=
values
.
Length
;
if
(
oldLength
<=
index
)
{
// we'll assume they're doing lots of things, and
// grow it to the full width of the table
Array
.
Resize
(
ref
values
,
table
.
FieldCount
);
for
(
int
i
=
oldLength
;
i
<
values
.
Length
;
i
++)
{
values
[
i
]
=
DeadValue
.
Default
;
}
}
return
values
[
index
]
=
value
;
}
...
...
Tests/Tests.cs
View file @
86476bc2
...
...
@@ -378,6 +378,40 @@ public void TestStrings()
.
IsSequenceEqualTo
(
new
[]
{
"a"
,
"b"
});
}
// see http://stackoverflow.com/questions/16726709/string-format-with-sql-wildcard-causing-dapper-query-to-break
public
void
CheckComplexConcat
()
{
string
end_wildcard
=
@"
SELECT * FROM #users16726709
WHERE (first_name LIKE CONCAT(@search_term, '%') OR last_name LIKE CONCAT(@search_term, '%'));"
;
string
both_wildcards
=
@"
SELECT * FROM #users16726709
WHERE (first_name LIKE CONCAT('%', @search_term, '%') OR last_name LIKE CONCAT('%', @search_term, '%'));"
;
string
formatted
=
@"
SELECT * FROM #users16726709
WHERE (first_name LIKE {0} OR last_name LIKE {0});"
;
string
use_end_only
=
@"CONCAT(@search_term, '%')"
;
string
use_both
=
@"CONCAT('%', @search_term, '%')"
;
// if true, slower query due to not being able to use indices, but will allow searching inside strings
bool
allow_start_wildcards
=
false
;
string
query
=
String
.
Format
(
formatted
,
allow_start_wildcards
?
use_both
:
use_end_only
);
string
term
=
"F"
;
// the term the user searched for
connection
.
Execute
(
@"create table #users16726709 (first_name varchar(200), last_name varchar(200))
insert #users16726709 values ('Fred','Bloggs') insert #users16726709 values ('Tony','Farcus') insert #users16726709 values ('Albert','Tenof')"
);
// Using Dapper
connection
.
Query
(
end_wildcard
,
new
{
search_term
=
term
}).
Count
().
IsEqualTo
(
2
);
connection
.
Query
(
both_wildcards
,
new
{
search_term
=
term
}).
Count
().
IsEqualTo
(
3
);
connection
.
Query
(
query
,
new
{
search_term
=
term
}).
Count
().
IsEqualTo
(
2
);
}
enum
EnumParam
:
short
{
None
,
A
,
B
...
...
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