Skip to content

Commit fc32710

Browse files
committed
- 增加 pgsql numeric -> BigInteger 映射;#1100
1 parent 7ed2d87 commit fc32710

File tree

27 files changed

+221
-242
lines changed

27 files changed

+221
-242
lines changed

Examples/base_entity/Program.cs

+60-5
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
using System.Diagnostics;
1515
using System.Linq;
1616
using System.Linq.Expressions;
17+
using System.Numerics;
1718
using System.Reflection;
1819
using System.Text.Encodings.Web;
1920
using System.Text.Json;
2021
using System.Text.Json.Serialization;
22+
using System.Text.RegularExpressions;
2123
using System.Threading;
2224
using System.Threading.Tasks;
2325

@@ -158,8 +160,11 @@ class TopicMapTypeToListDtoMap2
158160
public string Title { get; set; }
159161
public DateTime CreateTime { get; set; }
160162
}
161-
162-
163+
class tuint256tb_01
164+
{
165+
public Guid Id { get; set; }
166+
public BigInteger Number { get; set; }
167+
}
163168
class CommandTimeoutCascade : IDisposable
164169
{
165170
public static AsyncLocal<int> _asyncLocalTimeout = new AsyncLocal<int>();
@@ -177,16 +182,16 @@ static void Main(string[] args)
177182
//.UseSlave("data source=test1.db", "data source=test2.db", "data source=test3.db", "data source=test4.db")
178183
//.UseSlaveWeight(10, 1, 1, 5)
179184

180-
185+
181186
//.UseConnectionString(FreeSql.DataType.Firebird, @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5")
182187

183188

184189
//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=2")
185190

186191
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
187192

188-
//.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2")
189-
//.UseNameConvert(FreeSql.Internal.NameConvertType.ToLower)
193+
.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2")
194+
.UseNameConvert(FreeSql.Internal.NameConvertType.ToLower)
190195

191196
//.UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=2")
192197
//.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
@@ -211,6 +216,56 @@ static void Main(string[] args)
211216
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
212217
#endregion
213218

219+
if (fsql.Ado.DataType == DataType.PostgreSQL)
220+
{
221+
fsql.CodeFirst.IsNoneCommandParameter = false;
222+
fsql.Aop.AuditDataReader += (_, e) =>
223+
{
224+
var dbtype = e.DataReader.GetDataTypeName(e.Index);
225+
var m = Regex.Match(dbtype, @"numeric\((\d+)\)", RegexOptions.IgnoreCase);
226+
if (m.Success && int.Parse(m.Groups[1].Value) > 19)
227+
e.Value = e.DataReader.GetFieldValue<BigInteger>(e.Index); //否则会报溢出错误
228+
};
229+
230+
var num = BigInteger.Parse("57896044618658097711785492504343953926634992332820282019728792003956564819968");
231+
fsql.Delete<tuint256tb_01>().Where("1=1").ExecuteAffrows();
232+
if (1 != fsql.Insert(new tuint256tb_01()).ExecuteAffrows()) throw new Exception("not equal");
233+
var find = fsql.Select<tuint256tb_01>().ToList();
234+
if (find.Count != 1) throw new Exception("not single");
235+
if ("0" != find[0].Number.ToString()) throw new Exception("not equal");
236+
var item = new tuint256tb_01 { Number = num };
237+
if (1 != fsql.Insert(item).ExecuteAffrows()) throw new Exception("not equal");
238+
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
239+
if (find.Count != 1) throw new Exception("not single");
240+
if (item.Number != find[0].Number) throw new Exception("not equal");
241+
num = num - 1;
242+
item.Number = num;
243+
if (1 != fsql.Update<tuint256tb_01>().SetSource(item).ExecuteAffrows()) throw new Exception("not equal");
244+
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
245+
if (find.Count != 1) throw new Exception("not single");
246+
if ("57896044618658097711785492504343953926634992332820282019728792003956564819967" != find[0].Number.ToString()) throw new Exception("not equal");
247+
248+
num = BigInteger.Parse("57896044618658097711785492504343953926634992332820282019728792003956564819968");
249+
fsql.Delete<tuint256tb_01>().Where("1=1").ExecuteAffrows();
250+
if (1 != fsql.Insert(new tuint256tb_01()).NoneParameter().ExecuteAffrows()) throw new Exception("not equal");
251+
find = fsql.Select<tuint256tb_01>().ToList();
252+
if (find.Count != 1) throw new Exception("not single");
253+
if ("0" != find[0].Number.ToString()) throw new Exception("not equal");
254+
item = new tuint256tb_01 { Number = num };
255+
if (1 != fsql.Insert(item).NoneParameter().ExecuteAffrows()) throw new Exception("not equal");
256+
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
257+
if (find.Count != 1) throw new Exception("not single");
258+
if (item.Number != find[0].Number) throw new Exception("not equal");
259+
num = num - 1;
260+
item.Number = num;
261+
if (1 != fsql.Update<tuint256tb_01>().NoneParameter().SetSource(item).ExecuteAffrows()) throw new Exception("not equal");
262+
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
263+
if (find.Count != 1) throw new Exception("not single");
264+
if ("57896044618658097711785492504343953926634992332820282019728792003956564819967" != find[0].Number.ToString()) throw new Exception("not equal");
265+
}
266+
267+
268+
214269
fsql.Aop.CommandBefore += (_, e) =>
215270
{
216271
if (CommandTimeoutCascade._asyncLocalTimeout.Value > 0)

Examples/base_entity/base_entity.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
<ItemGroup>
1515
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
16+
<PackageReference Include="Npgsql.NetTopologySuite" Version="6.0.4" />
1617
</ItemGroup>
1718

1819
<ItemGroup>

FreeSql.Tests.VB/FreeSql.Tests.VB.vbproj

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
<PropertyGroup>
44
<RootNamespace>FreeSql.Tests.VB</RootNamespace>
5-
<TargetFramework>net5.0</TargetFramework>
6-
5+
<TargetFramework>net6.0</TargetFramework>
76
<IsPackable>false</IsPackable>
87
</PropertyGroup>
98

FreeSql.Tests/FreeSql.Tests.PerformanceTests/FreeSql.Tests.PerformanceTests.csproj

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
5-
4+
<TargetFramework>net6.0</TargetFramework>
65
<IsPackable>false</IsPackable>
76
</PropertyGroup>
87

FreeSql.Tests/FreeSql.Tests.Provider.GBase/FreeSql.Tests.Provider.GBase.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

FreeSql.Tests/FreeSql.Tests.Provider.MySqlConnector/FreeSql.Tests.Provider.MySqlConnector.csproj

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net5.0;netcoreapp3.1;</TargetFrameworks>
5-
4+
<TargetFrameworks>net6.0;netcoreapp3.1;</TargetFrameworks>
65
<IsPackable>false</IsPackable>
76
</PropertyGroup>
87

FreeSql.Tests/FreeSql.Tests.Provider.Odbc/FreeSql.Tests.Provider.Odbc.csproj

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
5-
4+
<TargetFramework>net6.0</TargetFramework>
65
<IsPackable>false</IsPackable>
76
</PropertyGroup>
87

FreeSql.Tests/FreeSql.Tests.Provider.PostgreSQL.NetTopologySuite/FreeSql.Tests.Provider.PostgreSQL.NetTopologySuite.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
5-
4+
<TargetFramework>net6.0</TargetFramework>
65
<IsPackable>false</IsPackable>
76
</PropertyGroup>
87

@@ -12,6 +11,7 @@
1211
</PropertyGroup>
1312

1413
<ItemGroup>
14+
<PackageReference Include="Npgsql.NetTopologySuite" Version="6.0.4" />
1515
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
1616
<PackageReference Include="xunit" Version="2.4.1" />
1717
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">

FreeSql.Tests/FreeSql.Tests.Provider.PostgreSQL.NetTopologySuite/PostgreSQL/PostgreSQLCodeFirstTest.cs

+64-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using FreeSql.DataAnnotations;
1+
using FreeSql.DataAnnotations;
22
using NetTopologySuite.Geometries;
33
using Newtonsoft.Json;
44
using Newtonsoft.Json.Linq;
@@ -11,19 +11,75 @@
1111
using System.Linq;
1212
using System.Net;
1313
using System.Net.NetworkInformation;
14+
using System.Numerics;
1415
using System.Text;
16+
using System.Text.RegularExpressions;
1517
using Xunit;
1618

1719
namespace FreeSql.Tests.PostgreSQL.NetTopologySuite
1820
{
1921
public class PostgreSQLCodeFirstTest
2022
{
2123

24+
[Fact]
25+
public void UInt256Crud2()
26+
{
27+
var fsql = g.pgsql;
28+
fsql.Aop.AuditDataReader += (_, e) =>
29+
{
30+
var dbtype = e.DataReader.GetDataTypeName(e.Index);
31+
var m = Regex.Match(dbtype, @"numeric\((\d+)\)", RegexOptions.IgnoreCase);
32+
if (m.Success && int.Parse(m.Groups[1].Value) > 19)
33+
e.Value = e.DataReader.GetFieldValue<BigInteger>(e.Index); //否则会报溢出错误
34+
};
35+
36+
var num = BigInteger.Parse("57896044618658097711785492504343953926634992332820282019728792003956564819968");
37+
fsql.Delete<tuint256tb_01>().Where("1=1").ExecuteAffrows();
38+
Assert.Equal(1, fsql.Insert(new tuint256tb_01()).ExecuteAffrows());
39+
var find = fsql.Select<tuint256tb_01>().ToList();
40+
Assert.Single(find);
41+
Assert.Equal("0", find[0].Number.ToString());
42+
var item = new tuint256tb_01 { Number = num };
43+
Assert.Equal(1, fsql.Insert(item).ExecuteAffrows());
44+
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
45+
Assert.Single(find);
46+
Assert.Equal(item.Number, find[0].Number);
47+
num = num - 1;
48+
item.Number = num;
49+
Assert.Equal(1, fsql.Update<tuint256tb_01>().SetSource(item).ExecuteAffrows());
50+
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
51+
Assert.Single(find);
52+
Assert.Equal("57896044618658097711785492504343953926634992332820282019728792003956564819967", find[0].Number.ToString());
53+
54+
num = BigInteger.Parse("57896044618658097711785492504343953926634992332820282019728792003956564819968");
55+
fsql.Delete<tuint256tb_01>().Where("1=1").ExecuteAffrows();
56+
Assert.Equal(1, fsql.Insert(new tuint256tb_01()).NoneParameter().ExecuteAffrows());
57+
find = fsql.Select<tuint256tb_01>().ToList();
58+
Assert.Single(find);
59+
Assert.Equal("0", find[0].Number.ToString());
60+
item = new tuint256tb_01 { Number = num };
61+
Assert.Equal(1, fsql.Insert(item).NoneParameter().ExecuteAffrows());
62+
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
63+
Assert.Single(find);
64+
Assert.Equal(item.Number, find[0].Number);
65+
num = num - 1;
66+
item.Number = num;
67+
Assert.Equal(1, fsql.Update<tuint256tb_01>().NoneParameter().SetSource(item).ExecuteAffrows());
68+
find = fsql.Select<tuint256tb_01>().Where(a => a.Id == item.Id).ToList();
69+
Assert.Single(find);
70+
Assert.Equal("57896044618658097711785492504343953926634992332820282019728792003956564819967", find[0].Number.ToString());
71+
}
72+
class tuint256tb_01
73+
{
74+
public Guid Id { get; set; }
75+
public BigInteger Number { get; set; }
76+
}
77+
2278
[Fact]
2379
public void GetComparisonDDLStatements()
2480
{
2581
var sql = g.pgsql.CodeFirst.GetComparisonDDLStatements<TableAllType>();
26-
Assert.True(string.IsNullOrEmpty(sql)); //测试运行两次后
82+
Assert.True(string.IsNullOrEmpty(sql)); //测试运行两次后
2783
g.pgsql.Select<TableAllType>();
2884
}
2985

@@ -43,8 +99,8 @@ public void CurdAllField()
4399

44100
var item2 = new TableAllType
45101
{
46-
testFieldBitArray = new BitArray(Encoding.UTF8.GetBytes("我是")),
47-
testFieldBitArrayArray = new[] { new BitArray(Encoding.UTF8.GetBytes("中国")), new BitArray(Encoding.UTF8.GetBytes("公民")) },
102+
testFieldBitArray = new BitArray(Encoding.UTF8.GetBytes("我是")),
103+
testFieldBitArrayArray = new[] { new BitArray(Encoding.UTF8.GetBytes("中国")), new BitArray(Encoding.UTF8.GetBytes("公民")) },
48104
testFieldBool = true,
49105
testFieldBoolArray = new[] { true, true, false, false },
50106
testFieldBoolArrayNullable = new bool?[] { true, true, null, false, false },
@@ -53,8 +109,8 @@ public void CurdAllField()
53109
testFieldByteArray = new byte[] { 0, 1, 2, 3, 4, 5, 6 },
54110
testFieldByteArrayNullable = new byte?[] { 0, 1, 2, 3, null, 4, 5, 6 },
55111
testFieldByteNullable = byte.MinValue,
56-
testFieldBytes = Encoding.UTF8.GetBytes("我是中国人"),
57-
testFieldBytesArray = new[] { Encoding.UTF8.GetBytes("我是中国人"), Encoding.UTF8.GetBytes("我是中国人") },
112+
testFieldBytes = Encoding.UTF8.GetBytes("我是中国人"),
113+
testFieldBytesArray = new[] { Encoding.UTF8.GetBytes("我是中国人"), Encoding.UTF8.GetBytes("我是中国人") },
58114
testFieldCidr = (IPAddress.Parse("10.0.0.0"), 8),
59115
testFieldCidrArray = new[] { (IPAddress.Parse("10.0.0.0"), 8), (IPAddress.Parse("192.168.0.0"), 16) },
60116
testFieldCidrArrayNullable = new (IPAddress, int)?[] { (IPAddress.Parse("10.0.0.0"), 8), null, (IPAddress.Parse("192.168.0.0"), 16) },
@@ -229,9 +285,9 @@ public void CurdAllField()
229285
testFieldShortArray = new short[] { 1, 2, 3, 4, 5 },
230286
testFieldShortArrayNullable = new short?[] { 1, 2, 3, null, 4, 5 },
231287
testFieldShortNullable = short.MinValue,
232-
testFieldString = "我是中国人string'\\?!@#$%^&*()_+{}}{~?><<>",
288+
testFieldString = "我是中国人string'\\?!@#$%^&*()_+{}}{~?><<>",
233289
testFieldChar = 'X',
234-
testFieldStringArray = new[] { "我是中国人String1", "我是中国人String2", null, "我是中国人String3" },
290+
testFieldStringArray = new[] { "我是中国人String1", "我是中国人String2", null, "我是中国人String3" },
235291
testFieldTimeSpan = TimeSpan.FromDays(1),
236292
testFieldTimeSpanArray = new[] { TimeSpan.FromDays(1), TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(60) },
237293
testFieldTimeSpanArrayNullable = new TimeSpan?[] { TimeSpan.FromDays(1), TimeSpan.FromSeconds(10), null, TimeSpan.FromSeconds(60) },

FreeSql.Tests/FreeSql.Tests.Provider.Sqlite.Data/FreeSql.Tests.Provider.Sqlite.Data.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
55
<Nullable>enable</Nullable>
6-
76
<IsPackable>false</IsPackable>
87
</PropertyGroup>
98

FreeSql.Tests/FreeSql.Tests/ExpressionTree/GetDataReaderValueBlockExpressionTest.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
using FreeSql.DataAnnotations;
2-
using FreeSql;
1+
using FreeSql.Internal;
32
using System;
4-
using System.Collections.Generic;
5-
using Xunit;
6-
using System.Linq;
7-
using Newtonsoft.Json.Linq;
8-
using NpgsqlTypes;
9-
using Npgsql.LegacyPostgis;
10-
using FreeSql.Internal;
113
using System.Linq.Expressions;
4+
using Xunit;
125

136
namespace FreeSql.ExpressionTree
147
{

FreeSql.Tests/FreeSql.Tests/Internal/CommonUtilsTest.cs

+5-18
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
1-
using FreeSql.DataAnnotations;
2-
using FreeSql;
3-
using System;
4-
using System.Collections.Generic;
5-
using Xunit;
6-
using System.Linq;
7-
using Newtonsoft.Json.Linq;
8-
using NpgsqlTypes;
9-
using Npgsql.LegacyPostgis;
10-
using System.Linq.Expressions;
11-
using System.Threading.Tasks;
12-
using System.ComponentModel.DataAnnotations;
13-
using System.Threading;
14-
using System.Data.SqlClient;
15-
using kwlib;
16-
using System.Diagnostics;
17-
using System.IO;
18-
using System.Text;
1+
using FreeSql;
192
using FreeSql.Internal;
203
using FreeSql.Internal.CommonProvider;
4+
using System;
5+
using System.ComponentModel.DataAnnotations;
6+
using System.Linq;
7+
using Xunit;
218

229
namespace FreeSql.InternalTests
2310
{

FreeSql.Tests/FreeSql.Tests/Issues/1021.cs

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,4 @@
1-
using FreeSql.DataAnnotations;
2-
using FreeSql;
3-
using System;
4-
using System.Collections.Generic;
5-
using Xunit;
6-
using System.Linq;
7-
using Newtonsoft.Json.Linq;
8-
using NpgsqlTypes;
9-
using Npgsql.LegacyPostgis;
10-
using System.Linq.Expressions;
11-
using System.Threading.Tasks;
12-
using System.ComponentModel.DataAnnotations;
13-
using System.Threading;
14-
using System.Data.SqlClient;
15-
using kwlib;
16-
using System.Diagnostics;
17-
using System.IO;
18-
using System.Text;
1+
using Xunit;
192

203
namespace FreeSql.Tests.Issues
214
{

FreeSql.Tests/FreeSql.Tests/Issues/1092.cs

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
using FreeSql.DataAnnotations;
2-
using FreeSql;
3-
using System;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
44
using System.Collections.Generic;
55
using Xunit;
6-
using System.Linq;
7-
using Newtonsoft.Json.Linq;
8-
using NpgsqlTypes;
9-
using Npgsql.LegacyPostgis;
10-
using System.Linq.Expressions;
11-
using System.Threading.Tasks;
12-
using System.ComponentModel.DataAnnotations;
13-
using System.Threading;
14-
using System.Data.SqlClient;
15-
using kwlib;
16-
using System.Diagnostics;
17-
using System.IO;
18-
using System.Text;
19-
using Newtonsoft.Json;
206

217
namespace FreeSql.Tests.Issues
228
{

0 commit comments

Comments
 (0)