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 {
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);

View file

@ -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,
};

View file

@ -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<std::io::Result<Self>> {
Box::pin(MariaDbRawConnection::establish(url))
}
// #[inline]
// fn establish(url: &str) -> BoxFuture<std::io::Result<Self>> {
// 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))
// }
// }

View file

@ -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

View file

@ -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,
// };

View file

@ -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<u8>,
null_bitmap: Vec<u8>,
flags: Vec<u8>,
buf: Vec<u8>,
index: u64,
pub struct MariaDbQueryParameters {
param_types: Vec<(u8, u8)>,
params: Vec<u8>,
null: Vec<u8>,
}
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<T>(mut self, value: T) -> Self
fn bind<T>(&mut self, value: T)
where
Self: Sized,
Self::Backend: HasSqlType<T>,
T: ToSql<Self::Backend>,
{
self.types
.push(<MariaDb as HasSqlType<T>>::metadata().field_type.0);
self.flags
.push(<MariaDb as HasSqlType<T>>::metadata().param_flag.0);
let metadata = <MariaDb as HasSqlType<T>>::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);
}
}
}

View file

@ -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,
}

View file

@ -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);