-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathstatement.rs
103 lines (91 loc) · 2.74 KB
/
statement.rs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use crate::any::{Any, AnyArguments, AnyColumn, AnyTypeInfo};
use crate::column::ColumnIndex;
use crate::database::Database;
use crate::error::Error;
use crate::ext::ustr::UStr;
use crate::sql_str::SqlStr;
use crate::statement::Statement;
use crate::HashMap;
use either::Either;
use std::sync::Arc;
pub struct AnyStatement {
#[doc(hidden)]
pub sql: SqlStr,
#[doc(hidden)]
pub parameters: Option<Either<Vec<AnyTypeInfo>, usize>>,
#[doc(hidden)]
pub column_names: Arc<HashMap<UStr, usize>>,
#[doc(hidden)]
pub columns: Vec<AnyColumn>,
}
impl Statement for AnyStatement {
type Database = Any;
fn to_owned(&self) -> AnyStatement {
AnyStatement {
sql: self.sql.clone(),
column_names: self.column_names.clone(),
parameters: self.parameters.clone(),
columns: self.columns.clone(),
}
}
fn sql_cloned(&self) -> SqlStr {
self.sql.clone()
}
fn into_sql(self) -> SqlStr {
self.sql
}
fn parameters(&self) -> Option<Either<&[AnyTypeInfo], usize>> {
match &self.parameters {
Some(Either::Left(types)) => Some(Either::Left(types)),
Some(Either::Right(count)) => Some(Either::Right(*count)),
None => None,
}
}
fn columns(&self) -> &[AnyColumn] {
&self.columns
}
impl_statement_query!(AnyArguments<'_>);
}
impl ColumnIndex<AnyStatement> for &'_ str {
fn index(&self, statement: &AnyStatement) -> Result<usize, Error> {
statement
.column_names
.get(*self)
.ok_or_else(|| Error::ColumnNotFound((*self).into()))
.copied()
}
}
impl AnyStatement {
#[doc(hidden)]
pub fn try_from_statement<S>(
statement: S,
column_names: Arc<HashMap<UStr, usize>>,
) -> crate::Result<Self>
where
S: Statement,
AnyTypeInfo: for<'a> TryFrom<&'a <S::Database as Database>::TypeInfo, Error = Error>,
AnyColumn: for<'a> TryFrom<&'a <S::Database as Database>::Column, Error = Error>,
{
let parameters = match statement.parameters() {
Some(Either::Left(parameters)) => Some(Either::Left(
parameters
.iter()
.map(AnyTypeInfo::try_from)
.collect::<Result<Vec<_>, _>>()?,
)),
Some(Either::Right(count)) => Some(Either::Right(count)),
None => None,
};
let columns = statement
.columns()
.iter()
.map(AnyColumn::try_from)
.collect::<Result<Vec<_>, _>>()?;
Ok(Self {
sql: statement.into_sql(),
columns,
column_names,
parameters,
})
}
}