-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
175 lines (154 loc) · 5.11 KB
/
Program.cs
File metadata and controls
175 lines (154 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using DotNet.Testcontainers.Builders;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Threading.Tasks;
using Testcontainers.PostgreSql;
namespace NpgsqlBenchmark
{
internal class Program
{
static async Task Main(string[] args)
{
var items = new List<BigNumberModel>()
{
new BigNumberModel
{
BigInteger = new System.Numerics.BigInteger(792281625)
},
new BigNumberModel
{
BigInteger = new System.Numerics.BigInteger(992281642)
}
};
await using var postgre =
new PostgreSqlBuilder()
.WithImage("postgres:16.0")
.WithPassword("dhgvbh73j")
.WithPortBinding(5432, true)
.WithAutoRemove(true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilExternalTcpPortIsAvailable(5432))
.Build();
await postgre.StartAsync();
await postgre.WaitContainerStateRunningAsync(TimeSpan.FromMinutes(1));
await postgre.WaitResponseAsync(TimeSpan.FromMinutes(1));
await using (var masterConnection = new NpgsqlConnection(postgre.GetConnectionString()))
{
await masterConnection.OpenAsync();
await using var command = masterConnection.CreateCommand();
command.CommandText = $@"
SELECT
datname
FROM
pg_database
WHERE
datname='gedaqtests'
;
";
var dbName = (string)await command.ExecuteScalarAsync();
if (dbName == null)
{
await using var createCmd = masterConnection.CreateCommand();
createCmd.CommandText = $@"
CREATE DATABASE gedaqtests TEMPLATE template0 CONNECTION LIMIT = -1;
;
";
createCmd.ExecuteNonQuery();
}
}
var builder = new NpgsqlConnectionStringBuilder(postgre.GetConnectionString());
builder.Database = "gedaqtests";
var dataSourceBuilder = new NpgsqlDataSourceBuilder(builder.ConnectionString);
await using var npgsqlDataSource = dataSourceBuilder.Build();
{
await using var connection = await npgsqlDataSource.OpenConnectionAsync();
await using var cmd = connection.CreateCommand();
cmd.CommandText = @"
CREATE TABLE public.bigintegernumeric
(
value numeric NOT NULL
);
";
await cmd.ExecuteNonQueryAsync();
}
await ImportAsync(npgsqlDataSource, items);
var exportedItems = await ExportAsync(npgsqlDataSource);
}
private static async Task ImportAsync(NpgsqlDataSource npgsqlDataSource, IEnumerable<BigNumberModel> collection)
{
await using var connection = await npgsqlDataSource.OpenConnectionAsync();
NpgsqlBinaryImporter import = null;
try
{
import = connection.BeginBinaryImport(@"
COPY public.bigintegernumeric
(
value
)
FROM STDIN (FORMAT BINARY)
");
foreach (var item in collection)
{
import.StartRow();
import.Write(item.BigInteger, NpgsqlTypes.NpgsqlDbType.Numeric);
}
import.Complete();
import.Dispose();
import = null;
}
finally
{
if (import != null)
{
try
{
import.Close();
}
catch { /* ignore */ }
import.Dispose();
}
}
}
private static async Task<List<BigNumberModel>> ExportAsync(NpgsqlDataSource npgsqlDataSource)
{
await using var connection = await npgsqlDataSource.OpenConnectionAsync();
NpgsqlBinaryExporter export = null;
try
{
export = await connection.BeginBinaryExportAsync(@"
COPY public.bigintegernumeric
(
value
) TO STDOUT (FORMAT BINARY)
");
var list = new List<BigNumberModel>();
while (await export.StartRowAsync() != -1)
{
var item = new BigNumberModel();
item.BigInteger = await export.ReadAsync<System.Numerics.BigInteger>(NpgsqlTypes.NpgsqlDbType.Numeric);
list.Add(item);
}
await export.DisposeAsync();
export = null;
return list;
}
finally
{
if (export != null)
{
try
{
await export.CancelAsync();
}
catch { /* ignore */ }
await export.DisposeAsync();
}
}
}
}
public class BigNumberModel
{
public BigInteger BigInteger { get; set; }
}
}