-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathclient.rs
More file actions
344 lines (302 loc) · 9.32 KB
/
client.rs
File metadata and controls
344 lines (302 loc) · 9.32 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::Mutex;
use adbc_core::{
options::{
AdbcVersion, InfoCode, ObjectDepth, OptionConnection, OptionDatabase, OptionStatement,
OptionValue,
},
Connection, Database, Driver, Optionable, Statement, LOAD_FLAG_DEFAULT,
};
use adbc_driver_manager::{ManagedConnection, ManagedDatabase, ManagedDriver, ManagedStatement};
use arrow_array::RecordBatchReader;
use arrow_ipc::reader::StreamReader;
use arrow_ipc::writer::StreamWriter;
#[derive(Debug, thiserror::Error)]
pub enum ClientError {
#[error("ADBC Error: {0}")]
Adbc(#[from] adbc_core::error::Error),
#[error("Arrow Error: {0}")]
Arrow(#[from] arrow_schema::ArrowError),
#[error("Other Error: {0}")]
Other(String),
}
pub type Result<T> = std::result::Result<T, ClientError>;
pub struct ConnectOptions {
pub driver: String,
pub entrypoint: Option<String>,
pub manifest_search_paths: Option<Vec<String>>,
pub profile_search_paths: Option<Vec<String>>,
pub load_flags: Option<u32>,
pub database_options: Option<HashMap<String, String>>,
}
pub struct GetObjectsOptions {
pub depth: i32,
pub catalog: Option<String>,
pub db_schema: Option<String>,
pub table_name: Option<String>,
pub table_type: Option<Vec<String>>,
pub column_name: Option<String>,
}
pub struct GetTableSchemaOptions {
pub catalog: Option<String>,
pub db_schema: Option<String>,
pub table_name: String,
}
pub struct AdbcDatabaseCore {
inner: ManagedDatabase,
}
impl AdbcDatabaseCore {
pub fn new(opts: ConnectOptions) -> Result<Self> {
let version = AdbcVersion::V110;
let load_flags = opts.load_flags.unwrap_or(LOAD_FLAG_DEFAULT);
let entrypoint = opts.entrypoint.as_ref().map(|s| s.as_bytes().to_vec());
let manifest_search_paths: Option<Vec<PathBuf>> = opts
.manifest_search_paths
.map(|paths| paths.into_iter().map(PathBuf::from).collect());
let profile_search_paths: Option<Vec<PathBuf>> = opts
.profile_search_paths
.map(|paths| paths.into_iter().map(PathBuf::from).collect());
let database_opts = opts.database_options.map(map_database_options);
let database = if opts.driver.contains(':') {
let provider = adbc_driver_manager::profile::FilesystemProfileProvider::new_with_search_paths(
profile_search_paths,
);
// URI-style ("sqlite:file::memory:") or profile URI ("profile://my_profile")
ManagedDatabase::from_uri_with_profile_provider(
&opts.driver,
entrypoint.as_deref(),
version,
load_flags,
manifest_search_paths,
provider,
database_opts.into_iter().flatten(),
)?
} else {
// Short name ("sqlite") or path ("/usr/lib/libadbc_driver_sqlite.so")
let mut driver = ManagedDriver::load_from_name(
&opts.driver,
entrypoint.as_deref(),
version,
load_flags,
manifest_search_paths,
)?;
match database_opts {
Some(db_opts) => driver.new_database_with_opts(db_opts)?,
None => driver.new_database()?,
}
};
Ok(Self { inner: database })
}
pub fn connect(&self, options: Option<HashMap<String, String>>) -> Result<AdbcConnectionCore> {
let conn = if let Some(opts) = options {
self
.inner
.new_connection_with_opts(map_connection_options(opts))?
} else {
self.inner.new_connection()?
};
Ok(AdbcConnectionCore {
inner: Mutex::new(conn),
})
}
}
pub struct AdbcConnectionCore {
inner: Mutex<ManagedConnection>,
}
impl AdbcConnectionCore {
pub fn new_statement(&self) -> Result<AdbcStatementCore> {
let mut conn = self
.inner
.lock()
.map_err(|e| ClientError::Other(e.to_string()))?;
let stmt = conn.new_statement()?;
Ok(AdbcStatementCore { inner: stmt })
}
pub fn set_option(&self, key: &str, value: &str) -> Result<()> {
let mut conn = self
.inner
.lock()
.map_err(|e| ClientError::Other(e.to_string()))?;
conn.set_option(
OptionConnection::Other(key.to_string()),
OptionValue::String(value.to_string()),
)?;
Ok(())
}
pub fn get_objects(&self, opts: GetObjectsOptions) -> Result<AdbcResultIteratorCore> {
let conn = self
.inner
.lock()
.map_err(|e| ClientError::Other(e.to_string()))?;
let depth = match opts.depth {
1 => ObjectDepth::Catalogs,
2 => ObjectDepth::Schemas,
3 => ObjectDepth::Tables,
_ => ObjectDepth::All,
};
let table_types_str: Option<Vec<&str>> = opts
.table_type
.as_ref()
.map(|v| v.iter().map(|s| s.as_str()).collect());
let reader = conn.get_objects(
depth,
opts.catalog.as_deref(),
opts.db_schema.as_deref(),
opts.table_name.as_deref(),
table_types_str,
opts.column_name.as_deref(),
)?;
Ok(AdbcResultIteratorCore {
reader,
exhausted: false,
})
}
pub fn get_table_schema(&self, opts: GetTableSchemaOptions) -> Result<Vec<u8>> {
let conn = self
.inner
.lock()
.map_err(|e| ClientError::Other(e.to_string()))?;
let schema = conn.get_table_schema(
opts.catalog.as_deref(),
opts.db_schema.as_deref(),
&opts.table_name,
)?;
let mut output = Vec::new();
let mut writer = StreamWriter::try_new(&mut output, &schema)?;
writer.finish()?;
Ok(output)
}
pub fn get_table_types(&self) -> Result<AdbcResultIteratorCore> {
let conn = self
.inner
.lock()
.map_err(|e| ClientError::Other(e.to_string()))?;
let reader = conn.get_table_types()?;
Ok(AdbcResultIteratorCore {
reader,
exhausted: false,
})
}
pub fn get_info(&self, info_codes: Option<Vec<u32>>) -> Result<AdbcResultIteratorCore> {
let conn = self
.inner
.lock()
.map_err(|e| ClientError::Other(e.to_string()))?;
let codes: Option<HashSet<InfoCode>> = info_codes.map(|v| {
v.into_iter()
.filter_map(|code| InfoCode::try_from(code).ok())
.collect::<HashSet<InfoCode>>()
});
let reader = conn.get_info(codes)?;
Ok(AdbcResultIteratorCore {
reader,
exhausted: false,
})
}
pub fn commit(&self) -> Result<()> {
let mut conn = self
.inner
.lock()
.map_err(|e| ClientError::Other(e.to_string()))?;
conn.commit()?;
Ok(())
}
pub fn rollback(&self) -> Result<()> {
let mut conn = self
.inner
.lock()
.map_err(|e| ClientError::Other(e.to_string()))?;
conn.rollback()?;
Ok(())
}
}
pub struct AdbcStatementCore {
inner: ManagedStatement,
}
impl AdbcStatementCore {
pub fn set_sql_query(&mut self, query: &str) -> Result<()> {
self.inner.set_sql_query(query)?;
Ok(())
}
pub fn set_option(&mut self, key: &str, value: &str) -> Result<()> {
self.inner.set_option(
OptionStatement::Other(key.to_string()),
OptionValue::String(value.to_string()),
)?;
Ok(())
}
pub fn execute_query(&mut self) -> Result<AdbcResultIteratorCore> {
let reader = self.inner.execute()?;
Ok(AdbcResultIteratorCore {
reader,
exhausted: false,
})
}
pub fn execute_update(&mut self) -> Result<i64> {
let rows = self.inner.execute_update()?;
Ok(rows.unwrap_or(-1))
}
pub fn bind(&mut self, c_data: Vec<u8>) -> Result<()> {
let reader =
StreamReader::try_new(std::io::Cursor::new(c_data), None).map_err(ClientError::Arrow)?;
self.inner.bind_stream(Box::new(reader))?;
Ok(())
}
}
pub struct AdbcResultIteratorCore {
reader: Box<dyn RecordBatchReader + Send>,
exhausted: bool,
}
impl AdbcResultIteratorCore {
pub fn next(&mut self) -> Result<Option<Vec<u8>>> {
if self.exhausted {
return Ok(None);
}
self.exhausted = true;
let schema = self.reader.schema();
let mut output = Vec::new();
let mut writer = StreamWriter::try_new(&mut output, &schema)?;
for batch in self.reader.by_ref() {
writer.write(&batch?)?;
}
writer.finish()?;
Ok(Some(output))
}
}
fn map_database_options(
opts: HashMap<String, String>,
) -> impl Iterator<Item = (OptionDatabase, OptionValue)> {
opts.into_iter().map(|(k, v)| {
let key = match k.as_str() {
"uri" => OptionDatabase::Uri,
"user" => OptionDatabase::Username,
"password" => OptionDatabase::Password,
other => OptionDatabase::Other(other.to_string()),
};
(key, OptionValue::String(v))
})
}
fn map_connection_options(
opts: HashMap<String, String>,
) -> impl Iterator<Item = (OptionConnection, OptionValue)> {
opts
.into_iter()
.map(|(k, v)| (OptionConnection::Other(k), OptionValue::String(v)))
}