| 1 |
use crate::expression::{Expression, NonAggregate, SelectableExpression}; |
| 2 |
use crate::insertable::*; |
| 3 |
use crate::query_builder::*; |
| 4 |
use crate::query_source::Table; |
| 5 |
|
| 6 |
/// Represents `(Columns) SELECT FROM ...` for use in an `INSERT` statement |
| 7 |
#[derive(Debug, Clone, Copy, QueryId)] |
| 8 |
pub struct InsertFromSelect<Select, Columns> { |
| 9 |
pub(in crate::query_builder) query: Select, |
| 10 |
pub(in crate::query_builder) columns: Columns, |
| 11 |
} |
| 12 |
|
| 13 |
impl<Select, Columns> InsertFromSelect<Select, Columns> { |
| 14 |
/// Construct a new `InsertFromSelect` where the target column list is |
| 15 |
/// `T::AllColumns`. |
| 16 |
pub fn new<T>(query: Select) -> Self |
| 17 |
where |
| 18 |
T: Table<AllColumns = Columns>, |
| 19 |
Columns: SelectableExpression<T> + NonAggregate, |
| 20 |
{ |
| 21 |
Self { |
| 22 |
query, |
| 23 |
columns: T::all_columns(), |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
/// Replace the target column list |
| 28 |
pub fn with_columns<C>(self, columns: C) -> InsertFromSelect<Select, C> { |
| 29 |
InsertFromSelect { |
| 30 |
query: self.query, |
| 31 |
columns, |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
impl<DB, Select, Columns> CanInsertInSingleQuery<DB> for InsertFromSelect<Select, Columns> |
| 37 |
where |
| 38 |
DB: Backend, |
| 39 |
{ |
| 40 |
fn rows_to_insert(&self) -> Option<usize> { |
| 41 |
None |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
impl<DB, Select, Columns> QueryFragment<DB> for InsertFromSelect<Select, Columns> |
| 46 |
where |
| 47 |
DB: Backend, |
| 48 |
Columns: ColumnList + Expression, |
| 49 |
Select: Query<SqlType = Columns::SqlType> + QueryFragment<DB>, |
| 50 |
{ |
| 51 |
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> { |
| 52 |
out.push_sql("("); |
| 53 |
self.columns.walk_ast(out.reborrow())?; |
| 54 |
out.push_sql(") "); |
| 55 |
self.query.walk_ast(out.reborrow())?; |
| 56 |
Ok(()) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
impl<Select, Columns> UndecoratedInsertRecord<Columns::Table> for InsertFromSelect<Select, Columns> |
| 61 |
where |
| 62 |
Columns: ColumnList + Expression, |
| 63 |
Select: Query<SqlType = Columns::SqlType>, |
| 64 |
{ |
| 65 |
} |