drizzle_postgres/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::{PostgresValue, values::OwnedPostgresValue};
12
13/// Generic prepared statement wrapper for PostgreSQL
14#[derive(Debug, Clone)]
15pub struct PreparedStatement<'a> {
16    pub inner: CorePreparedStatement<'a, crate::PostgresValue<'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| OwnedPostgresValue::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 PostgreSQL
39#[derive(Debug, Clone)]
40pub struct OwnedPreparedStatement {
41    pub inner: CoreOwnedPreparedStatement<crate::values::OwnedPostgresValue>,
42}
43
44impl<'a> From<PreparedStatement<'a>> for OwnedPreparedStatement {
45    fn from(value: PreparedStatement<'a>) -> Self {
46        let owned_params = value.inner.params.iter().map(|p| OwnedParam {
47            placeholder: p.placeholder,
48            value: p
49                .value
50                .clone()
51                .map(|v| OwnedPostgresValue::from(v.into_owned())),
52        });
53        let inner = CoreOwnedPreparedStatement {
54            text_segments: value.inner.text_segments,
55            params: owned_params.collect::<Box<[_]>>(),
56        };
57        Self { inner }
58    }
59}
60
61impl From<OwnedPreparedStatement> for PreparedStatement<'_> {
62    fn from(value: OwnedPreparedStatement) -> Self {
63        let postgresvalue = value.inner.params.iter().map(|v| {
64            Param::new(
65                v.placeholder,
66                v.value.clone().map(|v| Cow::Owned(PostgresValue::from(v))),
67            )
68        });
69        let inner = CorePreparedStatement {
70            text_segments: value.inner.text_segments,
71            params: postgresvalue.collect::<Box<[_]>>(),
72        };
73        PreparedStatement { inner }
74    }
75}
76
77impl OwnedPreparedStatement {}
78
79impl<'a> std::fmt::Display for PreparedStatement<'a> {
80    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81        write!(f, "{}", self.inner)
82    }
83}
84
85impl std::fmt::Display for OwnedPreparedStatement {
86    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87        write!(f, "{}", self.inner)
88    }
89}