From d332558b25943392f38c68faa4cf785f78011bea Mon Sep 17 00:00:00 2001 From: Austin Bonander Date: Fri, 22 Nov 2019 11:01:51 +0000 Subject: [PATCH] rename `FromSql/ToSql` -> `Decode/Encode` closes #18 --- sqlx-core/src/decode.rs | 18 ++++++++ sqlx-core/src/deserialize.rs | 18 -------- sqlx-core/src/{serialize.rs => encode.rs} | 24 +++++------ sqlx-core/src/lib.rs | 8 ++-- sqlx-core/src/mariadb/query.rs | 6 +-- sqlx-core/src/mariadb/types/binary.rs | 18 ++++---- sqlx-core/src/mariadb/types/boolean.rs | 12 +++--- sqlx-core/src/mariadb/types/character.rs | 18 ++++---- sqlx-core/src/mariadb/types/numeric.rs | 52 +++++++++++------------ sqlx-core/src/postgres/query.rs | 6 +-- sqlx-core/src/postgres/types/binary.rs | 18 ++++---- sqlx-core/src/postgres/types/boolean.rs | 12 +++--- sqlx-core/src/postgres/types/character.rs | 18 ++++---- sqlx-core/src/postgres/types/numeric.rs | 52 +++++++++++------------ sqlx-core/src/postgres/types/uuid.rs | 12 +++--- sqlx-core/src/query.rs | 6 +-- sqlx-core/src/row.rs | 10 ++--- sqlx-core/src/sql.rs | 4 +- 18 files changed, 156 insertions(+), 156 deletions(-) create mode 100644 sqlx-core/src/decode.rs delete mode 100644 sqlx-core/src/deserialize.rs rename sqlx-core/src/{serialize.rs => encode.rs} (68%) diff --git a/sqlx-core/src/decode.rs b/sqlx-core/src/decode.rs new file mode 100644 index 00000000..a9efaf19 --- /dev/null +++ b/sqlx-core/src/decode.rs @@ -0,0 +1,18 @@ +//! Types and traits related to deserializing values from the database. +use crate::{backend::Backend, types::HasSqlType}; + +// TODO: Allow decode to return an error (that can be unified) + +pub trait Decode { + fn decode(raw: Option<&[u8]>) -> Self; +} + +impl Decode for Option +where + DB: Backend + HasSqlType, + T: Decode, +{ + fn decode(raw: Option<&[u8]>) -> Self { + Some(T::decode(Some(raw?))) + } +} diff --git a/sqlx-core/src/deserialize.rs b/sqlx-core/src/deserialize.rs deleted file mode 100644 index 81b4fdbc..00000000 --- a/sqlx-core/src/deserialize.rs +++ /dev/null @@ -1,18 +0,0 @@ -//! Types and traits related to deserializing values from the database. -use crate::{backend::Backend, types::HasSqlType}; - -// TODO: Allow from_sql to return an error (that can be unified) - -pub trait FromSql { - fn from_sql(raw: Option<&[u8]>) -> Self; -} - -impl FromSql for Option -where - DB: Backend + HasSqlType, - T: FromSql, -{ - fn from_sql(raw: Option<&[u8]>) -> Self { - Some(T::from_sql(Some(raw?))) - } -} diff --git a/sqlx-core/src/serialize.rs b/sqlx-core/src/encode.rs similarity index 68% rename from sqlx-core/src/serialize.rs rename to sqlx-core/src/encode.rs index 95f73641..3bf39e84 100644 --- a/sqlx-core/src/serialize.rs +++ b/sqlx-core/src/encode.rs @@ -1,7 +1,7 @@ //! Types and traits related to serializing values for the database. use crate::{backend::Backend, types::HasSqlType}; -/// Annotates the result of [ToSql] to differentiate between an empty value and a null value. +/// Annotates the result of [Encode] to differentiate between an empty value and a null value. pub enum IsNull { /// The value was null (and no data was written to the buffer). Yes, @@ -19,39 +19,39 @@ pub enum IsNull { /// /// When possible, implementations of this trait should prefer using an /// existing implementation, rather than writing to `buf` directly. -pub trait ToSql { +pub trait Encode { /// Writes the value of `self` into `buf` as the expected format /// for the given backend. /// /// The return value indicates if this value should be represented as `NULL`. /// If this is the case, implementations **must not** write anything to `out`. - fn to_sql(&self, buf: &mut Vec) -> IsNull; + fn encode(&self, buf: &mut Vec) -> IsNull; } -/// [ToSql] is implemented for `Option` where `T` implements `ToSql`. An `Option` +/// [Encode] is implemented for `Option` where `T` implements `Encode`. An `Option` /// represents a nullable SQL value. -impl ToSql for Option +impl Encode for Option where DB: Backend + HasSqlType, - T: ToSql, + T: Encode, { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { + fn encode(&self, buf: &mut Vec) -> IsNull { if let Some(self_) = self { - self_.to_sql(buf) + self_.encode(buf) } else { IsNull::Yes } } } -impl ToSql for &'_ T +impl Encode for &'_ T where DB: Backend + HasSqlType, - T: ToSql, + T: Encode, { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { - (*self).to_sql(buf) + fn encode(&self, buf: &mut Vec) -> IsNull { + (*self).encode(buf) } } diff --git a/sqlx-core/src/lib.rs b/sqlx-core/src/lib.rs index 8f336df8..734f81d9 100644 --- a/sqlx-core/src/lib.rs +++ b/sqlx-core/src/lib.rs @@ -9,7 +9,7 @@ pub mod error; mod io; mod backend; -pub mod deserialize; +pub mod decode; #[cfg(any(feature = "postgres", feature = "mariadb"))] mod url; @@ -24,7 +24,7 @@ mod pool; #[macro_use] pub mod query; -pub mod serialize; +pub mod encode; mod sql; pub mod types; @@ -37,12 +37,12 @@ pub use self::{ backend::Backend, compiled::CompiledSql, connection::Connection, - deserialize::FromSql, + decode::Decode, error::{Error, Result}, executor::Executor, pool::Pool, row::{FromSqlRow, Row}, - serialize::ToSql, + encode::Encode, sql::{query, SqlQuery}, types::HasSqlType, }; diff --git a/sqlx-core/src/mariadb/query.rs b/sqlx-core/src/mariadb/query.rs index 3b936d8a..610005bc 100644 --- a/sqlx-core/src/mariadb/query.rs +++ b/sqlx-core/src/mariadb/query.rs @@ -2,7 +2,7 @@ use super::MariaDb; use crate::{ mariadb::types::MariaDbTypeMetadata, query::QueryParameters, - serialize::{IsNull, ToSql}, + encode::{IsNull, Encode}, types::HasSqlType, }; @@ -27,14 +27,14 @@ impl QueryParameters for MariaDbQueryParameters { where Self: Sized, Self::Backend: HasSqlType, - T: ToSql, + T: Encode, { let metadata = >::metadata(); let index = self.param_types.len(); self.param_types.push(metadata); - if let IsNull::Yes = value.to_sql(&mut self.params) { + if let IsNull::Yes = value.encode(&mut self.params) { self.null[index / 8] = self.null[index / 8] & (1 << index % 8); } } diff --git a/sqlx-core/src/mariadb/types/binary.rs b/sqlx-core/src/mariadb/types/binary.rs index 67a2ceb0..d235f897 100644 --- a/sqlx-core/src/mariadb/types/binary.rs +++ b/sqlx-core/src/mariadb/types/binary.rs @@ -3,8 +3,8 @@ use crate::{ protocol::{FieldType, ParameterFlag}, types::MariaDbTypeMetadata, }, - serialize::IsNull, - FromSql, HasSqlType, MariaDb, ToSql, + encode::IsNull, + Decode, HasSqlType, MariaDb, Encode, }; impl HasSqlType<[u8]> for MariaDb { @@ -22,21 +22,21 @@ impl HasSqlType> for MariaDb { } } -impl ToSql for [u8] { - fn to_sql(&self, buf: &mut Vec) -> IsNull { +impl Encode for [u8] { + fn encode(&self, buf: &mut Vec) -> IsNull { buf.extend_from_slice(self); IsNull::No } } -impl ToSql for Vec { - fn to_sql(&self, buf: &mut Vec) -> IsNull { - <[u8] as ToSql>::to_sql(self, buf) +impl Encode for Vec { + fn encode(&self, buf: &mut Vec) -> IsNull { + <[u8] as Encode>::to_sql(self, buf) } } -impl FromSql for Vec { - fn from_sql(raw: Option<&[u8]>) -> Self { +impl Decode for Vec { + fn decode(raw: Option<&[u8]>) -> Self { raw.unwrap().into() } } diff --git a/sqlx-core/src/mariadb/types/boolean.rs b/sqlx-core/src/mariadb/types/boolean.rs index 52e9c6e1..132b9b21 100644 --- a/sqlx-core/src/mariadb/types/boolean.rs +++ b/sqlx-core/src/mariadb/types/boolean.rs @@ -1,8 +1,8 @@ use super::{MariaDb, MariaDbTypeMetadata}; use crate::{ - deserialize::FromSql, + decode::Decode, mariadb::protocol::{FieldType, ParameterFlag}, - serialize::{IsNull, ToSql}, + encode::{IsNull, Encode}, types::HasSqlType, }; @@ -16,18 +16,18 @@ impl HasSqlType for MariaDb { } } -impl ToSql for bool { +impl Encode for bool { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { + fn encode(&self, buf: &mut Vec) -> IsNull { buf.push(*self as u8); IsNull::No } } -impl FromSql for bool { +impl Decode for bool { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { + fn decode(buf: Option<&[u8]>) -> Self { // TODO: Handle optionals buf.unwrap()[0] != 0 } diff --git a/sqlx-core/src/mariadb/types/character.rs b/sqlx-core/src/mariadb/types/character.rs index 41ad892b..a0b6154f 100644 --- a/sqlx-core/src/mariadb/types/character.rs +++ b/sqlx-core/src/mariadb/types/character.rs @@ -1,8 +1,8 @@ use super::{MariaDb, MariaDbTypeMetadata}; use crate::{ - deserialize::FromSql, + decode::Decode, mariadb::protocol::{FieldType, ParameterFlag}, - serialize::{IsNull, ToSql}, + encode::{IsNull, Encode}, types::HasSqlType, }; use std::str; @@ -25,25 +25,25 @@ impl HasSqlType for MariaDb { } } -impl ToSql for str { +impl Encode for str { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { + fn encode(&self, buf: &mut Vec) -> IsNull { buf.extend_from_slice(self.as_bytes()); IsNull::No } } -impl ToSql for String { +impl Encode for String { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { - >::to_sql(self.as_str(), buf) + fn encode(&self, buf: &mut Vec) -> IsNull { + >::to_sql(self.as_str(), buf) } } -impl FromSql for String { +impl Decode for String { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { + fn decode(buf: Option<&[u8]>) -> Self { // TODO: Handle nulls let s = if cfg!(debug_assertions) { diff --git a/sqlx-core/src/mariadb/types/numeric.rs b/sqlx-core/src/mariadb/types/numeric.rs index 300849eb..e85e192d 100644 --- a/sqlx-core/src/mariadb/types/numeric.rs +++ b/sqlx-core/src/mariadb/types/numeric.rs @@ -1,8 +1,8 @@ use super::{MariaDb, MariaDbTypeMetadata}; use crate::{ - deserialize::FromSql, + decode::Decode, mariadb::protocol::{FieldType, ParameterFlag}, - serialize::{IsNull, ToSql}, + encode::{IsNull, Encode}, types::HasSqlType, }; use byteorder::{BigEndian, ByteOrder}; @@ -18,18 +18,18 @@ impl HasSqlType for MariaDb { } } -impl ToSql for i16 { +impl Encode for i16 { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { + fn encode(&self, buf: &mut Vec) -> IsNull { buf.extend_from_slice(&self.to_be_bytes()); IsNull::No } } -impl FromSql for i16 { +impl Decode for i16 { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { + fn decode(buf: Option<&[u8]>) -> Self { BigEndian::read_i16(buf.unwrap()) } } @@ -45,18 +45,18 @@ impl HasSqlType for MariaDb { } } -impl ToSql for i32 { +impl Encode for i32 { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { + fn encode(&self, buf: &mut Vec) -> IsNull { buf.extend_from_slice(&self.to_be_bytes()); IsNull::No } } -impl FromSql for i32 { +impl Decode for i32 { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { + fn decode(buf: Option<&[u8]>) -> Self { BigEndian::read_i32(buf.unwrap()) } } @@ -72,18 +72,18 @@ impl HasSqlType for MariaDb { } } -impl ToSql for i64 { +impl Encode for i64 { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { + fn encode(&self, buf: &mut Vec) -> IsNull { buf.extend_from_slice(&self.to_be_bytes()); IsNull::No } } -impl FromSql for i64 { +impl Decode for i64 { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { + fn decode(buf: Option<&[u8]>) -> Self { BigEndian::read_i64(buf.unwrap()) } } @@ -99,17 +99,17 @@ impl HasSqlType for MariaDb { } } -impl ToSql for f32 { +impl Encode for f32 { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { - >::to_sql(&(self.to_bits() as i32), buf) + fn encode(&self, buf: &mut Vec) -> IsNull { + >::to_sql(&(self.to_bits() as i32), buf) } } -impl FromSql for f32 { +impl Decode for f32 { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { - f32::from_bits(>::from_sql(buf) as u32) + fn decode(buf: Option<&[u8]>) -> Self { + f32::from_bits(>::from_sql(buf) as u32) } } @@ -124,16 +124,16 @@ impl HasSqlType for MariaDb { } } -impl ToSql for f64 { +impl Encode for f64 { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { - >::to_sql(&(self.to_bits() as i64), buf) + fn encode(&self, buf: &mut Vec) -> IsNull { + >::to_sql(&(self.to_bits() as i64), buf) } } -impl FromSql for f64 { +impl Decode for f64 { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { - f64::from_bits(>::from_sql(buf) as u64) + fn decode(buf: Option<&[u8]>) -> Self { + f64::from_bits(>::from_sql(buf) as u64) } } diff --git a/sqlx-core/src/postgres/query.rs b/sqlx-core/src/postgres/query.rs index cbbb6304..4ec4e690 100644 --- a/sqlx-core/src/postgres/query.rs +++ b/sqlx-core/src/postgres/query.rs @@ -2,7 +2,7 @@ use super::Postgres; use crate::{ io::BufMut, query::QueryParameters, - serialize::{IsNull, ToSql}, + encode::{IsNull, Encode}, types::HasSqlType, }; use byteorder::{BigEndian, ByteOrder, NetworkEndian}; @@ -30,7 +30,7 @@ impl QueryParameters for PostgresQueryParameters { where Self: Sized, Self::Backend: HasSqlType, - T: ToSql, + T: Encode, { // TODO: When/if we receive types that do _not_ support BINARY, we need to check here // TODO: There is no need to be explicit unless we are expecting mixed BINARY / TEXT @@ -40,7 +40,7 @@ impl QueryParameters for PostgresQueryParameters { let pos = self.buf.len(); self.buf.put_i32::(0); - let len = if let IsNull::No = value.to_sql(&mut self.buf) { + let len = if let IsNull::No = value.encode(&mut self.buf) { (self.buf.len() - pos - 4) as i32 } else { // Write a -1 for the len to indicate NULL diff --git a/sqlx-core/src/postgres/types/binary.rs b/sqlx-core/src/postgres/types/binary.rs index d28a1711..750a08d1 100644 --- a/sqlx-core/src/postgres/types/binary.rs +++ b/sqlx-core/src/postgres/types/binary.rs @@ -1,7 +1,7 @@ use crate::{ postgres::types::{PostgresTypeFormat, PostgresTypeMetadata}, - serialize::IsNull, - FromSql, HasSqlType, Postgres, ToSql, + encode::IsNull, + Decode, HasSqlType, Postgres, Encode, }; impl HasSqlType<[u8]> for Postgres { @@ -20,21 +20,21 @@ impl HasSqlType> for Postgres { } } -impl ToSql for [u8] { - fn to_sql(&self, buf: &mut Vec) -> IsNull { +impl Encode for [u8] { + fn encode(&self, buf: &mut Vec) -> IsNull { buf.extend_from_slice(self); IsNull::No } } -impl ToSql for Vec { - fn to_sql(&self, buf: &mut Vec) -> IsNull { - <[u8] as ToSql>::to_sql(self, buf) +impl Encode for Vec { + fn encode(&self, buf: &mut Vec) -> IsNull { + <[u8] as Encode>::to_sql(self, buf) } } -impl FromSql for Vec { - fn from_sql(raw: Option<&[u8]>) -> Self { +impl Decode for Vec { + fn decode(raw: Option<&[u8]>) -> Self { raw.unwrap().into() } } diff --git a/sqlx-core/src/postgres/types/boolean.rs b/sqlx-core/src/postgres/types/boolean.rs index 6c9b57d3..d346bd6f 100644 --- a/sqlx-core/src/postgres/types/boolean.rs +++ b/sqlx-core/src/postgres/types/boolean.rs @@ -1,7 +1,7 @@ use super::{Postgres, PostgresTypeFormat, PostgresTypeMetadata}; use crate::{ - deserialize::FromSql, - serialize::{IsNull, ToSql}, + decode::Decode, + encode::{IsNull, Encode}, types::HasSqlType, }; @@ -15,18 +15,18 @@ impl HasSqlType for Postgres { } } -impl ToSql for bool { +impl Encode for bool { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { + fn encode(&self, buf: &mut Vec) -> IsNull { buf.push(*self as u8); IsNull::No } } -impl FromSql for bool { +impl Decode for bool { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { + fn decode(buf: Option<&[u8]>) -> Self { // TODO: Handle optionals buf.unwrap()[0] != 0 } diff --git a/sqlx-core/src/postgres/types/character.rs b/sqlx-core/src/postgres/types/character.rs index b3f63db3..c610ef05 100644 --- a/sqlx-core/src/postgres/types/character.rs +++ b/sqlx-core/src/postgres/types/character.rs @@ -1,7 +1,7 @@ use super::{Postgres, PostgresTypeFormat, PostgresTypeMetadata}; use crate::{ - deserialize::FromSql, - serialize::{IsNull, ToSql}, + decode::Decode, + encode::{IsNull, Encode}, types::HasSqlType, }; use std::str; @@ -24,25 +24,25 @@ impl HasSqlType for Postgres { } } -impl ToSql for str { +impl Encode for str { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { + fn encode(&self, buf: &mut Vec) -> IsNull { buf.extend_from_slice(self.as_bytes()); IsNull::No } } -impl ToSql for String { +impl Encode for String { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { - >::to_sql(self.as_str(), buf) + fn encode(&self, buf: &mut Vec) -> IsNull { + >::to_sql(self.as_str(), buf) } } -impl FromSql for String { +impl Decode for String { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { + fn decode(buf: Option<&[u8]>) -> Self { // TODO: Handle nulls let s = if cfg!(debug_assertions) { diff --git a/sqlx-core/src/postgres/types/numeric.rs b/sqlx-core/src/postgres/types/numeric.rs index 35b15f1b..e8f7d3ed 100644 --- a/sqlx-core/src/postgres/types/numeric.rs +++ b/sqlx-core/src/postgres/types/numeric.rs @@ -1,7 +1,7 @@ use super::{Postgres, PostgresTypeFormat, PostgresTypeMetadata}; use crate::{ - deserialize::FromSql, - serialize::{IsNull, ToSql}, + decode::Decode, + encode::{IsNull, Encode}, types::HasSqlType, }; use byteorder::{BigEndian, ByteOrder}; @@ -17,18 +17,18 @@ impl HasSqlType for Postgres { } } -impl ToSql for i16 { +impl Encode for i16 { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { + fn encode(&self, buf: &mut Vec) -> IsNull { buf.extend_from_slice(&self.to_be_bytes()); IsNull::No } } -impl FromSql for i16 { +impl Decode for i16 { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { + fn decode(buf: Option<&[u8]>) -> Self { BigEndian::read_i16(buf.unwrap()) } } @@ -44,18 +44,18 @@ impl HasSqlType for Postgres { } } -impl ToSql for i32 { +impl Encode for i32 { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { + fn encode(&self, buf: &mut Vec) -> IsNull { buf.extend_from_slice(&self.to_be_bytes()); IsNull::No } } -impl FromSql for i32 { +impl Decode for i32 { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { + fn decode(buf: Option<&[u8]>) -> Self { BigEndian::read_i32(buf.unwrap()) } } @@ -71,18 +71,18 @@ impl HasSqlType for Postgres { } } -impl ToSql for i64 { +impl Encode for i64 { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { + fn encode(&self, buf: &mut Vec) -> IsNull { buf.extend_from_slice(&self.to_be_bytes()); IsNull::No } } -impl FromSql for i64 { +impl Decode for i64 { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { + fn decode(buf: Option<&[u8]>) -> Self { BigEndian::read_i64(buf.unwrap()) } } @@ -98,17 +98,17 @@ impl HasSqlType for Postgres { } } -impl ToSql for f32 { +impl Encode for f32 { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { - >::to_sql(&(self.to_bits() as i32), buf) + fn encode(&self, buf: &mut Vec) -> IsNull { + >::to_sql(&(self.to_bits() as i32), buf) } } -impl FromSql for f32 { +impl Decode for f32 { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { - f32::from_bits(>::from_sql(buf) as u32) + fn decode(buf: Option<&[u8]>) -> Self { + f32::from_bits(>::from_sql(buf) as u32) } } @@ -123,16 +123,16 @@ impl HasSqlType for Postgres { } } -impl ToSql for f64 { +impl Encode for f64 { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { - >::to_sql(&(self.to_bits() as i64), buf) + fn encode(&self, buf: &mut Vec) -> IsNull { + >::to_sql(&(self.to_bits() as i64), buf) } } -impl FromSql for f64 { +impl Decode for f64 { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { - f64::from_bits(>::from_sql(buf) as u64) + fn decode(buf: Option<&[u8]>) -> Self { + f64::from_bits(>::from_sql(buf) as u64) } } diff --git a/sqlx-core/src/postgres/types/uuid.rs b/sqlx-core/src/postgres/types/uuid.rs index 4e5b9d4c..dc3ebb41 100644 --- a/sqlx-core/src/postgres/types/uuid.rs +++ b/sqlx-core/src/postgres/types/uuid.rs @@ -2,8 +2,8 @@ use uuid::Uuid; use super::{Postgres, PostgresTypeFormat, PostgresTypeMetadata}; use crate::{ - deserialize::FromSql, - serialize::{IsNull, ToSql}, + decode::Decode, + encode::{IsNull, Encode}, types::HasSqlType, }; @@ -17,18 +17,18 @@ impl HasSqlType for Postgres { } } -impl ToSql for Uuid { +impl Encode for Uuid { #[inline] - fn to_sql(&self, buf: &mut Vec) -> IsNull { + fn encode(&self, buf: &mut Vec) -> IsNull { buf.extend_from_slice(self.as_bytes()); IsNull::No } } -impl FromSql for Uuid { +impl Decode for Uuid { #[inline] - fn from_sql(buf: Option<&[u8]>) -> Self { + fn decode(buf: Option<&[u8]>) -> Self { // TODO: Handle optionals, error Uuid::from_slice(buf.unwrap()).unwrap() } diff --git a/sqlx-core/src/query.rs b/sqlx-core/src/query.rs index 88458590..02860bd0 100644 --- a/sqlx-core/src/query.rs +++ b/sqlx-core/src/query.rs @@ -1,4 +1,4 @@ -use crate::{backend::Backend, serialize::ToSql, types::HasSqlType}; +use crate::{backend::Backend, encode::Encode, types::HasSqlType}; pub trait QueryParameters: Send { type Backend: Backend; @@ -10,7 +10,7 @@ pub trait QueryParameters: Send { fn bind(&mut self, value: T) where Self::Backend: HasSqlType, - T: ToSql; + T: Encode; } pub trait IntoQueryParameters @@ -26,7 +26,7 @@ macro_rules! impl_into_query_parameters { impl<$($T,)+> crate::query::IntoQueryParameters<$B> for ($($T,)+) where $($B: crate::types::HasSqlType<$T>,)+ - $($T: crate::serialize::ToSql<$B>,)+ + $($T: crate::encode::Encode<$B>,)+ { fn into_params(self) -> <$B as crate::backend::Backend>::QueryParameters { let mut params = <<$B as crate::backend::Backend>::QueryParameters diff --git a/sqlx-core/src/row.rs b/sqlx-core/src/row.rs index 1ebfe283..484c636c 100644 --- a/sqlx-core/src/row.rs +++ b/sqlx-core/src/row.rs @@ -1,4 +1,4 @@ -use crate::{backend::Backend, deserialize::FromSql, types::HasSqlType}; +use crate::{backend::Backend, decode::Decode, types::HasSqlType}; pub trait Row: Send { type Backend: Backend; @@ -13,9 +13,9 @@ pub trait Row: Send { fn get(&self, index: usize) -> T where Self::Backend: HasSqlType, - T: FromSql, + T: Decode, { - T::from_sql(self.get_raw(index)) + T::decode(self.get_raw(index)) } } @@ -26,7 +26,7 @@ pub trait FromSqlRow { impl FromSqlRow for T where DB: Backend + HasSqlType, - T: FromSql, + T: Decode, { #[inline] fn from_row>(row: R) -> Self { @@ -40,7 +40,7 @@ macro_rules! impl_from_sql_row_tuple { impl<$($T,)+> crate::row::FromSqlRow<$B> for ($($T,)+) where $($B: crate::types::HasSqlType<$T>,)+ - $($T: crate::deserialize::FromSql<$B>,)+ + $($T: crate::decode::Decode<$B>,)+ { #[inline] fn from_row>(row: R) -> Self { diff --git a/sqlx-core/src/sql.rs b/sqlx-core/src/sql.rs index 27b1ce0a..139b85be 100644 --- a/sqlx-core/src/sql.rs +++ b/sqlx-core/src/sql.rs @@ -1,6 +1,6 @@ use crate::{ backend::Backend, error::Error, executor::Executor, query::QueryParameters, row::FromSqlRow, - serialize::ToSql, types::HasSqlType, + encode::Encode, types::HasSqlType, }; use futures_core::{future::BoxFuture, stream::BoxStream}; @@ -26,7 +26,7 @@ where pub fn bind(mut self, value: T) -> Self where DB: HasSqlType, - T: ToSql, + T: Encode, { self.params.bind(value); self