|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using LinqToDB; |
| 4 | +using LinqToDB.Data; |
| 5 | +using LinqToDB.Mapping; |
| 6 | +using Internal; |
| 7 | +using Linq2db.Ydb.Internal; |
| 8 | +using LinqToDB.Async; |
| 9 | +using LinqToDB.Internal.DataProvider.Ydb.Internal; |
| 10 | + |
| 11 | +namespace Linq2db; |
| 12 | + |
| 13 | +public sealed class SloTableContext : SloTableContext<SloTableContext.Linq2dbClient> |
| 14 | +{ |
| 15 | + protected override string Job => "Linq2DB"; |
| 16 | + |
| 17 | + static SloTableContext() |
| 18 | + { |
| 19 | + // Включаем ретраи SDK глобально (как и раньше) |
| 20 | + YdbSdkRetryPolicyRegistration.UseGloballyWithIdempotence( |
| 21 | + maxAttempts: 10, |
| 22 | + onRetry: (attempt, ex, delay) => { /* лог/метрики при желании */ } |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + public sealed class Linq2dbClient |
| 27 | + { |
| 28 | + private readonly string _connectionString; |
| 29 | + public Linq2dbClient(string connectionString) => _connectionString = connectionString; |
| 30 | + public DataConnection Open() => new DataConnection("YDB", _connectionString); |
| 31 | + } |
| 32 | + |
| 33 | + protected override Linq2dbClient CreateClient(Config config) => new(config.ConnectionString); |
| 34 | + |
| 35 | + protected override async Task Create(Linq2dbClient client, int operationTimeout) |
| 36 | + { |
| 37 | + await using var db = client.Open(); |
| 38 | + db.CommandTimeout = operationTimeout; |
| 39 | + |
| 40 | + try |
| 41 | + { |
| 42 | + await db.ExecuteAsync($@" |
| 43 | +CREATE TABLE `{SloTable.Name}` ( |
| 44 | + Guid Uuid, |
| 45 | + Id Int32, |
| 46 | + PayloadStr Text, |
| 47 | + PayloadDouble Double, |
| 48 | + PayloadTimestamp Timestamp, |
| 49 | + PRIMARY KEY (Guid, Id) |
| 50 | +)"); |
| 51 | + } |
| 52 | + catch |
| 53 | + { |
| 54 | + // Таблица уже есть — ок |
| 55 | + } |
| 56 | + |
| 57 | + if (!string.IsNullOrWhiteSpace(SloTable.Options)) |
| 58 | + await db.ExecuteAsync(SloTable.Options); |
| 59 | + } |
| 60 | + |
| 61 | + // ВАЖНО: вернуть >0 при успехе, иначе write-графики будут пустые. |
| 62 | + protected override async Task<int> Save(Linq2dbClient client, SloTable sloTable, int writeTimeout) |
| 63 | + { |
| 64 | + await using var db = client.Open(); |
| 65 | + db.CommandTimeout = writeTimeout; |
| 66 | + |
| 67 | + var sql = $@" |
| 68 | +UPSERT INTO `{SloTable.Name}` (Guid, Id, PayloadStr, PayloadDouble, PayloadTimestamp) |
| 69 | +VALUES (@Guid, @Id, @PayloadStr, @PayloadDouble, @PayloadTimestamp);"; |
| 70 | + |
| 71 | + var affected = await db.ExecuteAsync( |
| 72 | + sql, |
| 73 | + new DataParameter("Guid", sloTable.Guid, DataType.Guid), |
| 74 | + new DataParameter("Id", sloTable.Id, DataType.Int32), |
| 75 | + new DataParameter("PayloadStr", sloTable.PayloadStr, DataType.NVarChar), |
| 76 | + new DataParameter("PayloadDouble", sloTable.PayloadDouble, DataType.Double), |
| 77 | + new DataParameter("PayloadTimestamp",sloTable.PayloadTimestamp,DataType.DateTime2) |
| 78 | + ); |
| 79 | + |
| 80 | + return affected > 0 ? affected : 1; |
| 81 | + } |
| 82 | + |
| 83 | + protected override async Task<object?> Select(Linq2dbClient client, (Guid Guid, int Id) select, int readTimeout) |
| 84 | + { |
| 85 | + await using var db = client.Open(); |
| 86 | + db.CommandTimeout = readTimeout; |
| 87 | + |
| 88 | + var t = db.GetTable<SloRow>(); |
| 89 | + return await t.FirstOrDefaultAsync(r => r.Guid == select.Guid && r.Id == select.Id); |
| 90 | + } |
| 91 | + |
| 92 | + protected override async Task<int> SelectCount(Linq2dbClient client) |
| 93 | + { |
| 94 | + await using var db = client.Open(); |
| 95 | + return await db.GetTable<SloRow>().CountAsync(); |
| 96 | + } |
| 97 | + |
| 98 | + [Table(SloTable.Name)] |
| 99 | + private sealed class SloRow |
| 100 | + { |
| 101 | + [Column] public Guid Guid { get; set; } |
| 102 | + [Column] public int Id { get; set; } |
| 103 | + [Column] public string? PayloadStr { get; set; } |
| 104 | + [Column] public double PayloadDouble { get; set; } |
| 105 | + [Column] public DateTime PayloadTimestamp { get; set; } |
| 106 | + } |
| 107 | +} |
0 commit comments