toasty_sql/stmt/
copy_table.rs

1// TODO: Remove this file. This should be implementable with [`stmt::Insert`], however for
2// migrations we need to reference table names which are not part of the schema, and that
3// is currently not implemented.
4
5use super::{Name, Statement};
6
7/// A statement to copy rows from one table to another.
8///
9/// Generates: `INSERT INTO "target" ("t_col1", "t_col2") SELECT "s_col1", "s_col2" FROM "source"`
10#[derive(Debug, Clone)]
11pub struct CopyTable {
12    /// Source table name.
13    pub source: Name,
14
15    /// Target table name.
16    pub target: Name,
17
18    /// Column mappings: (target_column_name, source_column_name).
19    pub columns: Vec<(Name, Name)>,
20}
21
22impl Statement {
23    /// Creates a statement that copies rows from one table to another.
24    pub fn copy_table(source: Name, target: Name, columns: Vec<(Name, Name)>) -> Self {
25        CopyTable {
26            source,
27            target,
28            columns,
29        }
30        .into()
31    }
32}
33
34impl From<CopyTable> for Statement {
35    fn from(value: CopyTable) -> Self {
36        Self::CopyTable(value)
37    }
38}