1- using System . Threading . Tasks ;
2-
1+ using System . Collections . Generic ;
2+ using System . Threading . Tasks ;
33using ProGaudi . Tarantool . Client . Model ;
44using ProGaudi . Tarantool . Client . Model . Requests ;
55using ProGaudi . Tarantool . Client . Model . Responses ;
6+ using ProGaudi . Tarantool . Client . Utils ;
67
78namespace ProGaudi . Tarantool . Client
89{
@@ -12,6 +13,10 @@ public class Box : IBox
1213
1314 private readonly ILogicalConnection _logicalConnection ;
1415
16+ private BoxInfo _info ;
17+
18+ private bool _sqlReady ;
19+
1520 public Box ( ClientOptions options )
1621 {
1722 _clientOptions = options ;
@@ -22,10 +27,42 @@ public Box(ClientOptions options)
2227 Schema = new Schema ( _logicalConnection ) ;
2328 }
2429
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+
2546 public async Task Connect ( )
2647 {
2748 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 ] ;
2966 }
3067
3168 public static async Task < Box > Connect ( string replicationSource )
@@ -35,13 +72,6 @@ public static async Task<Box> Connect(string replicationSource)
3572 return box ;
3673 }
3774
38- public Metrics Metrics
39- {
40- get ;
41- }
42-
43- public bool IsConnected => _logicalConnection . IsConnected ( ) ;
44-
4575 public static Task < Box > Connect ( string host , int port )
4676 {
4777 return Connect ( $ "{ host } :{ port } ") ;
@@ -61,8 +91,6 @@ public void Dispose()
6191
6292 public ISchema GetSchema ( ) => Schema ;
6393
64- public ISchema Schema { get ; }
65-
6694 public Task ReloadSchema ( )
6795 {
6896 _clientOptions . LogWriter ? . WriteLine ( "Schema reloading..." ) ;
@@ -121,5 +149,19 @@ public Task<DataResponse<TResponse[]>> Eval<TResponse>(string expression)
121149 {
122150 return Eval < TarantoolTuple , TResponse > ( expression , TarantoolTuple . Empty ) ;
123151 }
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+ }
124166 }
125167}
0 commit comments