1
- using System . Threading . Tasks ;
2
-
1
+ using System . Collections . Generic ;
2
+ using System . Threading . Tasks ;
3
3
using ProGaudi . Tarantool . Client . Model ;
4
4
using ProGaudi . Tarantool . Client . Model . Requests ;
5
5
using ProGaudi . Tarantool . Client . Model . Responses ;
6
+ using ProGaudi . Tarantool . Client . Utils ;
6
7
7
8
namespace ProGaudi . Tarantool . Client
8
9
{
@@ -12,6 +13,10 @@ public class Box : IBox
12
13
13
14
private readonly ILogicalConnection _logicalConnection ;
14
15
16
+ private BoxInfo _info ;
17
+
18
+ private bool _sqlReady ;
19
+
15
20
public Box ( ClientOptions options )
16
21
{
17
22
_clientOptions = options ;
@@ -22,10 +27,42 @@ public Box(ClientOptions options)
22
27
Schema = new Schema ( _logicalConnection ) ;
23
28
}
24
29
30
+ public Metrics Metrics { get ; }
31
+
32
+ public bool IsConnected => _logicalConnection . IsConnected ( ) ;
33
+
34
+ public ISchema Schema { get ; }
35
+
36
+ public BoxInfo Info
37
+ {
38
+ get => _info ;
39
+ private set
40
+ {
41
+ _info = value ;
42
+ _sqlReady = value . IsSqlAvailable ( ) ;
43
+ }
44
+ }
45
+
25
46
public async Task Connect ( )
26
47
{
27
48
await _logicalConnection . Connect ( ) . ConfigureAwait ( false ) ;
28
- await ReloadSchema ( ) . ConfigureAwait ( false ) ;
49
+ await Task . WhenAll ( GetAdditionalTasks ( ) ) . ConfigureAwait ( false ) ;
50
+
51
+ IEnumerable < Task > GetAdditionalTasks ( )
52
+ {
53
+ if ( _clientOptions . ConnectionOptions . ReadSchemaOnConnect )
54
+ yield return ReloadSchema ( ) ;
55
+
56
+ if ( _clientOptions . ConnectionOptions . ReadBoxInfoOnConnect )
57
+ yield return ReloadBoxInfo ( ) ;
58
+ }
59
+ }
60
+
61
+ public async Task ReloadBoxInfo ( )
62
+ {
63
+ var report = await Eval < BoxInfo > ( "return box.info" ) . ConfigureAwait ( false ) ;
64
+ if ( report . Data . Length != 1 ) throw ExceptionHelper . CantParseBoxInfoResponse ( ) ;
65
+ Info = report . Data [ 0 ] ;
29
66
}
30
67
31
68
public static async Task < Box > Connect ( string replicationSource )
@@ -35,13 +72,6 @@ public static async Task<Box> Connect(string replicationSource)
35
72
return box ;
36
73
}
37
74
38
- public Metrics Metrics
39
- {
40
- get ;
41
- }
42
-
43
- public bool IsConnected => _logicalConnection . IsConnected ( ) ;
44
-
45
75
public static Task < Box > Connect ( string host , int port )
46
76
{
47
77
return Connect ( $ "{ host } :{ port } ") ;
@@ -61,8 +91,6 @@ public void Dispose()
61
91
62
92
public ISchema GetSchema ( ) => Schema ;
63
93
64
- public ISchema Schema { get ; }
65
-
66
94
public Task ReloadSchema ( )
67
95
{
68
96
_clientOptions . LogWriter ? . WriteLine ( "Schema reloading..." ) ;
@@ -121,5 +149,19 @@ public Task<DataResponse<TResponse[]>> Eval<TResponse>(string expression)
121
149
{
122
150
return Eval < TarantoolTuple , TResponse > ( expression , TarantoolTuple . Empty ) ;
123
151
}
152
+
153
+ public Task < DataResponse > ExecuteSql ( string query , params SqlParameter [ ] parameters )
154
+ {
155
+ if ( ! _sqlReady ) throw ExceptionHelper . SqlIsNotAvailable ( Info . Version ) ;
156
+
157
+ return _logicalConnection . SendRequest ( new ExecuteSqlRequest ( query , parameters ) ) ;
158
+ }
159
+
160
+ public Task < DataResponse < TResponse [ ] > > ExecuteSql < TResponse > ( string query , params SqlParameter [ ] parameters )
161
+ {
162
+ if ( ! _sqlReady ) throw ExceptionHelper . SqlIsNotAvailable ( Info . Version ) ;
163
+
164
+ return _logicalConnection . SendRequest < ExecuteSqlRequest , TResponse > ( new ExecuteSqlRequest ( query , parameters ) ) ;
165
+ }
124
166
}
125
167
}
0 commit comments