- 增加 pgsql numeric -> BigInteger 映射;#1100

This commit is contained in:
2881099
2022-05-13 12:32:26 +08:00
parent 7ed2d87b8d
commit fc32710421
27 changed files with 223 additions and 244 deletions

View File

@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
@ -12,6 +11,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql.NetTopologySuite" Version="6.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">

View File

@ -1,4 +1,4 @@
using FreeSql.DataAnnotations;
using FreeSql.DataAnnotations;
using NetTopologySuite.Geometries;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@ -11,7 +11,9 @@ using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using Xunit;
namespace FreeSql.Tests.PostgreSQL.NetTopologySuite
@ -19,11 +21,65 @@ namespace FreeSql.Tests.PostgreSQL.NetTopologySuite
public class PostgreSQLCodeFirstTest
{
[Fact]
public void UInt256Crud2()
{
var fsql = g.pgsql;
fsql.Aop.AuditDataReader += (_, e) =>
{
var dbtype = e.DataReader.GetDataTypeName(e.Index);
var m = Regex.Match(dbtype, @"numeric\((\d+)\)", RegexOptions.IgnoreCase);
if (m.Success && int.Parse(m.Groups[1].Value) > 19)
e.Value = e.DataReader.GetFieldValue<BigInteger>(e.Index); //否则会报溢出错误
};
var num = BigInteger.Parse("57896044618658097711785492504343953926634992332820282019728792003956564819968");
fsql.Delete<tuint256tb_01>().Where("1=1").ExecuteAffrows();
Assert.Equal(1, fsql.Insert(new tuint256tb_01()).ExecuteAffrows());
var find = fsql.Select<tuint256tb_01>().ToList();
Assert.Single(find);
Assert.Equal("0", find[0].Number.ToString());
var item = new tuint256tb_01 { Number = num };
Assert.Equal(1, fsql.Insert(item).ExecuteAffrows());
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
Assert.Single(find);
Assert.Equal(item.Number, find[0].Number);
num = num - 1;
item.Number = num;
Assert.Equal(1, fsql.Update<tuint256tb_01>().SetSource(item).ExecuteAffrows());
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
Assert.Single(find);
Assert.Equal("57896044618658097711785492504343953926634992332820282019728792003956564819967", find[0].Number.ToString());
num = BigInteger.Parse("57896044618658097711785492504343953926634992332820282019728792003956564819968");
fsql.Delete<tuint256tb_01>().Where("1=1").ExecuteAffrows();
Assert.Equal(1, fsql.Insert(new tuint256tb_01()).NoneParameter().ExecuteAffrows());
find = fsql.Select<tuint256tb_01>().ToList();
Assert.Single(find);
Assert.Equal("0", find[0].Number.ToString());
item = new tuint256tb_01 { Number = num };
Assert.Equal(1, fsql.Insert(item).NoneParameter().ExecuteAffrows());
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
Assert.Single(find);
Assert.Equal(item.Number, find[0].Number);
num = num - 1;
item.Number = num;
Assert.Equal(1, fsql.Update<tuint256tb_01>().NoneParameter().SetSource(item).ExecuteAffrows());
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
Assert.Single(find);
Assert.Equal("57896044618658097711785492504343953926634992332820282019728792003956564819967", find[0].Number.ToString());
}
class tuint256tb_01
{
public Guid Id { get; set; }
public BigInteger Number { get; set; }
}
[Fact]
public void GetComparisonDDLStatements()
{
var sql = g.pgsql.CodeFirst.GetComparisonDDLStatements<TableAllType>();
Assert.True(string.IsNullOrEmpty(sql)); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD>
Assert.True(string.IsNullOrEmpty(sql)); //测试运行两次后
g.pgsql.Select<TableAllType>();
}
@ -43,8 +99,8 @@ namespace FreeSql.Tests.PostgreSQL.NetTopologySuite
var item2 = new TableAllType
{
testFieldBitArray = new BitArray(Encoding.UTF8.GetBytes("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>")),
testFieldBitArrayArray = new[] { new BitArray(Encoding.UTF8.GetBytes("<EFBFBD>й<EFBFBD>")), new BitArray(Encoding.UTF8.GetBytes("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>")) },
testFieldBitArray = new BitArray(Encoding.UTF8.GetBytes("我是")),
testFieldBitArrayArray = new[] { new BitArray(Encoding.UTF8.GetBytes("中国")), new BitArray(Encoding.UTF8.GetBytes("公民")) },
testFieldBool = true,
testFieldBoolArray = new[] { true, true, false, false },
testFieldBoolArrayNullable = new bool?[] { true, true, null, false, false },
@ -53,8 +109,8 @@ namespace FreeSql.Tests.PostgreSQL.NetTopologySuite
testFieldByteArray = new byte[] { 0, 1, 2, 3, 4, 5, 6 },
testFieldByteArrayNullable = new byte?[] { 0, 1, 2, 3, null, 4, 5, 6 },
testFieldByteNullable = byte.MinValue,
testFieldBytes = Encoding.UTF8.GetBytes("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD>"),
testFieldBytesArray = new[] { Encoding.UTF8.GetBytes("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD>"), Encoding.UTF8.GetBytes("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD>") },
testFieldBytes = Encoding.UTF8.GetBytes("我是中国人"),
testFieldBytesArray = new[] { Encoding.UTF8.GetBytes("我是中国人"), Encoding.UTF8.GetBytes("我是中国人") },
testFieldCidr = (IPAddress.Parse("10.0.0.0"), 8),
testFieldCidrArray = new[] { (IPAddress.Parse("10.0.0.0"), 8), (IPAddress.Parse("192.168.0.0"), 16) },
testFieldCidrArrayNullable = new (IPAddress, int)?[] { (IPAddress.Parse("10.0.0.0"), 8), null, (IPAddress.Parse("192.168.0.0"), 16) },
@ -229,9 +285,9 @@ namespace FreeSql.Tests.PostgreSQL.NetTopologySuite
testFieldShortArray = new short[] { 1, 2, 3, 4, 5 },
testFieldShortArrayNullable = new short?[] { 1, 2, 3, null, 4, 5 },
testFieldShortNullable = short.MinValue,
testFieldString = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD>string'\\?!@#$%^&*()_+{}}{~?><<>",
testFieldString = "我是中国人string'\\?!@#$%^&*()_+{}}{~?><<>",
testFieldChar = 'X',
testFieldStringArray = new[] { "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD>String1", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD>String2", null, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD>String3" },
testFieldStringArray = new[] { "我是中国人String1", "我是中国人String2", null, "我是中国人String3" },
testFieldTimeSpan = TimeSpan.FromDays(1),
testFieldTimeSpanArray = new[] { TimeSpan.FromDays(1), TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(60) },
testFieldTimeSpanArrayNullable = new TimeSpan?[] { TimeSpan.FromDays(1), TimeSpan.FromSeconds(10), null, TimeSpan.FromSeconds(60) },