Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Dapper
提交
610d8f65
D
Dapper
项目概览
int
/
Dapper
11 个月 前同步成功
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Dapper
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
610d8f65
编写于
6月 15, 2017
作者:
N
Nick Craver
提交者:
GitHub
6月 15, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Revert "Enable SqlDataRecord TVPs on corefx. (#801)" (#803)
This reverts commit
e7ad0874
.
上级
e7ad0874
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
61 addition
and
104 deletion
+61
-104
Dapper.Tests/ParameterTests.cs
Dapper.Tests/ParameterTests.cs
+37
-98
Dapper/Dapper.csproj
Dapper/Dapper.csproj
+1
-1
Dapper/SqlDataRecordHandler.cs
Dapper/SqlDataRecordHandler.cs
+2
-0
Dapper/SqlDataRecordListTVPParameter.cs
Dapper/SqlDataRecordListTVPParameter.cs
+15
-1
Dapper/SqlMapper.cs
Dapper/SqlMapper.cs
+6
-4
未找到文件。
Dapper.Tests/ParameterTests.cs
浏览文件 @
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
浏览文件 @
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
浏览文件 @
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
浏览文件 @
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
浏览文件 @
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
]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录