Update MariaDbRawQuery to be MariaDbQueryParameters

Also coment out module imports for build failures so we get a green build to work on
This commit is contained in:
Ryan Leckey 2019-08-29 00:26:37 -07:00
parent c8559cac84
commit cbd56f3fa9
8 changed files with 86 additions and 116 deletions

View file

@ -1,14 +1,13 @@
use crate::backend::{Backend, BackendAssocRawQuery}; use crate::backend::Backend;
pub struct MariaDB; #[derive(Debug)]
pub struct MariaDb;
impl<'q> BackendAssocRawQuery<'q, MariaDB> for MariaDB { impl Backend for MariaDb {
type RawQuery = super::MariaDbRawQuery<'q>; type QueryParameters = super::MariaDbQueryParameters;
}
impl Backend for MariaDB {
type RawConnection = super::MariaDbRawConnection; type RawConnection = super::MariaDbRawConnection;
type Row = super::MariaDbRow; type Row = super::MariaDbRow;
} }
impl_from_sql_row_tuples_for_backend!(MariaDb); // TODO: impl_from_sql_row_tuples_for_backend!(MariaDb);
// TODO: impl_into_query_parameters_for_backend!(MariaDb);

View file

@ -1,5 +1,5 @@
use super::MariaDbRawConnection; use super::MariaDbRawConnection;
use crate::mariadb::{ use crate::mariadb::protocol::{
Capabilities, ComStmtExec, DeContext, Decode, EofPacket, ErrPacket, HandshakeResponsePacket, Capabilities, ComStmtExec, DeContext, Decode, EofPacket, ErrPacket, HandshakeResponsePacket,
InitialHandshakePacket, OkPacket, ProtocolType, StmtExecFlag, InitialHandshakePacket, OkPacket, ProtocolType, StmtExecFlag,
}; };

View file

@ -1,12 +1,10 @@
use crate::{ use crate::{
connection::RawConnection,
error::ErrorKind, error::ErrorKind,
mariadb::{ mariadb::{
protocol::encode, Capabilities, ComInitDb, ComPing, ComQuery, ComQuit, ComStmtPrepare, protocol::{encode, Capabilities, ComInitDb, ComPing, ComQuery, ComQuit, ComStmtPrepare,
ComStmtPrepareResp, DeContext, Decode, Decoder, Encode, ErrPacket, OkPacket, PacketHeader, ComStmtPrepareResp, DeContext, Decode, Decoder, Encode, ErrPacket, OkPacket, PacketHeader,
ProtocolType, ResultSet, ServerStatusFlag, ProtocolType, ResultSet, ServerStatusFlag},
}, },
query::RawQuery,
}; };
use byteorder::{ByteOrder, LittleEndian}; use byteorder::{ByteOrder, LittleEndian};
use bytes::{BufMut, Bytes, BytesMut}; use bytes::{BufMut, Bytes, BytesMut};
@ -276,24 +274,24 @@ impl MariaDbRawConnection {
} }
} }
impl RawConnection for MariaDbRawConnection { // impl RawConnection for MariaDbRawConnection {
type Backend = MariaDb; // type Backend = MariaDb;
#[inline] // #[inline]
fn establish(url: &str) -> BoxFuture<std::io::Result<Self>> { // fn establish(url: &str) -> BoxFuture<std::io::Result<Self>> {
Box::pin(MariaDbRawConnection::establish(url)) // Box::pin(MariaDbRawConnection::establish(url))
} // }
#[inline] // #[inline]
fn finalize<'c>(&'c mut self) -> BoxFuture<'c, std::io::Result<()>> { // fn finalize<'c>(&'c mut self) -> BoxFuture<'c, std::io::Result<()>> {
Box::pin(self.finalize()) // Box::pin(self.finalize())
} // }
fn execute<'c, 'q, Q: 'q>(&'c mut self, query: Q) -> BoxFuture<'c, std::io::Result<()>> // fn execute<'c, 'q, Q: 'q>(&'c mut self, query: Q) -> BoxFuture<'c, std::io::Result<()>>
where // where
Q: RawQuery<'q, Backend = Self::Backend>, // Q: RawQuery<'q, Backend = Self::Backend>,
{ // {
query.finish(self); // query.finish(self);
Box::pin(execute::execute(self)) // Box::pin(execute::execute(self))
} // }
} // }

