sea_query/table/
rename.rs

1use inherent::inherent;
2
3use crate::{SchemaStatementBuilder, backend::SchemaBuilder, types::*};
4
5/// Rename a table
6///
7/// # Examples
8///
9/// ```
10/// use sea_query::{tests_cfg::*, *};
11///
12/// let table = Table::rename().table(Font::Table, "font_new").to_owned();
13///
14/// assert_eq!(
15///     table.to_string(MysqlQueryBuilder),
16///     r#"RENAME TABLE `font` TO `font_new`"#
17/// );
18/// assert_eq!(
19///     table.to_string(PostgresQueryBuilder),
20///     r#"ALTER TABLE "font" RENAME TO "font_new""#
21/// );
22/// assert_eq!(
23///     table.to_string(SqliteQueryBuilder),
24///     r#"ALTER TABLE "font" RENAME TO "font_new""#
25/// );
26/// ```
27#[derive(Default, Debug, Clone)]
28pub struct TableRenameStatement {
29    pub(crate) from_name: Option<TableRef>,
30    pub(crate) to_name: Option<TableRef>,
31}
32
33impl TableRenameStatement {
34    /// Construct rename table statement
35    pub fn new() -> Self {
36        Self::default()
37    }
38
39    /// Set old and new table name
40    pub fn table<T, R>(&mut self, from_name: T, to_name: R) -> &mut Self
41    where
42        T: IntoTableRef,
43        R: IntoTableRef,
44    {
45        self.from_name = Some(from_name.into_table_ref());
46        self.to_name = Some(to_name.into_table_ref());
47        self
48    }
49
50    pub fn take(&mut self) -> Self {
51        Self {
52            from_name: self.from_name.take(),
53            to_name: self.to_name.take(),
54        }
55    }
56}
57
58#[inherent]
59impl SchemaStatementBuilder for TableRenameStatement {
60    pub fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String {
61        let mut sql = String::with_capacity(256);
62        schema_builder.prepare_table_rename_statement(self, &mut sql);
63        sql
64    }
65
66    pub fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String {
67        let mut sql = String::with_capacity(256);
68        schema_builder.prepare_table_rename_statement(self, &mut sql);
69        sql
70    }
71
72    pub fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String;
73}