Skip to content

Commit 72a95c5

Browse files
committed
Code review comments
1 parent 9ce24ba commit 72a95c5

File tree

4 files changed

+319
-500
lines changed

4 files changed

+319
-500
lines changed

src/ast/mod.rs

+65-195
Original file line numberDiff line numberDiff line change
@@ -2776,33 +2776,23 @@ pub enum Statement {
27762776
ShowColumns {
27772777
extended: bool,
27782778
full: bool,
2779-
show_in: Option<ShowStatementIn>,
2780-
filter: Option<ShowStatementFilter>,
2781-
filter_position: ShowStatementFilterPosition,
2779+
show_options: ShowStatementOptions,
27822780
},
27832781
/// ```sql
27842782
/// SHOW DATABASES
27852783
/// ```
27862784
ShowDatabases {
27872785
terse: bool,
27882786
history: bool,
2789-
filter: Option<ShowStatementFilter>,
2790-
show_in: Option<ShowStatementIn>,
2791-
starts_with: Option<Value>,
2792-
limit: Option<Expr>,
2793-
from: Option<Value>,
2787+
show_options: ShowStatementOptions,
27942788
},
27952789
/// ```sql
27962790
/// SHOW SCHEMAS
27972791
/// ```
27982792
ShowSchemas {
27992793
terse: bool,
28002794
history: bool,
2801-
filter: Option<ShowStatementFilter>,
2802-
show_in: Option<ShowStatementIn>,
2803-
starts_with: Option<Value>,
2804-
limit: Option<Expr>,
2805-
from: Option<Value>,
2795+
show_options: ShowStatementOptions,
28062796
},
28072797
/// ```sql
28082798
/// SHOW TABLES
@@ -2813,25 +2803,15 @@ pub enum Statement {
28132803
extended: bool,
28142804
full: bool,
28152805
external: bool,
2816-
filter: Option<ShowStatementFilter>,
2817-
show_in: Option<ShowStatementIn>,
2818-
starts_with: Option<Value>,
2819-
limit: Option<Expr>,
2820-
from: Option<Value>,
2821-
filter_position: ShowStatementFilterPosition,
2806+
show_options: ShowStatementOptions,
28222807
},
28232808
/// ```sql
28242809
/// SHOW VIEWS
28252810
/// ```
28262811
ShowViews {
28272812
terse: bool,
28282813
materialized: bool,
2829-
filter: Option<ShowStatementFilter>,
2830-
show_in: Option<ShowStatementIn>,
2831-
starts_with: Option<Value>,
2832-
limit: Option<Expr>,
2833-
from: Option<Value>,
2834-
filter_position: ShowStatementFilterPosition,
2814+
show_options: ShowStatementOptions,
28352815
},
28362816
/// ```sql
28372817
/// SHOW COLLATION
@@ -4411,113 +4391,39 @@ impl fmt::Display for Statement {
44114391
Statement::ShowColumns {
44124392
extended,
44134393
full,
4414-
show_in,
4415-
filter,
4416-
filter_position,
4394+
show_options,
44174395
} => {
44184396
write!(
44194397
f,
4420-
"SHOW {extended}{full}COLUMNS",
4398+
"SHOW {extended}{full}COLUMNS{show_options}",
44214399
extended = if *extended { "EXTENDED " } else { "" },
44224400
full = if *full { "FULL " } else { "" },
44234401
)?;
4424-
if filter_position == &ShowStatementFilterPosition::InTheMiddle {
4425-
if let Some(filter) = filter {
4426-
write!(f, " {filter}")?;
4427-
}
4428-
if let Some(show_in) = show_in {
4429-
write!(f, " {show_in}")?;
4430-
}
4431-
}
4432-
if filter_position == &ShowStatementFilterPosition::AtTheEnd {
4433-
if let Some(show_in) = show_in {
4434-
write!(f, " {show_in}")?;
4435-
}
4436-
if let Some(filter) = filter {
4437-
write!(f, " {filter}")?;
4438-
}
4439-
}
44404402
Ok(())
44414403
}
44424404
Statement::ShowDatabases {
44434405
terse,
44444406
history,
4445-
filter,
4446-
show_in,
4447-
starts_with,
4448-
limit,
4449-
from,
4407+
show_options,
44504408
} => {
44514409
write!(
44524410
f,
4453-
"SHOW {terse}DATABASES",
4454-
terse = if *terse { "TERSE " } else { "" }
4455-
)?;
4456-
write!(
4457-
f,
4458-
"{history}{filter}{show_in}{starts_with}{limit}{from}",
4411+
"SHOW {terse}DATABASES{history}{show_options}",
4412+
terse = if *terse { "TERSE " } else { "" },
44594413
history = if *history { " HISTORY" } else { "" },
4460-
filter = match filter.as_ref() {
4461-
Some(l) => format!(" {l}"),
4462-
None => String::new(),
4463-
},
4464-
show_in = match show_in {
4465-
Some(i) => format!(" {i}"),
4466-
None => String::new(),
4467-
},
4468-
starts_with = match starts_with.as_ref() {
4469-
Some(s) => format!(" STARTS WITH {s}"),
4470-
None => String::new(),
4471-
},
4472-
limit = match limit.as_ref() {
4473-
Some(l) => format!(" LIMIT {l}"),
4474-
None => String::new(),
4475-
},
4476-
from = match from.as_ref() {
4477-
Some(f) => format!(" FROM {f}"),
4478-
None => String::new(),
4479-
}
44804414
)?;
44814415
Ok(())
44824416
}
44834417
Statement::ShowSchemas {
44844418
terse,
44854419
history,
4486-
filter,
4487-
show_in,
4488-
starts_with,
4489-
limit,
4490-
from,
4420+
show_options,
44914421
} => {
44924422
write!(
44934423
f,
4494-
"SHOW {terse}SCHEMAS",
4495-
terse = if *terse { "TERSE " } else { "" }
4496-
)?;
4497-
write!(
4498-
f,
4499-
"{history}{filter}{show_in}{starts_with}{limit}{from}",
4424+
"SHOW {terse}SCHEMAS{history}{show_options}",
4425+
terse = if *terse { "TERSE " } else { "" },
45004426
history = if *history { " HISTORY" } else { "" },
4501-
filter = match filter.as_ref() {
4502-
Some(l) => format!(" {l}"),
4503-
None => String::new(),
4504-
},
4505-
show_in = match show_in {
4506-
Some(i) => format!(" {i}"),
4507-
None => String::new(),
4508-
},
4509-
starts_with = match starts_with.as_ref() {
4510-
Some(s) => format!(" STARTS WITH {s}"),
4511-
None => String::new(),
4512-
},
4513-
limit = match limit.as_ref() {
4514-
Some(l) => format!(" LIMIT {l}"),
4515-
None => String::new(),
4516-
},
4517-
from = match from.as_ref() {
4518-
Some(f) => format!(" FROM {f}"),
4519-
None => String::new(),
4520-
}
45214427
)?;
45224428
Ok(())
45234429
}
@@ -4527,110 +4433,30 @@ impl fmt::Display for Statement {
45274433
extended,
45284434
full,
45294435
external,
4530-
filter,
4531-
show_in,
4532-
starts_with,
4533-
limit,
4534-
from,
4535-
filter_position,
4436+
show_options,
45364437
} => {
45374438
write!(
45384439
f,
4539-
"SHOW {terse}{extended}{full}{external}TABLES",
4440+
"SHOW {terse}{extended}{full}{external}TABLES{history}{show_options}",
45404441
terse = if *terse { "TERSE " } else { "" },
45414442
extended = if *extended { "EXTENDED " } else { "" },
45424443
full = if *full { "FULL " } else { "" },
45434444
external = if *external { "EXTERNAL " } else { "" },
4544-
)?;
4545-
write!(
4546-
f,
4547-
"{history}{like_in_the_middle}{show_in}{starts_with}{limit}{from}{like_at_the_end}",
45484445
history = if *history { " HISTORY" } else { "" },
4549-
like_in_the_middle = if *filter_position
4550-
== ShowStatementFilterPosition::InTheMiddle
4551-
&& filter.is_some()
4552-
{
4553-
format!(" {}", filter.as_ref().unwrap())
4554-
} else {
4555-
String::new()
4556-
},
4557-
show_in = match show_in {
4558-
Some(i) => format!(" {i}"),
4559-
None => String::new(),
4560-
},
4561-
starts_with = match starts_with.as_ref() {
4562-
Some(s) => format!(" STARTS WITH {s}"),
4563-
None => String::new(),
4564-
},
4565-
limit = match limit.as_ref() {
4566-
Some(l) => format!(" LIMIT {l}"),
4567-
None => String::new(),
4568-
},
4569-
from = match from.as_ref() {
4570-
Some(f) => format!(" FROM {f}"),
4571-
None => String::new(),
4572-
},
4573-
like_at_the_end = if *filter_position
4574-
== ShowStatementFilterPosition::AtTheEnd
4575-
&& filter.is_some()
4576-
{
4577-
format!(" {}", filter.as_ref().unwrap())
4578-
} else {
4579-
String::new()
4580-
},
4581-
)
4446+
)?;
4447+
Ok(())
45824448
}
45834449
Statement::ShowViews {
45844450
terse,
45854451
materialized,
4586-
filter,
4587-
show_in,
4588-
starts_with,
4589-
limit,
4590-
from,
4591-
filter_position,
4452+
show_options,
45924453
} => {
45934454
write!(
45944455
f,
4595-
"SHOW {terse}{materialized}VIEWS",
4456+
"SHOW {terse}{materialized}VIEWS{show_options}",
45964457
terse = if *terse { "TERSE " } else { "" },
45974458
materialized = if *materialized { "MATERIALIZED " } else { "" }
45984459
)?;
4599-
write!(
4600-
f,
4601-
"{like_in_the_middle}{show_in}{starts_with}{limit}{from}{like_at_the_end}",
4602-
like_in_the_middle = if *filter_position
4603-
== ShowStatementFilterPosition::InTheMiddle
4604-
&& filter.is_some()
4605-
{
4606-
format!(" {}", filter.as_ref().unwrap())
4607-
} else {
4608-
String::new()
4609-
},
4610-
show_in = match show_in {
4611-
Some(i) => format!(" {i}"),
4612-
None => String::new(),
4613-
},
4614-
starts_with = match starts_with.as_ref() {
4615-
Some(s) => format!(" STARTS WITH {s}"),
4616-
None => String::new(),
4617-
},
4618-
limit = match limit.as_ref() {
4619-
Some(l) => format!(" LIMIT {l}"),
4620-
None => String::new(),
4621-
},
4622-
from = match from.as_ref() {
4623-
Some(f) => format!(" FROM {f}"),
4624-
None => String::new(),
4625-
},
4626-
like_at_the_end = if *filter_position == ShowStatementFilterPosition::AtTheEnd
4627-
&& filter.is_some()
4628-
{
4629-
format!(" {}", filter.as_ref().unwrap())
4630-
} else {
4631-
String::new()
4632-
}
4633-
)?;
46344460
Ok(())
46354461
}
46364462
Statement::ShowFunctions { filter } => {
@@ -7528,10 +7354,54 @@ impl Display for UtilityOption {
75287354
}
75297355
}
75307356

7357+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7358+
pub struct ShowStatementOptions {
7359+
pub show_in: Option<ShowStatementIn>,
7360+
pub starts_with: Option<Value>,
7361+
pub limit: Option<Expr>,
7362+
pub limit_from: Option<Value>,
7363+
pub filter_position: Option<ShowStatementFilterPosition>,
7364+
}
7365+
7366+
impl Display for ShowStatementOptions {
7367+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7368+
let (life_in_infix, like_in_suffix) = match &self.filter_position {
7369+
Some(ShowStatementFilterPosition::Infix(filter)) => {
7370+
(format!(" {filter}"), "".to_string())
7371+
}
7372+
Some(ShowStatementFilterPosition::Suffix(filter)) => {
7373+
("".to_string(), format!(" {filter}"))
7374+
}
7375+
None => ("".to_string(), "".to_string()),
7376+
};
7377+
write!(
7378+
f,
7379+
"{life_in_infix}{show_in}{starts_with}{limit}{from}{like_in_suffix}",
7380+
show_in = match &self.show_in {
7381+
Some(i) => format!(" {i}"),
7382+
None => String::new(),
7383+
},
7384+
starts_with = match &self.starts_with {
7385+
Some(s) => format!(" STARTS WITH {s}"),
7386+
None => String::new(),
7387+
},
7388+
limit = match &self.limit {
7389+
Some(l) => format!(" LIMIT {l}"),
7390+
None => String::new(),
7391+
},
7392+
from = match &self.limit_from {
7393+
Some(f) => format!(" FROM {f}"),
7394+
None => String::new(),
7395+
}
7396+
)?;
7397+
Ok(())
7398+
}
7399+
}
7400+
75317401
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
75327402
pub enum ShowStatementFilterPosition {
7533-
InTheMiddle, // Snowflake like
7534-
AtTheEnd, // MySQL like
7403+
Infix(ShowStatementFilter), // For example: SHOW COLUMNS LIKE '%name%' IN TABLE tbl
7404+
Suffix(ShowStatementFilter), // For example: SHOW COLUMNS IN tbl LIKE '%name%'
75357405
}
75367406

75377407
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]

0 commit comments

Comments
 (0)