diff --git a/src/mariadb/backend.rs b/src/mariadb/backend.rs index 28afc956..aabff1a5 100644 --- a/src/mariadb/backend.rs +++ b/src/mariadb/backend.rs @@ -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 { - type RawQuery = super::MariaDbRawQuery<'q>; -} - -impl Backend for MariaDB { +impl Backend for MariaDb { + type QueryParameters = super::MariaDbQueryParameters; type RawConnection = super::MariaDbRawConnection; 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); diff --git a/src/mariadb/connection/establish.rs b/src/mariadb/connection/establish.rs index f8d38dce..55513f92 100644 --- a/src/mariadb/connection/establish.rs +++ b/src/mariadb/connection/establish.rs @@ -1,5 +1,5 @@ use super::MariaDbRawConnection; -use crate::mariadb::{ +use crate::mariadb::protocol::{ Capabilities, ComStmtExec, DeContext, Decode, EofPacket, ErrPacket, HandshakeResponsePacket, InitialHandshakePacket, OkPacket, ProtocolType, StmtExecFlag, }; diff --git a/src/mariadb/connection/mod.rs b/src/mariadb/connection/mod.rs index 29adb580..b1c618f7 100644 --- a/src/mariadb/connection/mod.rs +++ b/src/mariadb/connection/mod.rs @@ -1,12 +1,10 @@ use crate::{ - connection::RawConnection, error::ErrorKind, 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, - ProtocolType, ResultSet, ServerStatusFlag, + ProtocolType, ResultSet, ServerStatusFlag}, }, - query::RawQuery, }; use byteorder::{ByteOrder, LittleEndian}; use bytes::{BufMut, Bytes, BytesMut}; @@ -276,24 +274,24 @@ impl MariaDbRawConnection { } } -impl RawConnection for MariaDbRawConnection { - type Backend = MariaDb; +// impl RawConnection for MariaDbRawConnection { +// type Backend = MariaDb; - #[inline] - fn establish(url: &str) -> BoxFuture> { - Box::pin(MariaDbRawConnection::establish(url)) - } +// #[inline] +// fn establish(url: &str) -> BoxFuture> { +// Box::pin(MariaDbRawConnection::establish(url)) +// } - #[inline] - fn finalize<'c>(&'c mut self) -> BoxFuture<'c, std::io::Result<()>> { - Box::pin(self.finalize()) - } +// #[inline] +// fn finalize<'c>(&'c mut self) -> BoxFuture<'c, std::io::Result<()>> { +// Box::pin(self.finalize()) +// } - fn execute<'c, 'q, Q: 'q>(&'c mut self, query: Q) -> BoxFuture<'c, std::io::Result<()>> - where - Q: RawQuery<'q, Backend = Self::Backend>, - { - query.finish(self); - Box::pin(execute::execute(self)) - } -} +// fn execute<'c, 'q, Q: 'q>(&'c mut self, query: Q) -> BoxFuture<'c, std::io::Result<()>> +// where +// Q: RawQuery<'q, Backend = Self::Backend>, +// { +// query.finish(self); +// Box::pin(execute::execute(self)) +// } +// } diff --git a/src/mariadb/mod.rs b/src/mariadb/mod.rs index 586eb95b..2b46ff09 100644 --- a/src/mariadb/mod.rs +++ b/src/mariadb/mod.rs @@ -1,20 +1,13 @@ -pub mod backend; -pub mod connection; -pub mod protocol; -pub mod query; -pub mod types; +// mod backend; +// mod connection; +mod protocol; +// mod query; +// pub mod types; -// Re-export all the things -pub use connection::{ConnContext, Framed, MariaDbRawConnection}; -pub use protocol::{ - 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 self::{ +// backend::MariaDb, connection::MariaDbRawConnection, query::MariaDbQueryParameters, +// row::MariaDbRow, +// }; -pub use backend::MariaDB; +// 1) Get protocol compiling using io::Buf / io::BufMut +// 2) Switch MariaDbRawConnection to use io::BufStream diff --git a/src/mariadb/protocol/mod.rs b/src/mariadb/protocol/mod.rs index 53a88866..5b6f57a2 100644 --- a/src/mariadb/protocol/mod.rs +++ b/src/mariadb/protocol/mod.rs @@ -4,32 +4,34 @@ // TODO: Handle lengths which are greater than 3 bytes // Either break the packet into several smaller ones, or // return error + // TODO: Handle different Capabilities for server and client + // TODO: Handle when capability is set, but field is None -pub mod decode; -pub mod encode; -pub mod error_codes; -pub mod packets; -pub mod types; +// pub mod decode; +// pub mod encode; +// pub mod error_codes; +// pub mod packets; +// pub mod types; // Re-export all the things -pub use packets::{ - AuthenticationSwitchRequestPacket, ColumnDefPacket, ColumnPacket, ComDebug, ComInitDb, ComPing, - ComProcessKill, ComQuery, ComQuit, ComResetConnection, ComSetOption, ComShutdown, ComSleep, - ComStatistics, ComStmtClose, ComStmtExec, ComStmtFetch, ComStmtPrepare, ComStmtPrepareOk, - ComStmtPrepareResp, ComStmtReset, EofPacket, ErrPacket, HandshakeResponsePacket, - InitialHandshakePacket, OkPacket, PacketHeader, ResultRow, ResultRowBinary, ResultRowText, - ResultSet, SSLRequestPacket, SetOptionOptions, ShutdownOptions, -}; +// pub use packets::{ +// AuthenticationSwitchRequestPacket, ColumnDefPacket, ColumnPacket, ComDebug, ComInitDb, ComPing, +// ComProcessKill, ComQuery, ComQuit, ComResetConnection, ComSetOption, ComShutdown, ComSleep, +// ComStatistics, ComStmtClose, ComStmtExec, ComStmtFetch, ComStmtPrepare, ComStmtPrepareOk, +// ComStmtPrepareResp, ComStmtReset, EofPacket, ErrPacket, HandshakeResponsePacket, +// InitialHandshakePacket, OkPacket, PacketHeader, ResultRow, ResultRowBinary, ResultRowText, +// 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::{ - Capabilities, FieldDetailFlag, FieldType, ProtocolType, ServerStatusFlag, SessionChangeType, - StmtExecFlag, -}; +// pub use types::{ +// Capabilities, FieldDetailFlag, FieldType, ProtocolType, ServerStatusFlag, SessionChangeType, +// StmtExecFlag, +// }; diff --git a/src/mariadb/query.rs b/src/mariadb/query.rs index ce513b0c..6d8adc2a 100644 --- a/src/mariadb/query.rs +++ b/src/mariadb/query.rs @@ -1,56 +1,41 @@ +use super::MariaDb; use crate::{ - mariadb::{protocol::types::ParamFlag, FieldType, MariaDbRawConnection}, - query::RawQuery, + query::QueryParameters, serialize::{IsNull, ToSql}, types::HasSqlType, }; -pub struct MariaDbRawQuery<'q> { - query: &'q str, - types: Vec, - null_bitmap: Vec, - flags: Vec, - buf: Vec, - index: u64, +pub struct MariaDbQueryParameters { + param_types: Vec<(u8, u8)>, + params: Vec, + null: Vec, } -impl<'q> RawQueryQuery<'q> for MariaDbRawQuery<'q> { +impl QueryParameters for MariaDbQueryParameters { type Backend = MariaDb; - fn new(query: &'q str) -> Self { + fn new() -> Self { Self { - query, - types: Vec::with_capacity(4), - null_bitmap: vec![0, 0, 0, 0], - flags: Vec::with_capacity(4), - buf: Vec::with_capacity(32), - index: 0, + param_types: Vec::with_capacity(4), + params: Vec::with_capacity(32), + null: 0, } } - fn bind(mut self, value: T) -> Self + fn bind(&mut self, value: T) where Self: Sized, Self::Backend: HasSqlType, T: ToSql, { - self.types - .push(>::metadata().field_type.0); - self.flags - .push(>::metadata().param_flag.0); + let metadata = >::metadata(); + let index = self.param_types.len(); - match value.to_sql(&mut self.buf) { - IsNull::Yes => { - self.null_bitmap[self.index / 8] = - self.null_bitmap[self.index / 8] & (1 << self.index % 8); - } - IsNull::No => {} + self.param_types + .push((metadata.field_type, metadata.param_flag)); + + if let IsNull::Yes = value.to_sql(&mut self.params) { + self.null[index / 8] = self.null[index / 8] & (1 << index % 8); } - - self } - - fn finish(self, conn: &mut MariaDbRawConnection) { - conn.prepare(self.query); - } -} +} \ No newline at end of file diff --git a/src/mariadb/types/mod.rs b/src/mariadb/types/mod.rs index 8a9eeed0..fef33928 100644 --- a/src/mariadb/types/mod.rs +++ b/src/mariadb/types/mod.rs @@ -1,18 +1,11 @@ -use super::MariaDB; -use crate::{ - mariadb::{protocol::types::ParamFlag, FieldType}, - types::TypeMetadata, -}; +use crate::types::TypeMetadata; +use super::protocol::FieldType; +use super::protocol::ParamFlag; +use super::backend::MariaDb; mod boolean; -pub enum MariaDbTypeFormat { - Text = 0, - Binary = 1, -} - pub struct MariaDbTypeMetadata { - pub format: MariaDbTypeFormat, pub field_type: FieldType, pub param_flag: ParamFlag, } diff --git a/src/postgres/backend.rs b/src/postgres/backend.rs index bf36e2af..5a24ee17 100644 --- a/src/postgres/backend.rs +++ b/src/postgres/backend.rs @@ -1,5 +1,6 @@ use crate::backend::Backend; +#[derive(Debug)] pub struct Postgres; impl Backend for Postgres { @@ -8,6 +9,5 @@ impl Backend for Postgres { type Row = super::PostgresRow; } -// Generates tuple impls for this backend impl_from_sql_row_tuples_for_backend!(Postgres); impl_into_query_parameters_for_backend!(Postgres);