Skip to content

Commit 2f10153

Browse files
authored
Add serde support to AST structs and enums (#196)
Apply serde to AST structs and enums to be serializable/deserializable. serde support is optional, can be activated by feature named "serde".
1 parent d9a7491 commit 2f10153

File tree

7 files changed

+63
-1
lines changed

7 files changed

+63
-1
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ name = "sqlparser"
1919
path = "src/lib.rs"
2020

2121
[dependencies]
22-
bigdecimal = { version = "0.1.0", optional = true }
22+
bigdecimal = { version = "0.1.0", features = ["serde"], optional = true }
2323
log = "0.4.5"
24+
serde = { version = "1.0", features = ["derive"], optional = true }
2425

2526
[dev-dependencies]
2627
simple_logger = "1.0.1"

src/ast/data_type.rs

+3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
// limitations under the License.
1212

1313
use super::ObjectName;
14+
#[cfg(feature = "serde")]
15+
use serde::{Deserialize, Serialize};
1416
use std::fmt;
1517

1618
/// SQL data types
1719
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
20+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1821
pub enum DataType {
1922
/// Fixed-length character type e.g. CHAR(10)
2023
Char(Option<u64>),

src/ast/ddl.rs

+8
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
//! AST types specific to CREATE/ALTER variants of [Statement]
1414
//! (commonly referred to as Data Definition Language, or DDL)
1515
use super::{display_comma_separated, DataType, Expr, Ident, ObjectName};
16+
#[cfg(feature = "serde")]
17+
use serde::{Deserialize, Serialize};
1618
use std::fmt;
1719

1820
/// An `ALTER TABLE` (`Statement::AlterTable`) operation
1921
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
22+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2023
pub enum AlterTableOperation {
2124
/// `ADD <table_constraint>`
2225
AddConstraint(TableConstraint),
@@ -36,6 +39,7 @@ impl fmt::Display for AlterTableOperation {
3639
/// A table-level constraint, specified in a `CREATE TABLE` or an
3740
/// `ALTER TABLE ADD <constraint>` statement.
3841
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
42+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3943
pub enum TableConstraint {
4044
/// `[ CONSTRAINT <name> ] { PRIMARY KEY | UNIQUE } (<columns>)`
4145
Unique {
@@ -95,6 +99,7 @@ impl fmt::Display for TableConstraint {
9599

96100
/// SQL column definition
97101
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
102+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
98103
pub struct ColumnDef {
99104
pub name: Ident,
100105
pub data_type: DataType,
@@ -129,6 +134,7 @@ impl fmt::Display for ColumnDef {
129134
/// non-constraint options, lumping them all together under the umbrella of
130135
/// "column options," and we allow any column option to be named.
131136
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
137+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
132138
pub struct ColumnOptionDef {
133139
pub name: Option<Ident>,
134140
pub option: ColumnOption,
@@ -143,6 +149,7 @@ impl fmt::Display for ColumnOptionDef {
143149
/// `ColumnOption`s are modifiers that follow a column definition in a `CREATE
144150
/// TABLE` statement.
145151
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
152+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
146153
pub enum ColumnOption {
147154
/// `NULL`
148155
Null,
@@ -220,6 +227,7 @@ fn display_constraint_name<'a>(name: &'a Option<Ident>) -> impl fmt::Display + '
220227
///
221228
/// Used in foreign key constraints in `ON UPDATE` and `ON DELETE` options.
222229
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
230+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
223231
pub enum ReferentialAction {
224232
Restrict,
225233
Cascade,

src/ast/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ mod operator;
1818
mod query;
1919
mod value;
2020

21+
#[cfg(feature = "serde")]
22+
use serde::{Deserialize, Serialize};
2123
use std::fmt;
2224

2325
pub use self::data_type::DataType;
@@ -71,6 +73,7 @@ where
7173

7274
/// An identifier, decomposed into its value or character data and the quote style.
7375
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
76+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7477
pub struct Ident {
7578
/// The value of the identifier without quotes.
7679
pub value: String,
@@ -127,6 +130,7 @@ impl fmt::Display for Ident {
127130

128131
/// A name of a table, view, custom type, etc., possibly multi-part, i.e. db.schema.obj
129132
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
133+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
130134
pub struct ObjectName(pub Vec<Ident>);
131135

132136
impl fmt::Display for ObjectName {
@@ -141,6 +145,7 @@ impl fmt::Display for ObjectName {
141145
/// (e.g. boolean vs string), so the caller must handle expressions of
142146
/// inappropriate type, like `WHERE 1` or `SELECT 1=1`, as necessary.
143147
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
148+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
144149
pub enum Expr {
145150
/// Identifier e.g. table name or column name
146151
Identifier(Ident),
@@ -308,6 +313,7 @@ impl fmt::Display for Expr {
308313

309314
/// A window specification (i.e. `OVER (PARTITION BY .. ORDER BY .. etc.)`)
310315
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
316+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
311317
pub struct WindowSpec {
312318
pub partition_by: Vec<Expr>,
313319
pub order_by: Vec<OrderByExpr>,
@@ -353,6 +359,7 @@ impl fmt::Display for WindowSpec {
353359
/// Note: The parser does not validate the specified bounds; the caller should
354360
/// reject invalid bounds like `ROWS UNBOUNDED FOLLOWING` before execution.
355361
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
362+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
356363
pub struct WindowFrame {
357364
pub units: WindowFrameUnits,
358365
pub start_bound: WindowFrameBound,
@@ -364,6 +371,7 @@ pub struct WindowFrame {
364371
}
365372

366373
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
374+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
367375
pub enum WindowFrameUnits {
368376
Rows,
369377
Range,
@@ -398,6 +406,7 @@ impl FromStr for WindowFrameUnits {
398406

399407
/// Specifies [WindowFrame]'s `start_bound` and `end_bound`
400408
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
409+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
401410
pub enum WindowFrameBound {
402411
/// `CURRENT ROW`
403412
CurrentRow,
@@ -422,6 +431,7 @@ impl fmt::Display for WindowFrameBound {
422431
/// A top-level statement (SELECT, INSERT, CREATE, etc.)
423432
#[allow(clippy::large_enum_variant)]
424433
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
434+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
425435
pub enum Statement {
426436
/// SELECT
427437
Query(Box<Query>),
@@ -766,6 +776,7 @@ impl fmt::Display for Statement {
766776

767777
/// SQL assignment `foo = expr` as used in SQLUpdate
768778
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
779+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
769780
pub struct Assignment {
770781
pub id: Ident,
771782
pub value: Expr,
@@ -779,6 +790,7 @@ impl fmt::Display for Assignment {
779790

780791
/// A function call
781792
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
793+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
782794
pub struct Function {
783795
pub name: ObjectName,
784796
pub args: Vec<Expr>,
@@ -805,6 +817,7 @@ impl fmt::Display for Function {
805817

806818
/// External table's available file format
807819
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
820+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
808821
pub enum FileFormat {
809822
TEXTFILE,
810823
SEQUENCEFILE,
@@ -856,6 +869,7 @@ impl FromStr for FileFormat {
856869
/// A `LISTAGG` invocation `LISTAGG( [ DISTINCT ] <expr>[, <separator> ] [ON OVERFLOW <on_overflow>] ) )
857870
/// [ WITHIN GROUP (ORDER BY <within_group1>[, ...] ) ]`
858871
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
872+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
859873
pub struct ListAgg {
860874
pub distinct: bool,
861875
pub expr: Box<Expr>,
@@ -892,6 +906,7 @@ impl fmt::Display for ListAgg {
892906

893907
/// The `ON OVERFLOW` clause of a LISTAGG invocation
894908
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
909+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
895910
pub enum ListAggOnOverflow {
896911
/// `ON OVERFLOW ERROR`
897912
Error,
@@ -925,6 +940,7 @@ impl fmt::Display for ListAggOnOverflow {
925940
}
926941

927942
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
943+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
928944
pub enum ObjectType {
929945
Table,
930946
View,
@@ -944,6 +960,7 @@ impl fmt::Display for ObjectType {
944960
}
945961

946962
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
963+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
947964
pub struct SqlOption {
948965
pub name: Ident,
949966
pub value: Value,
@@ -956,6 +973,7 @@ impl fmt::Display for SqlOption {
956973
}
957974

958975
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
976+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
959977
pub enum TransactionMode {
960978
AccessMode(TransactionAccessMode),
961979
IsolationLevel(TransactionIsolationLevel),
@@ -972,6 +990,7 @@ impl fmt::Display for TransactionMode {
972990
}
973991

974992
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
993+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
975994
pub enum TransactionAccessMode {
976995
ReadOnly,
977996
ReadWrite,
@@ -988,6 +1007,7 @@ impl fmt::Display for TransactionAccessMode {
9881007
}
9891008

9901009
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1010+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9911011
pub enum TransactionIsolationLevel {
9921012
ReadUncommitted,
9931013
ReadCommitted,
@@ -1008,6 +1028,7 @@ impl fmt::Display for TransactionIsolationLevel {
10081028
}
10091029

10101030
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1031+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10111032
pub enum ShowStatementFilter {
10121033
Like(String),
10131034
Where(Expr),
@@ -1024,6 +1045,7 @@ impl fmt::Display for ShowStatementFilter {
10241045
}
10251046

10261047
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1048+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10271049
pub enum SetVariableValue {
10281050
Ident(Ident),
10291051
Literal(Value),

src/ast/operator.rs

+4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13+
#[cfg(feature = "serde")]
14+
use serde::{Deserialize, Serialize};
1315
use std::fmt;
1416

1517
/// Unary operators
1618
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1720
pub enum UnaryOperator {
1821
Plus,
1922
Minus,
@@ -32,6 +35,7 @@ impl fmt::Display for UnaryOperator {
3235

3336
/// Binary operators
3437
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
38+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3539
pub enum BinaryOperator {
3640
Plus,
3741
Minus,

0 commit comments

Comments
 (0)