增加存储过程调用方法及测试

This commit is contained in:
falcon 2022-03-30 17:08:07 +08:00
parent 3404365b0b
commit 18d048218e
7 changed files with 134 additions and 25 deletions

View File

@ -33,7 +33,7 @@ namespace Falcon.SugarApi.Test
public int State { get; set; } = 0;
public BackTaskObject() {
this.Log = new TestLog();
this.Log = new TestLog<BackTaskObject>();
}
protected override void OnStop(BackgroundLongTask t) {

View File

@ -1,21 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net5;net6</TargetFrameworks>
<TargetFrameworks>net5</TargetFrameworks>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<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.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Falcon.SugarApi\Falcon.SugarApi.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ProjectExtensions>
<VisualStudio>
<UserProperties />
</VisualStudio>
</ProjectExtensions>
</Project>

View File

@ -8,6 +8,9 @@ using Falcon.SugarApi;
namespace Falcon.SugarApi.Test
{
/// <summary>
/// 字符串扩展方法测试
/// </summary>
[TestClass]
public class StringExtendTest
{

View File

@ -6,7 +6,7 @@ namespace Falcon.SugarApi.Test
/// <summary>
/// 测试用Logger实例
/// </summary>
public class TestLog : ILogger
public class TestLog<T> : ILogger,ILogger<T>
{
public IDisposable BeginScope<TState>(TState state) {
throw new NotImplementedException();

View File

@ -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
{
/// <summary>
/// 执行存储过程测试
/// </summary>
[TestClass]
public class UseStoredProcedureTest
{
/// <summary>
/// 执行Oracle存储过程
/// </summary>
[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);
}
/// <summary>
/// Oracle数据库
/// </summary>
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<string>("oracle"),
DbType = SqlSugar.DbType.Oracle,
Log = sc.GetValue<bool>("log"),
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute,
};
return new SugarDbContext(dbconfig, new TestLog<SugarDbContext>());
}
}
}
}

View File

@ -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"
}
}

View File

@ -171,23 +171,33 @@ namespace Falcon.SugarApi.DatabaseDefinitions
#endregion
#region
public IAdo GetAdo() {
/// <summary>
/// 获取Ado对象执行存储过程
/// </summary>
/// <returns>Ado对象</returns>
public IAdo UseStoredProcedure() {
return this.Ado.UseStoredProcedure();
}
/// <summary>
/// 获取一个Oracle游标类型参数用作返回值
/// </summary>
/// <param name="name"></param>
/// <param name="name">参数名称</param>
/// <returns></returns>
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>(T data) {
//return SqlSugarTool.GetParameters(new { pageStart = 1, pageEnd = 5, recordCount = 0 });
throw new NotSupportedException();
/// <summary>
/// 从对象获取存储过程参数
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="data">参数对象</param>
/// <returns>参数</returns>
/// <exception cref="NotSupportedException"></exception>
public SugarParameter[] GetParameters<T>(T data) {
return this.Ado.GetParameters(data);
}
#endregion
}
}