Skip to content

Commit 45a5f84

Browse files
committed
Move SQLOrderByExpr and Join* to query.rs (2/5)
1 parent d8173d4 commit 45a5f84

File tree

2 files changed

+93
-93
lines changed

2 files changed

+93
-93
lines changed

src/sqlast/mod.rs

Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod sqltype;
2020
mod table_key;
2121
mod value;
2222

23-
pub use self::query::SQLSelect;
23+
pub use self::query::{Join, JoinConstraint, JoinOperator, SQLOrderByExpr, SQLSelect};
2424
pub use self::sqltype::SQLType;
2525
pub use self::table_key::{AlterOperation, Key, TableKey};
2626
pub use self::value::Value;
@@ -308,29 +308,6 @@ impl ToString for SQLAssignment {
308308
}
309309
}
310310

311-
/// SQL ORDER BY expression
312-
#[derive(Debug, Clone, PartialEq)]
313-
pub struct SQLOrderByExpr {
314-
pub expr: Box<ASTNode>,
315-
pub asc: Option<bool>,
316-
}
317-
318-
impl SQLOrderByExpr {
319-
pub fn new(expr: Box<ASTNode>, asc: Option<bool>) -> Self {
320-
SQLOrderByExpr { expr, asc }
321-
}
322-
}
323-
324-
impl ToString for SQLOrderByExpr {
325-
fn to_string(&self) -> String {
326-
match self.asc {
327-
Some(true) => format!("{} ASC", self.expr.to_string()),
328-
Some(false) => format!("{} DESC", self.expr.to_string()),
329-
None => self.expr.to_string(),
330-
}
331-
}
332-
}
333-
334311
/// SQL column definition
335312
#[derive(Debug, Clone, PartialEq)]
336313
pub struct SQLColumnDef {
@@ -360,72 +337,3 @@ impl ToString for SQLColumnDef {
360337
s
361338
}
362339
}
363-
364-
#[derive(Debug, Clone, PartialEq)]
365-
pub struct Join {
366-
pub relation: ASTNode, // TableFactor
367-
pub join_operator: JoinOperator,
368-
}
369-
370-
impl ToString for Join {
371-
fn to_string(&self) -> String {
372-
fn prefix(constraint: &JoinConstraint) -> String {
373-
match constraint {
374-
JoinConstraint::Natural => "NATURAL ".to_string(),
375-
_ => "".to_string(),
376-
}
377-
}
378-
fn suffix(constraint: &JoinConstraint) -> String {
379-
match constraint {
380-
JoinConstraint::On(expr) => format!("ON {}", expr.to_string()),
381-
JoinConstraint::Using(attrs) => format!("USING({})", attrs.join(", ")),
382-
_ => "".to_string(),
383-
}
384-
}
385-
match &self.join_operator {
386-
JoinOperator::Inner(constraint) => format!(
387-
" {}JOIN {} {}",
388-
prefix(constraint),
389-
self.relation.to_string(),
390-
suffix(constraint)
391-
),
392-
JoinOperator::Cross => format!(" CROSS JOIN {}", self.relation.to_string()),
393-
JoinOperator::Implicit => format!(", {}", self.relation.to_string()),
394-
JoinOperator::LeftOuter(constraint) => format!(
395-
" {}LEFT JOIN {} {}",
396-
prefix(constraint),
397-
self.relation.to_string(),
398-
suffix(constraint)
399-
),
400-
JoinOperator::RightOuter(constraint) => format!(
401-
" {}RIGHT JOIN {} {}",
402-
prefix(constraint),
403-
self.relation.to_string(),
404-
suffix(constraint)
405-
),
406-
JoinOperator::FullOuter(constraint) => format!(
407-
" {}FULL JOIN {} {}",
408-
prefix(constraint),
409-
self.relation.to_string(),
410-
suffix(constraint)
411-
),
412-
}
413-
}
414-
}
415-
416-
#[derive(Debug, Clone, PartialEq)]
417-
pub enum JoinOperator {
418-
Inner(JoinConstraint),
419-
LeftOuter(JoinConstraint),
420-
RightOuter(JoinConstraint),
421-
FullOuter(JoinConstraint),
422-
Implicit,
423-
Cross,
424-
}
425-
426-
#[derive(Debug, Clone, PartialEq)]
427-
pub enum JoinConstraint {
428-
On(ASTNode),
429-
Using(Vec<String>),
430-
Natural,
431-
}

