drizzle_sqlite/builder/
prepared.rs

1use std::borrow::Cow;
2
3use drizzle_core::{
4    OwnedParam, Param,
5    prepared::{
6        PreparedStatement as CorePreparedStatement,
7        owned::OwnedPreparedStatement as CoreOwnedPreparedStatement,
8    },
9};
10
11use crate::{SQLiteValue, values::OwnedSQLiteValue};
12
13/// Generic prepared statement wrapper for SQLite
14#[derive(Debug, Clone)]
15pub struct PreparedStatement<'a> {
16    pub inner: CorePreparedStatement<'a, crate::SQLiteValue<'a>>,
17}
18
19impl<'a> PreparedStatement<'a> {
20    pub fn into_owned(&self) -> OwnedPreparedStatement {
21        let owned_params = self.inner.params.iter().map(|p| OwnedParam {
22            placeholder: p.placeholder,
23            value: p
24                .value
25                .clone()
26                .map(|v| OwnedSQLiteValue::from(v.into_owned())),
27        });
28
29        let inner = CoreOwnedPreparedStatement {
30            text_segments: self.inner.text_segments.clone(),
31            params: owned_params.collect::<Box<[_]>>(),
32        };
33
34        OwnedPreparedStatement { inner }
35    }
36}
37
38/// Generic owned prepared statement wrapper for SQLite
39#[derive(Debug, Clone)]
40pub struct OwnedPreparedStatement {
41    pub inner: CoreOwnedPreparedStatement<crate::values::OwnedSQLiteValue>,
42}
43impl<'a> From<PreparedStatement<'a>> for OwnedPreparedStatement {
44    fn from(value: PreparedStatement<'a>) -> Self {
45        let owned_params = value.inner.params.iter().map(|p| OwnedParam {
46            placeholder: p.placeholder,
47            value: p
48                .value
49                .clone()
50                .map(|v| OwnedSQLiteValue::from(v.into_owned())),
51        });
52        let inner = CoreOwnedPreparedStatement {
53            text_segments: value.inner.text_segments,
54            params: owned_params.collect::<Box<[_]>>(),
55        };
56        Self { inner }
57    }
58}
59
60impl From<OwnedPreparedStatement> for PreparedStatement<'_> {
61    fn from(value: OwnedPreparedStatement) -> Self {
62        let sqlitevalue = value.inner.params.iter().map(|v| {
63            Param::new(
64                v.placeholder,
65                v.value.clone().map(|v| Cow::Owned(SQLiteValue::from(v))),
66            )
67        });
68        let inner = CorePreparedStatement {
69            text_segments: value.inner.text_segments,
70            params: sqlitevalue.collect::<Box<[_]>>(),
71        };
72        PreparedStatement { inner }
73    }
74}
75
76impl<'a> std::fmt::Display for PreparedStatement<'a> {
77    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78        write!(f, "{}", self.inner)
79    }
80}
81
82impl std::fmt::Display for OwnedPreparedStatement {
83    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84        write!(f, "{}", self.inner)
85    }
86}