Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: impl Display for Statement #3744

Merged
merged 22 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/sql/src/statements/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use sqlparser::ast::ObjectName;
use sqlparser_derive::{Visit, VisitMut};

use crate::statements::OptionMap;
use crate::util::redact_sql_secrets;

macro_rules! format_sorted_hashmap {
($hashmap:expr) => {{
Expand All @@ -27,6 +28,7 @@ macro_rules! format_sorted_hashmap {
let mut result = String::new();
for key in sorted_keys {
if let Some(val) = hashmap.get(key) {
redact_sql_secrets(val);
tisonkun marked this conversation as resolved.
Show resolved Hide resolved
result.push_str(&format!("{} = {}, ", key, val));
}
}
Expand Down
18 changes: 7 additions & 11 deletions src/sql/src/statements/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use sqlparser_derive::{Visit, VisitMut};

use crate::ast::{ColumnDef, Ident, ObjectName, SqlOption, TableConstraint, Value as SqlValue};
use crate::statements::OptionMap;
use crate::util::redact_sql_secrets;

const LINE_SEP: &str = ",\n";
const COMMA_SEP: &str = ", ";
Expand Down Expand Up @@ -55,6 +56,7 @@ macro_rules! format_sorted_hashmap {
let mut result = String::new();
for key in sorted_keys {
if let Some(val) = hashmap.get(key) {
redact_sql_secrets(val);
result.push_str(&format!("{} = {}, ", key, val));
}
}
Expand Down Expand Up @@ -233,22 +235,16 @@ impl CreateDatabase {
pub fn name(&self) -> &ObjectName {
&self.name
}

#[inline]
fn format_if_not_exists(&self) -> &str {
if self.if_not_exists {
"IF NOT EXISTS"
} else {
""
}
}
}

impl Display for CreateDatabase {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let if_not_exists = self.format_if_not_exists();
f.write_str("CREATE DATABASE")?;
if self.if_not_exists {
f.write_str(" IF NOT EXISTS")?;
}
let name = self.name();
write!(f, r#"CREATE DATABASE {if_not_exists} {name}"#)
write!(f, r#" {name}"#)
}
}

Expand Down
32 changes: 10 additions & 22 deletions src/sql/src/statements/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,16 @@ impl DropTable {
pub fn drop_if_exists(&self) -> bool {
self.drop_if_exists
}

#[inline]
fn format_if_exists(&self) -> &str {
if self.drop_if_exists {
"IF EXISTS"
} else {
""
}
}
}

impl Display for DropTable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let if_exists = self.format_if_exists();
f.write_str("DROP TABLE")?;
if self.drop_if_exists() {
f.write_str(" IF EXISTS")?;
}
let table_name = self.table_name();
write!(f, r#"DROP TABLE {if_exists} {table_name}"#)
write!(f, r#" {table_name}"#)
}
}

Expand Down Expand Up @@ -84,21 +78,15 @@ impl DropDatabase {
pub fn drop_if_exists(&self) -> bool {
self.drop_if_exists
}

#[inline]
fn format_if_exists(&self) -> &str {
if self.drop_if_exists {
"IF EXISTS"
} else {
""
}
}
}

impl Display for DropDatabase {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let if_exists = self.format_if_exists();
f.write_str("DROP DATABASE")?;
if self.drop_if_exists() {
f.write_str(" IF EXISTS")?;
}
let name = self.name();
write!(f, r#"DROP DATABASE {if_exists} {name}"#)
write!(f, r#" {name}"#)
}
}
1 change: 1 addition & 0 deletions src/sql/src/statements/set_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl SetVariables {
}

pub fn format_value(&self) -> String {
// The number of value is always one.
self.value
.iter()
.map(|expr| format!("{}", expr))
Expand Down
32 changes: 10 additions & 22 deletions src/sql/src/statements/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,18 @@ impl ShowColumns {
None => String::default(),
}
}

#[inline]
fn format_full(&self) -> &str {
if self.full {
"FULL"
} else {
""
}
}
}

impl Display for ShowColumns {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("SHOW")?;
if self.full {
f.write_str(" FULL")?;
}
let kind = self.kind();
let table = self.format_table();
let database = self.format_database();
let full = self.format_full();
write!(f, r#"SHOW {full} {table} {database} {kind}"#)
write!(f, r#" {table} {database} {kind}"#)
}
}

Expand Down Expand Up @@ -169,23 +163,17 @@ impl ShowTables {
}
}
}

#[inline]
fn format_full(&self) -> &str {
if self.full {
"FULL"
} else {
""
}
}
}

impl Display for ShowTables {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let full = self.format_full();
f.write_str("SHOW")?;
if self.full {
f.write_str(" FULL")?;
}
let database = self.format_database();
let kind = self.kind();
write!(f, r#"SHOW {full} TABLES {database} {kind}"#)
write!(f, r#" {database} {kind}"#)
}
}

Expand Down