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
722c092d
Commit
722c092d
authored
Jun 06, 2011
by
Sam Saffron
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
addparameter was the wrong name, this sets up multiple
added test for TVP
parent
37dd9468
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
3 deletions
+67
-3
SqlMapper.cs
Dapper/SqlMapper.cs
+3
-3
Tests.cs
Tests/Tests.cs
+64
-0
No files found.
Dapper/SqlMapper.cs
View file @
722c092d
...
@@ -22,7 +22,7 @@ public static partial class SqlMapper
...
@@ -22,7 +22,7 @@ public static partial class SqlMapper
{
{
public
interface
IDynamicParameters
public
interface
IDynamicParameters
{
{
void
AddParameter
(
IDbCommand
command
);
void
AddParameter
s
(
IDbCommand
command
);
}
}
static
Link
<
Type
,
Action
<
IDbCommand
,
bool
>>
bindByNameCache
;
static
Link
<
Type
,
Action
<
IDbCommand
,
bool
>>
bindByNameCache
;
static
Action
<
IDbCommand
,
bool
>
GetBindByName
(
Type
commandType
)
static
Action
<
IDbCommand
,
bool
>
GetBindByName
(
Type
commandType
)
...
@@ -617,7 +617,7 @@ private static CacheInfo GetCacheInfo(Identity identity)
...
@@ -617,7 +617,7 @@ private static CacheInfo GetCacheInfo(Identity identity)
{
{
if
(
typeof
(
IDynamicParameters
).
IsAssignableFrom
(
identity
.
parametersType
))
if
(
typeof
(
IDynamicParameters
).
IsAssignableFrom
(
identity
.
parametersType
))
{
{
info
.
ParamReader
=
(
cmd
,
obj
)
=>
{
(
obj
as
IDynamicParameters
).
AddParameter
(
cmd
);
};
info
.
ParamReader
=
(
cmd
,
obj
)
=>
{
(
obj
as
IDynamicParameters
).
AddParameter
s
(
cmd
);
};
}
}
else
else
{
{
...
@@ -1379,7 +1379,7 @@ public DynamicParameters(object template)
...
@@ -1379,7 +1379,7 @@ public DynamicParameters(object template)
}
}
void
SqlMapper
.
IDynamicParameters
.
AddParameter
(
IDbCommand
command
)
void
SqlMapper
.
IDynamicParameters
.
AddParameter
s
(
IDbCommand
command
)
{
{
foreach
(
var
param
in
parameters
.
Values
)
foreach
(
var
param
in
parameters
.
Values
)
{
{
...
...
Tests/Tests.cs
View file @
722c092d
...
@@ -708,6 +708,70 @@ public void TestDapperSetsPrivates()
...
@@ -708,6 +708,70 @@ public void TestDapperSetsPrivates()
connection
.
Query
<
PrivateDan
>(
"select 'one' ShadowInDB"
).
First
().
Shadow
.
IsEqualTo
(
1
);
connection
.
Query
<
PrivateDan
>(
"select 'one' ShadowInDB"
).
First
().
Shadow
.
IsEqualTo
(
1
);
}
}
class
IntDynamicParam
:
Dapper
.
SqlMapper
.
IDynamicParameters
{
IEnumerable
<
int
>
numbers
;
public
IntDynamicParam
(
IEnumerable
<
int
>
numbers
)
{
this
.
numbers
=
numbers
;
}
public
void
AddParameters
(
IDbCommand
command
)
{
var
sqlCommand
=
(
SqlCommand
)
command
;
sqlCommand
.
CommandType
=
CommandType
.
StoredProcedure
;
List
<
Microsoft
.
SqlServer
.
Server
.
SqlDataRecord
>
number_list
=
new
List
<
Microsoft
.
SqlServer
.
Server
.
SqlDataRecord
>();
// Create an SqlMetaData object that describes our table type.
Microsoft
.
SqlServer
.
Server
.
SqlMetaData
[]
tvp_definition
=
{
new
Microsoft
.
SqlServer
.
Server
.
SqlMetaData
(
"n"
,
SqlDbType
.
Int
)
};
foreach
(
int
n
in
numbers
)
{
// Create a new record, using the metadata array above.
Microsoft
.
SqlServer
.
Server
.
SqlDataRecord
rec
=
new
Microsoft
.
SqlServer
.
Server
.
SqlDataRecord
(
tvp_definition
);
rec
.
SetInt32
(
0
,
n
);
// Set the value.
number_list
.
Add
(
rec
);
// Add it to the list.
}
// Add the table parameter.
var
p
=
sqlCommand
.
Parameters
.
Add
(
"@ints"
,
SqlDbType
.
Structured
);
p
.
Direction
=
ParameterDirection
.
Input
;
p
.
TypeName
=
"int_list_type"
;
p
.
Value
=
number_list
;
}
}
// SQL Server specific test to demonstrate TVP
public
void
TestTVP
()
{
try
{
connection
.
Execute
(
"CREATE TYPE int_list_type AS TABLE (n int NOT NULL PRIMARY KEY)"
);
connection
.
Execute
(
"CREATE PROC get_ints @ints int_list_type READONLY AS select * from @ints"
);
var
nums
=
connection
.
Query
<
int
>(
"get_ints"
,
new
IntDynamicParam
(
new
int
[]
{
1
,
2
,
3
})).
ToList
();
nums
[
0
].
IsEqualTo
(
1
);
nums
[
1
].
IsEqualTo
(
2
);
nums
[
2
].
IsEqualTo
(
3
);
nums
.
Count
.
IsEqualTo
(
3
);
}
finally
{
try
{
connection
.
Execute
(
"DROP PROC get_ints"
);
}
finally
{
connection
.
Execute
(
"DROP TYPE int_list_type"
);
}
}
}
/* TODO:
/* TODO:
*
*
public void TestMagicParam()
public void TestMagicParam()
...
...
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