View file

@ -1,20 +1,13 @@
pub mod backend; // mod backend;
pub mod connection; // mod connection;
pub mod protocol; mod protocol;
pub mod query; // mod query;
pub mod types; // pub mod types;
// Re-export all the things // pub use self::{
pub use connection::{ConnContext, Framed, MariaDbRawConnection}; // backend::MariaDb, connection::MariaDbRawConnection, query::MariaDbQueryParameters,
pub use protocol::{ // row::MariaDbRow,
AuthenticationSwitchRequestPacket, BufMut, Capabilities, ColumnDefPacket, ColumnPacket, // };
ComDebug, ComInitDb, ComPing, ComProcessKill, ComQuery, ComQuit, ComResetConnection,
ComSetOption, ComShutdown, ComSleep, ComStatistics, ComStmtClose, ComStmtExec, ComStmtFetch,
ComStmtPrepare, ComStmtPrepareOk, ComStmtPrepareResp, DeContext, Decode, Decoder, Encode,
EofPacket, ErrPacket, ErrorCode, FieldDetailFlag, FieldType, HandshakeResponsePacket,
InitialHandshakePacket, OkPacket, PacketHeader, ProtocolType, ResultRow, ResultRowBinary,
ResultRowText, ResultSet, SSLRequestPacket, ServerStatusFlag, SessionChangeType,
SetOptionOptions, ShutdownOptions, StmtExecFlag,
};
pub use backend::MariaDB; // 1) Get protocol compiling using io::Buf / io::BufMut
// 2) Switch MariaDbRawConnection to use io::BufStream

View file

@ -4,32 +4,34 @@
// TODO: Handle lengths which are greater than 3 bytes // TODO: Handle lengths which are greater than 3 bytes
// Either break the packet into several smaller ones, or // Either break the packet into several smaller ones, or
// return error // return error
// TODO: Handle different Capabilities for server and client // TODO: Handle different Capabilities for server and client
// TODO: Handle when capability is set, but field is None // TODO: Handle when capability is set, but field is None
pub mod decode; // pub mod decode;
pub mod encode; // pub mod encode;
pub mod error_codes; // pub mod error_codes;
pub mod packets; // pub mod packets;
pub mod types; // pub mod types;
// Re-export all the things // Re-export all the things
pub use packets::{ // pub use packets::{
AuthenticationSwitchRequestPacket, ColumnDefPacket, ColumnPacket, ComDebug, ComInitDb, ComPing, // AuthenticationSwitchRequestPacket, ColumnDefPacket, ColumnPacket, ComDebug, ComInitDb, ComPing,
ComProcessKill, ComQuery, ComQuit, ComResetConnection, ComSetOption, ComShutdown, ComSleep, // ComProcessKill, ComQuery, ComQuit, ComResetConnection, ComSetOption, ComShutdown, ComSleep,
ComStatistics, ComStmtClose, ComStmtExec, ComStmtFetch, ComStmtPrepare, ComStmtPrepareOk, // ComStatistics, ComStmtClose, ComStmtExec, ComStmtFetch, ComStmtPrepare, ComStmtPrepareOk,
ComStmtPrepareResp, ComStmtReset, EofPacket, ErrPacket, HandshakeResponsePacket, // ComStmtPrepareResp, ComStmtReset, EofPacket, ErrPacket, HandshakeResponsePacket,
InitialHandshakePacket, OkPacket, PacketHeader, ResultRow, ResultRowBinary, ResultRowText, // InitialHandshakePacket, OkPacket, PacketHeader, ResultRow, ResultRowBinary, ResultRowText,
ResultSet, SSLRequestPacket, SetOptionOptions, ShutdownOptions, // ResultSet, SSLRequestPacket, SetOptionOptions, ShutdownOptions,
}; // };
pub use decode::{DeContext, Decode, Decoder}; // pub use decode::{DeContext, Decode, Decoder};
pub use encode::{BufMut, Encode}; // pub use encode::{BufMut, Encode};
pub use error_codes::ErrorCode; // pub use error_codes::ErrorCode;
pub use types::{ // pub use types::{
Capabilities, FieldDetailFlag, FieldType, ProtocolType, ServerStatusFlag, SessionChangeType, // Capabilities, FieldDetailFlag, FieldType, ProtocolType, ServerStatusFlag, SessionChangeType,
StmtExecFlag, // StmtExecFlag,
}; // };

