Skip to main content

sea_query/table/
truncate.rs

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