mirror of
https://github.com/FalconWu2017/Falcon.StoredProcedureRunner.git
synced 2025-04-03 19:59:37 +08:00
第一版,从以前版本改写
This commit is contained in:
commit
9044a859ef
355
.gitignore
vendored
Normal file
355
.gitignore
vendored
Normal file
|
@ -0,0 +1,355 @@
|
||||||
|
# ---> VisualStudio
|
||||||
|
## Ignore Visual Studio temporary files, build results, and
|
||||||
|
## files generated by popular Visual Studio add-ons.
|
||||||
|
##
|
||||||
|
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
|
||||||
|
|
||||||
|
# User-specific files
|
||||||
|
*.rsuser
|
||||||
|
*.suo
|
||||||
|
*.user
|
||||||
|
*.userosscache
|
||||||
|
*.sln.docstates
|
||||||
|
|
||||||
|
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||||
|
*.userprefs
|
||||||
|
|
||||||
|
# Mono auto generated files
|
||||||
|
mono_crash.*
|
||||||
|
|
||||||
|
# Build results
|
||||||
|
[Dd]ebug/
|
||||||
|
[Dd]ebugPublic/
|
||||||
|
[Rr]elease/
|
||||||
|
[Rr]eleases/
|
||||||
|
x64/
|
||||||
|
x86/
|
||||||
|
[Aa][Rr][Mm]/
|
||||||
|
[Aa][Rr][Mm]64/
|
||||||
|
bld/
|
||||||
|
[Bb]in/
|
||||||
|
[Oo]bj/
|
||||||
|
[Ll]og/
|
||||||
|
[Ll]ogs/
|
||||||
|
|
||||||
|
# Visual Studio 2015/2017 cache/options directory
|
||||||
|
.vs/
|
||||||
|
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||||
|
#wwwroot/
|
||||||
|
|
||||||
|
# Visual Studio 2017 auto generated files
|
||||||
|
Generated\ Files/
|
||||||
|
|
||||||
|
# MSTest test Results
|
||||||
|
[Tt]est[Rr]esult*/
|
||||||
|
[Bb]uild[Ll]og.*
|
||||||
|
|
||||||
|
# NUnit
|
||||||
|
*.VisualState.xml
|
||||||
|
TestResult.xml
|
||||||
|
nunit-*.xml
|
||||||
|
|
||||||
|
# Build Results of an ATL Project
|
||||||
|
[Dd]ebugPS/
|
||||||
|
[Rr]eleasePS/
|
||||||
|
dlldata.c
|
||||||
|
|
||||||
|
# Benchmark Results
|
||||||
|
BenchmarkDotNet.Artifacts/
|
||||||
|
|
||||||
|
# .NET Core
|
||||||
|
project.lock.json
|
||||||
|
project.fragment.lock.json
|
||||||
|
artifacts/
|
||||||
|
|
||||||
|
# StyleCop
|
||||||
|
StyleCopReport.xml
|
||||||
|
|
||||||
|
# Files built by Visual Studio
|
||||||
|
*_i.c
|
||||||
|
*_p.c
|
||||||
|
*_h.h
|
||||||
|
*.ilk
|
||||||
|
*.meta
|
||||||
|
*.obj
|
||||||
|
*.iobj
|
||||||
|
*.pch
|
||||||
|
*.pdb
|
||||||
|
*.ipdb
|
||||||
|
*.pgc
|
||||||
|
*.pgd
|
||||||
|
*.rsp
|
||||||
|
*.sbr
|
||||||
|
*.tlb
|
||||||
|
*.tli
|
||||||
|
*.tlh
|
||||||
|
*.tmp
|
||||||
|
*.tmp_proj
|
||||||
|
*_wpftmp.csproj
|
||||||
|
*.log
|
||||||
|
*.vspscc
|
||||||
|
*.vssscc
|
||||||
|
.builds
|
||||||
|
*.pidb
|
||||||
|
*.svclog
|
||||||
|
*.scc
|
||||||
|
|
||||||
|
# Chutzpah Test files
|
||||||
|
_Chutzpah*
|
||||||
|
|
||||||
|
# Visual C++ cache files
|
||||||
|
ipch/
|
||||||
|
*.aps
|
||||||
|
*.ncb
|
||||||
|
*.opendb
|
||||||
|
*.opensdf
|
||||||
|
*.sdf
|
||||||
|
*.cachefile
|
||||||
|
*.VC.db
|
||||||
|
*.VC.VC.opendb
|
||||||
|
|
||||||
|
# Visual Studio profiler
|
||||||
|
*.psess
|
||||||
|
*.vsp
|
||||||
|
*.vspx
|
||||||
|
*.sap
|
||||||
|
|
||||||
|
# Visual Studio Trace Files
|
||||||
|
*.e2e
|
||||||
|
|
||||||
|
# TFS 2012 Local Workspace
|
||||||
|
$tf/
|
||||||
|
|
||||||
|
# Guidance Automation Toolkit
|
||||||
|
*.gpState
|
||||||
|
|
||||||
|
# ReSharper is a .NET coding add-in
|
||||||
|
_ReSharper*/
|
||||||
|
*.[Rr]e[Ss]harper
|
||||||
|
*.DotSettings.user
|
||||||
|
|
||||||
|
# TeamCity is a build add-in
|
||||||
|
_TeamCity*
|
||||||
|
|
||||||
|
# DotCover is a Code Coverage Tool
|
||||||
|
*.dotCover
|
||||||
|
|
||||||
|
# AxoCover is a Code Coverage Tool
|
||||||
|
.axoCover/*
|
||||||
|
!.axoCover/settings.json
|
||||||
|
|
||||||
|
# Coverlet is a free, cross platform Code Coverage Tool
|
||||||
|
coverage*[.json, .xml, .info]
|
||||||
|
|
||||||
|
# Visual Studio code coverage results
|
||||||
|
*.coverage
|
||||||
|
*.coveragexml
|
||||||
|
|
||||||
|
# NCrunch
|
||||||
|
_NCrunch_*
|
||||||
|
.*crunch*.local.xml
|
||||||
|
nCrunchTemp_*
|
||||||
|
|
||||||
|
# MightyMoose
|
||||||
|
*.mm.*
|
||||||
|
AutoTest.Net/
|
||||||
|
|
||||||
|
# Web workbench (sass)
|
||||||
|
.sass-cache/
|
||||||
|
|
||||||
|
# Installshield output folder
|
||||||
|
[Ee]xpress/
|
||||||
|
|
||||||
|
# DocProject is a documentation generator add-in
|
||||||
|
DocProject/buildhelp/
|
||||||
|
DocProject/Help/*.HxT
|
||||||
|
DocProject/Help/*.HxC
|
||||||
|
DocProject/Help/*.hhc
|
||||||
|
DocProject/Help/*.hhk
|
||||||
|
DocProject/Help/*.hhp
|
||||||
|
DocProject/Help/Html2
|
||||||
|
DocProject/Help/html
|
||||||
|
|
||||||
|
# Click-Once directory
|
||||||
|
publish/
|
||||||
|
|
||||||
|
# Publish Web Output
|
||||||
|
*.[Pp]ublish.xml
|
||||||
|
*.azurePubxml
|
||||||
|
# Note: Comment the next line if you want to checkin your web deploy settings,
|
||||||
|
# but database connection strings (with potential passwords) will be unencrypted
|
||||||
|
*.pubxml
|
||||||
|
*.publishproj
|
||||||
|
|
||||||
|
# Microsoft Azure Web App publish settings. Comment the next line if you want to
|
||||||
|
# checkin your Azure Web App publish settings, but sensitive information contained
|
||||||
|
# in these scripts will be unencrypted
|
||||||
|
PublishScripts/
|
||||||
|
|
||||||
|
# NuGet Packages
|
||||||
|
*.nupkg
|
||||||
|
# NuGet Symbol Packages
|
||||||
|
*.snupkg
|
||||||
|
# The packages folder can be ignored because of Package Restore
|
||||||
|
**/[Pp]ackages/*
|
||||||
|
# except build/, which is used as an MSBuild target.
|
||||||
|
!**/[Pp]ackages/build/
|
||||||
|
# Uncomment if necessary however generally it will be regenerated when needed
|
||||||
|
#!**/[Pp]ackages/repositories.config
|
||||||
|
# NuGet v3's project.json files produces more ignorable files
|
||||||
|
*.nuget.props
|
||||||
|
*.nuget.targets
|
||||||
|
|
||||||
|
# Microsoft Azure Build Output
|
||||||
|
csx/
|
||||||
|
*.build.csdef
|
||||||
|
|
||||||
|
# Microsoft Azure Emulator
|
||||||
|
ecf/
|
||||||
|
rcf/
|
||||||
|
|
||||||
|
# Windows Store app package directories and files
|
||||||
|
AppPackages/
|
||||||
|
BundleArtifacts/
|
||||||
|
Package.StoreAssociation.xml
|
||||||
|
_pkginfo.txt
|
||||||
|
*.appx
|
||||||
|
*.appxbundle
|
||||||
|
*.appxupload
|
||||||
|
|
||||||
|
# Visual Studio cache files
|
||||||
|
# files ending in .cache can be ignored
|
||||||
|
*.[Cc]ache
|
||||||
|
# but keep track of directories ending in .cache
|
||||||
|
!?*.[Cc]ache/
|
||||||
|
|
||||||
|
# Others
|
||||||
|
ClientBin/
|
||||||
|
~$*
|
||||||
|
*~
|
||||||
|
*.dbmdl
|
||||||
|
*.dbproj.schemaview
|
||||||
|
*.jfm
|
||||||
|
*.pfx
|
||||||
|
*.publishsettings
|
||||||
|
orleans.codegen.cs
|
||||||
|
|
||||||
|
# Including strong name files can present a security risk
|
||||||
|
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
|
||||||
|
#*.snk
|
||||||
|
|
||||||
|
# Since there are multiple workflows, uncomment next line to ignore bower_components
|
||||||
|
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
|
||||||
|
#bower_components/
|
||||||
|
|
||||||
|
# RIA/Silverlight projects
|
||||||
|
Generated_Code/
|
||||||
|
|
||||||
|
# Backup & report files from converting an old project file
|
||||||
|
# to a newer Visual Studio version. Backup files are not needed,
|
||||||
|
# because we have git ;-)
|
||||||
|
_UpgradeReport_Files/
|
||||||
|
Backup*/
|
||||||
|
UpgradeLog*.XML
|
||||||
|
UpgradeLog*.htm
|
||||||
|
ServiceFabricBackup/
|
||||||
|
*.rptproj.bak
|
||||||
|
|
||||||
|
# SQL Server files
|
||||||
|
*.mdf
|
||||||
|
*.ldf
|
||||||
|
*.ndf
|
||||||
|
|
||||||
|
# Business Intelligence projects
|
||||||
|
*.rdl.data
|
||||||
|
*.bim.layout
|
||||||
|
*.bim_*.settings
|
||||||
|
*.rptproj.rsuser
|
||||||
|
*- [Bb]ackup.rdl
|
||||||
|
*- [Bb]ackup ([0-9]).rdl
|
||||||
|
*- [Bb]ackup ([0-9][0-9]).rdl
|
||||||
|
|
||||||
|
# Microsoft Fakes
|
||||||
|
FakesAssemblies/
|
||||||
|
|
||||||
|
# GhostDoc plugin setting file
|
||||||
|
*.GhostDoc.xml
|
||||||
|
|
||||||
|
# Node.js Tools for Visual Studio
|
||||||
|
.ntvs_analysis.dat
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# Visual Studio 6 build log
|
||||||
|
*.plg
|
||||||
|
|
||||||
|
# Visual Studio 6 workspace options file
|
||||||
|
*.opt
|
||||||
|
|
||||||
|
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
|
||||||
|
*.vbw
|
||||||
|
|
||||||
|
# Visual Studio LightSwitch build output
|
||||||
|
**/*.HTMLClient/GeneratedArtifacts
|
||||||
|
**/*.DesktopClient/GeneratedArtifacts
|
||||||
|
**/*.DesktopClient/ModelManifest.xml
|
||||||
|
**/*.Server/GeneratedArtifacts
|
||||||
|
**/*.Server/ModelManifest.xml
|
||||||
|
_Pvt_Extensions
|
||||||
|
|
||||||
|
# Paket dependency manager
|
||||||
|
.paket/paket.exe
|
||||||
|
paket-files/
|
||||||
|
|
||||||
|
# FAKE - F# Make
|
||||||
|
.fake/
|
||||||
|
|
||||||
|
# CodeRush personal settings
|
||||||
|
.cr/personal
|
||||||
|
|
||||||
|
# Python Tools for Visual Studio (PTVS)
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
|
||||||
|
# Cake - Uncomment if you are using it
|
||||||
|
# tools/**
|
||||||
|
# !tools/packages.config
|
||||||
|
|
||||||
|
# Tabs Studio
|
||||||
|
*.tss
|
||||||
|
|
||||||
|
# Telerik's JustMock configuration file
|
||||||
|
*.jmconfig
|
||||||
|
|
||||||
|
# BizTalk build output
|
||||||
|
*.btp.cs
|
||||||
|
*.btm.cs
|
||||||
|
*.odx.cs
|
||||||
|
*.xsd.cs
|
||||||
|
|
||||||
|
# OpenCover UI analysis results
|
||||||
|
OpenCover/
|
||||||
|
|
||||||
|
# Azure Stream Analytics local run output
|
||||||
|
ASALocalRun/
|
||||||
|
|
||||||
|
# MSBuild Binary and Structured Log
|
||||||
|
*.binlog
|
||||||
|
|
||||||
|
# NVidia Nsight GPU debugger configuration file
|
||||||
|
*.nvuser
|
||||||
|
|
||||||
|
# MFractors (Xamarin productivity tool) working folder
|
||||||
|
.mfractor/
|
||||||
|
|
||||||
|
# Local History for Visual Studio
|
||||||
|
.localhistory/
|
||||||
|
|
||||||
|
# BeatPulse healthcheck temp database
|
||||||
|
healthchecksdb
|
||||||
|
|
||||||
|
# Backup folder for Package Reference Convert tool in Visual Studio 2017
|
||||||
|
MigrationBackup/
|
||||||
|
|
||||||
|
# Ionide (cross platform F# VS Code tools) working folder
|
||||||
|
.ionide/
|
||||||
|
|
25
src/Falcon.StoredProcedureRunner.sln
Normal file
25
src/Falcon.StoredProcedureRunner.sln
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 16
|
||||||
|
VisualStudioVersion = 16.0.30804.86
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Falcon.StoredProcedureRunner", "Falcon.StoredProcedureRunner\Falcon.StoredProcedureRunner.csproj", "{13D3139B-60C2-4785-ADCB-4F839BDEC3C4}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{13D3139B-60C2-4785-ADCB-4F839BDEC3C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{13D3139B-60C2-4785-ADCB-4F839BDEC3C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{13D3139B-60C2-4785-ADCB-4F839BDEC3C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{13D3139B-60C2-4785-ADCB-4F839BDEC3C4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {7ECD6C47-0547-4FB7-A481-829D28302EFD}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
|
@ -0,0 +1,29 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
|
||||||
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
|
<AssemblyVersion>1.0.4.0</AssemblyVersion>
|
||||||
|
<FileVersion>1.0.4.0</FileVersion>
|
||||||
|
<Version>1.0.4</Version>
|
||||||
|
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
|
||||||
|
<PackageProjectUrl>http://106.14.65.137/Falcon/Falcon.ModelSP</PackageProjectUrl>
|
||||||
|
<RepositoryUrl>http://106.14.65.137/Falcon/Falcon.ModelSP</RepositoryUrl>
|
||||||
|
<Description>EF Core 存储过程执行组件,可以模型化执行存储过程</Description>
|
||||||
|
<Authors>Falcon</Authors>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||||
|
<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 Condition="'$(TargetFramework)' == 'netstandard2.1'">
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.1" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
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
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Falcon.StoredProcedureRunner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 定义名称
|
||||||
|
/// </summary>
|
||||||
|
public class FalconSPPrarmNameAttribute:Attribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 参数名
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 定于存储过程参数名称
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">参数名称</param>
|
||||||
|
public FalconSPPrarmNameAttribute(string name) { this.Name = name; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Falcon.StoredProcedureRunner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 定义存储过程名称
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
|
public class FalconSPProcuderNameAttribute:Attribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 存储过程名称
|
||||||
|
/// </summary>
|
||||||
|
public string ProcuderName { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 定义存储过程名称
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="m"></param>
|
||||||
|
public FalconSPProcuderNameAttribute(string m) => this.ProcuderName = m;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Falcon.StoredProcedureRunner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 定义存储过程返回值类型
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Class,AllowMultiple = false,Inherited = false)]
|
||||||
|
public class FalconSPReturnTypeAttribute:Attribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 返回的数据类型
|
||||||
|
/// </summary>
|
||||||
|
public Type ReturnType { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 定义存储过程名称
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="t">存储过程返回值类型</param>
|
||||||
|
public FalconSPReturnTypeAttribute(Type t) => this.ReturnType = t;
|
||||||
|
}
|
||||||
|
}
|
49
src/Falcon.StoredProcedureRunner/IRunner.cs
Normal file
49
src/Falcon.StoredProcedureRunner/IRunner.cs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Falcon.StoredProcedureRunner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 执行数据库存储过程
|
||||||
|
/// </summary>
|
||||||
|
public interface IRunner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 通过数据库上下文执行无返回值的存储过程
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TPrarmType">参数类型</typeparam>
|
||||||
|
/// <param name="db">数据上下文</param>
|
||||||
|
/// <param name="data">参数数据</param>
|
||||||
|
int Execute<TPrarmType>(DbContext db,TPrarmType data);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 根据模型定义参数执行存储过程进行查询,参数类型必须定义ReturnTypeAttribute特性
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TPrarmType">存储过程参数类型</typeparam>
|
||||||
|
/// <param name="db">数据上下文</param>
|
||||||
|
/// <param name="data">存储过程参数</param>
|
||||||
|
/// <returns>返回类型枚举FalconSPReturnTypeAttribute定义的类型枚举。</returns>
|
||||||
|
IEnumerable<object> Run<TPrarmType>(DbContext db,TPrarmType data);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 通过数据库上下文执行存储过程,并返回查询结果
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TPrarmType">参数类型</typeparam>
|
||||||
|
/// <typeparam name="TReturnType">返回结果项类型</typeparam>
|
||||||
|
/// <param name="db">数据上下文</param>
|
||||||
|
/// <param name="data">参数数据</param>
|
||||||
|
/// <returns>查询结果枚举</returns>
|
||||||
|
IEnumerable<TReturnType> Run<TPrarmType, TReturnType>(DbContext db,TPrarmType data) where TReturnType : class, new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 通过数据库上下文执行存储过程,并返回查询结果
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="db">数据上下文</param>
|
||||||
|
/// <param name="prarmType">参数类型</param>
|
||||||
|
/// <param name="returnType">返回结果项类型</param>
|
||||||
|
/// <param name="data">执行参数</param>
|
||||||
|
/// <returns>查询结果枚举</returns>
|
||||||
|
IEnumerable<object> Run(DbContext db,Type prarmType,Type returnType ,object data) ;
|
||||||
|
}
|
||||||
|
}
|
191
src/Falcon.StoredProcedureRunner/Runner.cs
Normal file
191
src/Falcon.StoredProcedureRunner/Runner.cs
Normal file
|
@ -0,0 +1,191 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.Data.SqlClient;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Falcon.StoredProcedureRunner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 存储过程执行
|
||||||
|
/// </summary>
|
||||||
|
public partial class Runner:IRunner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 执行无返回值的存储过程
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TPrarmType">参数类型</typeparam>
|
||||||
|
/// <param name="db">数据上下文</param>
|
||||||
|
/// <param name="data">参数数据</param>
|
||||||
|
public int Execute<TPrarmType>(DbContext db,TPrarmType data) {
|
||||||
|
var parms = getParams(data).ToArray();
|
||||||
|
var pName = getProcuderName<TPrarmType>();
|
||||||
|
|
||||||
|
#if NETSTANDARD2_1
|
||||||
|
return db.Database.ExecuteSqlRaw(pName,parms);
|
||||||
|
#else
|
||||||
|
return db.Database.ExecuteSqlCommand(pName,parms);
|
||||||
|
#endif
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 根据模型定义参数执行存储过程进行查询,参数类型必须定义ReturnTypeAttribute特性
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TPrarmType">存储过程参数类型</typeparam>
|
||||||
|
/// <param name="db">数据上下文</param>
|
||||||
|
/// <param name="data">存储过程参数</param>
|
||||||
|
/// <returns>返回类型枚举FalconSPReturnTypeAttribute定义的类型枚举。</returns>
|
||||||
|
public IEnumerable<object> Run<TPrarmType>(DbContext db,TPrarmType data) {
|
||||||
|
var rType = getRequtnType(typeof(TPrarmType));
|
||||||
|
if(rType == null) {
|
||||||
|
throw new Exception("必须在参数类型上设置ReturnTypeAttribute");
|
||||||
|
}
|
||||||
|
return Run(db,typeof(TPrarmType),rType,data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 通过数据库上下文执行存储过程,并返回查询结果
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TPrarmType">参数类型</typeparam>
|
||||||
|
/// <typeparam name="TReturnType">返回结果项类型</typeparam>
|
||||||
|
/// <param name="db">数据上下文</param>
|
||||||
|
/// <param name="data">参数数据</param>
|
||||||
|
/// <returns>查询结果枚举</returns>
|
||||||
|
public IEnumerable<TReturnType> Run<TPrarmType, TReturnType>(DbContext db,TPrarmType data) where TReturnType : class, new() {
|
||||||
|
return Run(db,typeof(TPrarmType),typeof(TReturnType),data).Cast<TReturnType>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 通过数据库上下文执行存储过程,并返回查询结果
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="db">数据库上下文</param>
|
||||||
|
/// <param name="prarmType">参数类型</param>
|
||||||
|
/// <param name="returnType">返回值类型</param>
|
||||||
|
/// <param name="data">存储过程参数</param>
|
||||||
|
/// <returns>查询结果枚举</returns>
|
||||||
|
public IEnumerable<object> Run(DbContext db,Type prarmType,Type returnType,object data) {
|
||||||
|
var pm = getProcuderName(prarmType);
|
||||||
|
var paras = getParams(data).ToArray();
|
||||||
|
var connection = db.Database.GetDbConnection();
|
||||||
|
using(var cmd = connection.CreateCommand()) {
|
||||||
|
cmd.CommandText = pm;
|
||||||
|
cmd.CommandType = System.Data.CommandType.StoredProcedure;
|
||||||
|
cmd.Parameters.AddRange(paras);
|
||||||
|
connection.Open();
|
||||||
|
var dr = cmd.ExecuteReader();
|
||||||
|
var result = new List<object>();
|
||||||
|
if(!dr.CanGetColumnSchema())
|
||||||
|
return result;
|
||||||
|
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;
|
||||||
|
pi.SetValue(item,value);
|
||||||
|
}
|
||||||
|
result.Add(item);
|
||||||
|
}
|
||||||
|
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="pramType">返回值类型</param>
|
||||||
|
private static Type getRequtnType(Type pramType) {
|
||||||
|
var attr = pramType.GetCustomAttribute<FalconSPReturnTypeAttribute>();
|
||||||
|
if(attr != null && attr is FalconSPReturnTypeAttribute pna && pna.ReturnType != null) {
|
||||||
|
return pna.ReturnType;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取存储过程参数枚举
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">参数模型类型</typeparam>
|
||||||
|
/// <param name="data">参数实例</param>
|
||||||
|
private static IEnumerable<SqlParameter> getParams<T>(T data) {
|
||||||
|
if(data == null)
|
||||||
|
yield break;
|
||||||
|
foreach(var p in typeof(T).GetProperties()) {
|
||||||
|
if(!p.CanRead || ignoreProp(p))
|
||||||
|
continue;
|
||||||
|
yield return new SqlParameter($"@{getPrarmName(p)}",p.GetValue(data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user