From 18d048218e62c8a6b0253bbc588a5bb4a03af99c Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Wed, 30 Mar 2022 17:08:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AD=98=E5=82=A8=E8=BF=87?= =?UTF-8?q?=E7=A8=8B=E8=B0=83=E7=94=A8=E6=96=B9=E6=B3=95=E5=8F=8A=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi.Test/BackTaskTest.cs | 2 +- .../Falcon.SugarApi.Test.csproj | 50 +++++++++---- Falcon.SugarApi.Test/StringExtendTest.cs | 3 + Falcon.SugarApi.Test/TestLog.cs | 2 +- .../UseStoredProcedureTest.cs | 70 +++++++++++++++++++ Falcon.SugarApi.Test/appsettings.json | 6 ++ .../DatabaseDefinitions/SugarDbContext.cs | 26 ++++--- 7 files changed, 134 insertions(+), 25 deletions(-) create mode 100644 Falcon.SugarApi.Test/UseStoredProcedureTest.cs create mode 100644 Falcon.SugarApi.Test/appsettings.json diff --git a/Falcon.SugarApi.Test/BackTaskTest.cs b/Falcon.SugarApi.Test/BackTaskTest.cs index 4733012..3385f60 100644 --- a/Falcon.SugarApi.Test/BackTaskTest.cs +++ b/Falcon.SugarApi.Test/BackTaskTest.cs @@ -33,7 +33,7 @@ namespace Falcon.SugarApi.Test public int State { get; set; } = 0; public BackTaskObject() { - this.Log = new TestLog(); + this.Log = new TestLog(); } protected override void OnStop(BackgroundLongTask t) { diff --git a/Falcon.SugarApi.Test/Falcon.SugarApi.Test.csproj b/Falcon.SugarApi.Test/Falcon.SugarApi.Test.csproj index cdd13dd..b188aad 100644 --- a/Falcon.SugarApi.Test/Falcon.SugarApi.Test.csproj +++ b/Falcon.SugarApi.Test/Falcon.SugarApi.Test.csproj @@ -1,21 +1,41 @@ - + - - net5;net6 - enable + + net5 + enable - false - + false + - - - - - - + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + - - - + + + + + + + Always + + + + + + + + diff --git a/Falcon.SugarApi.Test/StringExtendTest.cs b/Falcon.SugarApi.Test/StringExtendTest.cs index 918b910..22e5381 100644 --- a/Falcon.SugarApi.Test/StringExtendTest.cs +++ b/Falcon.SugarApi.Test/StringExtendTest.cs @@ -8,6 +8,9 @@ using Falcon.SugarApi; namespace Falcon.SugarApi.Test { + /// + /// 字符串扩展方法测试 + /// [TestClass] public class StringExtendTest { diff --git a/Falcon.SugarApi.Test/TestLog.cs b/Falcon.SugarApi.Test/TestLog.cs index 0372f72..2e1f1b5 100644 --- a/Falcon.SugarApi.Test/TestLog.cs +++ b/Falcon.SugarApi.Test/TestLog.cs @@ -6,7 +6,7 @@ namespace Falcon.SugarApi.Test /// /// 测试用Logger实例 /// - public class TestLog : ILogger + public class TestLog : ILogger,ILogger { public IDisposable BeginScope(TState state) { throw new NotImplementedException(); diff --git a/Falcon.SugarApi.Test/UseStoredProcedureTest.cs b/Falcon.SugarApi.Test/UseStoredProcedureTest.cs new file mode 100644 index 0000000..57ac853 --- /dev/null +++ b/Falcon.SugarApi.Test/UseStoredProcedureTest.cs @@ -0,0 +1,70 @@ +using Falcon.SugarApi.DatabaseDefinitions; +using Microsoft.Extensions.Configuration; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SqlSugar; +using System.Xml.Linq; +using System.Linq; + +namespace Falcon.SugarApi.Test +{ + /// + /// 执行存储过程测试 + /// + [TestClass] + public class UseStoredProcedureTest + { + /// + /// 执行Oracle存储过程 + /// + [TestMethod] + public void RunOracle() { + using var db = OracleSugarDb; + Assert.IsNotNull(db); + + //Assert.Fail("下面测试需要链接数据并且满足测试条件后注释此条后继续"); + + var pn = "GetEmployee"; + //var orgid = "50e3d44d-9ca2-4fbd-9d5d-d32339b1b113"; + //var ps = db.Ado.GetParameters(new { v_orgaid = orgid }); + //var plist = ps.ToList(); + //plist.Add(db.OracleRefCursor()); + //ps = plist.ToArray(); + + var org = new SugarParameter(":v_orgaid", "50e3d44d-9ca2-4fbd-9d5d-d32339b1b113"); + //var data = new SugarParameter(":v_data", null, true) { IsRefCursor = true, DbType = System.Data.DbType.String }; + //var data1 = new SugarParameter(":v_data", null, true) { IsRefCursor = true }; + //var data = new SugarParameter(":v_data", ""); + //data.IsRefCursor = true; + //data.Direction = System.Data.ParameterDirection.Output; + var rtb = db.Ado.UseStoredProcedure().GetDataTable(pn, org, db.OracleRefCursor()); + + //var rtb = db.GetAdo().GetDataTable(pn,org,data); + Assert.IsNotNull(rtb); + rtb = db.UseStoredProcedure().GetDataTable(pn, db.GetParameters(new { v_orgaid = "50e3d44d-9ca2-4fbd-9d5d-d32339b1b113" })); + + var aaa = db.GetParameters(new { v_orgaid = "50e3d44d-9ca2-4fbd-9d5d-d32339b1b113" }).Concat(new SugarParameter[] { db.OracleRefCursor() }); + rtb = db.UseStoredProcedure().GetDataTable(pn, aaa); + Assert.IsNotNull(rtb); + } + + /// + /// Oracle数据库 + /// + private SugarDbContext OracleSugarDb { + get { + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddJsonFile("AppSettings.json"); + var config = configurationBuilder.Build(); + var sc = config.GetSection("Database"); + var dbconfig = new SugarConnectionConfig { + ConnectionString = sc.GetValue("oracle"), + DbType = SqlSugar.DbType.Oracle, + Log = sc.GetValue("log"), + IsAutoCloseConnection = true, + InitKeyType = InitKeyType.Attribute, + }; + return new SugarDbContext(dbconfig, new TestLog()); + } + } + } +} diff --git a/Falcon.SugarApi.Test/appsettings.json b/Falcon.SugarApi.Test/appsettings.json new file mode 100644 index 0000000..4ae98af --- /dev/null +++ b/Falcon.SugarApi.Test/appsettings.json @@ -0,0 +1,6 @@ +{ + "Database": { + "oracle": "DATA SOURCE=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST =10.241.113.11)(PORT = 1521)))(CONNECT_DATA = (SERVICE_NAME = orcl)));PASSWORD = harmonyhis;PERSIST SECURITY INFO = True;USER ID = HARMONYHIS;Connection Lifetime = 120;Connection Timeout = 60", + "log": "true" + } +} diff --git a/Falcon.SugarApi/DatabaseDefinitions/SugarDbContext.cs b/Falcon.SugarApi/DatabaseDefinitions/SugarDbContext.cs index 69cd80f..1b7468b 100644 --- a/Falcon.SugarApi/DatabaseDefinitions/SugarDbContext.cs +++ b/Falcon.SugarApi/DatabaseDefinitions/SugarDbContext.cs @@ -171,23 +171,33 @@ namespace Falcon.SugarApi.DatabaseDefinitions #endregion #region 执行存储过程 - public IAdo GetAdo() { + /// + /// 获取Ado对象执行存储过程 + /// + /// Ado对象 + public IAdo UseStoredProcedure() { return this.Ado.UseStoredProcedure(); } /// /// 获取一个Oracle游标类型参数用作返回值 /// - /// + /// 参数名称 /// - public SugarParameter OracleRefCursor(string name = "v_data") => - new(name, null, true) { IsRefCursor = true }; + public SugarParameter OracleRefCursor(string name = ":v_data") => + new(name, null, true) { IsRefCursor = true, DbType = System.Data.DbType.String }; - - public object GetParameters(T data) { - //return SqlSugarTool.GetParameters(new { pageStart = 1, pageEnd = 5, recordCount = 0 }); - throw new NotSupportedException(); + /// + /// 从对象获取存储过程参数 + /// + /// 对象类型 + /// 参数对象 + /// 参数 + /// + public SugarParameter[] GetParameters(T data) { + return this.Ado.GetParameters(data); } + #endregion } }