diff --git a/sqlx-core/src/mssql/arguments.rs b/sqlx-core/src/mssql/arguments.rs index af5d0adbc..f4ed37e8b 100644 --- a/sqlx-core/src/mssql/arguments.rs +++ b/sqlx-core/src/mssql/arguments.rs @@ -58,18 +58,10 @@ impl MssqlArguments { self.ordinal += arguments.ordinal; self.data.append(&mut arguments.data); } -} -impl<'q> Arguments<'q> for MssqlArguments { - type Database = Mssql; - - fn reserve(&mut self, _additional: usize, size: usize) { - self.data.reserve(size + 10); // est. 4 chars for name, 1 for status, 1 for TYPE_INFO - } - - fn add(&mut self, value: T) + pub(crate) fn add<'q, T>(&mut self, value: T) where - T: 'q + Encode<'q, Self::Database> + Type, + T: Encode<'q, Mssql> + Type, { let ty = value.produces().unwrap_or_else(T::type_info); @@ -109,3 +101,18 @@ impl<'q> Arguments<'q> for MssqlArguments { ty.0.put_value(data, value); // [ParamLenData] } } + +impl<'q> Arguments<'q> for MssqlArguments { + type Database = Mssql; + + fn reserve(&mut self, _additional: usize, size: usize) { + self.data.reserve(size + 10); // est. 4 chars for name, 1 for status, 1 for TYPE_INFO + } + + fn add(&mut self, value: T) + where + T: 'q + Encode<'q, Self::Database> + Type, + { + self.add(value) + } +} diff --git a/sqlx-core/src/mysql/arguments.rs b/sqlx-core/src/mysql/arguments.rs index 94a30f163..4a04561f2 100644 --- a/sqlx-core/src/mysql/arguments.rs +++ b/sqlx-core/src/mysql/arguments.rs @@ -1,5 +1,3 @@ -use std::ops::{Deref, DerefMut}; - use crate::arguments::Arguments; use crate::encode::{Encode, IsNull}; use crate::mysql::{MySql, MySqlTypeInfo}; @@ -13,6 +11,23 @@ pub struct MySqlArguments { pub(crate) null_bitmap: Vec, } +impl MySqlArguments { + pub(crate) fn add<'q, T>(&mut self, value: T) + where + T: Encode<'q, MySql> + Type, + { + let ty = value.produces().unwrap_or_else(T::type_info); + let index = self.types.len(); + + self.types.push(ty); + self.null_bitmap.resize((index / 8) + 1, 0); + + if let IsNull::Yes = value.encode(&mut self.values) { + self.null_bitmap[index / 8] |= (1 << (index % 8)) as u8; + } + } +} + impl<'q> Arguments<'q> for MySqlArguments { type Database = MySql; @@ -25,28 +40,6 @@ impl<'q> Arguments<'q> for MySqlArguments { where T: Encode<'q, Self::Database> + Type, { - let ty = value.produces().unwrap_or_else(T::type_info); - let index = self.types.len(); - - self.types.push(ty); - self.null_bitmap.resize((index / 8) + 1, 0); - - if let IsNull::Yes = value.encode(self) { - self.null_bitmap[index / 8] |= (1 << (index % 8)) as u8; - } - } -} - -impl Deref for MySqlArguments { - type Target = Vec; - - fn deref(&self) -> &Self::Target { - &self.values - } -} - -impl DerefMut for MySqlArguments { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.values + self.add(value) } } diff --git a/sqlx-core/src/mysql/protocol/statement/execute.rs b/sqlx-core/src/mysql/protocol/statement/execute.rs index 2186a927b..e97cc1213 100644 --- a/sqlx-core/src/mysql/protocol/statement/execute.rs +++ b/sqlx-core/src/mysql/protocol/statement/execute.rs @@ -18,7 +18,7 @@ impl<'q> Encode<'_, Capabilities> for Execute<'q> { buf.push(0); // NO_CURSOR buf.extend(&0_u32.to_le_bytes()); // iterations (always 1): int<4> - if !self.arguments.is_empty() { + if !self.arguments.values.is_empty() { buf.extend(&*self.arguments.null_bitmap); buf.push(1); // send type to server diff --git a/sqlx-core/src/postgres/arguments.rs b/sqlx-core/src/postgres/arguments.rs index ed379d14e..9d98470d3 100644 --- a/sqlx-core/src/postgres/arguments.rs +++ b/sqlx-core/src/postgres/arguments.rs @@ -31,6 +31,20 @@ pub struct PgArguments { pub(crate) buffer: PgArgumentBuffer, } +impl PgArguments { + pub(crate) fn add<'q, T>(&mut self, value: T) + where + T: Encode<'q, Postgres> + Type, + { + // remember the type information for this value + self.types + .push(value.produces().unwrap_or_else(T::type_info)); + + // encode the value into our buffer + self.buffer.encode(value); + } +} + impl<'q> Arguments<'q> for PgArguments { type Database = Postgres; @@ -43,12 +57,7 @@ impl<'q> Arguments<'q> for PgArguments { where T: Encode<'q, Self::Database> + Type, { - // remember the type information for this value - self.types - .push(value.produces().unwrap_or_else(T::type_info)); - - // encode the value into our buffer - self.buffer.encode(value); + self.add(value) } } diff --git a/sqlx-core/src/postgres/connection/describe.rs b/sqlx-core/src/postgres/connection/describe.rs index b9aa96511..cfbb1cb69 100644 --- a/sqlx-core/src/postgres/connection/describe.rs +++ b/sqlx-core/src/postgres/connection/describe.rs @@ -5,7 +5,6 @@ use std::sync::Arc; use futures_util::{stream, StreamExt, TryStreamExt}; use hashbrown::HashMap; -use crate::arguments::Arguments; use crate::describe::Column; use crate::error::Error; use crate::ext::ustr::UStr; @@ -22,8 +21,6 @@ impl PgConnection { &mut self, desc: Option, should_fetch: bool, - // columns: &mut Vec, - // column_names: &mut HashMap, ) -> Result<(), Error> { let mut columns = Vec::new(); let mut column_names = HashMap::new(); diff --git a/sqlx-core/src/sqlite/arguments.rs b/sqlx-core/src/sqlite/arguments.rs index e773d2447..7ae9b6fea 100644 --- a/sqlx-core/src/sqlite/arguments.rs +++ b/sqlx-core/src/sqlite/arguments.rs @@ -24,6 +24,17 @@ pub struct SqliteArguments<'q> { pub(crate) values: Vec>, } +impl<'q> SqliteArguments<'q> { + pub(crate) fn add(&mut self, value: T) + where + T: Encode<'q, Sqlite>, + { + if let IsNull::Yes = value.encode(&mut self.values) { + self.values.push(SqliteArgumentValue::Null); + } + } +} + impl<'q> Arguments<'q> for SqliteArguments<'q> { type Database = Sqlite; @@ -33,11 +44,9 @@ impl<'q> Arguments<'q> for SqliteArguments<'q> { fn add(&mut self, value: T) where - T: 'q + Encode<'q, Self::Database>, + T: Encode<'q, Self::Database>, { - if let IsNull::Yes = value.encode(&mut self.values) { - self.values.push(SqliteArgumentValue::Null); - } + self.add(value) } }