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
use crate::data_order::DataOrder;
use std::any::type_name;
use std::fmt;
use thiserror::Error;

pub type Result<T> = std::result::Result<T, ConnectorXError>;
pub type OutResult<T> = std::result::Result<T, ConnectorXOutError>;

#[derive(Error, Debug)]
pub enum ConnectorXOutError {
    #[error("File {0} not found.")]
    FileNotFoundError(String),

    #[error("Source {0} not supported.")]
    SourceNotSupport(String),

    #[error(transparent)]
    IOError(#[from] std::io::Error),

    #[error(transparent)]
    JsonError(#[from] serde_json::Error),

    #[cfg(feature = "federation")]
    #[error(transparent)]
    J4RSError(#[from] j4rs::errors::J4RsError),

    #[cfg(feature = "fed_exec")]
    #[error(transparent)]
    DataFusionError(#[from] datafusion::error::DataFusionError),

    #[error(transparent)]
    UrlParseError(#[from] url::ParseError),

    #[error(transparent)]
    ConnectorXInternalError(#[from] ConnectorXError),

    #[cfg(feature = "src_postgres")]
    #[error(transparent)]
    PostgresSourceError(#[from] crate::sources::postgres::PostgresSourceError),

    #[cfg(feature = "src_postgres")]
    #[error(transparent)]
    PostgresError(#[from] postgres::Error),

    #[cfg(feature = "src_mysql")]
    #[error(transparent)]
    MySQLSourceError(#[from] crate::sources::mysql::MySQLSourceError),

    #[cfg(feature = "src_mysql")]
    #[error(transparent)]
    MysqlError(#[from] r2d2_mysql::mysql::Error),

    #[cfg(feature = "src_mssql")]
    #[error(transparent)]
    MsSQLSourceError(#[from] crate::sources::mssql::MsSQLSourceError),

    #[cfg(feature = "src_mssql")]
    #[error(transparent)]
    MsSQL(#[from] tiberius::error::Error),

    #[cfg(feature = "src_sqlite")]
    #[error(transparent)]
    SQLiteSourceError(#[from] crate::sources::sqlite::SQLiteSourceError),

    #[cfg(feature = "src_sqlite")]
    #[error(transparent)]
    SQLiteError(#[from] rusqlite::Error),

    #[cfg(feature = "src_oracle")]
    #[error(transparent)]
    OracleSourceError(#[from] crate::sources::oracle::OracleSourceError),

    #[cfg(feature = "src_oracle")]
    #[error(transparent)]
    OracleError(#[from] r2d2_oracle::oracle::Error),

    #[cfg(feature = "src_bigquery")]
    #[error(transparent)]
    BigQuerySourceError(#[from] crate::sources::bigquery::BigQuerySourceError),

    #[cfg(feature = "src_bigquery")]
    #[error(transparent)]
    BigQueryError(#[from] gcp_bigquery_client::error::BQError),

    #[cfg(feature = "dst_arrow")]
    #[error(transparent)]
    ArrowError(#[from] crate::destinations::arrow::ArrowDestinationError),

    #[cfg(feature = "dst_arrow")]
    #[error(transparent)]
    ArrowStreamError(#[from] crate::destinations::arrowstream::ArrowDestinationError),

    #[cfg(feature = "dst_arrow2")]
    #[error(transparent)]
    Arrow2Error(#[from] crate::destinations::arrow2::Arrow2DestinationError),

    #[cfg(all(feature = "src_postgres", feature = "dst_arrow"))]
    #[error(transparent)]
    PostgresArrowTransportError(#[from] crate::transports::PostgresArrowTransportError),

    #[cfg(all(feature = "src_postgres", feature = "dst_arrow2"))]
    #[error(transparent)]
    PostgresArrow2TransportError(#[from] crate::transports::PostgresArrow2TransportError),

    #[cfg(all(feature = "src_mysql", feature = "dst_arrow"))]
    #[error(transparent)]
    MySQLArrowTransportError(#[from] crate::transports::MySQLArrowTransportError),

    #[cfg(all(feature = "src_mysql", feature = "dst_arrow2"))]
    #[error(transparent)]
    MySQLArrow2TransportError(#[from] crate::transports::MySQLArrow2TransportError),

    #[cfg(all(feature = "src_sqlite", feature = "dst_arrow"))]
    #[error(transparent)]
    SQLiteArrowTransportError(#[from] crate::transports::SQLiteArrowTransportError),

    #[cfg(all(feature = "src_sqlite", feature = "dst_arrow2"))]
    #[error(transparent)]
    SQLiteArrow2TransportError(#[from] crate::transports::SQLiteArrow2TransportError),

    #[cfg(all(feature = "src_mssql", feature = "dst_arrow"))]
    #[error(transparent)]
    MsSQLArrowTransportError(#[from] crate::transports::MsSQLArrowTransportError),

    #[cfg(all(feature = "src_mssql", feature = "dst_arrow2"))]
    #[error(transparent)]
    MsSQLArrow2TransportError(#[from] crate::transports::MsSQLArrow2TransportError),

    #[cfg(all(feature = "src_oracle", feature = "dst_arrow"))]
    #[error(transparent)]
    OracleArrowTransportError(#[from] crate::transports::OracleArrowTransportError),

    #[cfg(all(feature = "src_oracle", feature = "dst_arrow2"))]
    #[error(transparent)]
    OracleArrow2TransportError(#[from] crate::transports::OracleArrow2TransportError),

    #[cfg(all(feature = "src_bigquery", feature = "dst_arrow"))]
    #[error(transparent)]
    BigqueryArrowTransportError(#[from] crate::transports::BigQueryArrowTransportError),

    #[cfg(all(feature = "src_bigquery", feature = "dst_arrow2"))]
    #[error(transparent)]
    BigqueryArrow2TransportError(#[from] crate::transports::BigQueryArrow2TransportError),

    /// Any other errors that are too trivial to be put here explicitly.
    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

/// Errors that can be raised from this library.
#[derive(Error, Debug)]
pub enum ConnectorXError {
    /// The required type does not same as the schema defined.
    #[error("Data type unexpected: {0:?} expected, {1} found.")]
    TypeCheckFailed(String, &'static str),

    #[error("Data order not supported {0:?}.")]
    UnsupportedDataOrder(DataOrder),

    #[error("Cannot resolve data order: got {0:?} from source, {1:?} from destination.")]
    CannotResolveDataOrder(Vec<DataOrder>, Vec<DataOrder>),

    #[error("Cannot produce a {0}, context: {1}.")]
    CannotProduce(&'static str, ProduceContext),

    #[error("No conversion rule from {0} to {1}.")]
    NoConversionRule(String, String),

    #[error("Only support single query with SELECT statement, got {0}.")]
    SqlQueryNotSupported(String),

    #[error("Cannot get total number of rows in advance.")]
    CountError(),

    #[error(transparent)]
    SQLParserError(#[from] sqlparser::parser::ParserError),

    #[error(transparent)]
    StdIOError(#[from] std::io::Error),

    #[error(transparent)]
    StdVarError(#[from] std::env::VarError),

    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

impl ConnectorXError {
    pub fn cannot_produce<T>(context: Option<String>) -> Self {
        ConnectorXError::CannotProduce(type_name::<T>(), context.into())
    }
}

#[derive(Debug)]
pub enum ProduceContext {
    NoContext,
    Context(String),
}

impl From<Option<String>> for ProduceContext {
    fn from(val: Option<String>) -> Self {
        match val {
            Some(c) => ProduceContext::Context(c),
            None => ProduceContext::NoContext,
        }
    }
}

impl fmt::Display for ProduceContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ProduceContext::NoContext => write!(f, "No Context"),
            ProduceContext::Context(s) => write!(f, "{}", s),
        }
    }
}