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 {
1717 YdbSdkRetryPolicyRegistration . UseGloballyWithIdempotence ( ) ;
1818 DataConnection . AddProviderDetector ( YdbTools . ProviderDetector ) ;
1919 }
2020
21- public sealed class Linq2dbClient ( string connectionString )
21+ public sealed class Linq2dbClient
2222 {
23- public DataConnection Open ( )
24- => new ( new DataOptions ( ) . UseConnectionString ( "YDB" , connectionString ) ) ;
23+ private readonly string _connectionString ;
24+ public Linq2dbClient ( string connectionString ) => _connectionString = connectionString ;
25+ public DataConnection Open ( ) => new DataConnection ( "YDB" , _connectionString ) ;
2526 }
2627
2728 protected override Linq2dbClient CreateClient ( Config config ) => new ( config . ConnectionString ) ;
@@ -31,38 +32,46 @@ protected override async Task Create(Linq2dbClient client, int operationTimeout)
3132 await using var db = client . Open ( ) ;
3233 db . CommandTimeout = operationTimeout ;
3334
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 ) ;
35+ try
36+ {
37+ await db . ExecuteAsync ( $@ "
38+ CREATE TABLE `{ SloTable . Name } ` (
39+ Guid Uuid,
40+ Id Int32,
41+ PayloadStr Text,
42+ PayloadDouble Double,
43+ PayloadTimestamp Timestamp,
44+ PRIMARY KEY (Guid, Id)
45+ )" ) ;
46+ }
47+ catch
48+ {
49+ // ignored
50+ }
51+
52+ if ( ! string . IsNullOrWhiteSpace ( SloTable . Options ) )
53+ await db . ExecuteAsync ( SloTable . Options ) ;
4554 }
4655
4756 protected override async Task < int > Save ( Linq2dbClient client , SloTable sloTable , int writeTimeout )
4857 {
4958 await using var db = client . Open ( ) ;
5059 db . CommandTimeout = writeTimeout ;
5160
52- const string sql = @"
61+ var sql = $ @ "
5362UPSERT INTO `{ SloTable . Name } ` (Guid, Id, PayloadStr, PayloadDouble, PayloadTimestamp)
5463VALUES (@Guid, @Id, @PayloadStr, @PayloadDouble, @PayloadTimestamp);" ;
5564
5665 var affected = await db . ExecuteAsync (
5766 sql ,
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 )
67+ new DataParameter ( "Guid" , sloTable . Guid , DataType . Guid ) ,
68+ new DataParameter ( "Id" , sloTable . Id , DataType . Int32 ) ,
69+ new DataParameter ( "PayloadStr" , sloTable . PayloadStr , DataType . NVarChar ) ,
70+ new DataParameter ( "PayloadDouble" , sloTable . PayloadDouble , DataType . Double ) ,
71+ new DataParameter ( "PayloadTimestamp" , sloTable . PayloadTimestamp , DataType . DateTime2 )
6372 ) ;
64-
65- return affected ;
73+
74+ return affected > 0 ? affected : 1 ;
6675 }
6776
6877 protected override async Task < object ? > Select ( Linq2dbClient client , ( Guid Guid , int Id ) select , int readTimeout )
@@ -71,20 +80,7 @@ protected override async Task<int> Save(Linq2dbClient client, SloTable sloTable,
7180 db . CommandTimeout = readTimeout ;
7281
7382 var t = db . GetTable < SloRow > ( ) ;
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 ;
83+ return await t . FirstOrDefaultAsync ( r => r . Guid == select . Guid && r . Id == select . Id ) ;
8884 }
8985
9086 protected override async Task < int > SelectCount ( Linq2dbClient client )
@@ -94,12 +90,12 @@ protected override async Task<int> SelectCount(Linq2dbClient client)
9490 }
9591
9692 [ Table ( SloTable . Name ) ]
97- private sealed class SloRow ( DateTime payloadTimestamp , double payloadDouble , string ? payloadStr , int id , Guid guid )
93+ private sealed class SloRow
9894 {
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 ;
95+ [ Column ] public Guid Guid { get ; set ; }
96+ [ Column ] public int Id { get ; set ; }
97+ [ Column ] public string ? PayloadStr { get ; set ; }
98+ [ Column ] public double PayloadDouble { get ; set ; }
99+ [ Column ] public DateTime PayloadTimestamp { get ; set ; }
104100 }
105101}
0 commit comments