diff --git a/src/backend/postgres/table.rs b/src/backend/postgres/table.rs index a1a3e2f9..bf2f5c22 100644 --- a/src/backend/postgres/table.rs +++ b/src/backend/postgres/table.rs @@ -143,7 +143,9 @@ impl TableBuilder for PostgresQueryBuilder { if !first && !matches!( column_spec, - ColumnSpec::AutoIncrement | ColumnSpec::Generated { .. } + ColumnSpec::AutoIncrement + | ColumnSpec::Generated { .. } + | ColumnSpec::Using(_) ) { write!(sql, ", ").unwrap(); @@ -180,6 +182,10 @@ impl TableBuilder for PostgresQueryBuilder { ColumnSpec::Generated { .. } => {} ColumnSpec::Extra(string) => write!(sql, "{string}").unwrap(), ColumnSpec::Comment(_) => {} + ColumnSpec::Using(expr) => { + write!(sql, " USING ").unwrap(); + QueryBuilder::prepare_simple_expr(self, expr, sql); + } } false }); diff --git a/src/backend/table_builder.rs b/src/backend/table_builder.rs index 887b0576..1a2056cd 100644 --- a/src/backend/table_builder.rs +++ b/src/backend/table_builder.rs @@ -107,6 +107,7 @@ pub trait TableBuilder: } ColumnSpec::Extra(string) => write!(sql, "{string}").unwrap(), ColumnSpec::Comment(comment) => self.column_comment(comment, sql), + ColumnSpec::Using(_) => {} } } diff --git a/src/table/column.rs b/src/table/column.rs index 4f6d2ba9..f476f0e2 100644 --- a/src/table/column.rs +++ b/src/table/column.rs @@ -180,6 +180,7 @@ pub enum ColumnSpec { Generated { expr: SimpleExpr, stored: bool }, Extra(String), Comment(String), + Using(SimpleExpr), } // All interval fields @@ -721,6 +722,34 @@ impl ColumnDef { self } + /// Some extra options in custom string + /// ``` + /// use sea_query::{tests_cfg::*, *}; + /// let table = Table::alter() + /// .table(Char::Table) + /// .modify_column( + /// ColumnDef::new(Char::Id) + /// .integer() + /// .using(Expr::col(Char::Id).cast_as(Alias::new("integer"))) + /// ) + /// .to_owned(); + /// assert_eq!( + /// table.to_string(PostgresQueryBuilder), + /// [ + /// r#"ALTER TABLE "character""#, + /// r#"ALTER COLUMN "id" TYPE integer USING CAST("id" AS integer)"#, + /// ] + /// .join(" ") + /// ); + /// ``` + pub fn using(&mut self, value: T) -> &mut Self + where + T: Into, + { + self.spec.push(ColumnSpec::Using(value.into())); + self + } + /// MySQL only. pub fn comment(&mut self, string: T) -> &mut Self where