sea_query/table/
truncate.rs

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