Skip to content

Commit

Permalink
refactor: remove common module
Browse files Browse the repository at this point in the history
  • Loading branch information
xring committed Apr 6, 2024
1 parent a4bcc18 commit 146f3e7
Show file tree
Hide file tree
Showing 65 changed files with 1,265 additions and 1,102 deletions.
39 changes: 39 additions & 0 deletions src/base/algorithm_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use nom::branch::alt;
use nom::bytes::complete::{tag, tag_no_case};
use nom::character::complete::multispace0;
use nom::combinator::{map, opt};
use nom::sequence::tuple;
use nom::IResult;

use base::ParseSQLError;

/// ALGORITHM [=] {DEFAULT | INSTANT | INPLACE | COPY}
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum AlgorithmType {
Instant, // alter table only
Default,
Inplace,
Copy,
}

impl AlgorithmType {
/// algorithm_option:
/// ALGORITHM [=] {DEFAULT | INSTANT | INPLACE | COPY}
pub fn parse(i: &str) -> IResult<&str, AlgorithmType, ParseSQLError<&str>> {
map(
tuple((
tag_no_case("ALGORITHM"),
multispace0,
opt(tag("=")),
multispace0,
alt((
map(tag_no_case("DEFAULT"), |_| AlgorithmType::Default),
map(tag_no_case("INSTANT"), |_| AlgorithmType::Instant),
map(tag_no_case("INPLACE"), |_| AlgorithmType::Inplace),
map(tag_no_case("COPY"), |_| AlgorithmType::Copy),
)),
)),
|x| x.4,
)(i)
}
}
85 changes: 40 additions & 45 deletions src/common/arithmetic.rs → src/base/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use nom::{

use base::column::Column;
use base::error::ParseSQLErrorKind;
use base::{DataType, Literal, ParseSQLError};
use common::as_alias;
use base::{CommonParser, DataType, Literal, ParseSQLError};

#[derive(Debug, Clone, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum ArithmeticOperator {
Expand All @@ -41,6 +40,17 @@ impl ArithmeticOperator {
}
}

impl fmt::Display for ArithmeticOperator {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ArithmeticOperator::Add => write!(f, "+"),
ArithmeticOperator::Subtract => write!(f, "-"),
ArithmeticOperator::Multiply => write!(f, "*"),
ArithmeticOperator::Divide => write!(f, "/"),
}
}
}

#[derive(Debug, Clone, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum ArithmeticBase {
Column(Column),
Expand All @@ -66,6 +76,16 @@ impl ArithmeticBase {
}
}

impl fmt::Display for ArithmeticBase {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ArithmeticBase::Column(ref col) => write!(f, "{}", col),
ArithmeticBase::Scalar(ref lit) => write!(f, "{}", lit.to_string()),
ArithmeticBase::Bracketed(ref ari) => write!(f, "({})", ari),
}
}
}

#[derive(Debug, Clone, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum ArithmeticItem {
Base(ArithmeticBase),
Expand Down Expand Up @@ -156,6 +176,15 @@ impl ArithmeticItem {
}
}

impl fmt::Display for ArithmeticItem {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ArithmeticItem::Base(ref b) => write!(f, "{}", b),
ArithmeticItem::Expr(ref expr) => write!(f, "{}", expr),
}
}
}

#[derive(Debug, Clone, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct Arithmetic {
pub op: ArithmeticOperator,
Expand Down Expand Up @@ -186,6 +215,12 @@ impl Arithmetic {
}
}

impl fmt::Display for Arithmetic {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{} {} {}", self.left, self.op, self.right)
}
}