src/sqlast/query.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,95 @@ impl ToString for SQLSelect {
6868
s
6969
}
7070
}
71+
72+
#[derive(Debug, Clone, PartialEq)]
73+
pub struct Join {
74+
pub relation: ASTNode, // TableFactor
75+
pub join_operator: JoinOperator,
76+
}
77+
78+
impl ToString for Join {
79+
fn to_string(&self) -> String {
80+
fn prefix(constraint: &JoinConstraint) -> String {
81+
match constraint {
82+
JoinConstraint::Natural => "NATURAL ".to_string(),
83+
_ => "".to_string(),
84+
}
85+
}
86+
fn suffix(constraint: &JoinConstraint) -> String {
87+
match constraint {
88+
JoinConstraint::On(expr) => format!("ON {}", expr.to_string()),
89+
JoinConstraint::Using(attrs) => format!("USING({})", attrs.join(", ")),
90+
_ => "".to_string(),
91+
}
92+
}
93+
match &self.join_operator {
94+
JoinOperator::Inner(constraint) => format!(
95+
" {}JOIN {} {}",
96+
prefix(constraint),
97+
self.relation.to_string(),
98+
suffix(constraint)
99+
),
100+
JoinOperator::Cross => format!(" CROSS JOIN {}", self.relation.to_string()),
101+
JoinOperator::Implicit => format!(", {}", self.relation.to_string()),
102+
JoinOperator::LeftOuter(constraint) => format!(
103+
" {}LEFT JOIN {} {}",
104+
prefix(constraint),
105+
self.relation.to_string(),
106+
suffix(constraint)
107+
),
108+
JoinOperator::RightOuter(constraint) => format!(
109+
" {}RIGHT JOIN {} {}",
110+
prefix(constraint),
111+
self.relation.to_string(),
112+
suffix(constraint)
113+
),
114+
JoinOperator::FullOuter(constraint) => format!(
115+
" {}FULL JOIN {} {}",
116+
prefix(constraint),
117+
self.relation.to_string(),
118+
suffix(constraint)
119+
),
120+
}
121+
}
122+
}
123+
124+
#[derive(Debug, Clone, PartialEq)]
125+
pub enum JoinOperator {
126+
Inner(JoinConstraint),
127+
LeftOuter(JoinConstraint),
128+
RightOuter(JoinConstraint),
129+
FullOuter(JoinConstraint),
130+
Implicit,
131+
Cross,
132+
}
133+
134+
#[derive(Debug, Clone, PartialEq)]
135+
pub enum JoinConstraint {
136+
On(ASTNode),
137+
Using(Vec<String>),
138+
Natural,
139+
}
140+
141+
/// SQL ORDER BY expression
142+
#[derive(Debug, Clone, PartialEq)]
143+
pub struct SQLOrderByExpr {
144+
pub expr: Box<ASTNode>,
145+
pub asc: Option<bool>,
146+
}
147+
148+
impl SQLOrderByExpr {
149+
pub fn new(expr: Box<ASTNode>, asc: Option<bool>) -> Self {
150+
SQLOrderByExpr { expr, asc }
151+
}
152+
}
153+
154+
impl ToString for SQLOrderByExpr {
155+
fn to_string(&self) -> String {
156+
match self.asc {
157+
Some(true) => format!("{} ASC", self.expr.to_string()),
158+
Some(false) => format!("{} DESC", self.expr.to_string()),
159+
None => self.expr.to_string(),
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)