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".
This commit is contained in:
Taehoon Moon 2020-06-10 18:53:52 +09:00 committed by GitHub
parent d9a7491d9a
commit 2f1015339a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 63 additions and 1 deletions

View file

@ -19,8 +19,9 @@ name = "sqlparser"
path = "src/lib.rs"
[dependencies]
bigdecimal = { version = "0.1.0", optional = true }
bigdecimal = { version = "0.1.0", features = ["serde"], optional = true }
log = "0.4.5"
serde = { version = "1.0", features = ["derive"], optional = true }
[dev-dependencies]
simple_logger = "1.0.1"

View file

@ -11,10 +11,13 @@
// limitations under the License.
use super::ObjectName;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::fmt;
/// SQL data types
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DataType {
/// Fixed-length character type e.g. CHAR(10)
Char(Option<u64>),

View file

@ -13,10 +13,13 @@
//! AST types specific to CREATE/ALTER variants of [Statement]
//! (commonly referred to as Data Definition Language, or DDL)
use super::{display_comma_separated, DataType, Expr, Ident, ObjectName};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::fmt;
/// An `ALTER TABLE` (`Statement::AlterTable`) operation
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum AlterTableOperation {
/// `ADD <table_constraint>`
AddConstraint(TableConstraint),
@ -36,6 +39,7 @@ impl fmt::Display for AlterTableOperation {
/// A table-level constraint, specified in a `CREATE TABLE` or an
/// `ALTER TABLE ADD <constraint>` statement.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum TableConstraint {
/// `[ CONSTRAINT <name> ] { PRIMARY KEY | UNIQUE } (<columns>)`
Unique {
@ -95,6 +99,7 @@ impl fmt::Display for TableConstraint {
/// SQL column definition
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ColumnDef {
pub name: Ident,
pub data_type: DataType,
@ -129,6 +134,7 @@ impl fmt::Display for ColumnDef {
/// non-constraint options, lumping them all together under the umbrella of
/// "column options," and we allow any column option to be named.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ColumnOptionDef {
pub name: Option<Ident>,
pub option: ColumnOption,
@ -143,6 +149,7 @@ impl fmt::Display for ColumnOptionDef {
/// `ColumnOption`s are modifiers that follow a column definition in a `CREATE
/// TABLE` statement.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ColumnOption {
/// `NULL`
Null,
@ -220,6 +227,7 @@ fn display_constraint_name<'a>(name: &'a Option<Ident>) -> impl fmt::Display + '
///
/// Used in foreign key constraints in `ON UPDATE` and `ON DELETE` options.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ReferentialAction {
Restrict,
Cascade,

View file

@ -18,6 +18,8 @@ mod operator;
mod query;
mod value;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::fmt;
pub use self::data_type::DataType;
@ -71,6 +73,7 @@ where
/// An identifier, decomposed into its value or character data and the quote style.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Ident {
/// The value of the identifier without quotes.
pub value: String,
@ -127,6 +130,7 @@ impl fmt::Display for Ident {
/// A name of a table, view, custom type, etc., possibly multi-part, i.e. db.schema.obj
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ObjectName(pub Vec<Ident>);
impl fmt::Display for ObjectName {
@ -141,6 +145,7 @@ impl fmt::Display for ObjectName {
/// (e.g. boolean vs string), so the caller must handle expressions of
/// inappropriate type, like `WHERE 1` or `SELECT 1=1`, as necessary.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Expr {
/// Identifier e.g. table name or column name
Identifier(Ident),
@ -308,6 +313,7 @@ impl fmt::Display for Expr {
/// A window specification (i.e. `OVER (PARTITION BY .. ORDER BY .. etc.)`)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct WindowSpec {
pub partition_by: Vec<Expr>,
pub order_by: Vec<OrderByExpr>,
@ -353,6 +359,7 @@ impl fmt::Display for WindowSpec {
/// Note: The parser does not validate the specified bounds; the caller should
/// reject invalid bounds like `ROWS UNBOUNDED FOLLOWING` before execution.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct WindowFrame {
pub units: WindowFrameUnits,
pub start_bound: WindowFrameBound,
@ -364,6 +371,7 @@ pub struct WindowFrame {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum WindowFrameUnits {
Rows,
Range,
@ -398,6 +406,7 @@ impl FromStr for WindowFrameUnits {
/// Specifies [WindowFrame]'s `start_bound` and `end_bound`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum WindowFrameBound {
/// `CURRENT ROW`
CurrentRow,
@ -422,6 +431,7 @@ impl fmt::Display for WindowFrameBound {
/// A top-level statement (SELECT, INSERT, CREATE, etc.)
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Statement {
/// SELECT
Query(Box<Query>),
@ -766,6 +776,7 @@ impl fmt::Display for Statement {
/// SQL assignment `foo = expr` as used in SQLUpdate
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Assignment {
pub id: Ident,
pub value: Expr,
@ -779,6 +790,7 @@ impl fmt::Display for Assignment {
/// A function call
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Function {
pub name: ObjectName,
pub args: Vec<Expr>,
@ -805,6 +817,7 @@ impl fmt::Display for Function {
/// External table's available file format
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum FileFormat {
TEXTFILE,
SEQUENCEFILE,
@ -856,6 +869,7 @@ impl FromStr for FileFormat {
/// A `LISTAGG` invocation `LISTAGG( [ DISTINCT ] <expr>[, <separator> ] [ON OVERFLOW <on_overflow>] ) )
/// [ WITHIN GROUP (ORDER BY <within_group1>[, ...] ) ]`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ListAgg {
pub distinct: bool,
pub expr: Box<Expr>,
@ -892,6 +906,7 @@ impl fmt::Display for ListAgg {
/// The `ON OVERFLOW` clause of a LISTAGG invocation
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ListAggOnOverflow {
/// `ON OVERFLOW ERROR`
Error,
@ -925,6 +940,7 @@ impl fmt::Display for ListAggOnOverflow {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ObjectType {
Table,
View,
@ -944,6 +960,7 @@ impl fmt::Display for ObjectType {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SqlOption {
pub name: Ident,
pub value: Value,
@ -956,6 +973,7 @@ impl fmt::Display for SqlOption {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum TransactionMode {
AccessMode(TransactionAccessMode),
IsolationLevel(TransactionIsolationLevel),
@ -972,6 +990,7 @@ impl fmt::Display for TransactionMode {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum TransactionAccessMode {
ReadOnly,
ReadWrite,
@ -988,6 +1007,7 @@ impl fmt::Display for TransactionAccessMode {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum TransactionIsolationLevel {
ReadUncommitted,
ReadCommitted,
@ -1008,6 +1028,7 @@ impl fmt::Display for TransactionIsolationLevel {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ShowStatementFilter {
Like(String),
Where(Expr),
@ -1024,6 +1045,7 @@ impl fmt::Display for ShowStatementFilter {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum SetVariableValue {
Ident(Ident),
Literal(Value),

View file

@ -10,10 +10,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::fmt;
/// Unary operators
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum UnaryOperator {
Plus,
Minus,
@ -32,6 +35,7 @@ impl fmt::Display for UnaryOperator {
/// Binary operators
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum BinaryOperator {
Plus,
Minus,

View file

@ -11,10 +11,13 @@
// limitations under the License.
use super::*;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// The most complete variant of a `SELECT` query expression, optionally
/// including `WITH`, `UNION` / other set operations, and `ORDER BY`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Query {
/// WITH (common table expressions, or CTEs)
pub ctes: Vec<Cte>,
@ -55,6 +58,7 @@ impl fmt::Display for Query {
/// A node in a tree, representing a "query body" expression, roughly:
/// `SELECT ... [ {UNION|EXCEPT|INTERSECT} SELECT ...]`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum SetExpr {
/// Restricted SELECT .. FROM .. HAVING (no ORDER BY or set operations)
Select(Box<Select>),
@ -92,6 +96,7 @@ impl fmt::Display for SetExpr {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum SetOperator {
Union,
Except,
@ -112,6 +117,7 @@ impl fmt::Display for SetOperator {
/// appear either as the only body item of an `SQLQuery`, or as an operand
/// to a set operation like `UNION`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Select {
pub distinct: bool,
/// MSSQL syntax: `TOP (<N>) [ PERCENT ] [ WITH TIES ]`
@ -156,6 +162,7 @@ impl fmt::Display for Select {
/// of the columns returned by the query. The parser does not validate that the
/// number of columns in the query matches the number of columns in the query.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Cte {
pub alias: TableAlias,
pub query: Query,
@ -169,6 +176,7 @@ impl fmt::Display for Cte {
/// One item of the comma-separated list following `SELECT`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum SelectItem {
/// Any expression, not followed by `[ AS ] alias`
UnnamedExpr(Expr),
@ -192,6 +200,7 @@ impl fmt::Display for SelectItem {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TableWithJoins {
pub relation: TableFactor,
pub joins: Vec<Join>,
@ -209,6 +218,7 @@ impl fmt::Display for TableWithJoins {
/// A table name or a parenthesized subquery with an optional alias
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum TableFactor {
Table {
name: ObjectName,
@ -274,6 +284,7 @@ impl fmt::Display for TableFactor {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TableAlias {
pub name: Ident,
pub columns: Vec<Ident>,
@ -290,6 +301,7 @@ impl fmt::Display for TableAlias {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Join {
pub relation: TableFactor,
pub join_operator: JoinOperator,
@ -355,6 +367,7 @@ impl fmt::Display for Join {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum JoinOperator {
Inner(JoinConstraint),
LeftOuter(JoinConstraint),
@ -368,6 +381,7 @@ pub enum JoinOperator {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum JoinConstraint {
On(Expr),
Using(Vec<Ident>),
@ -376,6 +390,7 @@ pub enum JoinConstraint {
/// An `ORDER BY` expression
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct OrderByExpr {
pub expr: Expr,
/// Optional `ASC` or `DESC`
@ -402,6 +417,7 @@ impl fmt::Display for OrderByExpr {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Offset {
pub value: Expr,
pub rows: OffsetRows,
@ -415,6 +431,7 @@ impl fmt::Display for Offset {
/// Stores the keyword after `OFFSET <number>`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum OffsetRows {
/// Omitting ROW/ROWS is non-standard MySQL quirk.
None,
@ -433,6 +450,7 @@ impl fmt::Display for OffsetRows {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Fetch {
pub with_ties: bool,
pub percent: bool,
@ -452,6 +470,7 @@ impl fmt::Display for Fetch {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Top {
/// SQL semantic equivalent of LIMIT but with same structure as FETCH.
pub with_ties: bool,
@ -472,6 +491,7 @@ impl fmt::Display for Top {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Values(pub Vec<Vec<Expr>>);
impl fmt::Display for Values {

View file

@ -12,10 +12,13 @@
#[cfg(feature = "bigdecimal")]
use bigdecimal::BigDecimal;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::fmt;
/// Primitive SQL values such as number and string
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Value {
/// Numeric literal
#[cfg(not(feature = "bigdecimal"))]
@ -116,6 +119,7 @@ impl fmt::Display for Value {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DateTimeField {
Year,
Month,