-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlHelper.cs
More file actions
229 lines (176 loc) · 9.35 KB
/
SqlHelper.cs
File metadata and controls
229 lines (176 loc) · 9.35 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Npgsql;
using NpgsqlTypes;
using PostgreSQLCopyHelper;
namespace PostgresLoggingTrigger;
internal static class SqlHelper
{
private const string ConnectionString = "Server=localhost; Port=5432; Database=testdb; No Reset On Close=True; Username=postgres; Password=12345; Application Name=PostgresLoggingTrigger; SearchPath=trigger_test";
public static async Task DropSchemasAsync()
{
await DropSchemaAsync("trigger_test");
await DropSchemaAsync("logging");
}
public static Task CreateSchemaAsync() => ApplyFileAsync("SetupSchema.sql");
public static Task SetupLoggingUtilsAsync() => ApplyFileAsync("SetupLoggingUtils.sql");
public static Task SetupBatchLoggingUtilsAsync() => ApplyFileAsync("SetupBatchLoggingUtils.sql");
public static async Task SetupTableChangeLoggingAsync()
{
await using var connection = await OpenConnectionAsync();
await using var command = connection.CreateCommand();
command.CommandText = "SELECT logging.enable_table_changes_logging('trigger_test', 'foo')";
await command.ExecuteNonQueryAsync();
}
public static async Task PrintTableAsync(string table)
{
await using var connection = await OpenConnectionAsync();
await using var command = connection.CreateCommand();
command.CommandText = $"SELECT * FROM {table} ORDER BY id";
await using var reader = await command.ExecuteReaderAsync();
var fieldCount = reader.FieldCount;
const char separator = '\t';
// ReSharper disable AccessToDisposedClosure
Console.WriteLine(table);
Console.WriteLine(string.Join(separator, Enumerable.Range(0, fieldCount).Select(i => reader.GetName(i))));
while (await reader.ReadAsync())
{
Console.WriteLine(string.Join(separator, Enumerable.Range(0, fieldCount).Select(i => FormatValue(reader.GetValue(i)))));
}
// ReSharper restore AccessToDisposedClosure
}
public static async Task TruncateTableAsync(string table)
{
await using var connection = await OpenConnectionAsync();
await using var command = connection.CreateCommand();
command.CommandText = $"TRUNCATE TABLE {table}";
await command.ExecuteNonQueryAsync();
}
private static async Task SetApplicationUserAsync(NpgsqlConnection connection, NpgsqlTransaction transaction, string? userName)
{
if (userName is null)
{
return;
}
await using var command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandText = "SELECT logging.set_application_user(@application_user)";
command.Parameters.AddWithValue("@application_user", userName);
await command.ExecuteNonQueryAsync();
}
private static async Task DropSchemaAsync(string schema)
{
await using var connection = await OpenConnectionAsync();
await using var command = connection.CreateCommand();
command.CommandText = $"DROP SCHEMA IF EXISTS {schema} CASCADE";
await command.ExecuteNonQueryAsync();
}
private static async Task ApplyFileAsync(string fileName)
{
var commandText = await File.ReadAllTextAsync(fileName);
await using var connection = await OpenConnectionAsync();
await using var command = connection.CreateCommand();
command.CommandText = commandText;
await command.ExecuteNonQueryAsync();
}
private static string FormatValue(object? value) => value is null or DBNull ? "<NULL>" : value.ToString()!;
private static async Task<NpgsqlConnection> OpenConnectionAsync()
{
var connection = new NpgsqlConnection(ConnectionString);
await connection.OpenAsync();
return connection;
}
public static class ForFoo
{
public static Task AddAsync(Foo foo, string? userName) => AddAsync(new[] { foo }, userName);
public static async Task AddAsync(IReadOnlyList<Foo> foos, string? userName)
{
var commandBuilder = new StringBuilder(@"
INSERT INTO trigger_test.foo(added_at, unique_int_value, int_value, varchar_value) VALUES");
commandBuilder.Append(string.Join(",",
foos.Select((f, i) =>
$"(transaction_timestamp(), @unique_int_value{i}, @int_value{i}, @varchar_value{i})")));
commandBuilder.Append(
" ON CONFLICT(unique_int_value) DO UPDATE SET int_value = EXCLUDED.int_value, varchar_value=EXCLUDED.varchar_value");
await using var connection = await OpenConnectionAsync();
await using var transaction = await connection.BeginTransactionAsync();
await SetApplicationUserAsync(connection, transaction, userName);
await using var command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandText = commandBuilder.ToString();
for (var i = 0; i < foos.Count; i++)
{
var foo = foos[i];
command.Parameters.AddWithValue($"@unique_int_value{i}", foo.UniqueIntValue);
command.Parameters.AddWithValue($"@int_value{i}",
foo.IntValue.HasValue ? foo.IntValue.Value : DBNull.Value);
command.Parameters.AddWithValue($"@varchar_value{i}",
foo.VarcharValue != null ? foo.VarcharValue : DBNull.Value);
}
await command.ExecuteNonQueryAsync();
await transaction.CommitAsync();
}
public static Task DeleteAsync(Foo foo, string? userName) => DeleteAsync(new[] { foo }, userName);
public static async Task DeleteAsync(IReadOnlyList<Foo> foos, string? userName)
{
await using var connection = await OpenConnectionAsync();
await using var transaction = await connection.BeginTransactionAsync();
await SetApplicationUserAsync(connection, transaction, userName);
await using var command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandText = "DELETE FROM trigger_test.foo WHERE unique_int_value = ANY(:unique_int_values);";
command.Parameters.AddWithValue("unique_int_values", foos.Select(f => f.UniqueIntValue).ToArray())
.NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Integer;
await command.ExecuteNonQueryAsync();
await transaction.CommitAsync();
}
public static Task UpdateAsync(Foo foo, string? userName) => UpdateAsync(new[] { foo }, userName);
// just an example of bulk update, not necessarily efficient
public static async Task UpdateAsync(IReadOnlyList<Foo> foos, string? userName)
{
var commandBuilder = new StringBuilder(@"
UPDATE trigger_test.foo f SET unique_int_value = tmp.unique_int_value, int_value = tmp.int_value, varchar_value = tmp.varchar_value FROM (SELECT ");
commandBuilder.Append(string.Join(" UNION ",
foos.Select((f, i) =>
$"@unique_int_value{i} AS unique_int_value, @int_value{i} AS int_value, @varchar_value{i} AS varchar_value")));
commandBuilder.Append(") tmp WHERE f.unique_int_value = f.unique_int_value");
await using var connection = await OpenConnectionAsync();
await using var transaction = await connection.BeginTransactionAsync();
await SetApplicationUserAsync(connection, transaction, userName);
await using var command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandText = commandBuilder.ToString();
for (var i = 0; i < foos.Count; i++)
{
var foo = foos[i];
command.Parameters.AddWithValue($"@unique_int_value{i}", foo.UniqueIntValue);
command.Parameters.AddWithValue($"@int_value{i}",
foo.IntValue.HasValue ? foo.IntValue.Value : DBNull.Value);
command.Parameters.AddWithValue($"@varchar_value{i}",
foo.VarcharValue != null ? foo.VarcharValue : DBNull.Value);
}
command.Parameters.AddWithValue("unique_int_values", foos.Select(f => f.UniqueIntValue).ToArray())
.NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Integer;
await command.ExecuteNonQueryAsync();
await transaction.CommitAsync();
}
public static async Task CopyAsync(IReadOnlyList<Foo> foos, string? userName)
{
var now = DateTime.Now;
var copyHelper = new PostgreSQLCopyHelper.PostgreSQLCopyHelper<Foo>("trigger_test", "foo")
.MapTimeStamp("added_at", _ => now)
.MapInteger("unique_int_value", x => x.UniqueIntValue)
.MapNullable("int_value", x => x.IntValue, NpgsqlDbType.Integer)
.MapVarchar("varchar_value", x => x.VarcharValue);
await using var connection = await OpenConnectionAsync();
await using var transaction = await connection.BeginTransactionAsync();
await SetApplicationUserAsync(connection, transaction, userName);
await copyHelper.SaveAllAsync(connection, foos);
await transaction.CommitAsync();
}
}
}