Fix mariadb compile errors and remove useless Row wrapper

This commit is contained in:
Ryan Leckey 2019-12-02 21:11:49 -08:00
parent 55da9daaf1
commit 6925d5999c
12 changed files with 36 additions and 97 deletions

View File

@ -1,4 +1,5 @@
#![recursion_limit="256"]
#![allow(unused_imports)]
#[macro_use]
mod macros;

View File

@ -1,4 +1,6 @@
use super::{MariaDb, MariaDbQueryParameters, MariaDbRow};
use super::MariaDb;
use crate::mariadb::protocol::ResultRow;
use crate::mariadb::query::MariaDbQueryParameters;
use crate::{
backend::Backend,
describe::{Describe, ResultField},
@ -9,7 +11,7 @@ use crate::url::Url;
impl Backend for MariaDb {
type QueryParameters = MariaDbQueryParameters;
type Row = MariaDbRow;
type Row = ResultRow;
type TableIdent = String;
fn open(url: &str) -> BoxFuture<'static, crate::Result<Self>> {
@ -21,15 +23,11 @@ impl Backend for MariaDb {
})
}
fn close(mut self) -> BoxFuture<'static, crate::Result<()>> {
fn close(self) -> BoxFuture<'static, crate::Result<()>> {
Box::pin(async move {
self.close().await
})
}
// async fn ping(&mut self) -> crate::Result<()> {
// self.ping().await
// }
}
impl_from_row_for_backend!(MariaDb);

View File

