forked from transact-rs/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrow.rs
More file actions
59 lines (49 loc) 路 1.46 KB
/
Copy pathrow.rs
File metadata and controls
59 lines (49 loc) 路 1.46 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
use crate::database::HasRow;
use crate::row::{ColumnIndex, Row};
use crate::sqlite::statement::Statement;
use crate::sqlite::value::SqliteValue;
use crate::sqlite::{Sqlite, SqliteConnection};
pub struct SqliteRow<'c> {
pub(super) values: usize,
pub(super) statement: Option<usize>,
pub(super) connection: &'c mut SqliteConnection,
}
impl<'c> SqliteRow<'c> {
fn statement(&'c self) -> &'c Statement {
self.connection.statement(self.statement)
}
}
impl<'c> Row<'c> for SqliteRow<'c> {
type Database = Sqlite;
#[inline]
fn len(&self) -> usize {
self.values
}
fn try_get_raw<'r, I>(&'r self, index: I) -> crate::Result<SqliteValue<'r>>
where
'c: 'r,
I: ColumnIndex<Self::Database>,
{
let index = index.resolve(self)?;
let value = SqliteValue::new(self.statement(), index);
Ok(value)
}
}
impl ColumnIndex<Sqlite> for usize {
fn resolve(self, row: &<Sqlite as HasRow>::Row) -> crate::Result<usize> {
let len = Row::len(row);
if self >= len {
return Err(crate::Error::ColumnIndexOutOfBounds { len, index: self });
}
Ok(self)
}
}
impl ColumnIndex<Sqlite> for &'_ str {
fn resolve(self, row: &<Sqlite as HasRow>::Row) -> crate::Result<usize> {
row.statement()
.columns
.get(self)
.ok_or_else(|| crate::Error::ColumnNotFound((*self).into()))
.map(|&index| index as usize)
}
}