Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgreSQL pgsql numeric -> BigInteger 映射方法 int256 #1100

Closed
2881099 opened this issue May 13, 2022 · 0 comments
Closed

PostgreSQL pgsql numeric -> BigInteger 映射方法 int256 #1100

2881099 opened this issue May 13, 2022 · 0 comments

Comments

@2881099
Copy link
Collaborator

2881099 commented May 13, 2022

确实已安装: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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant