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
610d8f65
Commit
610d8f65
authored
Jun 15, 2017
by
Nick Craver
Committed by
GitHub
Jun 15, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revert "Enable SqlDataRecord TVPs on corefx. (#801)" (#803)
This reverts commit
e7ad0874
.
parent
e7ad0874
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
61 additions
and
104 deletions
+61
-104
ParameterTests.cs
Dapper.Tests/ParameterTests.cs
+37
-98
Dapper.csproj
Dapper/Dapper.csproj
+1
-1
SqlDataRecordHandler.cs
Dapper/SqlDataRecordHandler.cs
+2
-0
SqlDataRecordListTVPParameter.cs
Dapper/SqlDataRecordListTVPParameter.cs
+15
-1
SqlMapper.cs
Dapper/SqlMapper.cs
+6
-4
No files found.
Dapper.Tests/ParameterTests.cs
View file @
610d8f65
...
@@ -37,24 +37,6 @@ void SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, SqlMapper.Id
...
@@ -37,24 +37,6 @@ void SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, SqlMapper.Id
}
}
}
}
private
static
List
<
Microsoft
.
SqlServer
.
Server
.
SqlDataRecord
>
CreateSqlDataRecordList
(
IEnumerable
<
int
>
numbers
)
{
var
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.
var
rec
=
new
Microsoft
.
SqlServer
.
Server
.
SqlDataRecord
(
tvp_definition
);
rec
.
SetInt32
(
0
,
n
);
// Set the value.
number_list
.
Add
(
rec
);
// Add it to the list.
}
return
number_list
;
}
private
class
IntDynamicParam
:
SqlMapper
.
IDynamicParameters
private
class
IntDynamicParam
:
SqlMapper
.
IDynamicParameters
{
{
private
readonly
IEnumerable
<
int
>
numbers
;
private
readonly
IEnumerable
<
int
>
numbers
;
...
@@ -68,7 +50,18 @@ public void AddParameters(IDbCommand command, SqlMapper.Identity identity)
...
@@ -68,7 +50,18 @@ public void AddParameters(IDbCommand command, SqlMapper.Identity identity)
var
sqlCommand
=
(
SqlCommand
)
command
;
var
sqlCommand
=
(
SqlCommand
)
command
;
sqlCommand
.
CommandType
=
CommandType
.
StoredProcedure
;
sqlCommand
.
CommandType
=
CommandType
.
StoredProcedure
;
var
number_list
=
CreateSqlDataRecordList
(
numbers
);
var
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.
var
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.
// Add the table parameter.
var
p
=
sqlCommand
.
Parameters
.
Add
(
"ints"
,
SqlDbType
.
Structured
);
var
p
=
sqlCommand
.
Parameters
.
Add
(
"ints"
,
SqlDbType
.
Structured
);
...
@@ -91,7 +84,18 @@ public void AddParameter(IDbCommand command, string name)
...
@@ -91,7 +84,18 @@ public void AddParameter(IDbCommand command, string name)
var
sqlCommand
=
(
SqlCommand
)
command
;
var
sqlCommand
=
(
SqlCommand
)
command
;
sqlCommand
.
CommandType
=
CommandType
.
StoredProcedure
;
sqlCommand
.
CommandType
=
CommandType
.
StoredProcedure
;
var
number_list
=
CreateSqlDataRecordList
(
numbers
);
var
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.
var
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.
// Add the table parameter.
var
p
=
sqlCommand
.
Parameters
.
Add
(
name
,
SqlDbType
.
Structured
);
var
p
=
sqlCommand
.
Parameters
.
Add
(
name
,
SqlDbType
.
Structured
);
...
@@ -213,6 +217,7 @@ public void TestMassiveStrings()
...
@@ -213,6 +217,7 @@ public void TestMassiveStrings()
.
IsEqualTo
(
str
);
.
IsEqualTo
(
str
);
}
}
#if !COREFX
[
Fact
]
[
Fact
]
public
void
TestTVPWithAnonymousObject
()
public
void
TestTVPWithAnonymousObject
()
{
{
...
@@ -283,7 +288,18 @@ public new void AddParameters(IDbCommand command, SqlMapper.Identity identity)
...
@@ -283,7 +288,18 @@ public new void AddParameters(IDbCommand command, SqlMapper.Identity identity)
var
sqlCommand
=
(
SqlCommand
)
command
;
var
sqlCommand
=
(
SqlCommand
)
command
;
sqlCommand
.
CommandType
=
CommandType
.
StoredProcedure
;
sqlCommand
.
CommandType
=
CommandType
.
StoredProcedure
;
var
number_list
=
CreateSqlDataRecordList
(
numbers
);
var
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.
var
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.
// Add the table parameter.
var
p
=
sqlCommand
.
Parameters
.
Add
(
"ints"
,
SqlDbType
.
Structured
);
var
p
=
sqlCommand
.
Parameters
.
Add
(
"ints"
,
SqlDbType
.
Structured
);
...
@@ -327,83 +343,6 @@ public void TestTVPWithAdditionalParams()
...
@@ -327,83 +343,6 @@ public void TestTVPWithAdditionalParams()
}
}
}
}
[
Fact
]
public
void
TestSqlDataRecordListParametersWithAsTableValuedParameter
()
{
try
{
connection
.
Execute
(
"CREATE TYPE int_list_type AS TABLE (n int NOT NULL PRIMARY KEY)"
);
connection
.
Execute
(
"CREATE PROC get_ints @integers int_list_type READONLY AS select * from @integers"
);
var
records
=
CreateSqlDataRecordList
(
new
int
[]
{
1
,
2
,
3
});
var
nums
=
connection
.
Query
<
int
>(
"get_ints"
,
new
{
integers
=
records
.
AsTableValuedParameter
()
},
commandType
:
CommandType
.
StoredProcedure
).
ToList
();
nums
.
IsSequenceEqualTo
(
new
int
[]
{
1
,
2
,
3
});
nums
=
connection
.
Query
<
int
>(
"select * from @integers"
,
new
{
integers
=
records
.
AsTableValuedParameter
(
"int_list_type"
)
}).
ToList
();
nums
.
IsSequenceEqualTo
(
new
int
[]
{
1
,
2
,
3
});
try
{
connection
.
Query
<
int
>(
"select * from @integers"
,
new
{
integers
=
records
.
AsTableValuedParameter
()
}).
First
();
throw
new
InvalidOperationException
();
}
catch
(
Exception
ex
)
{
ex
.
Message
.
Equals
(
"The table type parameter 'ids' must have a valid type name."
);
}
}
finally
{
try
{
connection
.
Execute
(
"DROP PROC get_ints"
);
}
finally
{
connection
.
Execute
(
"DROP TYPE int_list_type"
);
}
}
}
[
Fact
]
public
void
TestSqlDataRecordListParametersWithTypeHandlers
()
{
try
{
connection
.
Execute
(
"CREATE TYPE int_list_type AS TABLE (n int NOT NULL PRIMARY KEY)"
);
connection
.
Execute
(
"CREATE PROC get_ints @integers int_list_type READONLY AS select * from @integers"
);
// Variable type has to be IEnumerable<SqlDataRecord> for TypeHandler to kick in.
IEnumerable
<
Microsoft
.
SqlServer
.
Server
.
SqlDataRecord
>
records
=
CreateSqlDataRecordList
(
new
int
[]
{
1
,
2
,
3
});
var
nums
=
connection
.
Query
<
int
>(
"get_ints"
,
new
{
integers
=
records
},
commandType
:
CommandType
.
StoredProcedure
).
ToList
();
nums
.
IsSequenceEqualTo
(
new
int
[]
{
1
,
2
,
3
});
try
{
connection
.
Query
<
int
>(
"select * from @integers"
,
new
{
integers
=
records
}).
First
();
throw
new
InvalidOperationException
();
}
catch
(
Exception
ex
)
{
ex
.
Message
.
Equals
(
"The table type parameter 'ids' must have a valid type name."
);
}
}
finally
{
try
{
connection
.
Execute
(
"DROP PROC get_ints"
);
}
finally
{
connection
.
Execute
(
"DROP TYPE int_list_type"
);
}
}
}
#if !COREFX
[
Fact
]
[
Fact
]
public
void
DataTableParameters
()
public
void
DataTableParameters
()
{
{
...
...
Dapper/Dapper.csproj
View file @
610d8f65
...
@@ -17,7 +17,7 @@
...
@@ -17,7 +17,7 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="System.Collections.Concurrent" Version="4.3.0" />
<PackageReference Include="System.Collections.Concurrent" Version="4.3.0" />
<PackageReference Include="System.Collections.NonGeneric" Version="4.3.0" />
<PackageReference Include="System.Collections.NonGeneric" Version="4.3.0" />
<PackageReference Include="System.Data.
SqlClient
" Version="4.3.0" />
<PackageReference Include="System.Data.
Common
" Version="4.3.0" />
<PackageReference Include="System.Dynamic.Runtime" Version="4.3.0" />
<PackageReference Include="System.Dynamic.Runtime" Version="4.3.0" />
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
...
...
Dapper/SqlDataRecordHandler.cs
View file @
610d8f65
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Data
;
using
System.Data
;
#if !COREFX
namespace
Dapper
namespace
Dapper
{
{
internal
sealed
class
SqlDataRecordHandler
:
SqlMapper
.
ITypeHandler
internal
sealed
class
SqlDataRecordHandler
:
SqlMapper
.
ITypeHandler
...
@@ -17,3 +18,4 @@ public void SetValue(IDbDataParameter parameter, object value)
...
@@ -17,3 +18,4 @@ public void SetValue(IDbDataParameter parameter, object value)
}
}
}
}
}
}
#
endif
\ No newline at end of file
Dapper/SqlDataRecordListTVPParameter.cs
View file @
610d8f65
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Data
;
using
System.Data
;
using
System.Reflection
;
#if !COREFX
namespace
Dapper
namespace
Dapper
{
{
/// <summary>
/// <summary>
...
@@ -22,6 +23,18 @@ public SqlDataRecordListTVPParameter(IEnumerable<Microsoft.SqlServer.Server.SqlD
...
@@ -22,6 +23,18 @@ public SqlDataRecordListTVPParameter(IEnumerable<Microsoft.SqlServer.Server.SqlD
this
.
typeName
=
typeName
;
this
.
typeName
=
typeName
;
}
}
private
static
readonly
Action
<
System
.
Data
.
SqlClient
.
SqlParameter
,
string
>
setTypeName
;
static
SqlDataRecordListTVPParameter
()
{
var
prop
=
typeof
(
System
.
Data
.
SqlClient
.
SqlParameter
).
GetProperty
(
nameof
(
System
.
Data
.
SqlClient
.
SqlParameter
.
TypeName
),
BindingFlags
.
Instance
|
BindingFlags
.
Public
);
if
(
prop
!=
null
&&
prop
.
PropertyType
==
typeof
(
string
)
&&
prop
.
CanWrite
)
{
setTypeName
=
(
Action
<
System
.
Data
.
SqlClient
.
SqlParameter
,
string
>)
Delegate
.
CreateDelegate
(
typeof
(
Action
<
System
.
Data
.
SqlClient
.
SqlParameter
,
string
>),
prop
.
GetSetMethod
());
}
}
void
SqlMapper
.
ICustomQueryParameter
.
AddParameter
(
IDbCommand
command
,
string
name
)
void
SqlMapper
.
ICustomQueryParameter
.
AddParameter
(
IDbCommand
command
,
string
name
)
{
{
var
param
=
command
.
CreateParameter
();
var
param
=
command
.
CreateParameter
();
...
@@ -41,3 +54,4 @@ internal static void Set(IDbDataParameter parameter, IEnumerable<Microsoft.SqlSe
...
@@ -41,3 +54,4 @@ internal static void Set(IDbDataParameter parameter, IEnumerable<Microsoft.SqlSe
}
}
}
}
}
}
#
endif
\ No newline at end of file
Dapper/SqlMapper.cs
View file @
610d8f65
...
@@ -219,22 +219,24 @@ private static void ResetTypeHandlers(bool clone)
...
@@ -219,22 +219,24 @@ private static void ResetTypeHandlers(bool clone)
typeHandlers
=
new
Dictionary
<
Type
,
ITypeHandler
>();
typeHandlers
=
new
Dictionary
<
Type
,
ITypeHandler
>();
#if !COREFX
#if !COREFX
AddTypeHandlerImpl
(
typeof
(
DataTable
),
new
DataTableHandler
(),
clone
);
AddTypeHandlerImpl
(
typeof
(
DataTable
),
new
DataTableHandler
(),
clone
);
#endif
try
try
{
{
AddSqlDataRecordsTypeHandler
(
clone
);
AddSqlDataRecordsTypeHandler
(
clone
);
}
}
catch
{
/* https://github.com/StackExchange/dapper-dot-net/issues/424 */
}
catch
{
/* https://github.com/StackExchange/dapper-dot-net/issues/424 */
}
#endif
AddTypeHandlerImpl
(
typeof
(
XmlDocument
),
new
XmlDocumentHandler
(),
clone
);
AddTypeHandlerImpl
(
typeof
(
XmlDocument
),
new
XmlDocumentHandler
(),
clone
);
AddTypeHandlerImpl
(
typeof
(
XDocument
),
new
XDocumentHandler
(),
clone
);
AddTypeHandlerImpl
(
typeof
(
XDocument
),
new
XDocumentHandler
(),
clone
);
AddTypeHandlerImpl
(
typeof
(
XElement
),
new
XElementHandler
(),
clone
);
AddTypeHandlerImpl
(
typeof
(
XElement
),
new
XElementHandler
(),
clone
);
}
}
#if !COREFX
[
MethodImpl
(
MethodImplOptions
.
NoInlining
)]
[
MethodImpl
(
MethodImplOptions
.
NoInlining
)]
private
static
void
AddSqlDataRecordsTypeHandler
(
bool
clone
)
private
static
void
AddSqlDataRecordsTypeHandler
(
bool
clone
)
{
{
AddTypeHandlerImpl
(
typeof
(
IEnumerable
<
Microsoft
.
SqlServer
.
Server
.
SqlDataRecord
>),
new
SqlDataRecordHandler
(),
clone
);
AddTypeHandlerImpl
(
typeof
(
IEnumerable
<
Microsoft
.
SqlServer
.
Server
.
SqlDataRecord
>),
new
SqlDataRecordHandler
(),
clone
);
}
}
#endif
/// <summary>
/// <summary>
/// Configure the specified type to be mapped to a given db-type.
/// Configure the specified type to be mapped to a given db-type.
...
@@ -3660,15 +3662,15 @@ public static void SetTypeName(this DataTable table, string typeName)
...
@@ -3660,15 +3662,15 @@ public static void SetTypeName(this DataTable table, string typeName)
/// <param name="table">The <see cref="DataTable"/> that has a type name associated with it.</param>
/// <param name="table">The <see cref="DataTable"/> that has a type name associated with it.</param>
public
static
string
GetTypeName
(
this
DataTable
table
)
=>
public
static
string
GetTypeName
(
this
DataTable
table
)
=>
table
?.
ExtendedProperties
[
DataTableTypeNameKey
]
as
string
;
table
?.
ExtendedProperties
[
DataTableTypeNameKey
]
as
string
;
#endif
/// <summary>
/// <summary>
/// Used to pass a IEnumerable<SqlDataRecord> as a
TableValuedParameter
.
/// Used to pass a IEnumerable<SqlDataRecord> as a
<see cref="TableValuedParameter"/>
.
/// </summary>
/// </summary>
/// <param name="list">The list of records to convert to TVPs.</param>
/// <param name="list">Th
h
e list of records to convert to TVPs.</param>
/// <param name="typeName">The sql parameter type name.</param>
/// <param name="typeName">The sql parameter type name.</param>
public
static
ICustomQueryParameter
AsTableValuedParameter
(
this
IEnumerable
<
Microsoft
.
SqlServer
.
Server
.
SqlDataRecord
>
list
,
string
typeName
=
null
)
=>
public
static
ICustomQueryParameter
AsTableValuedParameter
(
this
IEnumerable
<
Microsoft
.
SqlServer
.
Server
.
SqlDataRecord
>
list
,
string
typeName
=
null
)
=>
new
SqlDataRecordListTVPParameter
(
list
,
typeName
);
new
SqlDataRecordListTVPParameter
(
list
,
typeName
);
#endif
// one per thread
// one per thread
[
ThreadStatic
]
[
ThreadStatic
]
...
...
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