FreeSql icon indicating copy to clipboard operation
FreeSql copied to clipboard

PostgreSQL pgsql numeric -> BigInteger 映射方法 int256

Open 2881099 opened this issue 3 years ago • 0 comments

确实已安装:FreeSql.Provider.PostgreSQL v3.2.640+


第一步:单独引用 Npgsql.NetTopologySuite.dll 6.0.4

原因是 npgsql 6.0+ 才处理了 numeric -> BigInteger 映射

如果不升级报错 Can't cast database type numeric to BigInteger


第二步:处理读取,AOP 事件全局设置一次

如果不处理报错 Numeric value does not fit in a System.Decimal

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); //否则会报溢出错误
};

第三步:测试添加、更新、查询

class tuint256tb_01
{
    public Guid Id { get; set; }
    public BigInteger Number { get; set; }
}

var num = BigInteger.Parse("57896044618658097711785492504343953926634992332820282019728792003956564819968");
fsql.Delete<tuint256tb_01>().Where("1=1").ExecuteAffrows();
if (1 != fsql.Insert(new tuint256tb_01()).ExecuteAffrows()) throw new Exception("not equal");
var find = fsql.Select<tuint256tb_01>().ToList();
if (find.Count != 1) throw new Exception("not single");
if ("0" != find[0].Number.ToString()) throw new Exception("not equal");
var item = new tuint256tb_01 { Number = num };
if (1 != fsql.Insert(item).ExecuteAffrows()) throw new Exception("not equal");
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
if (find.Count != 1) throw new Exception("not single");
if (item.Number != find[0].Number) throw new Exception("not equal");
num = num - 1;
item.Number = num;
if (1 != fsql.Update<tuint256tb_01>().SetSource(item).ExecuteAffrows()) throw new Exception("not equal");
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
if (find.Count != 1) throw new Exception("not single");
if ("57896044618658097711785492504343953926634992332820282019728792003956564819967" != find[0].Number.ToString()) throw new Exception("not equal");

num = BigInteger.Parse("57896044618658097711785492504343953926634992332820282019728792003956564819968");
fsql.Delete<tuint256tb_01>().Where("1=1").ExecuteAffrows();
if (1 != fsql.Insert(new tuint256tb_01()).NoneParameter().ExecuteAffrows()) throw new Exception("not equal");
find = fsql.Select<tuint256tb_01>().ToList();
if (find.Count != 1) throw new Exception("not single");
if ("0" != find[0].Number.ToString()) throw new Exception("not equal");
item = new tuint256tb_01 { Number = num };
if (1 != fsql.Insert(item).NoneParameter().ExecuteAffrows()) throw new Exception("not equal");
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
if (find.Count != 1) throw new Exception("not single");
if (item.Number != find[0].Number) throw new Exception("not equal");
num = num - 1;
item.Number = num;
if (1 != fsql.Update<tuint256tb_01>().NoneParameter().SetSource(item).ExecuteAffrows()) throw new Exception("not equal");
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
if (find.Count != 1) throw new Exception("not single");
if ("57896044618658097711785492504343953926634992332820282019728792003956564819967" != find[0].Number.ToString()) throw new Exception("not equal");

2881099 avatar May 13 '22 03:05 2881099