drizzle_core/sql/
owned.rs

1use crate::prelude::*;
2use crate::{OwnedParam, SQL, SQLChunk, SQLColumnInfo, SQLParam, SQLTableInfo, ToSQL, Token};
3use smallvec::SmallVec;
4
5/// Owned version of SQLChunk with 'static lifetime
6#[derive(Default, Debug, Clone)]
7pub enum OwnedSQLChunk<V: SQLParam> {
8    #[default]
9    Empty,
10    Token(Token),
11    Ident(String),
12    Raw(String),
13    Param(OwnedParam<V>),
14    Table(&'static dyn SQLTableInfo),
15    Column(&'static dyn SQLColumnInfo),
16    Alias {
17        inner: Box<OwnedSQLChunk<V>>,
18        alias: String,
19    },
20}
21
22impl<'a, V: SQLParam> From<SQLChunk<'a, V>> for OwnedSQLChunk<V> {
23    fn from(value: SQLChunk<'a, V>) -> Self {
24        match value {
25            SQLChunk::Empty => Self::Empty,
26            SQLChunk::Token(token) => Self::Token(token),
27            SQLChunk::Ident(cow) => Self::Ident(cow.into_owned()),
28            SQLChunk::Raw(cow) => Self::Raw(cow.into_owned()),
29            SQLChunk::Param(param) => Self::Param(param.into()),
30            SQLChunk::Table(table) => Self::Table(table),
31            SQLChunk::Column(column) => Self::Column(column),
32            SQLChunk::Alias { inner, alias } => Self::Alias {
33                inner: Box::new((*inner).into()),
34                alias: alias.into_owned(),
35            },
36        }
37    }
38}
39
40impl<V: SQLParam> From<OwnedSQLChunk<V>> for SQLChunk<'static, V> {
41    fn from(value: OwnedSQLChunk<V>) -> Self {
42        match value {
43            OwnedSQLChunk::Empty => SQLChunk::Empty,
44            OwnedSQLChunk::Token(token) => SQLChunk::Token(token),
45            OwnedSQLChunk::Ident(s) => SQLChunk::Ident(Cow::Owned(s)),
46            OwnedSQLChunk::Raw(s) => SQLChunk::Raw(Cow::Owned(s)),
47            OwnedSQLChunk::Param(param) => SQLChunk::Param(param.into()),
48            OwnedSQLChunk::Table(table) => SQLChunk::Table(table),
49            OwnedSQLChunk::Column(column) => SQLChunk::Column(column),
50            OwnedSQLChunk::Alias { inner, alias } => SQLChunk::Alias {
51                inner: Box::new((*inner).into()),
52                alias: Cow::Owned(alias),
53            },
54        }
55    }
56}
57
58/// Owned version of SQL with 'static lifetime
59#[derive(Debug, Clone)]
60pub struct OwnedSQL<V: SQLParam> {
61    pub chunks: SmallVec<[OwnedSQLChunk<V>; 8]>,
62}
63
64impl<V: SQLParam> Default for OwnedSQL<V> {
65    fn default() -> Self {
66        Self {
67            chunks: SmallVec::new(),
68        }
69    }
70}
71
72impl<'a, V: SQLParam> From<SQL<'a, V>> for OwnedSQL<V> {
73    fn from(value: SQL<'a, V>) -> Self {
74        Self {
75            chunks: value.chunks.into_iter().map(Into::into).collect(),
76        }
77    }
78}
79
80impl<V: SQLParam> OwnedSQL<V> {
81    /// Convert to SQL with 'static lifetime
82    pub fn to_sql(&self) -> SQL<'static, V> {
83        SQL {
84            chunks: self.chunks.iter().cloned().map(Into::into).collect(),
85        }
86    }
87
88    /// Convert into SQL with 'static lifetime (consuming)
89    pub fn into_sql(self) -> SQL<'static, V> {
90        SQL {
91            chunks: self.chunks.into_iter().map(Into::into).collect(),
92        }
93    }
94}
95
96impl<V: SQLParam> ToSQL<'static, V> for OwnedSQL<V> {
97    fn to_sql(&self) -> SQL<'static, V> {
98        OwnedSQL::to_sql(self)
99    }
100}