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 simple extra param for table create statement #611

Merged
merged 10 commits into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions src/backend/table_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ pub trait TableBuilder:
write!(sql, " )").unwrap();

self.prepare_table_opt(create, sql);

if let Some(extra) = &create.extra {
write!(sql, " {extra}").unwrap();
}
}

/// Translate [`TableRef`] into SQL statement.
Expand Down
30 changes: 30 additions & 0 deletions src/table/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,13 +601,43 @@ impl ColumnDef {
}

/// Some extra options in custom string
/// ```
/// use sea_query::{tests_cfg::*, *};
/// let table = Table::create()
/// .table(Char::Table)
/// .col(
/// ColumnDef::new(Char::Id)
/// .uuid()
/// .extra("DEFAULT uuid_generate_v4()")
/// .primary_key()
/// .not_null(),
/// )
/// .col(
/// ColumnDef::new(Char::CreatedAt)
/// .timestamp_with_time_zone()
/// .extra("DEFAULT NOW()")
/// .not_null(),
/// )
/// .to_owned();
/// assert_eq!(
/// table.to_string(PostgresQueryBuilder),
/// [
/// r#"CREATE TABLE "character" ("#,
/// r#""id" uuid DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,"#,
/// r#""created_at" timestamp with time zone DEFAULT NOW() NOT NULL"#,
/// r#")"#,
/// ]
/// .join(" ")
/// );
/// ```
pub fn extra<T>(&mut self, string: T) -> &mut Self
where
T: Into<String>,
{
self.spec.push(ColumnSpec::Extra(string.into()));
self
}

pub fn get_column_name(&self) -> String {
self.name.to_string()
}
Expand Down
48 changes: 48 additions & 0 deletions src/table/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub struct TableCreateStatement {
pub(crate) foreign_keys: Vec<ForeignKeyCreateStatement>,
pub(crate) if_not_exists: bool,
pub(crate) check: Vec<SimpleExpr>,
pub(crate) extra: Option<String>,
}

/// All available table options
Expand Down Expand Up @@ -275,6 +276,52 @@ impl TableCreateStatement {
self.indexes.as_ref()
}

/// Rewriting extra param. You should take care self about concat extra params. Add extra after options.
/// Example for PostgresSQL [Citus](https://github.com/citusdata/citus) extension:
/// ```
/// use sea_query::{tests_cfg::*, *};
/// let table = Table::create()
/// .table(Char::Table)
/// .col(
/// ColumnDef::new(Char::Id)
/// .uuid()
/// .extra("DEFAULT uuid_generate_v4()")
/// .primary_key()
/// .not_null(),
/// )
/// .col(
/// ColumnDef::new(Char::CreatedAt)
/// .timestamp_with_time_zone()
/// .extra("DEFAULT NOW()")
/// .not_null(),
/// )
/// .col(ColumnDef::new(Char::UserData).json_binary().not_null())
/// .extra("USING columnar")
/// .to_owned();
/// assert_eq!(
/// table.to_string(PostgresQueryBuilder),
/// [
/// r#"CREATE TABLE "character" ("#,
/// r#""id" uuid DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,"#,
/// r#""created_at" timestamp with time zone DEFAULT NOW() NOT NULL,"#,
/// r#""user_data" jsonb NOT NULL"#,
/// r#") USING columnar"#,
/// ]
/// .join(" ")
/// );
/// ```
pub fn extra<T>(&mut self, extra: T) -> &mut Self
where
T: Into<String>,
{
self.extra = Some(extra.into());
self
}

pub fn get_extra(&self) -> Option<&String> {
self.extra.as_ref()
}

pub fn take(&mut self) -> Self {
Self {
table: self.table.take(),
Expand All @@ -285,6 +332,7 @@ impl TableCreateStatement {
foreign_keys: std::mem::take(&mut self.foreign_keys),
if_not_exists: self.if_not_exists,
check: std::mem::take(&mut self.check),
extra: std::mem::take(&mut self.extra),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/tests_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum Character {
FontId,
Ascii,
CreatedAt,
UserData,
}

/// A shorthand for [`Character`]
Expand All @@ -43,6 +44,7 @@ impl Iden for Character {
Self::FontId => "font_id",
Self::Ascii => "ascii",
Self::CreatedAt => "created_at",
Self::UserData => "user_data",
}
)
.unwrap();
Expand Down