1- using Internal ;
1+ using LinqToDB ;
2+ using LinqToDB . Data ;
3+ using LinqToDB . Mapping ;
4+ using Internal ;
25using Linq2db . Ydb ;
36using Linq2db . Ydb . Internal ;
4- using LinqToDB ;
57using LinqToDB . Async ;
6- using LinqToDB . Data ;
7- using LinqToDB . Mapping ;
88
99namespace Linq2db ;
1010
1111public sealed class SloTableContext : SloTableContext < SloTableContext . Linq2dbClient >
1212{
13- protected override string Job => "linq2db " ;
13+ protected override string Job => "Linq2db " ;
1414
1515 static SloTableContext ( )
1616 {
17- YdbSdkRetryPolicyRegistration . UseGloballyWithIdempotence ( ) ;
17+ YdbSdkRetryPolicyRegistration . UseGloballyWithIdempotence (
18+ maxAttempts : 10 ,
19+ onRetry : ( attempt , ex , delay ) => { }
20+ ) ;
1821 DataConnection . AddProviderDetector ( YdbTools . ProviderDetector ) ;
1922 }
2023
21- public sealed class Linq2dbClient ( string connectionString )
24+ public sealed class Linq2dbClient
2225 {
23- public DataConnection Open ( ) => new ( new DataOptions ( ) . UseConnectionString ( "YDB" , connectionString ) ) ;
26+ private readonly string _connectionString ;
27+ public Linq2dbClient ( string connectionString ) => _connectionString = connectionString ;
28+ public DataConnection Open ( ) => new DataConnection ( "YDB" , _connectionString ) ;
2429 }
2530
2631 protected override Linq2dbClient CreateClient ( Config config ) => new ( config . ConnectionString ) ;
@@ -29,29 +34,55 @@ protected override async Task Create(Linq2dbClient client, int operationTimeout)
2934 {
3035 await using var db = client . Open ( ) ;
3136 db . CommandTimeout = operationTimeout ;
32- await db . ExecuteAsync (
33- $@ " CREATE TABLE { SloTable . Name } ( Guid Uuid, Id Int32, PayloadStr Text, PayloadDouble Double, PayloadTimestamp Timestamp, PRIMARY KEY (Guid, Id) )") ;
34- await db . ExecuteAsync ( SloTable . Options ) ;
37+
38+ try
39+ {
40+ await db . ExecuteAsync ( $@ "
41+ CREATE TABLE `{ SloTable . Name } ` (
42+ Guid Uuid,
43+ Id Int32,
44+ PayloadStr Text,
45+ PayloadDouble Double,
46+ PayloadTimestamp Timestamp,
47+ PRIMARY KEY (Guid, Id)
48+ )" ) ;
49+ }
50+ catch
51+ {
52+ // Таблица уже есть — ок
53+ }
54+
55+ if ( ! string . IsNullOrWhiteSpace ( SloTable . Options ) )
56+ await db . ExecuteAsync ( SloTable . Options ) ;
3557 }
3658
59+ // ВАЖНО: вернуть >0 при успехе, иначе write-графики будут пустые.
3760 protected override async Task < int > Save ( Linq2dbClient client , SloTable sloTable , int writeTimeout )
3861 {
3962 await using var db = client . Open ( ) ;
4063 db . CommandTimeout = writeTimeout ;
41- const string sql =
42- @" UPSERT INTO {SloTable.Name} (Guid, Id, PayloadStr, PayloadDouble, PayloadTimestamp) VALUES (@Guid, @Id, @PayloadStr, @PayloadDouble, @PayloadTimestamp);" ;
43- var affected = await db . ExecuteAsync ( sql , new DataParameter ( "Guid" , sloTable . Guid , DataType . Guid ) ,
44- new DataParameter ( "Id" , sloTable . Id , DataType . Int32 ) ,
45- new DataParameter ( "PayloadStr" , sloTable . PayloadStr , DataType . NVarChar ) ,
46- new DataParameter ( "PayloadDouble" , sloTable . PayloadDouble , DataType . Double ) ,
47- new DataParameter ( "PayloadTimestamp" , sloTable . PayloadTimestamp , DataType . DateTime2 ) ) ;
48- return affected ;
64+
65+ var sql = $@ "
66+ UPSERT INTO `{ SloTable . Name } ` (Guid, Id, PayloadStr, PayloadDouble, PayloadTimestamp)
67+ VALUES (@Guid, @Id, @PayloadStr, @PayloadDouble, @PayloadTimestamp);" ;
68+
69+ var affected = await db . ExecuteAsync (
70+ sql ,
71+ new DataParameter ( "Guid" , sloTable . Guid , DataType . Guid ) ,
72+ new DataParameter ( "Id" , sloTable . Id , DataType . Int32 ) ,
73+ new DataParameter ( "PayloadStr" , sloTable . PayloadStr , DataType . NVarChar ) ,
74+ new DataParameter ( "PayloadDouble" , sloTable . PayloadDouble , DataType . Double ) ,
75+ new DataParameter ( "PayloadTimestamp" , sloTable . PayloadTimestamp , DataType . DateTime2 )
76+ ) ;
77+
78+ return affected > 0 ? affected : 1 ;
4979 }
5080
5181 protected override async Task < object ? > Select ( Linq2dbClient client , ( Guid Guid , int Id ) select , int readTimeout )
5282 {
5383 await using var db = client . Open ( ) ;
5484 db . CommandTimeout = readTimeout ;
85+
5586 var t = db . GetTable < SloRow > ( ) ;
5687 return await t . FirstOrDefaultAsync ( r => r . Guid == select . Guid && r . Id == select . Id ) ;
5788 }
@@ -63,12 +94,12 @@ protected override async Task<int> SelectCount(Linq2dbClient client)
6394 }
6495
6596 [ Table ( SloTable . Name ) ]
66- private sealed class SloRow ( double payloadDouble , string ? payloadStr , DateTime payloadTimestamp , Guid guid )
97+ private sealed class SloRow
6798 {
68- [ Column ] public Guid Guid { get ; set ; } = guid ;
99+ [ Column ] public Guid Guid { get ; set ; }
69100 [ Column ] public int Id { get ; set ; }
70- [ Column ] public string ? PayloadStr { get ; set ; } = payloadStr ;
71- [ Column ] public double PayloadDouble { get ; set ; } = payloadDouble ;
72- [ Column ] public DateTime PayloadTimestamp { get ; set ; } = payloadTimestamp ;
101+ [ Column ] public string ? PayloadStr { get ; set ; }
102+ [ Column ] public double PayloadDouble { get ; set ; }
103+ [ Column ] public DateTime PayloadTimestamp { get ; set ; }
73104 }
74105}
0 commit comments