1use std::{os::raw::c_int, str::FromStr};
20
21use crate::{
22 constants,
23 error::{Error, Status},
24};
25
26#[derive(Debug, Clone)]
30#[non_exhaustive]
31pub enum OptionValue {
32 String(String),
33 Bytes(Vec<u8>),
34 Int(i64),
35 Double(f64),
36}
37
38impl From<String> for OptionValue {
39 fn from(value: String) -> Self {
40 Self::String(value)
41 }
42}
43
44impl From<&str> for OptionValue {
45 fn from(value: &str) -> Self {
46 Self::String(value.into())
47 }
48}
49
50impl From<i64> for OptionValue {
51 fn from(value: i64) -> Self {
52 Self::Int(value)
53 }
54}
55
56impl From<f64> for OptionValue {
57 fn from(value: f64) -> Self {
58 Self::Double(value)
59 }
60}
61
62impl From<Vec<u8>> for OptionValue {
63 fn from(value: Vec<u8>) -> Self {
64 Self::Bytes(value)
65 }
66}
67
68impl From<&[u8]> for OptionValue {
69 fn from(value: &[u8]) -> Self {
70 Self::Bytes(value.into())
71 }
72}
73
74impl<const N: usize> From<[u8; N]> for OptionValue {
75 fn from(value: [u8; N]) -> Self {
76 Self::Bytes(value.into())
77 }
78}
79
80impl<const N: usize> From<&[u8; N]> for OptionValue {
81 fn from(value: &[u8; N]) -> Self {
82 Self::Bytes(value.into())
83 }
84}
85
86#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
90#[non_exhaustive]
91pub enum AdbcVersion {
92 V100,
94 #[default]
98 V110,
99}
100
101impl From<AdbcVersion> for c_int {
102 fn from(value: AdbcVersion) -> Self {
103 match value {
104 AdbcVersion::V100 => constants::ADBC_VERSION_1_0_0,
105 AdbcVersion::V110 => constants::ADBC_VERSION_1_1_0,
106 }
107 }
108}
109
110impl TryFrom<c_int> for AdbcVersion {
111 type Error = Error;
112 fn try_from(value: c_int) -> Result<Self, Self::Error> {
113 match value {
114 constants::ADBC_VERSION_1_0_0 => Ok(AdbcVersion::V100),
115 constants::ADBC_VERSION_1_1_0 => Ok(AdbcVersion::V110),
116 value => Err(Error::with_message_and_status(
117 format!("Unknown ADBC version: {value}"),
118 Status::InvalidArguments,
119 )),
120 }
121 }
122}
123
124impl FromStr for AdbcVersion {
125 type Err = Error;
126
127 fn from_str(s: &str) -> Result<Self, Self::Err> {
128 match s {
129 "1.0.0" | "1_0_0" | "100" => Ok(AdbcVersion::V100),
130 "1.1.0" | "1_1_0" | "110" => Ok(AdbcVersion::V110),
131 value => Err(Error::with_message_and_status(
132 format!("Unknown ADBC version: {value}"),
133 Status::InvalidArguments,
134 )),
135 }
136 }
137}
138
139#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
141#[non_exhaustive]
142pub enum InfoCode {
143 VendorName,
145 VendorVersion,
147 VendorArrowVersion,
149 VendorSql,
151 VendorSubstrait,
153 VendorSubstraitMinVersion,
155 VendorSubstraitMaxVersion,
157 DriverName,
159 DriverVersion,
161 DriverArrowVersion,
163 DriverAdbcVersion,
169}
170
171impl From<&InfoCode> for u32 {
172 fn from(value: &InfoCode) -> Self {
173 match value {
174 InfoCode::VendorName => constants::ADBC_INFO_VENDOR_NAME,
175 InfoCode::VendorVersion => constants::ADBC_INFO_VENDOR_VERSION,
176 InfoCode::VendorArrowVersion => constants::ADBC_INFO_VENDOR_ARROW_VERSION,
177 InfoCode::VendorSql => constants::ADBC_INFO_VENDOR_SQL,
178 InfoCode::VendorSubstrait => constants::ADBC_INFO_VENDOR_SUBSTRAIT,
179 InfoCode::VendorSubstraitMinVersion => {
180 constants::ADBC_INFO_VENDOR_SUBSTRAIT_MIN_VERSION
181 }
182 InfoCode::VendorSubstraitMaxVersion => {
183 constants::ADBC_INFO_VENDOR_SUBSTRAIT_MAX_VERSION
184 }
185 InfoCode::DriverName => constants::ADBC_INFO_DRIVER_NAME,
186 InfoCode::DriverVersion => constants::ADBC_INFO_DRIVER_VERSION,
187 InfoCode::DriverArrowVersion => constants::ADBC_INFO_DRIVER_ARROW_VERSION,
188 InfoCode::DriverAdbcVersion => constants::ADBC_INFO_DRIVER_ADBC_VERSION,
189 }
190 }
191}
192
193impl TryFrom<u32> for InfoCode {
194 type Error = Error;
195
196 fn try_from(value: u32) -> Result<Self, Self::Error> {
197 match value {
198 constants::ADBC_INFO_VENDOR_NAME => Ok(InfoCode::VendorName),
199 constants::ADBC_INFO_VENDOR_VERSION => Ok(InfoCode::VendorVersion),
200 constants::ADBC_INFO_VENDOR_ARROW_VERSION => Ok(InfoCode::VendorArrowVersion),
201 constants::ADBC_INFO_VENDOR_SQL => Ok(InfoCode::VendorSql),
202 constants::ADBC_INFO_VENDOR_SUBSTRAIT => Ok(InfoCode::VendorSubstrait),
203 constants::ADBC_INFO_VENDOR_SUBSTRAIT_MIN_VERSION => {
204 Ok(InfoCode::VendorSubstraitMinVersion)
205 }
206 constants::ADBC_INFO_VENDOR_SUBSTRAIT_MAX_VERSION => {
207 Ok(InfoCode::VendorSubstraitMaxVersion)
208 }
209 constants::ADBC_INFO_DRIVER_NAME => Ok(InfoCode::DriverName),
210 constants::ADBC_INFO_DRIVER_VERSION => Ok(InfoCode::DriverVersion),
211 constants::ADBC_INFO_DRIVER_ARROW_VERSION => Ok(InfoCode::DriverArrowVersion),
212 constants::ADBC_INFO_DRIVER_ADBC_VERSION => Ok(InfoCode::DriverAdbcVersion),
213 v => Err(Error::with_message_and_status(
214 format!("Unknown info code: {v}"),
215 Status::InvalidData,
216 )),
217 }
218 }
219}
220
221#[derive(Debug)]
223pub enum ObjectDepth {
224 All,
226 Catalogs,
228 Schemas,
230 Tables,
232 Columns,
234}
235
236impl From<ObjectDepth> for c_int {
237 fn from(value: ObjectDepth) -> Self {
238 match value {
239 ObjectDepth::All => constants::ADBC_OBJECT_DEPTH_ALL,
240 ObjectDepth::Catalogs => constants::ADBC_OBJECT_DEPTH_CATALOGS,
241 ObjectDepth::Schemas => constants::ADBC_OBJECT_DEPTH_DB_SCHEMAS,
242 ObjectDepth::Tables => constants::ADBC_OBJECT_DEPTH_TABLES,
243 ObjectDepth::Columns => constants::ADBC_OBJECT_DEPTH_COLUMNS,
244 }
245 }
246}
247
248impl TryFrom<c_int> for ObjectDepth {
249 type Error = Error;
250
251 fn try_from(value: c_int) -> Result<Self, Error> {
252 match value {
253 constants::ADBC_OBJECT_DEPTH_ALL => Ok(ObjectDepth::All),
254 constants::ADBC_OBJECT_DEPTH_CATALOGS => Ok(ObjectDepth::Catalogs),
255 constants::ADBC_OBJECT_DEPTH_DB_SCHEMAS => Ok(ObjectDepth::Schemas),
256 constants::ADBC_OBJECT_DEPTH_TABLES => Ok(ObjectDepth::Tables),
257 v => Err(Error::with_message_and_status(
258 format!("Unknown object depth: {v}"),
259 Status::InvalidData,
260 )),
261 }
262 }
263}
264
265#[derive(PartialEq, Eq, Hash, Debug, Clone)]
267#[non_exhaustive]
268pub enum OptionDatabase {
269 Uri,
275 Username,
281 Password,
287 Other(String),
289}
290
291impl AsRef<str> for OptionDatabase {
292 fn as_ref(&self) -> &str {
293 match self {
294 Self::Uri => constants::ADBC_OPTION_URI,
295 Self::Username => constants::ADBC_OPTION_USERNAME,
296 Self::Password => constants::ADBC_OPTION_PASSWORD,
297 Self::Other(key) => key,
298 }
299 }
300}
301
302impl From<&str> for OptionDatabase {
303 fn from(value: &str) -> Self {
304 match value {
305 constants::ADBC_OPTION_URI => Self::Uri,
306 constants::ADBC_OPTION_USERNAME => Self::Username,
307 constants::ADBC_OPTION_PASSWORD => Self::Password,
308 key => Self::Other(key.into()),
309 }
310 }
311}
312
313#[derive(PartialEq, Eq, Hash, Debug, Clone)]
315#[non_exhaustive]
316pub enum OptionConnection {
317 AutoCommit,
319 ReadOnly,
321 CurrentCatalog,
325 CurrentSchema,
329 IsolationLevel,
331 Other(String),
333}
334
335impl AsRef<str> for OptionConnection {
336 fn as_ref(&self) -> &str {
337 match self {
338 Self::AutoCommit => constants::ADBC_CONNECTION_OPTION_AUTOCOMMIT,
339 Self::ReadOnly => constants::ADBC_CONNECTION_OPTION_READ_ONLY,
340 Self::CurrentCatalog => constants::ADBC_CONNECTION_OPTION_CURRENT_CATALOG,
341 Self::CurrentSchema => constants::ADBC_CONNECTION_OPTION_CURRENT_DB_SCHEMA,
342 Self::IsolationLevel => constants::ADBC_CONNECTION_OPTION_ISOLATION_LEVEL,
343 Self::Other(key) => key,
344 }
345 }
346}
347
348impl From<&str> for OptionConnection {
349 fn from(value: &str) -> Self {
350 match value {
351 constants::ADBC_CONNECTION_OPTION_AUTOCOMMIT => Self::AutoCommit,
352 constants::ADBC_CONNECTION_OPTION_READ_ONLY => Self::ReadOnly,
353 constants::ADBC_CONNECTION_OPTION_CURRENT_CATALOG => Self::CurrentCatalog,
354 constants::ADBC_CONNECTION_OPTION_CURRENT_DB_SCHEMA => Self::CurrentSchema,
355 constants::ADBC_CONNECTION_OPTION_ISOLATION_LEVEL => Self::IsolationLevel,
356 key => Self::Other(key.into()),
357 }
358 }
359}
360
361#[derive(PartialEq, Eq, Hash, Debug, Clone)]
363#[non_exhaustive]
364pub enum OptionStatement {
365 IngestMode,
367 TargetTable,
369 TargetCatalog,
371 TargetDbSchema,
373 Temporary,
375 Incremental,
388 Progress,
402 MaxProgress,
414 Other(String),
416}
417
418impl AsRef<str> for OptionStatement {
419 fn as_ref(&self) -> &str {
420 match self {
421 Self::IngestMode => constants::ADBC_INGEST_OPTION_MODE,
422 Self::TargetTable => constants::ADBC_INGEST_OPTION_TARGET_TABLE,
423 Self::TargetCatalog => constants::ADBC_INGEST_OPTION_TARGET_CATALOG,
424 Self::TargetDbSchema => constants::ADBC_INGEST_OPTION_TARGET_DB_SCHEMA,
425 Self::Temporary => constants::ADBC_INGEST_OPTION_TEMPORARY,
426 Self::Incremental => constants::ADBC_STATEMENT_OPTION_INCREMENTAL,
427 Self::Progress => constants::ADBC_STATEMENT_OPTION_PROGRESS,
428 Self::MaxProgress => constants::ADBC_STATEMENT_OPTION_MAX_PROGRESS,
429 Self::Other(key) => key,
430 }
431 }
432}
433
434impl From<&str> for OptionStatement {
435 fn from(value: &str) -> Self {
436 match value {
437 constants::ADBC_INGEST_OPTION_MODE => Self::IngestMode,
438 constants::ADBC_INGEST_OPTION_TARGET_TABLE => Self::TargetTable,
439 constants::ADBC_INGEST_OPTION_TARGET_CATALOG => Self::TargetCatalog,
440 constants::ADBC_INGEST_OPTION_TARGET_DB_SCHEMA => Self::TargetDbSchema,
441 constants::ADBC_INGEST_OPTION_TEMPORARY => Self::Temporary,
442 constants::ADBC_STATEMENT_OPTION_INCREMENTAL => Self::Incremental,
443 constants::ADBC_STATEMENT_OPTION_PROGRESS => Self::Progress,
444 constants::ADBC_STATEMENT_OPTION_MAX_PROGRESS => Self::MaxProgress,
445 key => Self::Other(key.into()),
446 }
447 }
448}
449
450#[derive(Debug)]
452pub enum IsolationLevel {
453 Default,
455 ReadUncommitted,
458 ReadCommitted,
469 RepeatableRead,
475 Snapshot,
480 Serializable,
484 Linearizable,
493}
494
495impl From<IsolationLevel> for String {
496 fn from(value: IsolationLevel) -> Self {
497 match value {
498 IsolationLevel::Default => constants::ADBC_OPTION_ISOLATION_LEVEL_DEFAULT.into(),
499 IsolationLevel::ReadUncommitted => {
500 constants::ADBC_OPTION_ISOLATION_LEVEL_READ_UNCOMMITTED.into()
501 }
502 IsolationLevel::ReadCommitted => {
503 constants::ADBC_OPTION_ISOLATION_LEVEL_READ_COMMITTED.into()
504 }
505 IsolationLevel::RepeatableRead => {
506 constants::ADBC_OPTION_ISOLATION_LEVEL_REPEATABLE_READ.into()
507 }
508 IsolationLevel::Snapshot => constants::ADBC_OPTION_ISOLATION_LEVEL_SNAPSHOT.into(),
509 IsolationLevel::Serializable => {
510 constants::ADBC_OPTION_ISOLATION_LEVEL_SERIALIZABLE.into()
511 }
512 IsolationLevel::Linearizable => {
513 constants::ADBC_OPTION_ISOLATION_LEVEL_LINEARIZABLE.into()
514 }
515 }
516 }
517}
518
519impl From<IsolationLevel> for OptionValue {
520 fn from(value: IsolationLevel) -> Self {
521 Self::String(value.into())
522 }
523}
524
525#[derive(Debug)]
527pub enum IngestMode {
528 Create,
530 Append,
534 Replace,
541 CreateAppend,
549}
550
551impl From<IngestMode> for String {
552 fn from(value: IngestMode) -> Self {
553 match value {
554 IngestMode::Create => constants::ADBC_INGEST_OPTION_MODE_CREATE.into(),
555 IngestMode::Append => constants::ADBC_INGEST_OPTION_MODE_APPEND.into(),
556 IngestMode::Replace => constants::ADBC_INGEST_OPTION_MODE_REPLACE.into(),
557 IngestMode::CreateAppend => constants::ADBC_INGEST_OPTION_MODE_CREATE_APPEND.into(),
558 }
559 }
560}
561impl From<IngestMode> for OptionValue {
562 fn from(value: IngestMode) -> Self {
563 Self::String(value.into())
564 }
565}
566
567#[derive(Debug, Clone)]
569pub enum Statistics {
570 AverageByteWidth,
574 DistinctCount,
578 MaxByteWidth,
582 MaxValue,
584 MinValue,
586 NullCount,
589 RowCount,
592 Other { key: i16, name: String },
594}
595
596impl TryFrom<i16> for Statistics {
597 type Error = Error;
598 fn try_from(value: i16) -> Result<Self, Self::Error> {
599 match value {
600 constants::ADBC_STATISTIC_AVERAGE_BYTE_WIDTH_KEY => Ok(Self::AverageByteWidth),
601 constants::ADBC_STATISTIC_DISTINCT_COUNT_KEY => Ok(Self::DistinctCount),
602 constants::ADBC_STATISTIC_MAX_BYTE_WIDTH_KEY => Ok(Self::MaxByteWidth),
603 constants::ADBC_STATISTIC_MAX_VALUE_KEY => Ok(Self::MaxValue),
604 constants::ADBC_STATISTIC_MIN_VALUE_KEY => Ok(Self::MinValue),
605 constants::ADBC_STATISTIC_NULL_COUNT_KEY => Ok(Self::NullCount),
606 constants::ADBC_STATISTIC_ROW_COUNT_KEY => Ok(Self::RowCount),
607 _ => Err(Error::with_message_and_status(
608 format!("Unknown standard statistic key: {value}"),
609 Status::InvalidArguments,
610 )),
611 }
612 }
613}
614
615impl From<Statistics> for i16 {
616 fn from(value: Statistics) -> Self {
617 match value {
618 Statistics::AverageByteWidth => constants::ADBC_STATISTIC_AVERAGE_BYTE_WIDTH_KEY,
619 Statistics::DistinctCount => constants::ADBC_STATISTIC_DISTINCT_COUNT_KEY,
620 Statistics::MaxByteWidth => constants::ADBC_STATISTIC_MAX_BYTE_WIDTH_KEY,
621 Statistics::MaxValue => constants::ADBC_STATISTIC_MAX_VALUE_KEY,
622 Statistics::MinValue => constants::ADBC_STATISTIC_MIN_VALUE_KEY,
623 Statistics::NullCount => constants::ADBC_STATISTIC_NULL_COUNT_KEY,
624 Statistics::RowCount => constants::ADBC_STATISTIC_ROW_COUNT_KEY,
625 Statistics::Other { key, name: _ } => key,
626 }
627 }
628}
629
630impl AsRef<str> for Statistics {
631 fn as_ref(&self) -> &str {
632 match self {
633 Statistics::AverageByteWidth => constants::ADBC_STATISTIC_AVERAGE_BYTE_WIDTH_NAME,
634 Statistics::DistinctCount => constants::ADBC_STATISTIC_DISTINCT_COUNT_NAME,
635 Statistics::MaxByteWidth => constants::ADBC_STATISTIC_MAX_BYTE_WIDTH_NAME,
636 Statistics::MaxValue => constants::ADBC_STATISTIC_MAX_VALUE_NAME,
637 Statistics::MinValue => constants::ADBC_STATISTIC_MIN_VALUE_NAME,
638 Statistics::NullCount => constants::ADBC_STATISTIC_NULL_COUNT_NAME,
639 Statistics::RowCount => constants::ADBC_STATISTIC_ROW_COUNT_NAME,
640 Statistics::Other { key: _, name } => name,
641 }
642 }
643}
644
645impl std::fmt::Display for Statistics {
646 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
647 write!(f, "{}", self.as_ref())
648 }
649}