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