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

Add config options for Serializer and Formater #74

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
94 changes: 79 additions & 15 deletions src/core/src/stmt/sql/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub trait Params {
/// Serialize a statement to a SQL string
pub struct Serializer<'a> {
schema: &'a db::Schema,
quoted_table_names: bool,
quoted_column_names: bool,
}

struct Formatter<'a, T> {
Expand All @@ -27,6 +29,12 @@ struct Formatter<'a, T> {

/// Query paramaters (referenced by placeholders) are stored here.
params: &'a mut T,

/// Config option for quoting table names
quoted_table_names: bool,

/// Config option for quoting column names
quoted_column_names: bool,
}

impl Params for Vec<stmt::Value> {
Expand Down Expand Up @@ -88,7 +96,19 @@ impl AsRef<str> for SerializeResult {

impl<'a> Serializer<'a> {
pub fn new(schema: &'a db::Schema) -> Serializer<'a> {
Serializer { schema }
Serializer {
schema,
quoted_table_names: true,
quoted_column_names: true,
}
}
pub fn with_quoted_table_names(mut self, enabled: bool) -> Self {
self.quoted_table_names = enabled;
self
}
pub fn with_quoted_column_names(mut self, enabled: bool) -> Self {
self.quoted_column_names = enabled;
self
}

pub fn serialize_stmt(
Expand All @@ -102,6 +122,8 @@ impl<'a> Serializer<'a> {
dst: &mut ret,
schema: self.schema,
params,
quoted_table_names: self.quoted_table_names,
quoted_column_names: self.quoted_column_names,
};

fmt.statement(stmt).unwrap();
Expand All @@ -119,6 +141,8 @@ impl<'a> Serializer<'a> {
dst: &mut ret,
schema: self.schema,
params,
quoted_table_names: self.quoted_table_names,
quoted_column_names: self.quoted_column_names,
};

fmt.sql_statement(stmt).unwrap();
Expand Down Expand Up @@ -167,7 +191,10 @@ impl<T: Params> Formatter<'_, T> {
write!(self.dst, " ON ")?;

let table = self.schema.table(stmt.on);
self.ident_str(&table.name)?;

if self.quoted_table_names {
self.ident_str(&table.name)?;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see anything else. Would no table name be included at all in the fallback case?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are completely right, i did not put the else case so that would be an error


write!(self.dst, " (")?;

Expand Down Expand Up @@ -228,7 +255,9 @@ impl<T: Params> Formatter<'_, T> {
}

fn column_def(&mut self, stmt: &ColumnDef) -> fmt::Result {
self.ident(&stmt.name)?;
if self.quoted_column_names {
self.ident(&stmt.name)?;
}
write!(self.dst, " ")?;
self.ty(&stmt.ty)?;
Ok(())
Expand All @@ -249,7 +278,12 @@ impl<T: Params> Formatter<'_, T> {

for table_with_join in delete.from.as_table_with_joins() {
let table = self.schema.table(table_with_join.table);
write!(self.dst, "\"{}\"", table.name)?;

if self.quoted_table_names {
write!(self.dst, "\"{}\"", table.name)?;
} else {
write!(self.dst, "{}", table.name)?;
}
}

write!(self.dst, " WHERE ")?;
Expand All @@ -264,15 +298,27 @@ impl<T: Params> Formatter<'_, T> {
todo!()
};

write!(
self.dst,
"INSERT INTO \"{}\" (",
self.schema.table(insert_target).name
)?;
if self.quoted_table_names {
write!(
self.dst,
"INSERT INTO \"{}\" (",
self.schema.table(insert_target).name
)?;
} else {
write!(
self.dst,
"INSERT INTO {} (",
self.schema.table(insert_target).name
)?;
}

let mut s = "";
for column_id in &insert_target.columns {
write!(self.dst, "{}\"{}\"", s, self.schema.column(*column_id).name)?;
if self.quoted_column_names {
write!(self.dst, "{}\"{}\"", s, self.schema.column(*column_id).name)?;
} else {
write!(self.dst, "{}{}", s, self.schema.column(*column_id).name)?;
}
s = ", ";
}

Expand All @@ -294,11 +340,20 @@ impl<T: Params> Formatter<'_, T> {
fn update(&mut self, update: &Update) -> fmt::Result {
let table = self.schema.table(update.target.as_table().table);

write!(self.dst, "UPDATE \"{}\" SET", table.name)?;
if self.quoted_table_names {
write!(self.dst, "UPDATE \"{}\" SET", table.name)?;
} else {
write!(self.dst, "UPDATE {} SET", table.name)?;
}

for (index, assignment) in update.assignments.iter() {
let column = &table.columns[index];
write!(self.dst, " \"{}\" = ", column.name)?;

if self.quoted_column_names {
write!(self.dst, " \"{}\" = ", column.name)?;
} else {
write!(self.dst, " {} = ", column.name)?;
}

self.expr(&assignment.expr)?;
}
Expand Down Expand Up @@ -336,7 +391,12 @@ impl<T: Params> Formatter<'_, T> {

for table_with_join in select.source.as_table_with_joins() {
let table = self.schema.table(table_with_join.table);
write!(self.dst, "\"{}\"", table.name)?;

if self.quoted_table_names {
write!(self.dst, "\"{}\"", table.name)?;
} else {
write!(self.dst, "{}", table.name)?;
}
}

write!(self.dst, " WHERE ")?;
Expand Down Expand Up @@ -405,7 +465,9 @@ impl<T: Params> Formatter<'_, T> {
// TODO: at some point we need to conditionally scope the column
// name.
let column = self.schema.column(expr.column);
self.ident_str(&column.name)?;
if self.quoted_column_names {
self.ident_str(&column.name)?;
}
}
Expr::InList(ExprInList { expr, list }) => {
self.expr(expr)?;
Expand Down Expand Up @@ -529,7 +591,9 @@ impl<T: Params> Formatter<'_, T> {
fn name(&mut self, name: &Name) -> fmt::Result {
let mut s = "";
for ident in &name.0 {
self.ident(ident)?;
if self.quoted_table_names {
self.ident(ident)?;
}
write!(self.dst, "{s}")?;
s = ".";
}
Expand Down