sqlx_core_guts/sqlite/statement/
mod.rs

1use crate::column::ColumnIndex;
2use crate::error::Error;
3use crate::ext::ustr::UStr;
4use crate::sqlite::{Sqlite, SqliteArguments, SqliteColumn, SqliteTypeInfo};
5use crate::statement::Statement;
6use crate::HashMap;
7use either::Either;
8use std::borrow::Cow;
9use std::sync::Arc;
10
11mod handle;
12mod r#virtual;
13
14pub(crate) use handle::StatementHandle;
15pub(crate) use r#virtual::VirtualStatement;
16
17#[derive(Debug, Clone)]
18#[allow(clippy::rc_buffer)]
19pub struct SqliteStatement<'q> {
20    pub(crate) sql: Cow<'q, str>,
21    pub(crate) parameters: usize,
22    pub(crate) columns: Arc<Vec<SqliteColumn>>,
23    pub(crate) column_names: Arc<HashMap<UStr, usize>>,
24}
25
26impl<'q> Statement<'q> for SqliteStatement<'q> {
27    type Database = Sqlite;
28
29    fn to_owned(&self) -> SqliteStatement<'static> {
30        SqliteStatement::<'static> {
31            sql: Cow::Owned(self.sql.clone().into_owned()),
32            parameters: self.parameters,
33            columns: Arc::clone(&self.columns),
34            column_names: Arc::clone(&self.column_names),
35        }
36    }
37
38    fn sql(&self) -> &str {
39        &self.sql
40    }
41
42    fn parameters(&self) -> Option<Either<&[SqliteTypeInfo], usize>> {
43        Some(Either::Right(self.parameters))
44    }
45
46    fn columns(&self) -> &[SqliteColumn] {
47        &self.columns
48    }
49
50    impl_statement_query!(SqliteArguments<'_>);
51}
52
53impl ColumnIndex<SqliteStatement<'_>> for &'_ str {
54    fn index(&self, statement: &SqliteStatement<'_>) -> Result<usize, Error> {
55        statement
56            .column_names
57            .get(*self)
58            .ok_or_else(|| Error::ColumnNotFound((*self).into()))
59            .map(|v| *v)
60    }
61}
62
63#[cfg(feature = "any")]
64impl<'q> From<SqliteStatement<'q>> for crate::any::AnyStatement<'q> {
65    #[inline]
66    fn from(statement: SqliteStatement<'q>) -> Self {
67        crate::any::AnyStatement::<'q> {
68            columns: statement
69                .columns
70                .iter()
71                .map(|col| col.clone().into())
72                .collect(),
73            column_names: statement.column_names,
74            parameters: Some(Either::Right(statement.parameters)),
75            sql: statement.sql,
76        }
77    }
78}