1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// client.rs
//
// This file is a part of the eXtremeDB source code
// Copyright (c) 2020 McObject LLC
// All Rights Reserved
//! Remote SQL client functionality.
//!
//! This module implements the remote SQL engine, which can be used to connect
//! to the remote SQL servers and execute arbitrary SQL statements.
//!
//! # Examples
//!
//! Connecting to a server running on the localhost at a known port and
//! executing SQL statements:
//!
//! ```
//! # use extremedb::sql::engine::{Engine, LocalEngine, LocalEngineRef};
//! # use extremedb::sql::rsql::client::{self, RemoteEngine};
//! # use extremedb::sql::rsql::server::{self, Server};
//! # use extremedb::{connection, database, device, runtime, sql};
//! # use extremedb::device::util;
//! # use std::sync::Arc;
//! # use std::thread;
//! # use std::time::Duration;
//! #
//! # fn server_try_create<'a>(engine: &'a LocalEngine, port: u16) -> Option<Server<'a>> {
//! # let eref = LocalEngineRef::new(&engine);
//! # let res = Server::create(eref, server::Params::new(port));
//! # if res.is_ok() {
//! # let mut srv = res.unwrap();
//! # let res = srv.start();
//! # if res.is_ok() {
//! # thread::sleep(Duration::from_secs(3));
//! # Some(srv)
//! # } else {
//! # None
//! # }
//! # } else {
//! # None
//! # }
//! # }
//! #
//! # fn client_proc(runtime: &runtime::Runtime, port: u16) -> extremedb::Result<()> {
//! // let port = ...
//!
//! let rsql = RemoteEngine::connect(&runtime, client::Params::new("localhost", port))?;
//!
//! rsql.execute_statement("CREATE TABLE TestTable(i int, s string);", &[])?;
//! rsql.execute_statement("INSERT INTO TestTable(i, s) VALUES(1, 'Hello');", &[])?;
//! rsql.execute_statement("INSERT INTO TestTable(i, s) VALUES(2, 'World');", &[])?;
//! #
//! # Ok(())
//! # }
//! #
//! # fn main() -> extremedb::Result<()> {
//! # let runtime = Arc::new(runtime::Runtime::start(vec![]));
//! # let mut db_params = database::Params::new();
//! # db_params
//! # .ddl_dict_size(32768)
//! # .max_classes(100)
//! # .max_indexes(1000);
//! # let mut devs = util::DeviceContainer::new();
//! # let db = database::Database::open(&runtime, "test_db", None, devs.devices(), db_params)?;
//! # let conn = connection::Connection::new(&db)?;
//! # let engine = sql::engine::LocalEngine::new(&conn)?;
//! #
//! # let mut port = 25123;
//! # let mut attempts = 10;
//! #
//! # let mut srv = None;
//! # while srv.is_none() && attempts > 0 {
//! # srv = server_try_create(&engine, port);
//! #
//! # if srv.is_none() {
//! # port += 1;
//! # attempts -= 1;
//! # }
//! # }
//! #
//! # let mut srv = srv.expect("Failed to create the server");
//! #
//! # let rt = runtime.clone();
//! # let t_cli = thread::spawn(move || {
//! # client_proc(&rt, port).expect("Client failed");
//! # });
//! #
//! # t_cli.join().expect("Failed to join the client thread");
//! #
//! # srv.stop()?;
//! #
//! # // Run some data tests in test environment:
//! # let ds = engine.execute_query("SELECT COUNT(*) FROM TestTable;", &[])?;
//! # assert!(ds.is_some());
//! # let ds = ds.unwrap();
//! #
//! # let mut cur = ds.cursor()?;
//! # assert!(cur.advance()?);
//! #
//! # let rec = cur.current_record();
//! # assert!(rec.is_some());
//! # let rec = rec.unwrap();
//! #
//! # let val = rec.get_at(0)?;
//! # assert_eq!(val.to_i64()?, 2);
//! #
//! # Ok(())
//! # }
//! ```
use PhantomData;
use MaybeUninit;
use crateRuntime;
use crateEngine;
use crate;
use crate::;
/// Client connection parameters.
/// Remote SQL engine.
///
/// The remote SQL engine is used in a manner similar to the local engine.
/// When the connection to the server is established, it is possible to run
/// the SQL DDL and DML queries as usual.
///
/// However, it is impossible to create sessions using the remote engine, and
/// the transactions must be managed using the SQL transaction management
/// statements; [`Transaction`] objects cannot be used.
///
/// [`Transaction`]: ../../trans/struct.Transaction.html