mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-04-09 11:46:19 +00:00
row: RowIndex -> ColumnIndex and de-duplicate logic with macros
This commit is contained in:
@@ -27,13 +27,9 @@ impl DataRow {
|
||||
}
|
||||
|
||||
impl DataRow {
|
||||
pub(crate) fn read<'a>(
|
||||
connection: &mut PgConnection,
|
||||
// buffer: &'a [u8],
|
||||
// values: &'a mut Vec<Option<Range<u32>>>,
|
||||
) -> crate::Result<Self> {
|
||||
pub(crate) fn read(connection: &mut PgConnection) -> crate::Result<Self> {
|
||||
let buffer = connection.stream.buffer();
|
||||
let values = &mut connection.data_row_values_buf;
|
||||
let values = &mut connection.current_row_values;
|
||||
|
||||
values.clear();
|
||||
|
||||
@@ -64,22 +60,3 @@ impl DataRow {
|
||||
Ok(Self { len })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{DataRow, Decode};
|
||||
|
||||
const DATA_ROW: &[u8] = b"\0\x03\0\0\0\x011\0\0\0\x012\0\0\0\x013";
|
||||
|
||||
#[test]
|
||||
fn it_reads_data_row() {
|
||||
let mut values = Vec::new();
|
||||
let m = DataRow::read(DATA_ROW, &mut values).unwrap();
|
||||
|
||||
assert_eq!(m.len, 3);
|
||||
|
||||
assert_eq!(m.get(DATA_ROW, &values, 0), Some(&b"1"[..]));
|
||||
assert_eq!(m.get(DATA_ROW, &values, 1), Some(&b"2"[..]));
|
||||
assert_eq!(m.get(DATA_ROW, &values, 2), Some(&b"3"[..]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::decode::Decode;
|
||||
use crate::pool::PoolConnection;
|
||||
use crate::postgres::protocol::DataRow;
|
||||
use crate::postgres::{PgConnection, Postgres};
|
||||
use crate::row::{Row, RowIndex};
|
||||
use crate::row::{ColumnIndex, Row};
|
||||
use crate::types::Type;
|
||||
|
||||
pub struct PgRow<'c> {
|
||||
@@ -24,46 +24,16 @@ impl<'c> Row<'c> for PgRow<'c> {
|
||||
|
||||
fn try_get_raw<'i, I>(&'c self, index: I) -> crate::Result<Option<&'c [u8]>>
|
||||
where
|
||||
I: RowIndex<'c, Self> + 'i,
|
||||
I: ColumnIndex<'c, Self> + 'i,
|
||||
{
|
||||
index.try_get_raw(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'c> RowIndex<'c, PgRow<'c>> for usize {
|
||||
fn try_get_raw(self, row: &'c PgRow<'c>) -> crate::Result<Option<&'c [u8]>> {
|
||||
Ok(row.data.get(
|
||||
row.connection.stream.buffer(),
|
||||
&row.connection.data_row_values_buf,
|
||||
self,
|
||||
Ok(self.data.get(
|
||||
self.connection.stream.buffer(),
|
||||
&self.connection.current_row_values,
|
||||
index.try_resolve(self)?,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'c> RowIndex<'c, PgRow<'c>> for &'_ str {
|
||||
fn try_get_raw(self, row: &'c PgRow<'c>) -> crate::Result<Option<&'c [u8]>> {
|
||||
let index = row
|
||||
.columns
|
||||
.get(self)
|
||||
.ok_or_else(|| crate::Error::ColumnNotFound((*self).into()))?;
|
||||
|
||||
Ok(row.data.get(
|
||||
row.connection.stream.buffer(),
|
||||
&row.connection.data_row_values_buf,
|
||||
*index,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: impl_from_row_for_row!(PgRow);
|
||||
|
||||
impl<O: Unpin, F> crate::query::MapRow<Postgres> for F
|
||||
where
|
||||
F: for<'c> FnMut(PgRow<'c>) -> crate::Result<O>,
|
||||
{
|
||||
type Mapped = O;
|
||||
|
||||
fn map_row(&mut self, row: PgRow) -> crate::Result<O> {
|
||||
(self)(row)
|
||||
}
|
||||
}
|
||||
impl_map_row_for_row!(Postgres, PgRow);
|
||||
impl_column_index_for_row!(PgRow);
|
||||
impl_from_row_for_row!(PgRow);
|
||||
|
||||
Reference in New Issue
Block a user