1- using LinqToDB ;
2- using LinqToDB . Data ;
3- using LinqToDB . Mapping ;
4- using Internal ;
1+ using Internal ;
52using Linq2db . Ydb ;
63using Linq2db . Ydb . Internal ;
4+ using LinqToDB ;
75using 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 (
18- maxAttempts : 10 ,
19- onRetry : ( attempt , ex , delay ) => { }
20- ) ;
17+ YdbSdkRetryPolicyRegistration . UseGloballyWithIdempotence ( ) ;
2118 DataConnection . AddProviderDetector ( YdbTools . ProviderDetector ) ;
2219 }
2320
24- public sealed class Linq2dbClient
21+ public sealed class Linq2dbClient ( string connectionString )
2522 {
26- private readonly string _connectionString ;
27- public Linq2dbClient ( string connectionString ) => _connectionString = connectionString ;
28- public DataConnection Open ( ) => new DataConnection ( "YDB" , _connectionString ) ;
23+ public DataConnection Open ( )
24+ => new ( new DataOptions ( ) . UseConnectionString ( "YDB" , connectionString ) ) ;
2925 }
3026
3127 protected override Linq2dbClient CreateClient ( Config config ) => new ( config . ConnectionString ) ;
@@ -35,47 +31,38 @@ protected override async Task Create(Linq2dbClient client, int operationTimeout)
3531 await using var db = client . Open ( ) ;
3632 db . CommandTimeout = operationTimeout ;
3733
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 ) ;
34+ await db . ExecuteAsync ( $@ "
35+ CREATE TABLE `{ SloTable . Name } ` (
36+ Guid Uuid,
37+ Id Int32,
38+ PayloadStr Text,
39+ PayloadDouble Double,
40+ PayloadTimestamp Timestamp,
41+ PRIMARY KEY (Guid, Id)
42+ )" ) ;
43+
44+ await db . ExecuteAsync ( SloTable . Options ) ;
5745 }
5846
59- // ВАЖНО: вернуть >0 при успехе, иначе write-графики будут пустые.
6047 protected override async Task < int > Save ( Linq2dbClient client , SloTable sloTable , int writeTimeout )
6148 {
6249 await using var db = client . Open ( ) ;
6350 db . CommandTimeout = writeTimeout ;
6451
65- var sql = $ @ "
52+ const string sql = @"
6653UPSERT INTO `{SloTable.Name}` (Guid, Id, PayloadStr, PayloadDouble, PayloadTimestamp)
6754VALUES (@Guid, @Id, @PayloadStr, @PayloadDouble, @PayloadTimestamp);" ;
6855
6956 var affected = await db . ExecuteAsync (
7057 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 )
58+ new DataParameter ( "Guid" , sloTable . Guid , DataType . Guid ) ,
59+ new DataParameter ( "Id" , sloTable . Id , DataType . Int32 ) ,
60+ new DataParameter ( "PayloadStr" , sloTable . PayloadStr , DataType . NVarChar ) ,
61+ new DataParameter ( "PayloadDouble" , sloTable . PayloadDouble , DataType . Double ) ,
62+ new DataParameter ( "PayloadTimestamp" , sloTable . PayloadTimestamp , DataType . DateTime2 )
7663 ) ;
77-
78- return affected > 0 ? affected : 1 ;
64+
65+ return affected ;
7966 }
8067
8168 protected override async Task < object ? > Select ( Linq2dbClient client , ( Guid Guid , int Id ) select , int readTimeout )
@@ -84,7 +71,20 @@ protected override async Task<int> Save(Linq2dbClient client, SloTable sloTable,
8471 db . CommandTimeout = readTimeout ;
8572
8673 var t = db . GetTable < SloRow > ( ) ;
87- return await t . FirstOrDefaultAsync ( r => r . Guid == select . Guid && r . Id == select . Id ) ;
74+
75+ var row = await t
76+ . Where ( r => r . Guid == select . Guid && r . Id == select . Id )
77+ . Select ( r => new
78+ {
79+ r . Guid ,
80+ r . Id ,
81+ r . PayloadStr ,
82+ r . PayloadDouble ,
83+ r . PayloadTimestamp
84+ } )
85+ . FirstOrDefaultAsync ( ) ;
86+
87+ return row ;
8888 }
8989
9090 protected override async Task < int > SelectCount ( Linq2dbClient client )
@@ -94,12 +94,12 @@ protected override async Task<int> SelectCount(Linq2dbClient client)
9494 }
9595
9696 [ Table ( SloTable . Name ) ]
97- private sealed class SloRow
97+ private sealed class SloRow ( DateTime payloadTimestamp , double payloadDouble , string ? payloadStr , int id , Guid guid )
9898 {
99- [ Column ] public Guid Guid { get ; set ; }
100- [ Column ] public int Id { get ; set ; }
101- [ Column ] public string ? PayloadStr { get ; set ; }
102- [ Column ] public double PayloadDouble { get ; set ; }
103- [ Column ] public DateTime PayloadTimestamp { get ; set ; }
99+ [ Column ] public Guid Guid { get ; } = guid ;
100+ [ Column ] public int Id { get ; } = id ;
101+ [ Column ] public string ? PayloadStr { get ; } = payloadStr ;
102+ [ Column ] public double PayloadDouble { get ; } = payloadDouble ;
103+ [ Column ] public DateTime PayloadTimestamp { get ; } = payloadTimestamp ;
104104 }
105105}
0 commit comments