sql_bridge/
error.rs

1use sqlparser::ast::{
2    AlterTableOperation, AssignmentTarget, BinaryOperator, ColumnOption, DataType, Expr,
3    FunctionArg, FunctionArguments, IndexColumn, JoinConstraint, JoinOperator, ObjectType,
4    ReferentialAction, SelectItem, SetExpr, Statement, TableConstraint, TableFactor,
5    TableWithJoins, Value,
6};
7
8use crate::ast::Selection;
9
10use sqlparser::parser::ParserError;
11use std::io;
12use std::string::FromUtf8Error;
13
14#[derive(Debug)]
15pub enum Error {
16    DataType {
17        data_type: Box<DataType>,
18    },
19    ColumnOption {
20        option: Box<ColumnOption>,
21    },
22    OnDeleteConstrait {
23        referential_action: ReferentialAction,
24    },
25    PrimaryKey {
26        reason: &'static str,
27    },
28    PrimaryKeyWithExpression {
29        expr: Box<Expr>,
30    },
31    ForeignKey {
32        reason: &'static str,
33    },
34    TableConstraint {
35        constraint: Box<TableConstraint>,
36    },
37    CompoundIdentifier {
38        length: usize,
39    },
40    SelectionValue {
41        value: Box<Value>,
42    },
43    SelectionInList {
44        selection: Box<Selection>,
45    },
46    SelectionFromExpr {
47        expr: Box<Expr>,
48    },
49    InsertSourceExpression {
50        expr: Box<Expr>,
51    },
52    InsertSourceValue {
53        value: Box<Value>,
54    },
55    UpdateExpression {
56        expr: Box<Expr>,
57    },
58    UpdateValue {
59        value: Box<Value>,
60    },
61    BinaryOperator {
62        op: BinaryOperator,
63    },
64    Keyword {
65        keyword: &'static str,
66    },
67    JoinConstraint {
68        constraint: Box<JoinConstraint>,
69    },
70    JoinOperator {
71        op: Box<JoinOperator>,
72    },
73    TableAlias,
74    /// Postgre + MSSQL
75    TableValuedFunctions,
76    ///  MSSQL-specific `WITH (...)` hints such as NOLOCK.
77    TableWithHints,
78    /// Table time-travel, as supported by BigQuery and MSSQL.
79    TableVersioning,
80    /// Postgres.
81    TableWithOrdinality,
82    /// Mysql
83    TableWithPartitions,
84    /// Optional PartiQL JsonPath: <https://partiql.org/dql/from.html>
85    TableWithJsonPath,
86    /// Optional table sample modifier
87    /// See: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#sample-clause>
88    TableWithSampleModifier,
89    /// Optional index hints(mysql)
90    /// See: <https://dev.mysql.com/doc/refman/8.4/en/index-hints.html>
91    TableWithIndexHints,
92    TableFactor {
93        factor: Box<TableFactor>,
94    },
95    TableJoins {
96        table_joins: Vec<TableWithJoins>,
97    },
98    Drop {
99        reason: &'static str,
100        object_type: Option<ObjectType>,
101    },
102    AlterTable {
103        reason: &'static str,
104    },
105    AlterTableOp {
106        op: Box<AlterTableOperation>,
107    },
108    ObjectName {
109        reason: &'static str,
110    },
111    CreateTable {
112        reason: &'static str,
113    },
114    FunctionArguments {
115        reason: &'static str,
116        arguments: Box<FunctionArguments>,
117    },
118    FunctionArgument {
119        reason: &'static str,
120        argument: Box<FunctionArg>,
121    },
122    CreateIndex {
123        reason: &'static str,
124    },
125    CreateIndexColumn {
126        column: Box<IndexColumn>,
127    },
128    CTE,
129    Fetch,
130    Limit,
131    Locks,
132    For,
133    Select {
134        set_expr: Box<SetExpr>,
135    },
136    Top,
137    EmptyProjections,
138    Count {
139        reason: &'static str,
140        args: Vec<crate::ast::FunctionArg>,
141    },
142    Function {
143        name: String,
144    },
145    Projection {
146        select_item: Box<SelectItem>,
147    },
148    OrderBy {
149        reason: &'static str,
150    },
151    GroupBy {
152        reason: &'static str,
153    },
154    Insert {
155        reason: &'static str,
156    },
157    InsertSourceEmpty,
158    InsertTableObject,
159    InsertSource {
160        set_expr: Box<SetExpr>,
161    },
162    Update {
163        reason: &'static str,
164    },
165    UpdateTableType {
166        table_factor: Box<TableFactor>,
167    },
168    UpdateAssignmentTarget {
169        target: Box<AssignmentTarget>,
170    },
171    Delete {
172        reason: &'static str,
173    },
174    DeleteToSql {
175        reason: &'static str,
176    },
177    DropIndex {
178        reason: &'static str,
179    },
180    Statement {
181        statement: Box<Statement>,
182    },
183    Serial,
184    Io(io::Error),
185    Parser(ParserError),
186    Utf8(FromUtf8Error),
187}
188
189impl std::fmt::Display for Error {
190    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
191        match self {
192            Error::DataType { data_type } => {
193                write!(f, "unsupported data type: {data_type:?}")
194            }
195            Error::ColumnOption { option } => {
196                write!(f, "unsupported column option: {option:?}")
197            }
198            Error::OnDeleteConstrait { referential_action } => {
199                write!(
200                    f,
201                    "unsupported on delete constraint in foreign key: {}",
202                    referential_action
203                )
204            }
205            Error::PrimaryKey { reason } => {
206                write!(f, "unsupported primary key with {}", reason)
207            }
208            Error::PrimaryKeyWithExpression { expr } => {
209                write!(
210                    f,
211                    "unsupported primary key with unsupported expression: {expr:?}"
212                )
213            }
214            Error::ForeignKey { reason } => {
215                write!(f, "unsupported foreign key with {}", reason)
216            }
217            Error::TableConstraint { constraint } => {
218                write!(f, "unsupported table constraint: {constraint:?}")
219            }
220            Error::CompoundIdentifier { length } => {
221                write!(f, "unsupported compound identifier with length {}", length)
222            }
223            Error::SelectionValue { value } => {
224                write!(f, "unsupporetd selection value: {value:?} ")
225            }
226            Error::SelectionInList { selection } => {
227                write!(f, "unsupporetd in list selection: {selection:?} ")
228            }
229            Error::SelectionFromExpr { expr } => {
230                write!(f, "unsupported selection expr: {expr:?}")
231            }
232            Error::InsertSourceExpression { expr } => {
233                write!(f, "unsupported insert source expr: {expr:?}")
234            }
235            Error::InsertSourceValue { value } => {
236                write!(f, "unsupported insert source value: {value:?}")
237            }
238            Error::UpdateExpression { expr } => {
239                write!(f, "unsupported update from expr: {expr:?}")
240            }
241            Error::UpdateValue { value } => {
242                write!(f, "unsupported update value: {value:?}")
243            }
244            Error::BinaryOperator { op } => {
245                write!(f, "unsupported binary operator: {op:?}")
246            }
247            Error::Keyword { keyword } => {
248                write!(f, "unsupported keyword: '{keyword}'")
249            }
250            Error::JoinConstraint { constraint } => {
251                write!(f, "unsupported join constraint: {constraint:?}")
252            }
253            Error::JoinOperator { op } => {
254                write!(f, "unsupported join operator: {op:?}")
255            }
256            Error::TableAlias => {
257                write!(f, "table aliasing is unsupported")
258            }
259            Error::TableValuedFunctions => {
260                write!(
261                    f,
262                    "arguments of a table-valued function are not supported in table factor"
263                )
264            }
265            Error::TableWithHints => {
266                write!(f, "with hints are not supported in table factor")
267            }
268            Error::TableVersioning => {
269                write!(f, "table versioning is not supported in table factor")
270            }
271            Error::TableWithOrdinality => {
272                write!(f, "table with ordinality is not supported in table factor")
273            }
274            Error::TableWithPartitions => {
275                write!(f, "partitions are not supported in table factor")
276            }
277            Error::TableWithJsonPath => {
278                write!(f, "json path is not supported in table factor")
279            }
280            Error::TableWithSampleModifier => {
281                write!(f, "sample is not supported in table factor")
282            }
283            Error::TableWithIndexHints => {
284                write!(f, "index hints are not supported in table factor")
285            }
286            Error::TableFactor { factor } => {
287                write!(f, "unsupported table factor: {factor:?}")
288            }
289            Error::TableJoins { table_joins } => {
290                write!(
291                    f,
292                    "select with multiple tables is not supported yet: {table_joins:?}"
293                )
294            }
295            Error::Drop {
296                reason,
297                object_type,
298            } => match object_type {
299                None => write!(f, "unsupported drop: {reason}"),
300                Some(ot) => write!(f, "unsupported drop: {reason} {ot:?}"),
301            },
302            Error::AlterTable { reason } => {
303                write!(f, "unsupported alter table: {reason}")
304            }
305            Error::AlterTableOp { op } => {
306                write!(f, "unsupported operation: {op:?}")
307            }
308            Error::ObjectName { reason } => {
309                write!(f, "failed to parse object name: {reason}")
310            }
311            Error::CreateTable { reason } => {
312                write!(f, "unsupported create table: {reason}")
313            }
314            Error::FunctionArguments { reason, arguments } => {
315                write!(
316                    f,
317                    "unsupported function arguments: {reason}, function arguments: {arguments:?}"
318                )
319            }
320            Error::FunctionArgument { reason, argument } => {
321                write!(
322                    f,
323                    "unsupported function argument: {reason}, function argument: {argument:?}"
324                )
325            }
326            Error::CreateIndex { reason } => {
327                write!(f, "unsupported create index: {reason}")
328            }
329            Error::CreateIndexColumn { column } => {
330                write!(f, "unsupported create index column: {column:?}")
331            }
332            Error::CTE => {
333                write!(f, "CTE are not supported")
334            }
335            Error::Fetch => {
336                write!(f, "Fetch is not supported")
337            }
338            Error::Limit => {
339                write!(f, "limit is not supported")
340            }
341            Error::Locks => {
342                write!(f, "locks are not supported")
343            }
344            Error::For => {
345                write!(f, "for clause is not supported")
346            }
347            Error::Select { set_expr } => {
348                write!(f, "unsupported select set expr: {set_expr:?}")
349            }
350            Error::Top => {
351                write!(f, "top is not supported")
352            }
353            Error::EmptyProjections => {
354                write!(f, "empty projections are not supported")
355            }
356            Error::Count { reason, args } => {
357                write!(f, "unsupported count: {reason}, args: {args:?}")
358            }
359            Error::Function { name } => {
360                write!(f, "unsupported function '{name}'")
361            }
362            Error::Projection { select_item } => {
363                write!(f, "unsupported projection select item: {select_item:?}")
364            }
365            Error::OrderBy { reason } => {
366                write!(f, "{reason}")
367            }
368            Error::GroupBy { reason } => {
369                write!(f, "{reason}")
370            }
371            Error::Insert { reason } => {
372                write!(f, "unsupported insert: {reason}")
373            }
374            Error::InsertSourceEmpty => {
375                write!(f, "unsupported insert, source is empty")
376            }
377            Error::InsertTableObject => {
378                write!(f, "unsupported table name type")
379            }
380            Error::InsertSource { set_expr } => {
381                write!(f, "unsupported insert source: {set_expr:?}")
382            }
383            Error::Update { reason } => {
384                write!(f, "unsupported update: {reason}")
385            }
386            Error::UpdateTableType { table_factor } => {
387                write!(f, "unsupported table type: {table_factor:?}")
388            }
389            Error::UpdateAssignmentTarget { target } => {
390                write!(f, "unsupported assignment target: {target:?}")
391            }
392            Error::Delete { reason } => {
393                write!(f, "unsupported delete: {reason}")
394            }
395            Error::DeleteToSql { reason } => {
396                write!(f, "unsupported delete: {reason}")
397            }
398            Error::DropIndex { reason } => {
399                write!(f, "unsupported drop index: {reason}")
400            }
401            Error::Statement { statement } => {
402                write!(f, "unsupported statement: {statement:?}")
403            }
404            Error::Serial => {
405                write!(
406                    f,
407                    "expected smallserial/serial/bigserial with `PRIMARY KEY` constraint"
408                )
409            }
410            Error::Io(err) => {
411                write!(f, "IO error: {}", err)
412            }
413            Error::Parser(err) => {
414                write!(f, "Parser error: {err}")
415            }
416            Error::Utf8(err) => {
417                write!(f, "UTF-8 error: {err}")
418            }
419        }
420    }
421}
422
423impl From<io::Error> for Error {
424    fn from(err: io::Error) -> Self {
425        Error::Io(err)
426    }
427}
428
429impl From<ParserError> for Error {
430    fn from(err: ParserError) -> Self {
431        Error::Parser(err)
432    }
433}
434
435impl From<FromUtf8Error> for Error {
436    fn from(err: FromUtf8Error) -> Self {
437        Error::Utf8(err)
438    }
439}
440
441impl std::error::Error for Error {}