Skip to content

Commit 391a54b

Browse files
authored
Merge pull request #131 from nickolay/pr/refactoring
Assorted code simplification and doc improvements
2 parents 275e6b1 + 086ba12 commit 391a54b

File tree

5 files changed

+211
-291
lines changed

5 files changed

+211
-291
lines changed

src/ast/mod.rs

+50-59
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ where
7171
/// Identifier name, in the originally quoted form (e.g. `"id"`)
7272
pub type Ident = String;
7373

74+
/// A name of a table, view, custom type, etc., possibly multi-part, i.e. db.schema.obj
75+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
76+
pub struct ObjectName(pub Vec<Ident>);
77+
78+
impl fmt::Display for ObjectName {
79+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80+
write!(f, "{}", display_separated(&self.0, "."))
81+
}
82+
}
83+
7484
/// An SQL expression of any type.
7585
///
7686
/// The parser does not distinguish between expressions of different types
@@ -165,12 +175,9 @@ pub enum Expr {
165175
impl fmt::Display for Expr {
166176
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
167177
match self {
168-
Expr::Identifier(s) => write!(f, "{}", s),
178+
Expr::Identifier(s) => f.write_str(s),
169179
Expr::Wildcard => f.write_str("*"),
170-
Expr::QualifiedWildcard(q) => {
171-
write!(f, "{}", display_separated(q, "."))?;
172-
f.write_str(".*")
173-
}
180+
Expr::QualifiedWildcard(q) => write!(f, "{}.*", display_separated(q, ".")),
174181
Expr::CompoundIdentifier(s) => write!(f, "{}", display_separated(s, ".")),
175182
Expr::IsNull(ast) => write!(f, "{} IS NULL", ast),
176183
Expr::IsNotNull(ast) => write!(f, "{} IS NOT NULL", ast),
@@ -285,11 +292,16 @@ impl fmt::Display for WindowSpec {
285292

286293
/// Specifies the data processed by a window function, e.g.
287294
/// `RANGE UNBOUNDED PRECEDING` or `ROWS BETWEEN 5 PRECEDING AND CURRENT ROW`.
295+
///
296+
/// Note: The parser does not validate the specified bounds; the caller should
297+
/// reject invalid bounds like `ROWS UNBOUNDED FOLLOWING` before execution.
288298
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
289299
pub struct WindowFrame {
290300
pub units: WindowFrameUnits,
291301
pub start_bound: WindowFrameBound,
292-
/// The right bound of the `BETWEEN .. AND` clause.
302+
/// The right bound of the `BETWEEN .. AND` clause. The end bound of `None`
303+
/// indicates the shorthand form (e.g. `ROWS 1 PRECEDING`), which must
304+
/// behave the same as `end_bound = WindowFrameBound::CurrentRow`.
293305
pub end_bound: Option<WindowFrameBound>,
294306
// TBD: EXCLUDE
295307
}
@@ -327,14 +339,14 @@ impl FromStr for WindowFrameUnits {
327339
}
328340
}
329341

342+
/// Specifies [WindowFrame]'s `start_bound` and `end_bound`
330343
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
331344
pub enum WindowFrameBound {
332345
/// `CURRENT ROW`
333346
CurrentRow,
334347
/// `<N> PRECEDING` or `UNBOUNDED PRECEDING`
335348
Preceding(Option<u64>),
336-
/// `<N> FOLLOWING` or `UNBOUNDED FOLLOWING`. This can only appear in
337-
/// [WindowFrame::end_bound].
349+
/// `<N> FOLLOWING` or `UNBOUNDED FOLLOWING`.
338350
Following(Option<u64>),
339351
}
340352

@@ -416,11 +428,16 @@ pub enum Statement {
416428
name: ObjectName,
417429
operation: AlterTableOperation,
418430
},
419-
/// DROP TABLE
431+
/// DROP
420432
Drop {
433+
/// The type of the object to drop: TABLE, VIEW, etc.
421434
object_type: ObjectType,
435+
/// An optional `IF EXISTS` clause. (Non-standard.)
422436
if_exists: bool,
437+
/// One or more objects to drop. (ANSI SQL requires exactly one.)
423438
names: Vec<ObjectName>,
439+
/// Whether `CASCADE` was specified. This will be `false` when
440+
/// `RESTRICT` or no drop behavior at all was specified.
424441
cascade: bool,
425442
},
426443
/// `{ BEGIN [ TRANSACTION | WORK ] | START TRANSACTION } ...`
@@ -596,16 +613,6 @@ impl fmt::Display for Statement {
596613
}
597614
}
598615

599-
/// A name of a table, view, custom type, etc., possibly multi-part, i.e. db.schema.obj
600-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
601-
pub struct ObjectName(pub Vec<Ident>);
602-
603-
impl fmt::Display for ObjectName {
604-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
605-
write!(f, "{}", display_separated(&self.0, "."))
606-
}
607-
}
608-
609616
/// SQL assignment `foo = expr` as used in SQLUpdate
610617
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
611618
pub struct Assignment {
@@ -660,19 +667,15 @@ pub enum FileFormat {
660667
impl fmt::Display for FileFormat {
661668
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
662669
use self::FileFormat::*;
663-
write!(
664-
f,
665-
"{}",
666-
match self {
667-
TEXTFILE => "TEXTFILE",
668-
SEQUENCEFILE => "SEQUENCEFILE",
669-
ORC => "ORC",
670-
PARQUET => "PARQUET",
671-
AVRO => "AVRO",
672-
RCFILE => "RCFILE",
673-
JSONFILE => "TEXTFILE",
674-
}
675-
)
670+
f.write_str(match self {
671+
TEXTFILE => "TEXTFILE",
672+
SEQUENCEFILE => "SEQUENCEFILE",
673+
ORC => "ORC",
674+
PARQUET => "PARQUET",
675+
AVRO => "AVRO",
676+
RCFILE => "RCFILE",
677+
JSONFILE => "TEXTFILE",
678+
})
676679
}
677680
}
678681

@@ -707,14 +710,10 @@ pub enum ObjectType {
707710

708711
impl fmt::Display for ObjectType {
709712
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
710-
write!(
711-
f,
712-
"{}",
713-
match self {
714-
ObjectType::Table => "TABLE",
715-
ObjectType::View => "VIEW",
716-
}
717-
)
713+
f.write_str(match self {
714+
ObjectType::Table => "TABLE",
715+
ObjectType::View => "VIEW",
716+
})
718717
}
719718
}
720719

@@ -740,7 +739,7 @@ impl fmt::Display for TransactionMode {
740739
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
741740
use TransactionMode::*;
742741
match self {
743-
AccessMode(access_mode) => write!(f, "{}", access_mode.to_string()),
742+
AccessMode(access_mode) => write!(f, "{}", access_mode),
744743
IsolationLevel(iso_level) => write!(f, "ISOLATION LEVEL {}", iso_level),
745744
}
746745
}
@@ -755,14 +754,10 @@ pub enum TransactionAccessMode {
755754
impl fmt::Display for TransactionAccessMode {
756755
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
757756
use TransactionAccessMode::*;
758-
write!(
759-
f,
760-
"{}",
761-
match self {
762-
ReadOnly => "READ ONLY",
763-
ReadWrite => "READ WRITE",
764-
}
765-
)
757+
f.write_str(match self {
758+
ReadOnly => "READ ONLY",
759+
ReadWrite => "READ WRITE",
760+
})
766761
}
767762
}
768763

@@ -777,15 +772,11 @@ pub enum TransactionIsolationLevel {
777772
impl fmt::Display for TransactionIsolationLevel {
778773
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
779774
use TransactionIsolationLevel::*;
780-
write!(
781-
f,
782-
"{}",
783-
match self {
784-
ReadUncommitted => "READ UNCOMMITTED",
785-
ReadCommitted => "READ COMMITTED",
786-
RepeatableRead => "REPEATABLE READ",
787-
Serializable => "SERIALIZABLE",
788-
}
789-
)
775+
f.write_str(match self {
776+
ReadUncommitted => "READ UNCOMMITTED",
777+
ReadCommitted => "READ COMMITTED",
778+
RepeatableRead => "REPEATABLE READ",
779+
Serializable => "SERIALIZABLE",
780+
})
790781
}
791782
}

src/ast/operator.rs

+22-30
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ pub enum UnaryOperator {
2222

2323
impl fmt::Display for UnaryOperator {
2424
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25-
write!(
26-
f,
27-
"{}",
28-
match self {
29-
UnaryOperator::Plus => "+",
30-
UnaryOperator::Minus => "-",
31-
UnaryOperator::Not => "NOT",
32-
}
33-
)
25+
f.write_str(match self {
26+
UnaryOperator::Plus => "+",
27+
UnaryOperator::Minus => "-",
28+
UnaryOperator::Not => "NOT",
29+
})
3430
}
3531
}
3632

@@ -56,26 +52,22 @@ pub enum BinaryOperator {
5652

5753
impl fmt::Display for BinaryOperator {
5854
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59-
write!(
60-
f,
61-
"{}",
62-
match self {
63-
BinaryOperator::Plus => "+",
64-
BinaryOperator::Minus => "-",
65-
BinaryOperator::Multiply => "*",
66-
BinaryOperator::Divide => "/",
67-
BinaryOperator::Modulus => "%",
68-
BinaryOperator::Gt => ">",
69-
BinaryOperator::Lt => "<",
70-
BinaryOperator::GtEq => ">=",
71-
BinaryOperator::LtEq => "<=",
72-
BinaryOperator::Eq => "=",
73-
BinaryOperator::NotEq => "<>",
74-
BinaryOperator::And => "AND",
75-
BinaryOperator::Or => "OR",
76-
BinaryOperator::Like => "LIKE",
77-
BinaryOperator::NotLike => "NOT LIKE",
78-
}
79-
)
55+
f.write_str(match self {
56+
BinaryOperator::Plus => "+",
57+
BinaryOperator::Minus => "-",
58+
BinaryOperator::Multiply => "*",
59+
BinaryOperator::Divide => "/",
60+
BinaryOperator::Modulus => "%",
61+
BinaryOperator::Gt => ">",
62+
BinaryOperator::Lt => "<",
63+
BinaryOperator::GtEq => ">=",
64+
BinaryOperator::LtEq => "<=",
65+
BinaryOperator::Eq => "=",
66+
BinaryOperator::NotEq => "<>",
67+
BinaryOperator::And => "AND",
68+
BinaryOperator::Or => "OR",
69+
BinaryOperator::Like => "LIKE",
70+
BinaryOperator::NotLike => "NOT LIKE",
71+
})
8072
}
8173
}

src/ast/query.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,11 @@ pub enum SetOperator {
100100

101101
impl fmt::Display for SetOperator {
102102
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
103-
write!(
104-
f,
105-
"{}",
106-
match self {
107-
SetOperator::Union => "UNION",
108-
SetOperator::Except => "EXCEPT",
109-
SetOperator::Intersect => "INTERSECT",
110-
}
111-
)
103+
f.write_str(match self {
104+
SetOperator::Union => "UNION",
105+
SetOperator::Except => "EXCEPT",
106+
SetOperator::Intersect => "INTERSECT",
107+
})
112108
}
113109
}
114110

src/ast/value.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,14 @@ pub enum DateTimeField {
128128

129129
impl fmt::Display for DateTimeField {
130130
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131-
write!(
132-
f,
133-
"{}",
134-
match self {
135-
DateTimeField::Year => "YEAR",
136-
DateTimeField::Month => "MONTH",
137-
DateTimeField::Day => "DAY",
138-
DateTimeField::Hour => "HOUR",
139-
DateTimeField::Minute => "MINUTE",
140-
DateTimeField::Second => "SECOND",
141-
}
142-
)
131+
f.write_str(match self {
132+
DateTimeField::Year => "YEAR",
133+
DateTimeField::Month => "MONTH",
134+
DateTimeField::Day => "DAY",
135+
DateTimeField::Hour => "HOUR",
136+
DateTimeField::Minute => "MINUTE",
137+
DateTimeField::Second => "SECOND",
138+
})
143139
}
144140
}
145141

0 commit comments

Comments
 (0)