#[derive(Debug, Clone, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct ArithmeticExpression {
pub ari: Arithmetic,
Expand All @@ -195,7 +230,7 @@ pub struct ArithmeticExpression {
impl ArithmeticExpression {
pub fn parse(i: &str) -> IResult<&str, ArithmeticExpression, ParseSQLError<&str>> {
map(
pair(Arithmetic::parse, opt(as_alias)),
pair(Arithmetic::parse, opt(CommonParser::as_alias)),
|(ari, opt_alias)| ArithmeticExpression {
ari,
alias: opt_alias.map(String::from),
Expand All @@ -222,42 +257,6 @@ impl ArithmeticExpression {
}
}

impl fmt::Display for ArithmeticOperator {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ArithmeticOperator::Add => write!(f, "+"),
ArithmeticOperator::Subtract => write!(f, "-"),
ArithmeticOperator::Multiply => write!(f, "*"),
ArithmeticOperator::Divide => write!(f, "/"),
}
}
}

impl fmt::Display for ArithmeticBase {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ArithmeticBase::Column(ref col) => write!(f, "{}", col),
ArithmeticBase::Scalar(ref lit) => write!(f, "{}", lit.to_string()),
ArithmeticBase::Bracketed(ref ari) => write!(f, "({})", ari),
}
}
}

impl fmt::Display for ArithmeticItem {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ArithmeticItem::Base(ref b) => write!(f, "{}", b),
ArithmeticItem::Expr(ref expr) => write!(f, "{}", expr),
}
}
}

impl fmt::Display for Arithmetic {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{} {} {}", self.left, self.op, self.right)
}
}

impl fmt::Display for ArithmeticExpression {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.alias {
Expand All @@ -269,9 +268,9 @@ impl fmt::Display for ArithmeticExpression {

#[cfg(test)]
mod tests {
use base::arithmetic::ArithmeticBase::Scalar;
use base::arithmetic::ArithmeticOperator::{Add, Divide, Multiply, Subtract};
use base::column::{Column, FunctionArgument, FunctionExpression};
use common::arithmetic::ArithmeticBase::Scalar;
use common::arithmetic::ArithmeticOperator::{Add, Divide, Multiply, Subtract};

use super::*;

Expand Down Expand Up @@ -488,9 +487,5 @@ mod tests {
let qs = "56";
let res = Arithmetic::parse(qs);
assert!(res.is_err());
// assert_eq!(
// nom::Err::Error(nom::error::Error::new(qs, ErrorKind::Tag)),
// res.err().unwrap()
// );
}
}
39 changes: 19 additions & 20 deletions src/common/case.rs → src/base/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use nom::sequence::{delimited, terminated, tuple};
use nom::IResult;

use base::column::Column;
use base::condition::ConditionExpression;
use base::error::ParseSQLError;
use base::Literal;
use common::condition::ConditionExpression;

#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct CaseWhenExpression {
Expand All @@ -20,31 +20,30 @@ pub struct CaseWhenExpression {

impl CaseWhenExpression {
pub fn parse(i: &str) -> IResult<&str, CaseWhenExpression, ParseSQLError<&str>> {
let (remaining_input, (_, _, _, _, condition, _, _, _, column, _, else_val, _)) =
tuple((
tag_no_case("CASE"),
multispace1,
tag_no_case("WHEN"),
let (input, (_, _, _, _, condition, _, _, _, column, _, else_val, _)) = tuple((
tag_no_case("CASE"),
multispace1,
tag_no_case("WHEN"),
multispace0,
ConditionExpression::condition_expr,
multispace0,
tag_no_case("THEN"),
multispace0,
Column::without_alias,
multispace0,
opt(delimited(
terminated(tag_no_case("ELSE"), multispace0),
Literal::parse,
multispace0,
ConditionExpression::condition_expr,
multispace0,
tag_no_case("THEN"),
multispace0,
Column::without_alias,
multispace0,
opt(delimited(
terminated(tag_no_case("ELSE"), multispace0),
Literal::parse,
multispace0,
)),
tag_no_case("END"),
))(i)?;
)),
tag_no_case("END"),
))(i)?;

let then_expr = ColumnOrLiteral::Column(column);
let else_expr = else_val.map(ColumnOrLiteral::Literal);

Ok((
remaining_input,
input,
CaseWhenExpression {
condition,
then_expr,
Expand Down
7 changes: 7 additions & 0 deletions src/base/check_constraint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// `[CONSTRAINT [symbol]] CHECK (expr) [[NOT] ENFORCED]`
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct CheckConstraintDefinition {
pub symbol: Option<String>,
pub expr: String,
pub enforced: bool,
}
Loading

0 comments on commit 146f3e7

Please sign in to comment.