use crate::{
Action, AnyQuery, ColumnDef, Dataset, Expression, Fragment, Join, JoinType, PrimaryKeyType,
separated_by,
writer::{Context, SqlCoreWriter, SqlValueWriter},
};
pub trait SqlFragmentWriter: SqlValueWriter {
fn write_join_type(&self, _context: &mut Context, out: &mut AnyQuery, join_type: &JoinType) {
out.push_str(match &join_type {
JoinType::Default => "JOIN",
JoinType::Inner => "INNER JOIN",
JoinType::Outer => "OUTER JOIN",
JoinType::Left => "LEFT JOIN",
JoinType::Right => "RIGHT JOIN",
JoinType::Cross => "CROSS JOIN",
JoinType::Natural => "NATURAL JOIN",
});
}
fn write_join(
&self,
context: &mut Context,
out: &mut AnyQuery,
join: &Join<&dyn Dataset, &dyn Dataset, &dyn Expression>,
) {
let mut context = context.switch_fragment(Fragment::SqlJoin);
context.current.qualify_columns = true;
join.lhs
.write_table_name(self.as_dyn(), &mut context.current, out);
out.push(' ');
self.write_join_type(&mut context.current, out, &join.join);
out.push(' ');
join.rhs
.write_table_name(self.as_dyn(), &mut context.current, out);
if let Some(on) = &join.on {
out.push_str(" ON ");
on.write_query(self.as_dyn(), &mut context.current, out);
}
}
fn write_create_table_column_fragment(
&self,
context: &mut Context,
out: &mut AnyQuery,
column: &ColumnDef,
) {
self.write_identifier(context, out, &column.name(), true);
out.push(' ');
let len = out.len();
self.write_column_overridden_type(context, out, column, &column.column_type);
let didnt_write_type = out.len() == len;
if didnt_write_type {
SqlCoreWriter::write_column_type(self, context, out, &column.value);
}
if !column.nullable && column.primary_key == PrimaryKeyType::None {
out.push_str(" NOT NULL");
}
if column.default.is_set() {
out.push_str(" DEFAULT ");
column.default.write_query(self.as_dyn(), context, out);
}
if column.primary_key == PrimaryKeyType::PrimaryKey {
out.push_str(" PRIMARY KEY");
}
if column.unique && column.primary_key != PrimaryKeyType::PrimaryKey {
out.push_str(" UNIQUE");
}
if !column.comment.is_empty() {
self.write_column_comment_inline(context, out, column);
}
}
fn write_create_table_primary_key_fragment<'a, It>(
&self,
context: &mut Context,
out: &mut AnyQuery,
primary_key: It,
) where
Self: Sized,
It: IntoIterator<Item = &'a ColumnDef>,
It::IntoIter: Clone,
{
out.push_str(",\nPRIMARY KEY (");
separated_by(
out,
primary_key,
|out, col| {
self.write_identifier(
&mut context
.switch_fragment(Fragment::SqlCreateTablePrimaryKey)
.current,
out,
col.name(),
true,
);
},
", ",
);
out.push(')');
}
fn write_create_table_references_action(
&self,
_context: &mut Context,
out: &mut AnyQuery,
action: &Action,
) {
out.push_str(match action {
Action::NoAction => "NO ACTION",
Action::Restrict => "RESTRICT",
Action::Cascade => "CASCADE",
Action::SetNull => "SET NULL",
Action::SetDefault => "SET DEFAULT",
});
}
fn write_column_comment_inline(
&self,
_context: &mut Context,
_out: &mut AnyQuery,
_column: &ColumnDef,
) {
}
fn write_insert_update_fragment<'a>(
&self,
context: &mut Context,
out: &mut AnyQuery,
primary_key: impl Iterator<Item = &'a ColumnDef> + Clone,
columns: impl Iterator<Item = &'a ColumnDef> + Clone,
) where
Self: Sized,
{
let mut pk = primary_key.clone().peekable();
if pk.peek().is_none() {
return;
}
out.push_str("\nON CONFLICT");
context.fragment = Fragment::SqlInsertIntoOnConflict;
out.push_str(" (");
separated_by(
out,
pk,
|out, col| {
self.write_identifier(context, out, col.name(), true);
},
", ",
);
out.push(')');
let mut update_cols = columns
.filter(|c| !primary_key.clone().any(|pk| pk.name() == c.name()))
.peekable();
if update_cols.peek().is_some() {
out.push_str(" DO UPDATE SET\n");
separated_by(
out,
update_cols,
|out, col| {
self.write_identifier(context, out, col.name(), true);
out.push_str(" = EXCLUDED.");
self.write_identifier(context, out, col.name(), true);
},
",\n",
);
} else {
out.push_str(" DO NOTHING");
}
}
fn write_limit_offset(
&self,
_context: &mut Context,
out: &mut AnyQuery,
limit: Option<u32>,
offset: Option<u32>,
) {
if let Some(limit) = limit {
out.push_str("\nLIMIT ");
out.push_str(itoa::Buffer::new().format(limit));
}
if let Some(offset) = offset {
out.push_str("\nOFFSET ");
out.push_str(itoa::Buffer::new().format(offset));
}
}
}