mirror of
https://github.com/FalconWu2017/Falcon.StoredProcedureRunner.git
synced 2025-04-07 23:29:38 +08:00
Compare commits
No commits in common. "master" and "v1.3.0" have entirely different histories.
|
@ -1,11 +1,5 @@
|
||||||
# Falcon.StoredProcedureRunner
|
# Falcon.StoredProcedureRunner
|
||||||
按照ORM方式调用数据库存储过程。
|
按照ORM方式调用数据库存储过程。
|
||||||
|
|
||||||
**仓库 URL:**
|
**Nuget URL:**
|
||||||
https://github.com/FalconWu2017/Falcon.StoredProcedureRunner
|
|
||||||
|
|
||||||
<https://gitea.com/Falcon/Falcon.StoredProcedureRunner>
|
|
||||||
|
|
||||||
|
|
||||||
**包 URL:**
|
|
||||||
<https://www.nuget.org/packages/Falcon.StoredProcedureRunner/>
|
<https://www.nuget.org/packages/Falcon.StoredProcedureRunner/>
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
[*.cs]
|
|
||||||
|
|
||||||
# CS8618: 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
|
|
||||||
dotnet_diagnostic.CS8618.severity = none
|
|
||||||
|
|
||||||
# CS1591: 缺少对公共可见类型或成员的 XML 注释
|
|
||||||
dotnet_diagnostic.CS1591.severity = none
|
|
||||||
|
|
||||||
[*.{cs,vb}]
|
|
||||||
dotnet_style_qualification_for_property=true:suggestion
|
|
||||||
dotnet_style_qualification_for_method=false:suggestion
|
|
26
src/Faclon.StoredProcedureRunner.Example/Database/MyDb.cs
Normal file
26
src/Faclon.StoredProcedureRunner.Example/Database/MyDb.cs
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using Faclon.StoredProcedureRunner.Example.Database.sp;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Falcon.StoredProcedureRunner;
|
||||||
|
|
||||||
|
namespace Faclon.StoredProcedureRunner.Example.Database
|
||||||
|
{
|
||||||
|
public class MyDb:DbContext
|
||||||
|
{
|
||||||
|
public IRunner SpRunner { get; set; }
|
||||||
|
|
||||||
|
public MyDb(DbContextOptions options,IRunner spRunner) : base(options) {
|
||||||
|
this.SpRunner = spRunner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Sp1_Result> RunSp1(Sp1 data) {
|
||||||
|
return this.SpRunner.Run<Sp1,Sp1_Result>(this,data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int RunSp1No(Sp1 data) {
|
||||||
|
return this.SpRunner.Execute(this,data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
59
src/Faclon.StoredProcedureRunner.Example/Database/sp/Sp1.cs
Normal file
59
src/Faclon.StoredProcedureRunner.Example/Database/sp/Sp1.cs
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using Falcon.StoredProcedureRunner;
|
||||||
|
|
||||||
|
namespace Faclon.StoredProcedureRunner.Example.Database.sp
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 测试用存储过程
|
||||||
|
/// </summary>
|
||||||
|
[FalconSPReturnType(typeof(Sp1_Result))]
|
||||||
|
[FalconSPProcuderName("TestSp1")]
|
||||||
|
public class Sp1
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 整数1
|
||||||
|
/// </summary>
|
||||||
|
public int P1 { get; set; } = 1;
|
||||||
|
/// <summary>
|
||||||
|
/// 整数2
|
||||||
|
/// </summary>
|
||||||
|
public int P2 { get; set; } = 2;
|
||||||
|
/// <summary>
|
||||||
|
/// 字符串P3
|
||||||
|
/// </summary>
|
||||||
|
[FalconSPPrarmType(System.Data.SqlDbType.VarChar)]
|
||||||
|
public string P3 { get; set; } = "abcd";
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 存储过程执行结果
|
||||||
|
/// </summary>
|
||||||
|
public class Sp1_Result
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 结果行号
|
||||||
|
/// </summary>
|
||||||
|
public int Id { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 求和
|
||||||
|
/// </summary>
|
||||||
|
public int Jia { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 求差
|
||||||
|
/// </summary>
|
||||||
|
public int Jian { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 求乘积
|
||||||
|
/// </summary>
|
||||||
|
public int Chen { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 求除法
|
||||||
|
/// </summary>
|
||||||
|
public double Chu { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 字符串返回值
|
||||||
|
/// </summary>
|
||||||
|
public string s { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFrameworks>net5;netcoreapp3.1</TargetFrameworks>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Falcon.StoredProcedureRunner\Falcon.StoredProcedureRunner.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.0" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.1" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
34
src/Faclon.StoredProcedureRunner.Example/Program.cs
Normal file
34
src/Faclon.StoredProcedureRunner.Example/Program.cs
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
using System;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Linq;
|
||||||
|
using Faclon.StoredProcedureRunner.Example.Database;
|
||||||
|
using Falcon.StoredProcedureRunner;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Faclon.StoredProcedureRunner.Example
|
||||||
|
{
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args) {
|
||||||
|
IRunner runner = new Runner();
|
||||||
|
var conStr = "Server =.\\SQLSERVER2008R2;Database = test;User ID = sa;Password = 111";
|
||||||
|
var opb = new DbContextOptionsBuilder();
|
||||||
|
opb.UseSqlServer(conStr);
|
||||||
|
var db = new MyDb(opb.Options,runner);
|
||||||
|
Console.WriteLine("测试调用存储过程,获取返回值");
|
||||||
|
var r = db.RunSp1(new Database.sp.Sp1());
|
||||||
|
Console.WriteLine("返回记录数{0},应该为2",r.Count());
|
||||||
|
var fir = r.First();
|
||||||
|
Console.WriteLine("Id为{0},应该为1",fir.Id);
|
||||||
|
Console.WriteLine("Jia{0},应该为3",fir.Jia);
|
||||||
|
Console.WriteLine("Jian{0},应该为-1",fir.Jian);
|
||||||
|
Console.WriteLine("Chen{0},应该为2",fir.Chen);
|
||||||
|
Console.WriteLine("Chu{0},应该为0.5",fir.Chu);
|
||||||
|
Console.WriteLine("s{0},应该为abc1",fir.s);
|
||||||
|
|
||||||
|
Console.WriteLine("测试无返回值调用");
|
||||||
|
var r1 = db.RunSp1No(new Database.sp.Sp1());
|
||||||
|
Console.WriteLine("返回结果{0}",r1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,16 +1,11 @@
|
||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio Version 17
|
# Visual Studio Version 16
|
||||||
VisualStudioVersion = 17.0.31912.275
|
VisualStudioVersion = 16.0.30804.86
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Falcon.StoredProcedureRunner", "Falcon.StoredProcedureRunner\Falcon.StoredProcedureRunner.csproj", "{57A72FF8-1FA8-47A1-8F96-30740890C877}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Falcon.StoredProcedureRunner", "Falcon.StoredProcedureRunner\Falcon.StoredProcedureRunner.csproj", "{13D3139B-60C2-4785-ADCB-4F839BDEC3C4}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Falcon.StoredProcedureRunnerTests", "Falcon.StoredProcedureRunnerTests\Falcon.StoredProcedureRunnerTests.csproj", "{E872783D-5387-49BE-9845-7F755F29696E}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Faclon.StoredProcedureRunner.Example", "Faclon.StoredProcedureRunner.Example\Faclon.StoredProcedureRunner.Example.csproj", "{40610016-0C9C-462D-B352-49D6C33DA5FC}"
|
||||||
EndProject
|
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C43175B4-F376-4DC3-B46E-CCD1E6ADE67D}"
|
|
||||||
ProjectSection(SolutionItems) = preProject
|
|
||||||
.editorconfig = .editorconfig
|
|
||||||
EndProjectSection
|
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
@ -18,19 +13,19 @@ Global
|
||||||
Release|Any CPU = Release|Any CPU
|
Release|Any CPU = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
{57A72FF8-1FA8-47A1-8F96-30740890C877}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{13D3139B-60C2-4785-ADCB-4F839BDEC3C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{57A72FF8-1FA8-47A1-8F96-30740890C877}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{13D3139B-60C2-4785-ADCB-4F839BDEC3C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{57A72FF8-1FA8-47A1-8F96-30740890C877}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{13D3139B-60C2-4785-ADCB-4F839BDEC3C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{57A72FF8-1FA8-47A1-8F96-30740890C877}.Release|Any CPU.Build.0 = Release|Any CPU
|
{13D3139B-60C2-4785-ADCB-4F839BDEC3C4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{E872783D-5387-49BE-9845-7F755F29696E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{40610016-0C9C-462D-B352-49D6C33DA5FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{E872783D-5387-49BE-9845-7F755F29696E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{40610016-0C9C-462D-B352-49D6C33DA5FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{E872783D-5387-49BE-9845-7F755F29696E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{40610016-0C9C-462D-B352-49D6C33DA5FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{E872783D-5387-49BE-9845-7F755F29696E}.Release|Any CPU.Build.0 = Release|Any CPU
|
{40610016-0C9C-462D-B352-49D6C33DA5FC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {19EB0FAF-B61A-422D-90AC-A602956F6875}
|
SolutionGuid = {7ECD6C47-0547-4FB7-A481-829D28302EFD}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner.Attributes
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 表示调用时候忽略此属性
|
|
||||||
/// </summary>
|
|
||||||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
|
|
||||||
public class FalconSPIgnoreAttribute : Attribute
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,25 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Data;
|
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner.Attributes
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 定义存储过程参数方向。默认Input
|
|
||||||
/// </summary>
|
|
||||||
[AttributeUsage(AttributeTargets.Property,AllowMultiple =false,Inherited =false)]
|
|
||||||
public class FalconSPPrarmDirectionAttribute:Attribute
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 参数方向
|
|
||||||
/// </summary>
|
|
||||||
public ParameterDirection Direction { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 定义参数方向
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="direction"></param>
|
|
||||||
public FalconSPPrarmDirectionAttribute(ParameterDirection direction)
|
|
||||||
=> this.Direction = direction;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,39 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner.Attributes
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 定义存储过程参数类型
|
|
||||||
/// </summary>
|
|
||||||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
|
|
||||||
public class FalconSPPrarmTypeAttribute : Attribute
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 参数名
|
|
||||||
/// </summary>
|
|
||||||
public FalconSPDbType PType { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 数据长度
|
|
||||||
/// </summary>
|
|
||||||
public int? Size { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 定于存储过程参数名称
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="pDbType">参数类型.DbType</param>
|
|
||||||
/// <param name="size">类型长度</param>
|
|
||||||
public FalconSPPrarmTypeAttribute(FalconSPDbType pDbType, int size)
|
|
||||||
{
|
|
||||||
this.PType = pDbType;
|
|
||||||
this.Size = size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 定于存储过程参数名称
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="pDbType">参数类型.DbType</param>
|
|
||||||
public FalconSPPrarmTypeAttribute(FalconSPDbType pDbType) {
|
|
||||||
this.PType = pDbType;
|
|
||||||
this.Size = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,21 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner.Attributes
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 定义存储过程返回临时表名称。主要应用与oracle数据库通过临时表返回结果。
|
|
||||||
/// </summary>
|
|
||||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
|
|
||||||
public class FalconSPTempTableAttribute : Attribute
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 存储返回临时表名称
|
|
||||||
/// </summary>
|
|
||||||
public string TableName { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 定义存储返回临时表名称
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="name">临时表名称</param>
|
|
||||||
public FalconSPTempTableAttribute(string name) => TableName=name;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 不支持的数据库类型
|
|
||||||
/// </summary>
|
|
||||||
public class DatabaseNotSupportedException : Exception
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 实例化不支持的数据库对象异常
|
|
||||||
/// </summary>
|
|
||||||
public DatabaseNotSupportedException() : base("仅仅支持Oracle和SqlServer数据库")
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,9 +1,8 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net5.0</TargetFrameworks>
|
<TargetFrameworks>netstandard2.1;netstandard2.0;</TargetFrameworks>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Version>2.1.0</Version>
|
<Version>1.3.0</Version>
|
||||||
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
|
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
|
||||||
<PackageProjectUrl>https://github.com/FalconWu2017/Falcon.StoredProcedureRunner</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/FalconWu2017/Falcon.StoredProcedureRunner</PackageProjectUrl>
|
||||||
<RepositoryUrl>https://github.com/FalconWu2017/Falcon.StoredProcedureRunner</RepositoryUrl>
|
<RepositoryUrl>https://github.com/FalconWu2017/Falcon.StoredProcedureRunner</RepositoryUrl>
|
||||||
|
@ -12,12 +11,18 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.9" />
|
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.0" />
|
||||||
<PackageReference Include="Oracle.EntityFrameworkCore" Version="5.21.3" />
|
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.4" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||||
<None Include="..\.editorconfig" Link=".editorconfig" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.10" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.10" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.10" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.1" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -1,207 +0,0 @@
|
||||||
namespace Falcon.StoredProcedureRunner
|
|
||||||
{
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 定义数据库数据类型
|
|
||||||
/// </summary>
|
|
||||||
public enum FalconSPDbType
|
|
||||||
{
|
|
||||||
#pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Int64. A 64-bit signed integer.
|
|
||||||
SqlServerBigInt = 0,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Array of type System.Byte. A fixed-length stream of binary data ranging
|
|
||||||
// between 1 and 8,000 bytes.
|
|
||||||
SqlServerBinary = 1,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Boolean. An unsigned numeric value that can be 0, 1, or null.
|
|
||||||
SqlServerBit = 2,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.String. A fixed-length stream of non-Unicode characters ranging between
|
|
||||||
// 1 and 8,000 characters.
|
|
||||||
SqlServerChar = 3,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.DateTime. Date and time data ranging in value from January 1, 1753 to
|
|
||||||
// December 31, 9999 to an accuracy of 3.33 milliseconds.
|
|
||||||
SqlServerDateTime = 4,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Decimal. A fixed precision and scale numeric value between -10 38 -1 and
|
|
||||||
// 10 38 -1.
|
|
||||||
SqlServerDecimal = 5,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Double. A floating point number within the range of -1.79E +308 through
|
|
||||||
// 1.79E +308.
|
|
||||||
SqlServerFloat = 6,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Array of type System.Byte. A variable-length stream of binary data ranging
|
|
||||||
// from 0 to 2 31 -1 (or 2,147,483,647) bytes.
|
|
||||||
SqlServerImage = 7,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Int32. A 32-bit signed integer.
|
|
||||||
SqlServerInt = 8,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Decimal. A currency value ranging from -2 63 (or -9,223,372,036,854,775,808)
|
|
||||||
// to 2 63 -1 (or +9,223,372,036,854,775,807) with an accuracy to a ten-thousandth
|
|
||||||
// of a currency unit.
|
|
||||||
SqlServerMoney = 9,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.String. A fixed-length stream of Unicode characters ranging between 1
|
|
||||||
// and 4,000 characters.
|
|
||||||
SqlServerNChar = 10,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.String. A variable-length stream of Unicode data with a maximum length
|
|
||||||
// of 2 30 - 1 (or 1,073,741,823) characters.
|
|
||||||
SqlServerNText = 11,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.String. A variable-length stream of Unicode characters ranging between
|
|
||||||
// 1 and 4,000 characters. Implicit conversion fails if the string is greater than
|
|
||||||
// 4,000 characters. Explicitly set the object when working with strings longer
|
|
||||||
// than 4,000 characters. Use System.Data.SqlDbType.NVarChar when the database column
|
|
||||||
// is nvarchar(max).
|
|
||||||
SqlServerNVarChar = 12,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Single. A floating point number within the range of -3.40E +38 through
|
|
||||||
// 3.40E +38.
|
|
||||||
SqlServerReal = 13,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Guid. A globally unique identifier (or GUID).
|
|
||||||
SqlServerUniqueIdentifier = 14,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.DateTime. Date and time data ranging in value from January 1, 1900 to
|
|
||||||
// June 6, 2079 to an accuracy of one minute.
|
|
||||||
SqlServerSmallDateTime = 0xF,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Int16. A 16-bit signed integer.
|
|
||||||
SqlServerSmallInt = 0x10,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Decimal. A currency value ranging from -214,748.3648 to +214,748.3647
|
|
||||||
// with an accuracy to a ten-thousandth of a currency unit.
|
|
||||||
SqlServerSmallMoney = 17,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.String. A variable-length stream of non-Unicode data with a maximum length
|
|
||||||
// of 2 31 -1 (or 2,147,483,647) characters.
|
|
||||||
SqlServerText = 18,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Array of type System.Byte. Automatically generated binary numbers, which
|
|
||||||
// are guaranteed to be unique within a database. timestamp is used typically as
|
|
||||||
// a mechanism for version-stamping table rows. The storage size is 8 bytes.
|
|
||||||
SqlServerTimestamp = 19,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Byte. An 8-bit unsigned integer.
|
|
||||||
SqlServerTinyInt = 20,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Array of type System.Byte. A variable-length stream of binary data ranging
|
|
||||||
// between 1 and 8,000 bytes. Implicit conversion fails if the byte array is greater
|
|
||||||
// than 8,000 bytes. Explicitly set the object when working with byte arrays larger
|
|
||||||
// than 8,000 bytes.
|
|
||||||
SqlServerVarBinary = 21,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.String. A variable-length stream of non-Unicode characters ranging between
|
|
||||||
// 1 and 8,000 characters. Use System.Data.SqlDbType.VarChar when the database column
|
|
||||||
// is varchar(max).
|
|
||||||
SqlServerVarChar = 22,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// System.Object. A special data type that can contain numeric, string, binary,
|
|
||||||
// or date data as well as the SQL Server values Empty and Null, which is assumed
|
|
||||||
// if no other type is declared.
|
|
||||||
SqlServerVariant = 23,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// An XML value. Obtain the XML as a string using the System.Data.SqlClient.SqlDataReader.GetValue(System.Int32)
|
|
||||||
// method or System.Data.SqlTypes.SqlXml.Value property, or as an System.Xml.XmlReader
|
|
||||||
// by calling the System.Data.SqlTypes.SqlXml.CreateReader method.
|
|
||||||
SqlServerXml = 25,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// A SQL Server user-defined type (UDT).
|
|
||||||
SqlServerUdt = 29,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// A special data type for specifying structured data contained in table-valued
|
|
||||||
// parameters.
|
|
||||||
SqlServerStructured = 30,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// Date data ranging in value from January 1,1 AD through December 31, 9999 AD.
|
|
||||||
SqlServerDate = 0x1F,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// Time data based on a 24-hour clock. Time value range is 00:00:00 through 23:59:59.9999999
|
|
||||||
// with an accuracy of 100 nanoseconds. Corresponds to a SQL Server time value.
|
|
||||||
SqlServerTime = 0x20,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// Date and time data. Date value range is from January 1,1 AD through December
|
|
||||||
// 31, 9999 AD. Time value range is 00:00:00 through 23:59:59.9999999 with an accuracy
|
|
||||||
// of 100 nanoseconds.
|
|
||||||
SqlServerDateTime2 = 33,
|
|
||||||
//
|
|
||||||
// 摘要:
|
|
||||||
// Date and time data with time zone awareness. Date value range is from January
|
|
||||||
// 1,1 AD through December 31, 9999 AD. Time value range is 00:00:00 through 23:59:59.9999999
|
|
||||||
// with an accuracy of 100 nanoseconds. Time zone value range is -14:00 through
|
|
||||||
// +14:00.
|
|
||||||
SqlServerDateTimeOffset = 34,
|
|
||||||
|
|
||||||
OracleBFile = 101,
|
|
||||||
OracleBlob = 102,
|
|
||||||
OracleByte = 103,
|
|
||||||
OracleChar = 104,
|
|
||||||
OracleClob = 105,
|
|
||||||
OracleDate = 106,
|
|
||||||
OracleDecimal = 107,
|
|
||||||
OracleDouble = 108,
|
|
||||||
OracleLong = 109,
|
|
||||||
OracleLongRaw = 110,
|
|
||||||
OracleInt16 = 111,
|
|
||||||
OracleInt32 = 112,
|
|
||||||
OracleInt64 = 113,
|
|
||||||
OracleIntervalDS = 114,
|
|
||||||
OracleIntervalYM = 115,
|
|
||||||
OracleNClob = 116,
|
|
||||||
OracleNChar = 117,
|
|
||||||
OracleNVarchar2 = 119,
|
|
||||||
OracleRaw = 120,
|
|
||||||
OracleRefCursor = 121,
|
|
||||||
OracleSingle = 122,
|
|
||||||
OracleTimeStamp = 123,
|
|
||||||
OracleTimeStampLTZ = 124,
|
|
||||||
OracleTimeStampTZ = 125,
|
|
||||||
OracleVarchar2 = 126,
|
|
||||||
OracleXmlType = 0x7F,
|
|
||||||
OracleArray = 0x80,
|
|
||||||
OracleObject = 129,
|
|
||||||
OracleRef = 130,
|
|
||||||
OracleBinaryDouble = 132,
|
|
||||||
OracleBinaryFloat = 133,
|
|
||||||
OracleBoolean = 134,
|
|
||||||
OracleJson = 135
|
|
||||||
|
|
||||||
#pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释
|
|
||||||
}
|
|
||||||
}
|
|
12
src/Falcon.StoredProcedureRunner/FalconSPIgnoreAttribute.cs
Normal file
12
src/Falcon.StoredProcedureRunner/FalconSPIgnoreAttribute.cs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Falcon.StoredProcedureRunner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 表示调用时候忽略此属性
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Property)]
|
||||||
|
public class FalconSPIgnoreAttribute:Attribute
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,11 +1,10 @@
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner.Attributes
|
namespace Falcon.StoredProcedureRunner
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 定义名称
|
/// 定义名称
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
|
|
||||||
public class FalconSPPrarmNameAttribute:Attribute
|
public class FalconSPPrarmNameAttribute:Attribute
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
|
@ -0,0 +1,21 @@
|
||||||
|
using System;
|
||||||
|
using System.Data;
|
||||||
|
|
||||||
|
namespace Falcon.StoredProcedureRunner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 定义存储过程参数类型
|
||||||
|
/// </summary>
|
||||||
|
public class FalconSPPrarmTypeAttribute:Attribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 参数名
|
||||||
|
/// </summary>
|
||||||
|
public SqlDbType PType { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 定于存储过程参数名称
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="pType">参数类型</param>
|
||||||
|
public FalconSPPrarmTypeAttribute(SqlDbType pType) { this.PType = pType; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner.Attributes
|
namespace Falcon.StoredProcedureRunner
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 定义存储过程名称
|
/// 定义存储过程名称
|
|
@ -1,12 +1,12 @@
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner.Attributes
|
namespace Falcon.StoredProcedureRunner
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 定义存储过程返回值类型
|
/// 定义存储过程返回值类型
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
|
[AttributeUsage(AttributeTargets.Class,AllowMultiple = false,Inherited = false)]
|
||||||
public class FalconSPReturnTypeAttribute : Attribute
|
public class FalconSPReturnTypeAttribute:Attribute
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 返回的数据类型
|
/// 返回的数据类型
|
|
@ -1,7 +0,0 @@
|
||||||
namespace Falcon.StoredProcedureRunner
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 面向Oracle的执行器
|
|
||||||
/// </summary>
|
|
||||||
public interface IOracleRunner : IRunner { }
|
|
||||||
}
|
|
|
@ -15,7 +15,7 @@ namespace Falcon.StoredProcedureRunner
|
||||||
/// <typeparam name="TPrarmType">参数类型</typeparam>
|
/// <typeparam name="TPrarmType">参数类型</typeparam>
|
||||||
/// <param name="db">数据上下文</param>
|
/// <param name="db">数据上下文</param>
|
||||||
/// <param name="data">参数数据</param>
|
/// <param name="data">参数数据</param>
|
||||||
int Execute<TPrarmType>(DbContext db, TPrarmType data);
|
int Execute<TPrarmType>(DbContext db,TPrarmType data);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 通过数据库上下文执行存储过程,并返回查询结果
|
/// 通过数据库上下文执行存储过程,并返回查询结果
|
||||||
|
@ -25,14 +25,6 @@ namespace Falcon.StoredProcedureRunner
|
||||||
/// <param name="returnType">返回结果项类型</param>
|
/// <param name="returnType">返回结果项类型</param>
|
||||||
/// <param name="data">执行参数</param>
|
/// <param name="data">执行参数</param>
|
||||||
/// <returns>查询结果枚举</returns>
|
/// <returns>查询结果枚举</returns>
|
||||||
IEnumerable<object> Run(DbContext db, Type prarmType, Type returnType, object data);
|
IEnumerable<object> Run(DbContext db,Type prarmType,Type returnType ,object data) ;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// (存在sql注入风险)执行Sql语句,并将数据库返回结果以json数据对象返回。
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="db">数据上下文</param>
|
|
||||||
/// <param name="sql">要执行的sql语句</param>
|
|
||||||
/// <returns>数据库返回值json格式</returns>
|
|
||||||
string RunRaw(DbContext db, string sql);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Falcon.StoredProcedureRunner.Attributes;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner
|
namespace Falcon.StoredProcedureRunner
|
||||||
|
@ -46,8 +45,8 @@ namespace Falcon.StoredProcedureRunner
|
||||||
return runner.Run(db,typeof(TPrarmType),typeof(TReturnType),data).Cast<TReturnType>();
|
return runner.Run(db,typeof(TPrarmType),typeof(TReturnType),data).Cast<TReturnType>();
|
||||||
} catch(InvalidCastException ice) {
|
} catch(InvalidCastException ice) {
|
||||||
throw new ReturnTypeCastException(ice);
|
throw new ReturnTypeCastException(ice);
|
||||||
} catch(Exception) {
|
} catch(Exception ex) {
|
||||||
throw;
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner
|
namespace Falcon.StoredProcedureRunner
|
||||||
{
|
{
|
||||||
|
@ -13,8 +12,7 @@ namespace Falcon.StoredProcedureRunner
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="services">注册服务集合</param>
|
/// <param name="services">注册服务集合</param>
|
||||||
/// <returns>服务集合</returns>
|
/// <returns>服务集合</returns>
|
||||||
public static IServiceCollection UseFalconSPRunner(this IServiceCollection services)
|
public static IServiceCollection UseFalconSPRunner(this IServiceCollection services) {
|
||||||
{
|
|
||||||
return services.AddFalconSPRunner();
|
return services.AddFalconSPRunner();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,12 +21,8 @@ namespace Falcon.StoredProcedureRunner
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="services">注册服务集合</param>
|
/// <param name="services">注册服务集合</param>
|
||||||
/// <returns>服务集合</returns>
|
/// <returns>服务集合</returns>
|
||||||
public static IServiceCollection AddFalconSPRunner(this IServiceCollection services)
|
public static IServiceCollection AddFalconSPRunner(this IServiceCollection services) {
|
||||||
{
|
return services.AddSingleton<IRunner,Runner>();
|
||||||
services.AddSingleton<IRunner, Runner>();
|
|
||||||
services.AddSingleton<IOracleRunner, OracleRunner>();
|
|
||||||
services.AddSingleton<ISqlServerRunner, SqlServerRunner>();
|
|
||||||
return services;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
namespace Falcon.StoredProcedureRunner
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 面向SqlServer的执行器
|
|
||||||
/// </summary>
|
|
||||||
public interface ISqlServerRunner : IRunner { }
|
|
||||||
}
|
|
|
@ -1,124 +0,0 @@
|
||||||
using Microsoft.Data.SqlClient;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using Oracle.ManagedDataAccess.Client;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Data;
|
|
||||||
using System.Data.Common;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Transactions;
|
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 面向Oracle的执行器
|
|
||||||
/// </summary>
|
|
||||||
public class OracleRunner : RunnerBase, IOracleRunner
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 执行参数
|
|
||||||
/// </summary>
|
|
||||||
public OracleRunnerOptions Options { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 通过提供构造选项构造Oracle执行器
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="option">选项</param>
|
|
||||||
public OracleRunner(IOptions<OracleRunnerOptions> option = null)
|
|
||||||
{
|
|
||||||
this.Options = option?.Value ?? new OracleRunnerOptions();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 创建存储过程参数
|
|
||||||
/// </summary>
|
|
||||||
protected override IDataParameter CreateDbParameter(string name, object val, int? type, int? size)
|
|
||||||
{
|
|
||||||
DbParameter par =
|
|
||||||
type.HasValue && size.HasValue ? new OracleParameter(name, (OracleDbType)type, size.Value) :
|
|
||||||
type.HasValue && !size.HasValue ? new OracleParameter(name, (OracleDbType)type) :
|
|
||||||
!type.HasValue && !size.HasValue ? new OracleParameter(name, val) :
|
|
||||||
null;
|
|
||||||
par.Value = val;
|
|
||||||
return par as IDataParameter;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 表是否存在
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="db">数据库上下文</param>
|
|
||||||
/// <param name="tableName">表名称</param>
|
|
||||||
/// <returns>存在True,不存在False</returns>
|
|
||||||
protected override bool IsTableExists(DbContext db, string tableName)
|
|
||||||
{
|
|
||||||
throw new System.NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取Oracle数据库链接
|
|
||||||
/// </summary>
|
|
||||||
protected override DbConnection GetDbConnection(DbContext context)
|
|
||||||
{
|
|
||||||
return context.Database.GetDbConnection() as OracleConnection;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取数据库前缀
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>前缀</returns>
|
|
||||||
protected override string GetParamProfix()
|
|
||||||
{
|
|
||||||
return ":";
|
|
||||||
}
|
|
||||||
|
|
||||||
public override IEnumerable<object> Run(DbContext db, Type prarmType, Type returnType, object data)
|
|
||||||
{
|
|
||||||
return this.Options.ReturnModel switch
|
|
||||||
{
|
|
||||||
ReturnModel.Return => RunByReturn(db, prarmType, returnType, data),
|
|
||||||
ReturnModel.TempTable => RunByTempTable(db, prarmType, returnType, data),
|
|
||||||
_ => throw new NotSupportedException(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
protected IEnumerable<object> RunByReturn(DbContext db, Type prarmType, Type returnType, object data)
|
|
||||||
{
|
|
||||||
var pm = GetProcuderName(prarmType);
|
|
||||||
var paras = getParams(prarmType, data).ToArray();
|
|
||||||
DataSet ds = new();
|
|
||||||
var conn = db.Database.GetDbConnection() as OracleConnection;
|
|
||||||
using var cmd = conn.CreateCommand();
|
|
||||||
cmd.CommandType = CommandType.StoredProcedure;
|
|
||||||
cmd.CommandText = pm;
|
|
||||||
cmd.Parameters.AddRange(paras);
|
|
||||||
var da = new OracleDataAdapter(cmd);
|
|
||||||
da.Fill(ds);
|
|
||||||
cmd.Parameters.Clear();
|
|
||||||
return DataSetToIEnumerable(ds, returnType);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected IEnumerable<object> RunByTempTable(DbContext db, Type prarmType, Type returnType, object data)
|
|
||||||
{
|
|
||||||
var pm = GetProcuderName(prarmType);
|
|
||||||
var paras = getParams(prarmType, data).ToArray();
|
|
||||||
var tempTable = GetTempTableName(returnType);
|
|
||||||
using TransactionScope tran = new(TransactionScopeOption.Required);
|
|
||||||
using var cmd = db.Database.GetDbConnection().CreateCommand() as OracleCommand;
|
|
||||||
cmd.CommandType = CommandType.StoredProcedure;
|
|
||||||
cmd.CommandText = pm;
|
|
||||||
cmd.Parameters.AddRange(paras);
|
|
||||||
//cmd.BindByName = true;
|
|
||||||
db.Database.GetDbConnection().Open();
|
|
||||||
cmd.ExecuteNonQuery();
|
|
||||||
cmd.Parameters.Clear();
|
|
||||||
cmd.CommandText= $"select * from {tempTable}";
|
|
||||||
cmd.CommandType = CommandType.Text;
|
|
||||||
var da = new OracleDataAdapter(cmd);
|
|
||||||
DataSet ds = new();
|
|
||||||
da.Fill(ds);
|
|
||||||
tran.Complete();
|
|
||||||
return DataSetToIEnumerable(ds, returnType);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
namespace Falcon.StoredProcedureRunner
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Oracle执行配置
|
|
||||||
/// </summary>
|
|
||||||
public class OracleRunnerOptions
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 获取数据返回方式 DbReader 1或TempTable 2
|
|
||||||
/// </summary>
|
|
||||||
public ReturnModel ReturnModel { get; set; } = ReturnModel.TempTable;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -6,7 +6,7 @@ namespace Falcon.StoredProcedureRunner
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设置返回对象值时候发生异常
|
/// 设置返回对象值时候发生异常
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ReturnDataSetValueException : Exception
|
public class ReturnDataSetValueException:Exception
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 实例化一个给返回数据设置值的异常
|
/// 实例化一个给返回数据设置值的异常
|
||||||
|
@ -16,7 +16,7 @@ namespace Falcon.StoredProcedureRunner
|
||||||
/// <param name="pi">要设置值的属性</param>
|
/// <param name="pi">要设置值的属性</param>
|
||||||
/// <param name="value">要设置的值</param>
|
/// <param name="value">要设置的值</param>
|
||||||
/// <param name="innException">内部异常</param>
|
/// <param name="innException">内部异常</param>
|
||||||
public ReturnDataSetValueException(int rowId, string cName, PropertyInfo pi, object value, Exception innException)
|
public ReturnDataSetValueException(int rowId,string cName, PropertyInfo pi,object value,Exception innException)
|
||||||
: base($"存储过程返回第[{rowId}]行[{cName}]列数据值为({value.GetType().FullName})[{value}]无法赋给属性({pi.PropertyType.FullName})[{pi.Name}]。", innException) { }
|
: base($"存储过程返回第[{rowId}]行[{cName}]列数据值为({value.GetType().FullName})[{value}]无法赋给属性({pi.PropertyType.FullName})[{pi.Name}]。",innException) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,17 +0,0 @@
|
||||||
namespace Falcon.StoredProcedureRunner
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 获取返回值方式
|
|
||||||
/// </summary>
|
|
||||||
public enum ReturnModel
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 通过存储过程返回值获取数据
|
|
||||||
/// </summary>
|
|
||||||
Return = 1,
|
|
||||||
/// <summary>
|
|
||||||
/// 通过临时表表获取数据
|
|
||||||
/// </summary>
|
|
||||||
TempTable = 2
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,54 +1,187 @@
|
||||||
using Microsoft.EntityFrameworkCore;
|
using System;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using Microsoft.Data.SqlClient;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner
|
namespace Falcon.StoredProcedureRunner
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 通用存储过程执行器
|
/// 存储过程执行
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Runner : IRunner
|
public partial class Runner:IRunner
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 注册的所有服务
|
/// 执行无返回值的存储过程
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IServiceProvider Service { get; }
|
/// <typeparam name="TPrarmType">参数类型</typeparam>
|
||||||
|
/// <param name="db">数据上下文</param>
|
||||||
|
/// <param name="data">参数数据</param>
|
||||||
|
public int Execute<TPrarmType>(DbContext db,TPrarmType data) {
|
||||||
|
var parms = getParams(typeof(TPrarmType),data).ToArray();
|
||||||
|
var pName = getProcuderName<TPrarmType>();
|
||||||
|
var paramStr = getParamStr(typeof(TPrarmType),data);
|
||||||
|
var str = $"exec {pName} {paramStr}";
|
||||||
|
return db.Database.ExecuteSqlRaw(str,parms);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 通过注册的服务实例化执行器
|
/// 通过数据库上下文执行存储过程,并返回查询结果
|
||||||
/// </summary>
|
|
||||||
/// <param name="service">服务集合</param>
|
|
||||||
public Runner(IServiceProvider service)
|
|
||||||
{
|
|
||||||
Service = service;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Execute<TPrarmType>(DbContext db, TPrarmType data)
|
|
||||||
{
|
|
||||||
return GetRunner(db).Execute<TPrarmType>(db, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<object> Run(DbContext db, Type prarmType, Type returnType, object data)
|
|
||||||
{
|
|
||||||
return GetRunner(db).Run(db, prarmType, returnType, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public string RunRaw(DbContext db, string sql)
|
|
||||||
{
|
|
||||||
return GetRunner(db).RunRaw(db, sql);
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// 获取特定类型的数据库runnner
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="db">数据库上下文</param>
|
/// <param name="db">数据库上下文</param>
|
||||||
/// <returns>特定类型的执行器</returns>
|
/// <param name="prarmType">参数类型</param>
|
||||||
/// <exception cref="DatabaseNotSupportedException"></exception>
|
/// <param name="returnType">返回值类型</param>
|
||||||
protected IRunner GetRunner(DbContext db)
|
/// <param name="data">存储过程参数</param>
|
||||||
{
|
/// <returns>查询结果枚举</returns>
|
||||||
return
|
public IEnumerable<object> Run(DbContext db,Type prarmType,Type returnType,object data) {
|
||||||
db.Database.IsOracle() ? Service.GetService<IOracleRunner>() :
|
var pm = getProcuderName(prarmType);
|
||||||
db.Database.IsSqlServer() ? Service.GetService<IOracleRunner>() :
|
var paras = getParams(prarmType,data).ToArray();
|
||||||
throw new DatabaseNotSupportedException();
|
var connection = db.Database.GetDbConnection();
|
||||||
|
using(var cmd = connection.CreateCommand()) {
|
||||||
|
cmd.CommandText = pm;
|
||||||
|
cmd.CommandType = CommandType.StoredProcedure;
|
||||||
|
cmd.Parameters.AddRange(paras);
|
||||||
|
connection.Open();
|
||||||
|
var dr = cmd.ExecuteReader();
|
||||||
|
var result = new List<object>();
|
||||||
|
if(!dr.CanGetColumnSchema())
|
||||||
|
return result;
|
||||||
|
int rowId = 0;
|
||||||
|
while(dr.Read()) {
|
||||||
|
var item = returnType.Assembly.CreateInstance(returnType.FullName);
|
||||||
|
var columnSchema = dr.GetColumnSchema();
|
||||||
|
for(var i = 0;i < columnSchema.Count;i++) {
|
||||||
|
var name = dr.GetName(i);
|
||||||
|
var value = dr.IsDBNull(i) ? null : dr.GetValue(i);
|
||||||
|
var pi = getProperty(returnType,name);
|
||||||
|
if(pi == null || !pi.CanWrite)
|
||||||
|
continue;
|
||||||
|
try {
|
||||||
|
pi.SetValue(item,value);
|
||||||
|
} catch(Exception ex) {
|
||||||
|
throw new ReturnDataSetValueException(rowId,name,pi,value,ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
result.Add(item);
|
||||||
|
rowId++;
|
||||||
|
}
|
||||||
|
connection.Close();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class Runner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取存储过程名
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="pramType">参数类型</param>
|
||||||
|
private static string getProcuderName(Type pramType) {
|
||||||
|
var attr = pramType.GetCustomAttribute<FalconSPProcuderNameAttribute>();
|
||||||
|
if(attr != null && attr is FalconSPProcuderNameAttribute pna && !string.IsNullOrEmpty(pna.ProcuderName)) {
|
||||||
|
return pna.ProcuderName;
|
||||||
|
}
|
||||||
|
return pramType.Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取存储过程参数枚举
|
||||||
|
/// </summary>
|
||||||
|
///<param name="type">数据的类型</param>
|
||||||
|
/// <param name="data">参数实例</param>
|
||||||
|
private static IEnumerable<SqlParameter> getParams(Type type,object data) {
|
||||||
|
if(data == null)
|
||||||
|
yield break;
|
||||||
|
foreach(var p in type.GetProperties()) {
|
||||||
|
if(!p.CanRead || ignoreProp(p))
|
||||||
|
continue;
|
||||||
|
var pt = getPrarmType(p);
|
||||||
|
if(pt.HasValue) {
|
||||||
|
var np = new SqlParameter($"@{getPrarmName(p)}",pt.Value);
|
||||||
|
np.Value = p.GetValue(data);
|
||||||
|
yield return np;
|
||||||
|
} else {
|
||||||
|
yield return new SqlParameter($"@{getPrarmName(p)}",p.GetValue(data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取存储过程参数类型
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="p">对应的属性</param>
|
||||||
|
private static SqlDbType? getPrarmType(PropertyInfo p) {
|
||||||
|
var np = p.GetCustomAttribute<FalconSPPrarmTypeAttribute>(true);
|
||||||
|
if(np != null && np is FalconSPPrarmTypeAttribute na) {
|
||||||
|
return na.PType;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否忽略属性
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="p">要检查的属性</param>
|
||||||
|
private static bool ignoreProp(PropertyInfo p) {
|
||||||
|
return p.GetCustomAttribute<FalconSPIgnoreAttribute>(true) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取存储过程参数名称
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="p">对应的属性</param>
|
||||||
|
private static string getPrarmName(PropertyInfo p) {
|
||||||
|
var np = p.GetCustomAttribute<FalconSPPrarmNameAttribute>(true);
|
||||||
|
if(np != null && np is FalconSPPrarmNameAttribute na) {
|
||||||
|
return na.Name;
|
||||||
|
}
|
||||||
|
return p.Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取存储过程名
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">参数模型</typeparam>
|
||||||
|
private static string getProcuderName<T>() {
|
||||||
|
var attr = typeof(T).GetCustomAttribute<FalconSPProcuderNameAttribute>(true);
|
||||||
|
if(attr != null && attr is FalconSPProcuderNameAttribute pna && !string.IsNullOrEmpty(pna.ProcuderName)) {
|
||||||
|
return pna.ProcuderName;
|
||||||
|
}
|
||||||
|
return typeof(T).Name;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 忽略大小写获取类型的属性
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="t">类型</param>
|
||||||
|
/// <param name="name">属性名称</param>
|
||||||
|
/// <returns>属性</returns>
|
||||||
|
private static PropertyInfo getProperty(Type t,string name) {
|
||||||
|
foreach(var item in t.GetProperties()) {
|
||||||
|
if(item.Name.ToLower() == name.ToLower()) {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 生成存储过程参数字符串
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type">参数类型</param>
|
||||||
|
/// <param name="data">参数对象</param>
|
||||||
|
/// <returns>一个参数字符串。比如@p2=@p4,@p1=@p3</returns>
|
||||||
|
private static string getParamStr(Type type,object data) {
|
||||||
|
var paras = getParams(type,data).ToArray();
|
||||||
|
var result = " ";
|
||||||
|
for(int i = 0;i < paras.Count();i++) {
|
||||||
|
result += $"{paras[i].ParameterName}={{{i}}},";
|
||||||
|
}
|
||||||
|
return result.TrimEnd(',');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,314 +0,0 @@
|
||||||
using Falcon.StoredProcedureRunner.Attributes;
|
|
||||||
using Microsoft.Data.SqlClient;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Oracle.ManagedDataAccess.Client;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Data;
|
|
||||||
using System.Data.Common;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 存储过程执行
|
|
||||||
/// </summary>
|
|
||||||
public abstract class RunnerBase : IRunner
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 执行无返回值的存储过程
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TPrarmType">参数类型</typeparam>
|
|
||||||
/// <param name="db">数据上下文</param>
|
|
||||||
/// <param name="data">参数数据</param>
|
|
||||||
public int Execute<TPrarmType>(DbContext db, TPrarmType data)
|
|
||||||
{
|
|
||||||
throw new NotSupportedException();
|
|
||||||
//var parms = getParams(db, typeof(TPrarmType), data).ToArray();
|
|
||||||
//var pName = getProcuderName<TPrarmType>();
|
|
||||||
//var conn = db.Database.GetDbConnection();
|
|
||||||
|
|
||||||
|
|
||||||
//var paramStr = getParamStr(typeof(TPrarmType), data);
|
|
||||||
//var str = $"exec {pName} {paramStr}";
|
|
||||||
//return db.Database.ExecuteSqlRaw(str, parms);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 通过数据库上下文执行存储过程,并返回查询结果
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="db">数据库上下文</param>
|
|
||||||
/// <param name="prarmType">参数类型</param>
|
|
||||||
/// <param name="returnType">返回值类型</param>
|
|
||||||
/// <param name="data">存储过程参数</param>
|
|
||||||
/// <returns>查询结果枚举</returns>
|
|
||||||
public abstract IEnumerable<object> Run(DbContext db, Type prarmType, Type returnType, object data);
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 通过数据库上下文执行存储过程,并返回查询结果
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="db">数据库上下文</param>
|
|
||||||
/// <param name="prarmType">参数类型</param>
|
|
||||||
/// <param name="returnType">返回值类型</param>
|
|
||||||
/// <param name="data">存储过程参数</param>
|
|
||||||
/// <returns>查询结果枚举</returns>
|
|
||||||
protected virtual IEnumerable<object> RunByReader(DbContext db, Type prarmType, Type returnType, object data)
|
|
||||||
{
|
|
||||||
var pm = GetProcuderName(prarmType);
|
|
||||||
var paras = getParams( prarmType, data).ToArray();
|
|
||||||
var conn = GetDbConnection(db);
|
|
||||||
using var cmd = conn.CreateCommand();
|
|
||||||
cmd.CommandText = pm;
|
|
||||||
cmd.CommandType = CommandType.StoredProcedure;
|
|
||||||
cmd.Parameters.AddRange(paras);
|
|
||||||
conn.Open();
|
|
||||||
using var dr = cmd.ExecuteReader();
|
|
||||||
cmd.Parameters.Clear();
|
|
||||||
var columnSchema = dr.GetColumnSchema();
|
|
||||||
var result = new List<object>();
|
|
||||||
if (!dr.CanGetColumnSchema())
|
|
||||||
return result;
|
|
||||||
int rowId = 0;
|
|
||||||
while (dr.Read())
|
|
||||||
{
|
|
||||||
var item = returnType.Assembly.CreateInstance(returnType.FullName);
|
|
||||||
for (var i = 0; i < columnSchema.Count; i++)
|
|
||||||
{
|
|
||||||
var name = dr.GetName(i);
|
|
||||||
var value = dr.IsDBNull(i) ? null : dr.GetValue(i);
|
|
||||||
var pi = getProperty(returnType, name);
|
|
||||||
if (pi == null || !pi.CanWrite)
|
|
||||||
continue;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
pi.SetValue(item, value);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
throw new ReturnDataSetValueException(rowId, name, pi, value, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result.Add(item);
|
|
||||||
rowId++;
|
|
||||||
}
|
|
||||||
conn.Close();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取数据库链接
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="context">数据库上下文</param>
|
|
||||||
/// <returns>数据链接</returns>
|
|
||||||
protected abstract DbConnection GetDbConnection(DbContext context);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 将Dataset转换为IEnumerable
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="ds">数据集</param>
|
|
||||||
/// <param name="returnType">枚举元素类型</param>
|
|
||||||
/// <returns>枚举类型</returns>
|
|
||||||
/// <exception cref="ReturnDataSetValueException">数据赋值异常</exception>
|
|
||||||
public IEnumerable<object> DataSetToIEnumerable(DataSet ds, Type returnType)
|
|
||||||
{
|
|
||||||
var result = new List<object>();
|
|
||||||
var rows = ds.Tables[0].Rows;
|
|
||||||
var cols = ds.Tables[0].Columns;
|
|
||||||
for (int i = 0; i < rows.Count; i++)
|
|
||||||
{
|
|
||||||
var item = returnType.Assembly.CreateInstance(returnType.FullName);
|
|
||||||
var row = rows[i];
|
|
||||||
for (int y = 0; y < cols.Count; y++)
|
|
||||||
{
|
|
||||||
var name = cols[y].ColumnName;
|
|
||||||
var val = row[y] is DBNull ? null : row[y];
|
|
||||||
var pi = getProperty(returnType, name);
|
|
||||||
if (pi == null || !pi.CanWrite)
|
|
||||||
continue;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
pi.SetValue(item, val);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
throw new ReturnDataSetValueException(i, name, pi, val, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result.Add(item);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// (存在sql注入风险)执行Sql语句,并将数据库返回结果以json数据对象返回。
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="db">数据上下文</param>
|
|
||||||
/// <param name="sql">要执行的sql语句</param>
|
|
||||||
/// <returns>数据库返回值json格式</returns>
|
|
||||||
public string RunRaw(DbContext db, string sql)
|
|
||||||
{
|
|
||||||
var connection = db.Database.GetDbConnection();
|
|
||||||
using var cmd = connection.CreateCommand();
|
|
||||||
cmd.CommandText = sql;
|
|
||||||
cmd.CommandType = System.Data.CommandType.Text;
|
|
||||||
connection.Open();
|
|
||||||
var dr = cmd.ExecuteReader();
|
|
||||||
var result = new StringBuilder();
|
|
||||||
if (!dr.CanGetColumnSchema())
|
|
||||||
return "";
|
|
||||||
while (dr.Read())
|
|
||||||
{
|
|
||||||
var item = new StringBuilder();
|
|
||||||
var columnSchema = dr.GetColumnSchema();
|
|
||||||
for (var i = 0; i < columnSchema.Count; i++)
|
|
||||||
{
|
|
||||||
var name = dr.GetName(i);
|
|
||||||
var value = dr.IsDBNull(i) ? null : dr.GetValue(i);
|
|
||||||
item.Append($"\"{name}\":\"{value}\",");
|
|
||||||
}
|
|
||||||
result.Append($"{{{item.ToString().TrimEnd(',')}}},");
|
|
||||||
}
|
|
||||||
connection.Close();
|
|
||||||
return "[" + result.ToString().TrimEnd(',') + "]";
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取返回类型临时表名称
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="returnType">返回类型</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
protected virtual string GetTempTableName(Type returnType)
|
|
||||||
{
|
|
||||||
var attr = returnType.GetCustomAttribute<FalconSPTempTableAttribute>();
|
|
||||||
return attr?.TableName ?? returnType.Name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取存储过程名
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="pramType">参数类型</param>
|
|
||||||
protected virtual string GetProcuderName(Type pramType)
|
|
||||||
{
|
|
||||||
var attr = pramType.GetCustomAttribute<FalconSPProcuderNameAttribute>();
|
|
||||||
return attr?.ProcuderName ?? pramType.Name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取存储过程参数枚举
|
|
||||||
/// </summary>
|
|
||||||
///<param name="type">数据的类型</param>
|
|
||||||
/// <param name="data">参数实例</param>
|
|
||||||
protected virtual IEnumerable<IDataParameter> getParams(Type type, object data)
|
|
||||||
{
|
|
||||||
if (data == null)
|
|
||||||
yield break;
|
|
||||||
var fixC = GetParamProfix();
|
|
||||||
foreach (var p in type.GetProperties())
|
|
||||||
{
|
|
||||||
if (!p.CanRead || IsIgnoreProp(p))
|
|
||||||
continue;
|
|
||||||
var name = GetPrarmName(p, fixC);
|
|
||||||
var val = p.GetValue(data);
|
|
||||||
var pt = getPrarmType(p);
|
|
||||||
var para = CreateDbParameter(name, val, pt.type, pt.size);
|
|
||||||
para.Direction = GetParaDirection(p);
|
|
||||||
yield return para;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取数据库参数前缀
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>前缀字符串</returns>
|
|
||||||
protected abstract string GetParamProfix();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取存储过程参数类型
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="p">对应的属性</param>
|
|
||||||
private (int? type, int? size) getPrarmType(PropertyInfo p)
|
|
||||||
{
|
|
||||||
var np = p.GetCustomAttribute<FalconSPPrarmTypeAttribute>();
|
|
||||||
if (np != null && np is FalconSPPrarmTypeAttribute na)
|
|
||||||
{
|
|
||||||
return ((int)np.PType, np.Size);
|
|
||||||
}
|
|
||||||
return (null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 创建存储过程参数
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="name">参数名称</param>
|
|
||||||
/// <param name="val">参数值</param>
|
|
||||||
/// <param name="type">存储过程参数数据库类型</param>
|
|
||||||
/// <param name="size">数据长度</param>
|
|
||||||
/// <returns>参数</returns>
|
|
||||||
protected abstract IDataParameter CreateDbParameter(string name, object val, int? type, int? size);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取参数的方向。默认Input
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="p">参数映射属性</param>
|
|
||||||
/// <returns>方向或null</returns>
|
|
||||||
protected virtual ParameterDirection GetParaDirection(PropertyInfo p)
|
|
||||||
{
|
|
||||||
var attr = p.GetCustomAttribute<FalconSPPrarmDirectionAttribute>();
|
|
||||||
return attr == null ? ParameterDirection.Input : attr.Direction;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 是否忽略属性
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="p">要检查的属性</param>
|
|
||||||
protected virtual bool IsIgnoreProp(PropertyInfo p)
|
|
||||||
{
|
|
||||||
return p.GetCustomAttribute<FalconSPIgnoreAttribute>(true) != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取存储过程参数名称
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="p">对应的属性</param>
|
|
||||||
/// <param name="prefixChar">存储过程参数前缀</param>
|
|
||||||
protected virtual string GetPrarmName(PropertyInfo p, string prefixChar)
|
|
||||||
{
|
|
||||||
var np = p.GetCustomAttribute<FalconSPPrarmNameAttribute>(true);
|
|
||||||
if (np != null && np is FalconSPPrarmNameAttribute na)
|
|
||||||
{
|
|
||||||
var name = na.Name;
|
|
||||||
return name.StartsWith(prefixChar) ? name : prefixChar + name;
|
|
||||||
}
|
|
||||||
return prefixChar + p.Name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 忽略大小写获取类型的属性
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="t">类型</param>
|
|
||||||
/// <param name="name">属性名称</param>
|
|
||||||
/// <returns>属性</returns>
|
|
||||||
protected virtual PropertyInfo getProperty(Type t, string name)
|
|
||||||
{
|
|
||||||
foreach (var item in t.GetProperties())
|
|
||||||
{
|
|
||||||
if (item.Name.ToLower() == name.ToLower())
|
|
||||||
{
|
|
||||||
return item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 表是否存在
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="db">数据库上下文</param>
|
|
||||||
/// <param name="tableName">表名称</param>
|
|
||||||
/// <returns>存在True,不存在False</returns>
|
|
||||||
protected abstract bool IsTableExists(DbContext db, string tableName);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,90 +0,0 @@
|
||||||
using Microsoft.Data.SqlClient;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using Oracle.ManagedDataAccess.Client;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Data;
|
|
||||||
using System.Data.Common;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 面向SqlServer的执行器
|
|
||||||
/// </summary>
|
|
||||||
public class SqlServerRunner : RunnerBase, ISqlServerRunner
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 执行参数
|
|
||||||
/// </summary>
|
|
||||||
public SqlServerRunnerOtions Options { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 通过提供构造选项构造Oracle执行器
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="option">选项</param>
|
|
||||||
public SqlServerRunner(IOptions<SqlServerRunnerOtions> option = null)
|
|
||||||
{
|
|
||||||
this.Options = option?.Value ?? new SqlServerRunnerOtions();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 表是否存在
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="db">数据库上下文</param>
|
|
||||||
/// <param name="tableName">表名称</param>
|
|
||||||
/// <returns>存在True,不存在False</returns>
|
|
||||||
protected override bool IsTableExists(DbContext db, string tableName)
|
|
||||||
{
|
|
||||||
throw new System.NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override IEnumerable<object> Run(DbContext db, Type prarmType, Type returnType, object data)
|
|
||||||
{
|
|
||||||
return RunByReturn(db, prarmType, returnType, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected IEnumerable<object> RunByReturn(DbContext db, Type prarmType, Type returnType, object data)
|
|
||||||
{
|
|
||||||
var pm = GetProcuderName(prarmType);
|
|
||||||
var paras = getParams(prarmType, data).ToArray();
|
|
||||||
DbConnection connection = db.Database.GetDbConnection();
|
|
||||||
DataSet ds = new();
|
|
||||||
var conn = db.Database.GetDbConnection() as SqlConnection;
|
|
||||||
using var cmd = conn.CreateCommand();
|
|
||||||
cmd.CommandType = CommandType.StoredProcedure;
|
|
||||||
cmd.CommandText = pm;
|
|
||||||
cmd.Parameters.AddRange(paras);
|
|
||||||
var da = new SqlDataAdapter(cmd);
|
|
||||||
da.Fill(ds);
|
|
||||||
cmd.Parameters.Clear();
|
|
||||||
return DataSetToIEnumerable(ds, returnType);
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// 获取SqlServer数据库链接
|
|
||||||
/// </summary>
|
|
||||||
protected override DbConnection GetDbConnection(DbContext context)
|
|
||||||
{
|
|
||||||
return context.Database.GetDbConnection();
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// 获取存储过程参数前缀
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
protected override string GetParamProfix()
|
|
||||||
{
|
|
||||||
return "@";
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override IDataParameter CreateDbParameter(string name, object val, int? type, int? size)
|
|
||||||
{
|
|
||||||
DbParameter par =
|
|
||||||
type.HasValue && size.HasValue ? new SqlParameter(name, (SqlDbType)type, size.Value) :
|
|
||||||
type.HasValue && !size.HasValue ? new SqlParameter(name, (SqlDbType)type) :
|
|
||||||
!type.HasValue && !size.HasValue ? new SqlParameter(name, val) :
|
|
||||||
null;
|
|
||||||
par.Value = val;
|
|
||||||
return par as IDataParameter;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,10 +0,0 @@
|
||||||
namespace Falcon.StoredProcedureRunner
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Sqlserver执行配置
|
|
||||||
/// </summary>
|
|
||||||
public class SqlServerRunnerOtions
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
{
|
|
||||||
"db": { //存储在客户端机密中
|
|
||||||
"ver": "",
|
|
||||||
"oracle": "",
|
|
||||||
"sqlserver": ""
|
|
||||||
},
|
|
||||||
"OracleRunner": {
|
|
||||||
"ReturnModel": 2
|
|
||||||
},
|
|
||||||
"SqlRunner": {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,43 +0,0 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFrameworks>net5.0</TargetFrameworks>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
|
||||||
|
|
||||||
<UserSecretsId>44468cb2-b159-42bd-a799-b3f004be5f68</UserSecretsId>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="5.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
|
|
||||||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
|
|
||||||
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
|
|
||||||
<PackageReference Include="coverlet.collector" Version="3.1.0" />
|
|
||||||
|
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.9" />
|
|
||||||
<PackageReference Include="Oracle.EntityFrameworkCore" Version="5.21.3" />
|
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.4" />
|
|
||||||
|
|
||||||
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\Falcon.StoredProcedureRunner\Falcon.StoredProcedureRunner.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<None Update="AppSettings.json">
|
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
|
||||||
</None>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Falcon\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
|
@ -1,14 +0,0 @@
|
||||||
--Oracle测试用存储过程
|
|
||||||
create or replace procedure GetEmployee
|
|
||||||
(
|
|
||||||
v_orgaid IN NVARCHAR2 DEFAULT NULL
|
|
||||||
,v_data out sys_refcursor
|
|
||||||
)is
|
|
||||||
begin
|
|
||||||
open v_data for
|
|
||||||
select a.empcode,a.empname,a.empactive
|
|
||||||
from app_employee a
|
|
||||||
where a.orgaid=v_orgaid;
|
|
||||||
|
|
||||||
end;
|
|
||||||
/
|
|
|
@ -1,97 +0,0 @@
|
||||||
using Falcon.StoredProcedureRunner;
|
|
||||||
using Falcon.StoredProcedureRunner.Attributes;
|
|
||||||
using System.Data;
|
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunnerTests
|
|
||||||
{
|
|
||||||
internal class GetEmployee
|
|
||||||
{
|
|
||||||
public string v_orgaid { get; set; }
|
|
||||||
|
|
||||||
[FalconSPPrarmDirection(ParameterDirection.Output)]
|
|
||||||
[FalconSPPrarmType(FalconSPDbType.OracleRefCursor)]
|
|
||||||
public object v_data { get; set; }
|
|
||||||
}
|
|
||||||
internal class GetEmployeeResult
|
|
||||||
{
|
|
||||||
public string empcode { get; set; }
|
|
||||||
public string empname { get; set; }
|
|
||||||
public string empactive { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 体检接口 挂号查询
|
|
||||||
/// </summary>
|
|
||||||
public class Tjjk_Tjgh
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 医疗机构代码
|
|
||||||
/// </summary>
|
|
||||||
public string v_yljgdm { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 身份证号码
|
|
||||||
/// </summary>
|
|
||||||
public string v_sfz { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 挂号日期 格式yyyyMMdd
|
|
||||||
/// </summary>
|
|
||||||
public string v_ghrq { get; set; }
|
|
||||||
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// 挂号查询结果
|
|
||||||
/// </summary>
|
|
||||||
[FalconSPTempTable("TT_Tjjk_Tjgh")]
|
|
||||||
public class Tjjk_TjghResult
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 卡号
|
|
||||||
/// </summary>
|
|
||||||
public string kh { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 姓名
|
|
||||||
/// </summary>
|
|
||||||
public string xm { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 身份证号码
|
|
||||||
/// </summary>
|
|
||||||
public string sfzh { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 科室编号
|
|
||||||
/// </summary>
|
|
||||||
public string ksbh { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 科室名称
|
|
||||||
/// </summary>
|
|
||||||
public string ksmc { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 区县标志 本区 外区
|
|
||||||
/// </summary>
|
|
||||||
public string qxbz { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 挂号编号
|
|
||||||
/// </summary>
|
|
||||||
public string ghbh { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 挂号状态.已挂号 未挂号 已退费
|
|
||||||
/// </summary>
|
|
||||||
public string state { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
internal class GetDoctorData
|
|
||||||
{
|
|
||||||
public string v_orgaid { get; set; }
|
|
||||||
|
|
||||||
[FalconSPPrarmDirection(ParameterDirection.Output)]
|
|
||||||
[FalconSPPrarmType(FalconSPDbType.OracleRefCursor)]
|
|
||||||
public object v_data { get; set; }
|
|
||||||
}
|
|
||||||
internal class GetDoctorDataResult
|
|
||||||
{
|
|
||||||
public string Name_ { get; set; }
|
|
||||||
public string Dept_Name { get; set; }
|
|
||||||
public string Consulting_Room { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,229 +0,0 @@
|
||||||
using Falcon.StoredProcedureRunnerTests;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using System.Linq;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Oracle.ManagedDataAccess.Client;
|
|
||||||
using System.Data;
|
|
||||||
using System.ComponentModel.DataAnnotations;
|
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunner.Tests
|
|
||||||
{
|
|
||||||
[TestClass()]
|
|
||||||
public class RunnerTests
|
|
||||||
{
|
|
||||||
[TestMethod("Runner创建测试")]
|
|
||||||
public void RunTest()
|
|
||||||
{
|
|
||||||
var config = GetConfiguration();
|
|
||||||
var services = CreateServiceScope(config);
|
|
||||||
System.Console.WriteLine("数据注入完成");
|
|
||||||
System.Console.WriteLine("sqlRunner开始测试");
|
|
||||||
var sqlRunner = GetSqlRunner(services);
|
|
||||||
Assert.IsNotNull(sqlRunner);
|
|
||||||
Assert.IsNotNull(sqlRunner as ISqlServerRunner);
|
|
||||||
Assert.IsNotNull(sqlRunner as SqlServerRunner);
|
|
||||||
var sRunner = sqlRunner as SqlServerRunner;
|
|
||||||
Assert.IsNotNull(sRunner.Options);
|
|
||||||
System.Console.WriteLine("OracleRunner开始测试");
|
|
||||||
var oracleRunner = GetOracleRunner(services);
|
|
||||||
Assert.IsNotNull(oracleRunner);
|
|
||||||
Assert.IsNotNull(oracleRunner as IOracleRunner);
|
|
||||||
Assert.IsNotNull(oracleRunner as OracleRunner);
|
|
||||||
var oRunner = oracleRunner as OracleRunner;
|
|
||||||
Assert.IsNotNull(oRunner.Options);
|
|
||||||
Assert.IsTrue(oRunner.Options.ReturnModel == ReturnModel.TempTable);
|
|
||||||
|
|
||||||
//OracleTest();
|
|
||||||
//System.Console.WriteLine("---------------------");
|
|
||||||
//SqlserverTest();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
[TestMethod("Sqlserver执行测试")]
|
|
||||||
public void SqlserverTest()
|
|
||||||
{
|
|
||||||
var config = GetConfiguration();
|
|
||||||
var services = CreateServiceScope(config);
|
|
||||||
var db = CreateSqlDbContext(config);
|
|
||||||
Assert.IsNotNull(db);
|
|
||||||
var runner = GetSqlRunner(services);
|
|
||||||
Assert.IsNotNull(runner);
|
|
||||||
var result = runner.Run<SqlserverModel, SqlserverModelResult>(db, new SqlserverModel
|
|
||||||
{
|
|
||||||
p1 = 1,
|
|
||||||
p2 = 2,
|
|
||||||
p3 = "abc,"
|
|
||||||
}).ToList();
|
|
||||||
Assert.IsNotNull(result);
|
|
||||||
System.Console.WriteLine("result ok");
|
|
||||||
CollectionAssert.AllItemsAreNotNull(result);
|
|
||||||
Assert.IsTrue(result.Count > 0);
|
|
||||||
System.Console.WriteLine($"result count {result.Count}");
|
|
||||||
System.Console.WriteLine("SqlserverTest over!");
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod("Oracle执行测试")]
|
|
||||||
public void OracleTest()
|
|
||||||
{
|
|
||||||
var config = GetConfiguration();
|
|
||||||
var services = CreateServiceScope(config);
|
|
||||||
var db = CreateOracleDbContext(config);
|
|
||||||
Assert.IsNotNull(db);
|
|
||||||
var runner = GetOracleRunner(services);
|
|
||||||
Assert.IsNotNull(runner);
|
|
||||||
|
|
||||||
var result = runner.Run<Tjjk_Tjgh, Tjjk_TjghResult>(db, new Tjjk_Tjgh
|
|
||||||
{
|
|
||||||
v_yljgdm = "50e3d44d-9ca2-4fbd-9d5d-d32339b1b113",
|
|
||||||
v_ghrq = "20211105",
|
|
||||||
v_sfz = "610528199002165174",
|
|
||||||
}).ToList();
|
|
||||||
Assert.IsNotNull(result);
|
|
||||||
System.Console.WriteLine("result ok");
|
|
||||||
CollectionAssert.AllItemsAreNotNull(result);
|
|
||||||
Assert.IsTrue(result.Count > 0);
|
|
||||||
System.Console.WriteLine($"result count {result.Count}");
|
|
||||||
System.Console.WriteLine("OracleTest over!");
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod("Oracle原始执行执行测试")]
|
|
||||||
public void OracleRawTest()
|
|
||||||
{
|
|
||||||
var config = GetConfiguration();
|
|
||||||
var services = CreateServiceScope(config);
|
|
||||||
var db = CreateOracleDbContext(config);
|
|
||||||
Assert.IsNotNull(db);
|
|
||||||
var cmd = db.Database.GetDbConnection().CreateCommand() as OracleCommand;
|
|
||||||
cmd.CommandType = CommandType.StoredProcedure;
|
|
||||||
cmd.CommandText = "Tjjk_Tjgh";
|
|
||||||
var a = new OracleParameter(":v_yljgdm", OracleDbType.Char, 36)
|
|
||||||
{
|
|
||||||
Value = "50e3d44d-9ca2-4fbd-9d5d-d32339b1b113"
|
|
||||||
};
|
|
||||||
cmd.Parameters.Add(a);
|
|
||||||
|
|
||||||
var c = new OracleParameter(":v_sfz", OracleDbType.Varchar2, 36);
|
|
||||||
c.Value = "610528199002165174";
|
|
||||||
cmd.Parameters.Add(c);
|
|
||||||
|
|
||||||
var b = new OracleParameter(":v_ghrq", OracleDbType.Char, 8);
|
|
||||||
b.Value = "20211105";
|
|
||||||
cmd.Parameters.Add(b);
|
|
||||||
db.Database.GetDbConnection().Open();
|
|
||||||
cmd.ExecuteNonQuery();
|
|
||||||
cmd.CommandText = "select * from tt_tjjk_tjgh";
|
|
||||||
cmd.CommandType = CommandType.Text;
|
|
||||||
var da = new OracleDataAdapter(cmd);
|
|
||||||
DataSet ds = new();
|
|
||||||
da.Fill(ds);
|
|
||||||
System.Console.WriteLine(ds.Tables[0].Rows.Count);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DbContext CreateSqlDbContext(IConfiguration config)
|
|
||||||
{
|
|
||||||
var ora = config.GetSection("db:sqlserver").Value;
|
|
||||||
var buider = new DbContextOptionsBuilder();
|
|
||||||
return new DbContext(buider.UseSqlServer(ora).Options);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DbContext CreateOracleDbContext(IConfiguration config)
|
|
||||||
{
|
|
||||||
var ora = config.GetSection("db:oracle").Value;
|
|
||||||
var oraVer = config.GetSection("db:ver").Value;
|
|
||||||
var buider = new DbContextOptionsBuilder();
|
|
||||||
return new DbContext(buider.UseOracle(ora, o => o.UseOracleSQLCompatibility(oraVer)).Options);
|
|
||||||
}
|
|
||||||
|
|
||||||
///// <summary>
|
|
||||||
///// 体检接口 挂号查询测试
|
|
||||||
///// </summary>
|
|
||||||
//[TestMethod()]
|
|
||||||
//public void Tjjk_TjghTest()
|
|
||||||
//{
|
|
||||||
// System.Console.WriteLine("OracleTest start!");
|
|
||||||
// var configurationBuilder = new ConfigurationBuilder();
|
|
||||||
// configurationBuilder.AddJsonFile("AppSettings.json");
|
|
||||||
// configurationBuilder.AddUserSecrets(this.GetType().Assembly);
|
|
||||||
// var config = configurationBuilder.Build();
|
|
||||||
// var ora = config.GetSection("db:oracle").Value;
|
|
||||||
// var oraVer = config.GetSection("db:ver").Value;
|
|
||||||
// var buider = new DbContextOptionsBuilder();
|
|
||||||
// var db = new DbContext(buider.UseOracle(ora, o => o.UseOracleSQLCompatibility(oraVer)).Options);
|
|
||||||
|
|
||||||
// IRunner runner = new Runner();
|
|
||||||
// var result = runner.Run<Tjjk_Tjgh, Tjjk_TjghResult>(db, new Tjjk_Tjgh
|
|
||||||
// {
|
|
||||||
// v_ghrq = "20211105",
|
|
||||||
// v_sfz = "610528199002165174",
|
|
||||||
// v_yljgdm = "50e3d44d-9ca2-4fbd-9d5d-d32339b1b113",
|
|
||||||
// }).ToList();
|
|
||||||
// Assert.IsNotNull(result);
|
|
||||||
// System.Console.WriteLine("result ok");
|
|
||||||
// CollectionAssert.AllItemsAreNotNull(result);
|
|
||||||
// Assert.IsTrue(result.Count > 0);
|
|
||||||
// System.Console.WriteLine($"result count {result.Count}");
|
|
||||||
// System.Console.WriteLine("OracleTest over!");
|
|
||||||
//}
|
|
||||||
|
|
||||||
//[TestMethod()]
|
|
||||||
//public void GetDoctorDataTest()
|
|
||||||
//{
|
|
||||||
// System.Console.WriteLine("OracleTest start!");
|
|
||||||
// var configurationBuilder = new ConfigurationBuilder();
|
|
||||||
// configurationBuilder.AddJsonFile("AppSettings.json");
|
|
||||||
// configurationBuilder.AddUserSecrets(this.GetType().Assembly);
|
|
||||||
// var config = configurationBuilder.Build();
|
|
||||||
// var ora = config.GetSection("db:oracle").Value;
|
|
||||||
// var oraVer = config.GetSection("db:ver").Value;
|
|
||||||
// var buider = new DbContextOptionsBuilder();
|
|
||||||
// var db = new DbContext(buider.UseOracle(ora, o => o.UseOracleSQLCompatibility(oraVer)).Options);
|
|
||||||
|
|
||||||
// IRunner runner = new Runner();
|
|
||||||
// var result = runner.Run<GetDoctorData, GetDoctorDataResult>(db, new GetDoctorData
|
|
||||||
// {
|
|
||||||
// v_orgaid = "50e3d44d-9ca2-4fbd-9d5d-d32339b1b113",
|
|
||||||
// }).ToList();
|
|
||||||
// Assert.IsNotNull(result);
|
|
||||||
// System.Console.WriteLine("result ok");
|
|
||||||
// CollectionAssert.AllItemsAreNotNull(result);
|
|
||||||
// Assert.IsTrue(result.Count > 0);
|
|
||||||
// System.Console.WriteLine($"result count {result.Count}");
|
|
||||||
// System.Console.WriteLine("OracleTest over!");
|
|
||||||
//}
|
|
||||||
|
|
||||||
public static IConfiguration GetConfiguration()
|
|
||||||
{
|
|
||||||
var configurationBuilder = new ConfigurationBuilder();
|
|
||||||
configurationBuilder.AddJsonFile("AppSettings.json");
|
|
||||||
configurationBuilder.AddUserSecrets(typeof(RunnerTests).Assembly);
|
|
||||||
return configurationBuilder.Build();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private IServiceScope CreateServiceScope(IConfiguration config)
|
|
||||||
{
|
|
||||||
IServiceCollection services = new ServiceCollection();
|
|
||||||
AddOptions(config, services);
|
|
||||||
services.AddFalconSPRunner();
|
|
||||||
return services.BuildServiceProvider().CreateScope();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void AddOptions(IConfiguration config, IServiceCollection services)
|
|
||||||
{
|
|
||||||
services.Configure<OracleRunnerOptions>(config.GetSection("OracleRunner"));
|
|
||||||
services.Configure<SqlServerRunnerOtions>(config.GetSection("SqlRunner"));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IRunner? GetSqlRunner(IServiceScope services)
|
|
||||||
{
|
|
||||||
return services.ServiceProvider.GetService<ISqlServerRunner>() as IRunner;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IRunner? GetOracleRunner(IServiceScope services)
|
|
||||||
{
|
|
||||||
return services.ServiceProvider.GetService<IOracleRunner>() as IRunner;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Binary file not shown.
|
@ -1,22 +0,0 @@
|
||||||
using Falcon.StoredProcedureRunner.Attributes;
|
|
||||||
|
|
||||||
namespace Falcon.StoredProcedureRunnerTests
|
|
||||||
{
|
|
||||||
[FalconSPProcuderName("TestSp1")]
|
|
||||||
public class SqlserverModel
|
|
||||||
{
|
|
||||||
public int p1 { get; set; }
|
|
||||||
public int p2 { get; set; }
|
|
||||||
public string p3 { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SqlserverModelResult
|
|
||||||
{
|
|
||||||
public double Id { get; set; }
|
|
||||||
public string s { get; set; }
|
|
||||||
public double jia { get; set; }
|
|
||||||
public double jian { get; set; }
|
|
||||||
public double chen { get; set; }
|
|
||||||
public double chu { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user