-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDatabase.cs
More file actions
637 lines (528 loc) · 22.9 KB
/
Copy pathDatabase.cs
File metadata and controls
637 lines (528 loc) · 22.9 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
using System.Collections;
using System.Globalization;
using System.Reflection;
using Blaze3SDK.Blaze.GameReporting;
using NLog;
using Npgsql;
using Tdf;
using ZamboniCommonComponents.Structs.TdfTagged;
using GameReport = Blaze3SDK.Blaze.GameReportingLegacy.GameReport;
namespace Zamboni3;
public class Database
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public readonly static string ConnectionString = Program.ZamboniConfig.DatabaseConnectionString;
public readonly bool isEnabled;
private static readonly Dictionary<string, HashSet<string>> _knownColumns = new();
private static readonly Dictionary<string, string> ColumnRenames = new() { ["ctid"] = "ct_id" };
private ulong fallbackGameIdCounter = 1;
public Database()
{
try
{
using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
isEnabled = true;
Logger.Warn("Database is accessible.");
}
catch (Exception)
{
isEnabled = false;
Logger.Warn("Database is not accessible. Gamedata wont be saved");
return;
}
CreateGameIdSequence();
CreateGamesTable();
CreateReportTable();
CreateSoReportTable();
CreateOtpReportTable();
CreateHutReportTable();
CreateLegacyGamesTable();
CreateLegacyReportTable();
CreateLegacyOtpReportTable();
CreateLegacySoReportTable();
CreateLegacyHutReportTable();
CreateUserSettingsTable();
}
private void CreateGameIdSequence()
{
using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
const string createSequenceQuery = @"
CREATE SEQUENCE IF NOT EXISTS zamboni_game_id_seq
START 1
INCREMENT 1;
";
using var cmd = new NpgsqlCommand(createSequenceQuery, conn);
cmd.ExecuteNonQuery();
}
private void CreateGamesTable()
{
using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
const string createTableQuery = @"
CREATE TABLE IF NOT EXISTS games (
game_id NUMERIC(20,0) PRIMARY KEY,
gtyp VARCHAR,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);";
using var cmd = new NpgsqlCommand(createTableQuery, conn);
cmd.ExecuteNonQuery();
}
private void CreateReportTable()
{
using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
const string createTableQuery = @"
CREATE TABLE IF NOT EXISTS reports_vs (
game_id NUMERIC(20,0) NOT NULL,
user_id NUMERIC(20,0) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (game_id, user_id)
);";
using var cmd = new NpgsqlCommand(createTableQuery, conn);
cmd.ExecuteNonQuery();
}
private void CreateSoReportTable()
{
using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
const string createTableQuery = @"
CREATE TABLE IF NOT EXISTS reports_so (
game_id NUMERIC(20,0) NOT NULL,
user_id NUMERIC(20,0) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (game_id, user_id)
);";
using var cmd = new NpgsqlCommand(createTableQuery, conn);
cmd.ExecuteNonQuery();
}
private void CreateOtpReportTable()
{
using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
const string createTableQuery = @"
CREATE TABLE IF NOT EXISTS reports_otp (
game_id NUMERIC(20,0) NOT NULL,
user_id NUMERIC(20,0) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (game_id, user_id)
);";
using var cmd = new NpgsqlCommand(createTableQuery, conn);
cmd.ExecuteNonQuery();
}
private void CreateHutReportTable()
{
using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
const string createTableQuery = @"
CREATE TABLE IF NOT EXISTS reports_hut (
game_id NUMERIC(20,0) NOT NULL,
user_id NUMERIC(20,0) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (game_id, user_id)
);";
using var cmd = new NpgsqlCommand(createTableQuery, conn);
cmd.ExecuteNonQuery();
}
private void CreateLegacyGamesTable()
{
using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
const string createTableQuery = @"
CREATE TABLE IF NOT EXISTS games_l (
game_id BIGINT PRIMARY KEY,
fnsh BOOLEAN,
gtyp INTEGER,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);";
using var cmd = new NpgsqlCommand(createTableQuery, conn);
cmd.ExecuteNonQuery();
}
private void CreateLegacyReportTable()
{
using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
const string createTableQuery = @"
CREATE TABLE IF NOT EXISTS reports_l (
-- Primary Keys / Identifiers
game_id BIGINT NOT NULL,
user_id BIGINT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (game_id, user_id)
);";
using var cmd = new NpgsqlCommand(createTableQuery, conn);
cmd.ExecuteNonQuery();
}
private void CreateLegacySoReportTable()
{
using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
const string createTableQuery = @"
CREATE TABLE IF NOT EXISTS so_reports_l (
-- Primary Keys / Identifiers (Assumed)
game_id BIGINT NOT NULL,
user_id BIGINT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (game_id, user_id)
);";
using var cmd = new NpgsqlCommand(createTableQuery, conn);
cmd.ExecuteNonQuery();
}
private void CreateLegacyOtpReportTable()
{
using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
const string createTableQuery = @"
CREATE TABLE IF NOT EXISTS otp_reports_l (
-- Primary Keys / Identifiers
game_id BIGINT NOT NULL,
user_id BIGINT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (game_id, user_id)
);";
using var cmd = new NpgsqlCommand(createTableQuery, conn);
cmd.ExecuteNonQuery();
}
private void CreateLegacyHutReportTable()
{
using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
const string createTableQuery = @"
CREATE TABLE IF NOT EXISTS hut_reports_l (
-- Primary Keys / Identifiers
game_id BIGINT NOT NULL,
user_id BIGINT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (game_id, user_id)
);";
using var cmd = new NpgsqlCommand(createTableQuery, conn);
cmd.ExecuteNonQuery();
}
private void CreateUserSettingsTable()
{
using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
const string createTableQuery = @"
CREATE TABLE IF NOT EXISTS user_settings (
user_id BIGINT NOT NULL,
config_key TEXT NOT NULL,
config_value TEXT NOT NULL,
PRIMARY KEY (user_id, config_key)
);";
using var cmd = new NpgsqlCommand(createTableQuery, conn);
cmd.ExecuteNonQuery();
}
public static async Task<SortedDictionary<string, string>> GetAllUserSettings(long userId)
{
var settings = new SortedDictionary<string, string>();
await using var conn = new NpgsqlConnection(ConnectionString);
await conn.OpenAsync();
const string sql = "SELECT config_key, config_value FROM user_settings WHERE user_id = @userId;";
await using var cmd = new NpgsqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@userId", userId);
await using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
settings.Add(reader.GetString(0), reader.GetString(1));
}
return settings;
}
public static async Task<string> GetUserSetting(long userId, string configKey)
{
await using var conn = new NpgsqlConnection(ConnectionString);
await conn.OpenAsync();
const string sql = "SELECT config_value FROM user_settings WHERE user_id = @userId AND config_key = @configKey;";
await using var cmd = new NpgsqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@userId", userId);
cmd.Parameters.AddWithValue("@configKey", configKey);
await using var reader = await cmd.ExecuteReaderAsync();
if (await reader.ReadAsync())
{
return reader.GetString(0);
}
return "";
}
public static async Task SetUserSetting(long userId, string configKey, string configValue)
{
await using var conn = new NpgsqlConnection(ConnectionString);
await conn.OpenAsync();
const string sql = @"
INSERT INTO user_settings (user_id, config_key, config_value)
VALUES (@user_id, @config_key, @config_value)
ON CONFLICT (user_id, config_key)
DO UPDATE SET config_value = EXCLUDED.config_value;";
await using var cmd = new NpgsqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@user_id", userId);
cmd.Parameters.AddWithValue("@config_key", configKey);
cmd.Parameters.AddWithValue("@config_value", configValue);
await cmd.ExecuteNonQueryAsync();
}
public async Task InsertReport(SubmitGameReportRequest request, ulong reporterUserId)
{
await InsertGameData(request);
await InsertReportData(request, reporterUserId);
}
private static async Task InsertGameData(SubmitGameReportRequest request)
{
await using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
var gameId = (decimal)request.mGameReport.mGameReportingId;
var gameType = request.mGameReport.mGameTypeName;
var reportData = ((Report)request.mGameReport.mReport).mGameInfoReport;
const string insertMainQuery = @"
INSERT INTO games (game_id, gtyp) VALUES (@game_id, @gtyp)
ON CONFLICT (game_id) DO NOTHING;";
await using (var cmd = new NpgsqlCommand(insertMainQuery, conn))
{
cmd.Parameters.AddWithValue("game_id", gameId);
cmd.Parameters.AddWithValue("gtyp", gameType);
cmd.ExecuteNonQuery();
}
await ProcessObject(conn, "games", reportData, gameId);
}
private static async Task ProcessObject(NpgsqlConnection conn, string table, object? obj, decimal gameId, ulong? userId = null, ulong? reporterUserId = null)
{
if (obj == null) return;
if (userId != null && reporterUserId != null)
{
if (WhoReportedTuple.Count > 100) WhoReportedTuple.RemoveRange(0, 50);
if (WhoReportedTuple.Contains(((ulong GameId, ulong PlayerToBeReported, ulong Reporter))(gameId, userId, userId)))
{
return;
}
}
foreach (var field in obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
var value = field.GetValue(obj);
if (value == null) continue;
if (value is IDictionary dict)
{
foreach (DictionaryEntry entry in dict)
await ExecuteDynamicUpsert(conn, table, entry.Key.ToString()!, entry.Value, gameId, userId);
continue;
}
if (!field.FieldType.IsPrimitive && field.FieldType != typeof(string) && field.FieldType != typeof(decimal))
{
await ProcessObject(conn, table, value, gameId, userId);
continue;
}
var tag = field.GetCustomAttribute<TdfMember>()?.Tag;
if (tag != null) await ExecuteDynamicUpsert(conn, table, tag, value, gameId, userId);
}
if (userId != null && reporterUserId != null)
{
WhoReportedTuple.Add(((ulong GameId, ulong PlayerToBeReported, ulong Reporter))(gameId, userId, reporterUserId));
}
}
private static async Task ExecuteDynamicUpsert(NpgsqlConnection conn, string table, string tag, object? value, decimal game_id, ulong? user_id)
{
var query = "";
var column = ToColumn(tag);
var mapped = MapType(value);
EnsureColumn(conn, table, column, mapped);
if (table.Equals("games"))
query = $@"
INSERT INTO games (game_id, {column}) VALUES (@game_id, @value)
ON CONFLICT (game_id) DO UPDATE SET {column} = EXCLUDED.{column};";
else if (table.Equals("reports_vs"))
query = $@"
INSERT INTO reports_vs (game_id, user_id, {column}) VALUES (@game_id, @user_id, @value)
ON CONFLICT (game_id, user_id) DO UPDATE SET {column} = EXCLUDED.{column};";
else if (table.Equals("reports_so"))
query = $@"
INSERT INTO reports_so (game_id, user_id, {column}) VALUES (@game_id, @user_id, @value)
ON CONFLICT (game_id, user_id) DO UPDATE SET {column} = EXCLUDED.{column};";
else if (table.Equals("reports_otp"))
query = $@"
INSERT INTO reports_otp (game_id, user_id, {column}) VALUES (@game_id, @user_id, @value)
ON CONFLICT (game_id, user_id) DO UPDATE SET {column} = EXCLUDED.{column};";
else if (table.Equals("reports_hut"))
query = $@"
INSERT INTO reports_hut (game_id, user_id, {column}) VALUES (@game_id, @user_id, @value)
ON CONFLICT (game_id, user_id) DO UPDATE SET {column} = EXCLUDED.{column};";
await using var cmd = new NpgsqlCommand(query, conn);
cmd.Parameters.AddWithValue("game_id", game_id);
if (user_id.HasValue) cmd.Parameters.AddWithValue("user_id", (decimal)user_id.Value);
cmd.Parameters.AddWithValue("value", mapped);
cmd.ExecuteNonQuery();
}
private static async Task InsertReportData(SubmitGameReportRequest request, ulong reporterUserId)
{
await using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
var table = request.mGameReport.mGameTypeName switch
{
"gameType1" => "reports_vs",
"gameType2" => "reports_so",
"gameType3" => "reports_otp",
"gameType6" => "reports_hut",
_ => throw new NotImplementedException($"Game type {request.mGameReport.mGameTypeName} is not mapped.")
};
var gameId = (decimal)request.mGameReport.mGameReportingId;
var reportData = ((Report)request.mGameReport.mReport).mPlayerReports;
foreach (var user_id in reportData.Keys)
{
var insertMainQuery = $@"
INSERT INTO {table} (game_id, user_id) VALUES (@game_id, @user_id)
ON CONFLICT (game_id, user_id) DO NOTHING;";
await using (var cmd = new NpgsqlCommand(insertMainQuery, conn))
{
cmd.Parameters.AddWithValue("game_id", gameId);
cmd.Parameters.AddWithValue("user_id", (decimal)user_id);
cmd.ExecuteNonQuery();
}
await ProcessObject(conn, table, reportData[user_id], gameId, user_id, reporterUserId);
}
}
private static object MapType(object? val)
{
return val switch
{
ulong uLongValue => (decimal)uLongValue,
uint uIntValue => (long)uIntValue,
ushort uShortValue => (int)uShortValue,
_ => val ?? DBNull.Value
};
}
private static object ParseLegacy(string raw)
{
if (long.TryParse(raw, out var l)) return l;
if (double.TryParse(raw, NumberStyles.Float, CultureInfo.InvariantCulture, out var d)) return d;
return raw;
}
private static readonly List<(ulong GameId, ulong PlayerToBeReported, ulong Reporter)> WhoReportedTuple = new();
public async Task InsertLegacyReport(GameReport report, long reporterUserId)
{
await using var conn = new NpgsqlConnection(ConnectionString);
await conn.OpenAsync();
const string insertGameQuery = @"
INSERT INTO games_l (
game_id, fnsh, gtyp
) VALUES (
@game_id, @fnsh, @gtyp
)
ON CONFLICT (game_id) DO NOTHING;";
await using var cmd = new NpgsqlCommand(insertGameQuery, conn);
cmd.Parameters.AddWithValue("game_id", (decimal)report.mGameReportingId);
cmd.Parameters.AddWithValue("fnsh", report.mFinished);
cmd.Parameters.AddWithValue("gtyp", (long)report.mGameTypeId);
cmd.Parameters.AddWithValue("prcs", report.mProcess);
await cmd.ExecuteNonQueryAsync();
var gameAttributeMap = report.mAttributeMap;
foreach (var key in gameAttributeMap.Keys)
{
var column = ToColumn(key);
var raw = gameAttributeMap[key];
EnsureColumn(conn, "games_l", column, ParseLegacy(raw));
var insertGameAttributeQuery = $@"
INSERT INTO games_l (game_id, {column})
VALUES (@game_id, @value)
ON CONFLICT (game_id) DO UPDATE
SET {column} = EXCLUDED.{column};";
await using var cmd1 = new NpgsqlCommand(insertGameAttributeQuery, conn);
cmd1.Parameters.AddWithValue("game_id", (decimal)report.mGameReportingId);
cmd1.Parameters.AddWithValue("value", ParseLegacy(raw));
await cmd1.ExecuteNonQueryAsync();
}
var tableName = "reports_l";
switch (report.mGameTypeId)
{
case 1:
tableName = "reports_l";
break;
case 2:
tableName = "so_reports_l";
break;
case 3:
tableName = "otp_reports_l";
break;
case 6:
tableName = "hut_reports_l";
break;
}
var mPlayerReportMap = report.mPlayerReportMap;
foreach (var userId in mPlayerReportMap.Keys)
{
var insertPlayerQuery = $@"
INSERT INTO {tableName} (
game_id, user_id
) VALUES (
@game_id, @user_id
)
ON CONFLICT (game_id, user_id) DO NOTHING;";
await using var cmd1 = new NpgsqlCommand(insertPlayerQuery, conn);
cmd1.Parameters.AddWithValue("game_id", (long)report.mGameReportingId);
cmd1.Parameters.AddWithValue("user_id", userId);
await cmd1.ExecuteNonQueryAsync();
}
foreach (var pl in mPlayerReportMap.Keys)
{
if (WhoReportedTuple.Count > 100) WhoReportedTuple.RemoveRange(0, 50);
if (WhoReportedTuple.Contains(((ulong GameId, ulong PlayerToBeReported, ulong Reporter))(report.mGameReportingId, pl, pl))) continue;
foreach (var key in mPlayerReportMap[pl].mAttributeMap.Keys)
{
var column = ToColumn(key);
EnsureColumn(conn, tableName, column, ParseLegacy(mPlayerReportMap[pl].mAttributeMap[key]));
var insertPlayerAttributeQuery = $@"
INSERT INTO {tableName} (game_id, user_id, {column})
VALUES (@game_id, @user_id, @value)
ON CONFLICT (game_id, user_id) DO UPDATE
SET {column} = EXCLUDED.{column};";
await using var cmd1 = new NpgsqlCommand(insertPlayerAttributeQuery, conn);
cmd1.Parameters.AddWithValue("game_id", (long)report.mGameReportingId);
cmd1.Parameters.AddWithValue("user_id", pl);
cmd1.Parameters.AddWithValue("value", ParseLegacy(mPlayerReportMap[pl].mAttributeMap[key]));
await cmd1.ExecuteNonQueryAsync();
}
WhoReportedTuple.Add(((ulong GameId, ulong PlayerToBeReported, ulong Reporter))(report.mGameReportingId, pl, reporterUserId));
}
}
private static string ToColumn(string key)
{
var column = key.ToLowerInvariant();
if (ColumnRenames.TryGetValue(column, out var renamed)) column = renamed;
return column;
}
private static string InferType(object? val) => val switch
{
ulong or decimal => "NUMERIC(20,0)",
uint or long or int or ushort or short => "BIGINT",
double or float => "DOUBLE PRECISION",
bool => "BOOLEAN",
_ => "TEXT",
};
private static void EnsureColumn(NpgsqlConnection conn, string table, string column, object? sampleValue)
{
if (!_knownColumns.TryGetValue(table, out var cols))
{
_knownColumns[table] = cols = LoadColumns(conn, table);
}
if (cols.Contains(column)) return;
string type = InferType(sampleValue);
using var cmd = new NpgsqlCommand($"ALTER TABLE {table} ADD COLUMN IF NOT EXISTS \"{column}\" {type}", conn);
cmd.ExecuteNonQuery();
cols.Add(column);
}
private static HashSet<string> LoadColumns(NpgsqlConnection conn, string table)
{
var cols = new HashSet<string>();
using var cmd = new NpgsqlCommand("SELECT column_name FROM information_schema.columns " + "WHERE table_schema = 'public' AND table_name = @t", conn);
cmd.Parameters.AddWithValue("t", table);
using var reader = cmd.ExecuteReader();
while (reader.Read()) cols.Add(reader.GetString(0));
return cols;
}
public ulong GetNextGameId()
{
if (!isEnabled) return fallbackGameIdCounter++;
using var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
//TODO: PSQL overflows at 9 quintillion. Though game client cant receive a game id max of 18 quintillion.
using var cmd = new NpgsqlCommand("SELECT nextval('zamboni_game_id_seq');", conn);
var result = cmd.ExecuteScalar();
if (result == null || result == DBNull.Value)
throw new InvalidOperationException("Sequence returned no value.");
return (ulong)(long)result;
}
}