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
d9fedd9b
Commit
d9fedd9b
authored
Oct 26, 2012
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix behavior when removing values/keys from DapperRow
parent
3bfebc24
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
36 deletions
+50
-36
SqlMapper.cs
Dapper/SqlMapper.cs
+38
-35
Tests.cs
Tests/Tests.cs
+12
-1
No files found.
Dapper/SqlMapper.cs
View file @
d9fedd9b
...
...
@@ -1319,10 +1319,22 @@ public DapperRow(DapperTable table, object[] values)
this
.
table
=
table
;
this
.
values
=
values
;
}
int
ICollection
<
KeyValuePair
<
string
,
object
>>.
Count
private
sealed
class
DeadValue
{
public
static
readonly
DeadValue
Default
=
new
DeadValue
();
private
DeadValue
()
{
}
}
int
ICollection
<
KeyValuePair
<
string
,
object
>>.
Count
{
get
{
return
table
.
FieldCount
;
}
get
{
int
count
=
0
;
for
(
int
i
=
0
;
i
<
values
.
Length
;
i
++)
{
if
(!(
values
[
i
]
is
DeadValue
))
count
++;
}
return
count
;
}
}
public
bool
TryGetValue
(
string
name
,
out
object
value
)
...
...
@@ -1335,40 +1347,24 @@ public bool TryGetValue(string name, out object value)
}
// exists, **even if** we don't have a value; consider table rows heterogeneous
value
=
index
<
values
.
Length
?
values
[
index
]
:
null
;
if
(
value
is
DeadValue
)
{
// pretend it isn't here
value
=
null
;
return
false
;
}
return
true
;
}
/* public object SetValue(string name, object value)
{
var index = Table.IndexOfName(name);
if (index == -1)
{
if (m_additionalValues == null)
{
m_additionalValues = new Dictionary<string, object>();
}
m_additionalValues[name ?? ""] = value;
return value;
}
return SetFieldValueImpl(index, value);
}*/
public
override
string
ToString
()
{
var
sb
=
new
StringBuilder
(
"{DapperRow"
);
foreach
(
var
kv
in
this
)
{
var
value
=
kv
.
Value
;
sb
.
Append
(
", "
);
sb
.
Append
(
kv
.
Key
);
sb
.
Append
(
", "
).
Append
(
kv
.
Key
);
if
(
value
!=
null
)
{
sb
.
Append
(
" = '"
);
sb
.
Append
(
kv
.
Value
);
sb
.
Append
(
'\''
);
sb
.
Append
(
" = '"
).
Append
(
kv
.
Value
).
Append
(
'\''
);
}
else
{
...
...
@@ -1376,11 +1372,11 @@ public override string ToString()
}
}
sb
.
Append
(
'}'
);
return
sb
.
ToString
();
return
sb
.
Append
(
'}'
).
ToString
();
}
public
System
.
Dynamic
.
DynamicMetaObject
GetMetaObject
(
System
.
Linq
.
Expressions
.
Expression
parameter
)
System
.
Dynamic
.
DynamicMetaObject
System
.
Dynamic
.
IDynamicMetaObjectProvider
.
GetMetaObject
(
System
.
Linq
.
Expressions
.
Expression
parameter
)
{
return
new
DapperRowMetaObject
(
parameter
,
System
.
Dynamic
.
BindingRestrictions
.
Empty
,
this
);
}
...
...
@@ -1391,7 +1387,10 @@ public System.Dynamic.DynamicMetaObject GetMetaObject(System.Linq.Expressions.Ex
for
(
var
i
=
0
;
i
<
names
.
Length
;
i
++)
{
object
value
=
i
<
values
.
Length
?
values
[
i
]
:
null
;
yield
return
new
KeyValuePair
<
string
,
object
>(
names
[
i
],
value
);
if
(!(
value
is
DeadValue
))
{
yield
return
new
KeyValuePair
<
string
,
object
>(
names
[
i
],
value
);
}
}
}
...
...
@@ -1410,7 +1409,8 @@ IEnumerator IEnumerable.GetEnumerator()
void
ICollection
<
KeyValuePair
<
string
,
object
>>.
Clear
()
{
// removes values for **this row**, but doesn't change the fundamental table
values
=
new
object
[
0
];
for
(
int
i
=
0
;
i
<
values
.
Length
;
i
++)
values
[
i
]
=
DeadValue
.
Default
;
}
bool
ICollection
<
KeyValuePair
<
string
,
object
>>.
Contains
(
KeyValuePair
<
string
,
object
>
item
)
...
...
@@ -1444,7 +1444,9 @@ IEnumerator IEnumerable.GetEnumerator()
bool
IDictionary
<
string
,
object
>.
ContainsKey
(
string
key
)
{
return
table
.
FieldExists
(
key
);
int
index
=
table
.
IndexOfName
(
key
);
if
(
index
<
0
||
index
>=
values
.
Length
||
values
[
index
]
is
DeadValue
)
return
false
;
return
true
;
}
void
IDictionary
<
string
,
object
>.
Add
(
string
key
,
object
value
)
...
...
@@ -1456,8 +1458,9 @@ IEnumerator IEnumerable.GetEnumerator()
bool
IDictionary
<
string
,
object
>.
Remove
(
string
key
)
{
int
index
=
table
.
IndexOfName
(
key
);
if
(
index
>=
0
&&
index
<
values
.
Length
)
values
[
index
]
=
null
;
return
index
>=
0
;
if
(
index
<
0
||
index
>=
values
.
Length
||
values
[
index
]
is
DeadValue
)
return
false
;
values
[
index
]
=
DeadValue
.
Default
;
return
true
;
}
object
IDictionary
<
string
,
object
>.
this
[
string
key
]
...
...
@@ -1478,7 +1481,7 @@ public object SetValue(string key, object value)
// grow it to the full width of the table
Array
.
Resize
(
ref
values
,
table
.
FieldCount
);
}
return
values
[
index
]
=
value
;
return
values
[
index
]
=
value
;
}
ICollection
<
string
>
IDictionary
<
string
,
object
>.
Keys
...
...
Tests/Tests.cs
View file @
d9fedd9b
...
...
@@ -2127,7 +2127,18 @@ public void TestDynamicMutation()
var
obj
=
connection
.
Query
(
"select 1 as [a], 2 as [b], 3 as [c]"
).
Single
();
((
int
)
obj
.
a
).
IsEqualTo
(
1
);
IDictionary
<
string
,
object
>
dict
=
obj
;
dict
.
Remove
(
"a"
);
Assert
.
Equals
(
3
,
dict
.
Count
);
Assert
.
IsTrue
(
dict
.
Remove
(
"a"
));
Assert
.
IsFalse
(
dict
.
Remove
(
"d"
));
Assert
.
Equals
(
2
,
dict
.
Count
);
dict
.
Add
(
"d"
,
4
);
Assert
.
Equals
(
3
,
dict
.
Count
);
Assert
.
Equals
(
"b,c,d"
,
string
.
Join
(
","
,
dict
.
Keys
.
OrderBy
(
x
=>
x
)));
Assert
.
Equals
(
"2,3,4"
,
string
.
Join
(
","
,
dict
.
OrderBy
(
x
=>
x
.
Key
).
Select
(
x
=>
x
.
Value
)));
Assert
.
Equals
(
2
,
(
int
)
obj
.
b
);
Assert
.
Equals
(
3
,
(
int
)
obj
.
c
);
Assert
.
Equals
(
4
,
(
int
)
obj
.
d
);
try
{
((
int
)
obj
.
a
).
IsEqualTo
(
1
);
...
...
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