forked from launchbadge/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatement.rs
More file actions
83 lines (72 loc) · 2.26 KB
/
statement.rs
File metadata and controls
83 lines (72 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use super::{PgColumn, PgTypeInfo};
use crate::column::ColumnIndex;
use crate::error::Error;
use crate::ext::ustr::UStr;
use crate::{PgArguments, Postgres};
use std::sync::Arc;
use sqlx_core::sql_str::SqlStr;
pub(crate) use sqlx_core::statement::Statement;
use sqlx_core::{Either, HashMap};
#[derive(Debug, Clone)]
pub struct PgStatement {
pub(crate) sql: SqlStr,
pub(crate) metadata: Arc<PgStatementMetadata>,
}
#[derive(Debug, Default)]
pub(crate) struct PgStatementMetadata {
pub(crate) columns: Vec<PgColumn>,
// This `Arc` is not redundant; it's used to avoid deep-copying this map for the `Any` backend.
// See `sqlx-postgres/src/any.rs`
pub(crate) column_names: Arc<HashMap<UStr, usize>>,
pub(crate) parameters: Vec<PgTypeInfo>,
}
impl Statement for PgStatement {
type Database = Postgres;
fn into_sql(self) -> SqlStr {
self.sql
}
fn sql(&self) -> &SqlStr {
&self.sql
}
fn parameters(&self) -> Option<Either<&[PgTypeInfo], usize>> {
Some(Either::Left(&self.metadata.parameters))
}
fn columns(&self) -> &[PgColumn] {
&self.metadata.columns
}
impl_statement_query!(PgArguments);
}
impl ColumnIndex<PgStatement> for &'_ str {
fn index(&self, statement: &PgStatement) -> Result<usize, Error> {
statement
.metadata
.column_names
.get(*self)
.ok_or_else(|| Error::ColumnNotFound((*self).into()))
.copied()
}
}
// #[cfg(feature = "any")]
// impl<'q> From<PgStatement<'q>> for crate::any::AnyStatement<'q> {
// #[inline]
// fn from(statement: PgStatement<'q>) -> Self {
// crate::any::AnyStatement::<'q> {
// columns: statement
// .metadata
// .columns
// .iter()
// .map(|col| col.clone().into())
// .collect(),
// column_names: statement.metadata.column_names.clone(),
// parameters: Some(Either::Left(
// statement
// .metadata
// .parameters
// .iter()
// .map(|ty| ty.clone().into())
// .collect(),
// )),
// sql: statement.sql,
// }
// }
// }