drizzle_core/prepared/
owned.rs1use crate::prelude::*;
2use crate::{
3 OwnedParam, ParamBind, SQL, SQLChunk, ToSQL,
4 prepared::{PreparedStatement, bind_values_internal},
5 traits::SQLParam,
6};
7use compact_str::CompactString;
8use core::fmt;
9use smallvec::SmallVec;
10
11#[derive(Debug, Clone)]
13pub struct OwnedPreparedStatement<V: SQLParam> {
14 pub text_segments: Box<[CompactString]>,
16 pub params: Box<[OwnedParam<V>]>,
18 pub sql: CompactString,
20}
21impl<V: SQLParam> core::fmt::Display for OwnedPreparedStatement<V> {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 write!(f, "{}", self.sql())
24 }
25}
26
27impl<'a, V: SQLParam> From<PreparedStatement<'a, V>> for OwnedPreparedStatement<V> {
28 fn from(prepared: PreparedStatement<'a, V>) -> Self {
29 OwnedPreparedStatement {
30 text_segments: prepared.text_segments,
31 params: prepared.params.into_iter().map(|p| p.into()).collect(),
32 sql: prepared.sql,
33 }
34 }
35}
36
37impl<V: SQLParam> OwnedPreparedStatement<V> {
38 pub fn bind<'a, T: SQLParam + Into<V>>(
41 &self,
42 param_binds: impl IntoIterator<Item = ParamBind<'a, T>>,
43 ) -> (&str, impl Iterator<Item = V>) {
44 let bound_params = bind_values_internal(
45 &self.params,
46 param_binds,
47 |p| p.placeholder.name,
48 |p| p.value.as_ref(), );
50
51 (self.sql.as_str(), bound_params)
52 }
53
54 pub fn sql(&self) -> &str {
56 self.sql.as_str()
57 }
58}
59
60impl<'a, V: SQLParam> ToSQL<'a, V> for OwnedPreparedStatement<V> {
61 fn to_sql(&self) -> SQL<'a, V> {
62 let capacity = self.text_segments.len() + self.params.len();
64 let mut chunks = SmallVec::with_capacity(capacity);
65
66 let mut param_iter = self.params.iter();
69
70 for text_segment in &self.text_segments {
71 chunks.push(SQLChunk::Raw(Cow::Owned(text_segment.to_string())));
72
73 if let Some(param) = param_iter.next() {
75 chunks.push(SQLChunk::Param(param.clone().into()));
76 }
77 }
78
79 SQL { chunks }
80 }
81}