View file

@ -1,56 +1,41 @@
use super::MariaDb;
use crate::{ use crate::{
mariadb::{protocol::types::ParamFlag, FieldType, MariaDbRawConnection}, query::QueryParameters,
query::RawQuery,
serialize::{IsNull, ToSql}, serialize::{IsNull, ToSql},
types::HasSqlType, types::HasSqlType,
}; };
pub struct MariaDbRawQuery<'q> { pub struct MariaDbQueryParameters {
query: &'q str, param_types: Vec<(u8, u8)>,
types: Vec<u8>, params: Vec<u8>,
null_bitmap: Vec<u8>, null: Vec<u8>,
flags: Vec<u8>,
buf: Vec<u8>,
index: u64,
} }
impl<'q> RawQueryQuery<'q> for MariaDbRawQuery<'q> { impl QueryParameters for MariaDbQueryParameters {
type Backend = MariaDb; type Backend = MariaDb;
fn new(query: &'q str) -> Self { fn new() -> Self {
Self { Self {
query, param_types: Vec::with_capacity(4),
types: Vec::with_capacity(4), params: Vec::with_capacity(32),
null_bitmap: vec![0, 0, 0, 0], null: 0,
flags: Vec::with_capacity(4),
buf: Vec::with_capacity(32),
index: 0,
} }
} }
fn bind<T>(mut self, value: T) -> Self fn bind<T>(&mut self, value: T)
where where
Self: Sized, Self: Sized,
Self::Backend: HasSqlType<T>, Self::Backend: HasSqlType<T>,
T: ToSql<Self::Backend>, T: ToSql<Self::Backend>,
{ {
self.types let metadata = <MariaDb as HasSqlType<T>>::metadata();
.push(<MariaDb as HasSqlType<T>>::metadata().field_type.0); let index = self.param_types.len();
self.flags
.push(<MariaDb as HasSqlType<T>>::metadata().param_flag.0);
match value.to_sql(&mut self.buf) { self.param_types
IsNull::Yes => { .push((metadata.field_type, metadata.param_flag));
self.null_bitmap[self.index / 8] =
self.null_bitmap[self.index / 8] & (1 << self.index % 8); if let IsNull::Yes = value.to_sql(&mut self.params) {
} self.null[index / 8] = self.null[index / 8] & (1 << index % 8);
IsNull::No => {}
} }
self
} }
}
fn finish(self, conn: &mut MariaDbRawConnection) {
conn.prepare(self.query);
}
}

View file

@ -1,18 +1,11 @@
use super::MariaDB; use crate::types::TypeMetadata;
use crate::{ use super::protocol::FieldType;
mariadb::{protocol::types::ParamFlag, FieldType}, use super::protocol::ParamFlag;
types::TypeMetadata, use super::backend::MariaDb;
};
mod boolean; mod boolean;
pub enum MariaDbTypeFormat {
Text = 0,
Binary = 1,
}
pub struct MariaDbTypeMetadata { pub struct MariaDbTypeMetadata {
pub format: MariaDbTypeFormat,
pub field_type: FieldType, pub field_type: FieldType,
pub param_flag: ParamFlag, pub param_flag: ParamFlag,
} }

View file

@ -1,5 +1,6 @@
use crate::backend::Backend; use crate::backend::Backend;
#[derive(Debug)]
pub struct Postgres; pub struct Postgres;
impl Backend for Postgres { impl Backend for Postgres {
@ -8,6 +9,5 @@ impl Backend for Postgres {
type Row = super::PostgresRow; type Row = super::PostgresRow;
} }
// Generates tuple impls for this backend
impl_from_sql_row_tuples_for_backend!(Postgres); impl_from_sql_row_tuples_for_backend!(Postgres);
impl_into_query_parameters_for_backend!(Postgres); impl_into_query_parameters_for_backend!(Postgres);