@ -7,7 +7,7 @@ use crate::{
ComStmtExecute, ComStmtPrepare, ComStmtPrepareOk, Encode, EofPacket, ErrPacket,
OkPacket, ResultRow, StmtExecFlag,
},
MariaDbQueryParameters,
query::MariaDbQueryParameters,
},
Error, Result,
};
@ -157,17 +157,6 @@ impl MariaDb {
})
}
pub(super) async fn check_eof(&mut self) -> Result<()> {
if !self
.capabilities
.contains(Capabilities::CLIENT_DEPRECATE_EOF)
{
let _ = EofPacket::decode(self.receive().await?)?;
}
Ok(())
}
pub(super) async fn send_prepare<'c>(
&'c mut self,
statement: &'c str,
@ -189,35 +178,6 @@ impl MariaDb {
ComStmtPrepareOk::decode(packet).map_err(Into::into)
}
pub(super) async fn step(
&mut self,
columns: &Vec<ColumnDefinitionPacket>,
packet: &[u8],
) -> Result<Option<ResultRow>> {
// For each row in the result set we will receive a ResultRow packet.
// We may receive an [OkPacket], [EofPacket], or [ErrPacket] (depending on if EOFs are enabled) to finalize the iteration.
if packet[0] == 0xFE && packet.len() < 0xFF_FF_FF {
// NOTE: It's possible for a ResultRow to start with 0xFE (which would normally signify end-of-rows)
// but it's not possible for an Ok/Eof to be larger than 0xFF_FF_FF.
if !self
.capabilities
.contains(Capabilities::CLIENT_DEPRECATE_EOF)
{
let _eof = EofPacket::decode(packet)?;
Ok(None)
} else {
let _ok = OkPacket::decode(packet, self.capabilities)?;
Ok(None)
}
} else if packet[0] == 0xFF {
let _ = ErrPacket::decode(packet)?;
// TODO: Should be error
Ok(None)
} else {
Ok(Some(ResultRow::decode(packet, columns)?))
}
}
pub(super) async fn column_definitions(
&mut self
) -> Result<Vec<ColumnDefinitionPacket>> {

View File

@ -1,4 +1,4 @@
use super::{MariaDb, MariaDbQueryParameters, MariaDbRow};
use super::MariaDb;
use crate::{
backend::Backend,
describe::{Describe, ResultField},
@ -8,6 +8,7 @@ use crate::{
Capabilities, ColumnCountPacket, ColumnDefinitionPacket, ComStmtExecute, EofPacket,
ErrPacket, OkPacket, ResultRow, StmtExecFlag,
},
mariadb::query::MariaDbQueryParameters,
row::FromRow,
url::Url,
};
@ -28,7 +29,7 @@ impl Executor for MariaDb {
Box::pin(async move {
let prepare = self.send_prepare(query).await?;
self.send_execute(prepare.statement_id, params);
self.send_execute(prepare.statement_id, params).await?;
let columns = self.column_definitions().await?;
let capabilities = self.capabilities;
@ -77,7 +78,7 @@ impl Executor for MariaDb {
Box::pin(async_stream::try_stream! {
let prepare = self.send_prepare(query).await?;
self.send_execute(prepare.statement_id, params);
self.send_execute(prepare.statement_id, params).await?;
let columns = self.column_definitions().await?;
let capabilities = self.capabilities;

View File

@ -9,4 +9,4 @@ mod query;
mod row;
pub mod types;
pub use self::{connection::MariaDb, query::MariaDbQueryParameters, row::MariaDbRow};
pub use self::connection::MariaDb;

View File

@ -3,20 +3,17 @@ use crate::{
row::Row,
};
#[derive(Debug)]
pub struct MariaDbRow(pub(crate) ResultRow);
impl Row for MariaDbRow {
impl Row for ResultRow {
type Backend = MariaDb;
#[inline]
fn len(&self) -> usize {
self.0.values.len()
self.values.len()
}
#[inline]
fn get_raw(&self, index: usize) -> Option<&[u8]> {
self.0.values[index]
self.values[index]
.as_ref()
.map(|value| unsafe { value.as_ref() })
}

View File

@ -1,16 +1,18 @@
use super::{connection::Step, Postgres, PostgresQueryParameters, PostgresRow};
use super::{connection::Step, Postgres};
use crate::{
backend::Backend,
describe::{Describe, ResultField},
postgres::protocol::DataRow,
params::QueryParameters,
url::Url,
};
use futures_core::{future::BoxFuture, stream::BoxStream};
use crate::postgres::query::PostgresQueryParameters;
impl Backend for Postgres {
type QueryParameters = PostgresQueryParameters;
type Row = PostgresRow;
type Row = DataRow;
type TableIdent = u32;
@ -33,7 +35,7 @@ impl Backend for Postgres {
})
}
fn close(mut self) -> BoxFuture<'static, crate::Result<()>> {
fn close(self) -> BoxFuture<'static, crate::Result<()>> {
Box::pin(self.terminate())
}
}

View File

@ -2,7 +2,6 @@ use crate::{
io::{Buf, BufStream},
postgres::{
protocol::{self, Decode, Encode, Message},
PostgresDatabaseError, PostgresQueryParameters, PostgresRow,
},
};
use async_std::net::TcpStream;
@ -11,6 +10,8 @@ use std::{
io,
net::{Shutdown, SocketAddr},
};
use crate::postgres::query::PostgresQueryParameters;
use crate::postgres::error::PostgresDatabaseError;
pub struct Postgres {
stream: BufStream<TcpStream>,
@ -193,7 +194,7 @@ impl Postgres {
}
Message::DataRow(body) => {
return Ok(Some(Step::Row(PostgresRow(body))));
return Ok(Some(Step::Row(body)));
}
Message::ReadyForQuery(_) => {
@ -291,7 +292,7 @@ impl Postgres {
#[derive(Debug)]
pub(super) enum Step {
Command(u64),
Row(PostgresRow),
Row(protocol::DataRow),
ParamDesc(Box<protocol::ParameterDescription>),
RowDesc(Box<protocol::RowDescription>),
}

View File

@ -1,4 +1,4 @@
use super::{connection::Step, Postgres, PostgresQueryParameters, PostgresRow};
use super::{connection::Step, Postgres};
use crate::{
backend::Backend,
describe::{Describe, ResultField},
@ -104,7 +104,7 @@ impl Executor for Postgres {
query: &'q str,
) -> BoxFuture<'e, crate::Result<Describe<Self::Backend>>> {
Box::pin(async move {
self.parse("", query, &PostgresQueryParameters::new());
self.parse("", query, &QueryParameters::new());
self.describe("");
self.sync().await?;

View File

@ -14,6 +14,5 @@ pub mod protocol;
pub mod types;
pub use self::{
connection::Postgres, error::PostgresDatabaseError, query::PostgresQueryParameters,
row::PostgresRow,
connection::Postgres
};

View File

@ -11,7 +11,7 @@ use std::{
pub struct DataRow {
#[used]
buffer: Pin<Box<[u8]>>,
values: Box<[Option<NonNull<[u8]>>]>,
pub(crate) values: Box<[Option<NonNull<[u8]>>]>,
}
// SAFE: Raw pointers point to pinned memory inside the struct
@ -46,31 +46,14 @@ impl Decode for DataRow {
}
}
impl DataRow {
#[inline]
pub fn is_empty(&self) -> bool {
self.values.is_empty()
}
#[inline]
pub fn len(&self) -> usize {
self.values.len()
}
#[inline]
pub fn get(&self, index: usize) -> Option<&[u8]> {
self.values[index]
.as_ref()
.map(|value| unsafe { value.as_ref() })
}
}
impl Debug for DataRow {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use crate::row::Row;
write!(f, "DataRow(")?;
f.debug_list()
.entries((0..self.len()).map(|i| self.get(i).map(ByteStr)))
.entries((0..self.len()).map(|i| self.get_raw(i).map(ByteStr)))
.finish()?;
write!(f, ")")?;

View File

@ -1,19 +1,16 @@
use super::{protocol::DataRow, Postgres};
use crate::row::Row;
#[derive(Debug)]
pub struct PostgresRow(pub(crate) DataRow);
impl Row for PostgresRow {
impl Row for DataRow {
type Backend = Postgres;
#[inline]
fn len(&self) -> usize {
self.0.len()
self.values.len()
}
#[inline]
fn get_raw(&self, index: usize) -> Option<&[u8]> {
self.0.get(index)
self.values[index]
.as_ref()
.map(|value| unsafe { value.as_ref() })
}
}