Skip to main content

drizzle_sqlite/
helpers.rs

1#[cfg(not(feature = "std"))]
2use crate::prelude::*;
3use crate::traits::SQLiteTable;
4use crate::values::SQLiteValue;
5use drizzle_core::{
6    ColumnRef, SQL, Token, helpers as core_helpers,
7    traits::{SQLModel, ToSQL},
8};
9
10// Re-export core helpers with SQLiteValue type for convenience
11pub(crate) use core_helpers::{
12    delete, except, except_all, from, group_by_expr, having, insert, intersect, intersect_all,
13    limit, offset, order_by, select, select_distinct, set, union, union_all, update, r#where,
14};
15
16// Re-export Join from core
17pub use drizzle_core::Join;
18
19drizzle_core::impl_join_arg_trait!(
20    table_trait: SQLiteTable<'a>,
21    table_info_trait: drizzle_core::SQLTableInfo,
22    condition_trait: ToSQL<'a, SQLiteValue<'a>>,
23    value_type: SQLiteValue<'a>,
24);
25
26/// Helper to convert column info to SQL for joining (column names only for INSERT)
27fn columns_info_to_sql<'a>(columns: &[ColumnRef]) -> SQL<'a, SQLiteValue<'a>> {
28    let mut sql = SQL::with_capacity_chunks(columns.len().saturating_mul(2));
29    for (idx, col) in columns.iter().enumerate() {
30        if idx > 0 {
31            sql.push_mut(Token::COMMA);
32        }
33        sql.append_mut(SQL::ident(col.name));
34    }
35    sql
36}
37
38// Generate all join helper functions using the shared macro
39drizzle_core::impl_join_helpers!(
40    table_trait: SQLiteTable<'a>,
41    condition_trait: ToSQL<'a, SQLiteValue<'a>>,
42    sql_type: SQL<'a, SQLiteValue<'a>>,
43);
44
45/// Creates a VALUES clause for INSERT statements.
46/// All rows must declare the same set of columns.
47pub(crate) fn values<'a, Table, T>(
48    rows: impl IntoIterator<Item = Table::Insert<T>>,
49) -> SQL<'a, SQLiteValue<'a>>
50where
51    Table: SQLiteTable<'a> + Default,
52{
53    let rows: Vec<Table::Insert<T>> = rows.into_iter().collect();
54
55    if rows.is_empty() {
56        return SQL::from(Token::VALUES);
57    }
58
59    // Since all rows have the same PATTERN, they all have the same columns
60    // Get column info from the first row (all rows will have the same columns)
61    let columns_info = rows[0].columns();
62    let columns_slice = columns_info.as_ref();
63
64    // Check if this is a DEFAULT VALUES case (no columns)
65    if columns_slice.is_empty() {
66        return SQL::from_iter([Token::DEFAULT, Token::VALUES]);
67    }
68
69    let columns_sql = columns_info_to_sql(columns_slice);
70    let mut values_sql = SQL::with_capacity_chunks(rows.len().saturating_mul(4));
71    for (idx, row) in rows.iter().enumerate() {
72        if idx > 0 {
73            values_sql.push_mut(Token::COMMA);
74        }
75        values_sql.push_mut(Token::LPAREN);
76        values_sql.append_mut(row.values());
77        values_sql.push_mut(Token::RPAREN);
78    }
79
80    columns_sql.parens().push(Token::VALUES).append(values_sql)
81}
82
83/// Helper function to create a RETURNING clause - `SQLite` specific
84pub(crate) fn returning<'a, 'b, I>(columns: I) -> SQL<'a, SQLiteValue<'a>>
85where
86    I: ToSQL<'a, SQLiteValue<'a>>,
87{
88    let columns = columns.into_sql();
89    let columns = if columns.chunks.is_empty() {
90        SQL::from(Token::STAR)
91    } else {
92        columns
93    };
94    SQL::from(Token::RETURNING).append(columns)
95}