From b6b24cd927438c4cfc3e8fa738d12cf54321d52b Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Tue, 23 Feb 2021 16:11:10 -0800 Subject: [PATCH] feat(mysql): impl Type for bool --- sqlx-mysql/src/protocol/row.rs | 2 -- sqlx-mysql/src/raw_value.rs | 14 +++++++++++++- sqlx-mysql/src/types/bool.rs | 15 +++++++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/sqlx-mysql/src/protocol/row.rs b/sqlx-mysql/src/protocol/row.rs index 02c2ef1a..039b6609 100644 --- a/sqlx-mysql/src/protocol/row.rs +++ b/sqlx-mysql/src/protocol/row.rs @@ -8,8 +8,6 @@ use crate::{MySqlColumn, MySqlRawValueFormat, MySqlTypeId}; #[derive(Debug)] pub(crate) struct Row { pub(crate) format: MySqlRawValueFormat, - - // TODO: profile storage + byte ranges pub(crate) values: Vec>, } diff --git a/sqlx-mysql/src/raw_value.rs b/sqlx-mysql/src/raw_value.rs index 141507c7..876cf837 100644 --- a/sqlx-mysql/src/raw_value.rs +++ b/sqlx-mysql/src/raw_value.rs @@ -1,6 +1,8 @@ +use std::convert::TryFrom; use std::str::from_utf8; use bytes::Bytes; +use bytestring::ByteString; use sqlx_core::decode::{Error as DecodeError, Result as DecodeResult}; use sqlx_core::Decode; @@ -37,11 +39,13 @@ impl<'r> MySqlRawValue<'r> { } /// Returns the type information for this value. - pub fn type_info(&self) -> &'r MySqlTypeInfo { + #[must_use] + pub const fn type_info(&self) -> &'r MySqlTypeInfo { self.type_info } /// Returns the format of this value. + #[must_use] pub const fn format(&self) -> MySqlRawValueFormat { self.format } @@ -51,12 +55,20 @@ impl<'r> MySqlRawValue<'r> { self.value.map(|bytes| &**bytes).ok_or(DecodeError::UnexpectedNull) } + pub(crate) fn as_shared_bytes(&self) -> DecodeResult { + self.value.cloned().ok_or(DecodeError::UnexpectedNull) + } + /// Returns a `&str` slice from the underlying byte view of this value, /// if it contains valid UTF-8. pub fn as_str(&self) -> DecodeResult<&'r str> { self.as_bytes().and_then(|bytes| from_utf8(bytes).map_err(DecodeError::NotUtf8)) } + pub(crate) fn as_shared_str(&self) -> DecodeResult { + ByteString::try_from(self.as_shared_bytes()?).map_err(DecodeError::NotUtf8) + } + /// Decode this value into the target type. pub fn decode>(self) -> DecodeResult { >::decode(self) diff --git a/sqlx-mysql/src/types/bool.rs b/sqlx-mysql/src/types/bool.rs index e23faa05..c96f920e 100644 --- a/sqlx-mysql/src/types/bool.rs +++ b/sqlx-mysql/src/types/bool.rs @@ -1,13 +1,20 @@ -use sqlx_core::{decode, encode}; +use sqlx_core::{decode, encode, Type}; use sqlx_core::{Decode, Encode}; -use crate::{MySql, MySqlOutput, MySqlRawValue, MySqlTypeInfo}; +use crate::{MySql, MySqlOutput, MySqlRawValue, MySqlTypeId, MySqlTypeInfo}; // In MySQL, a boolean is an alias for `TINYINT(1) UNSIGNED` // the functions below delegate functionality to the `u8` impls -// TODO: accepts(ty) -> ty.is_integer() -// TODO: compatible(ty) -> ty.is_integer() +impl Type for bool { + fn type_id() -> MySqlTypeId { + >::type_id() + } + + fn compatible(ty: &MySqlTypeInfo) -> bool { + >::compatible(ty) + } +} impl Encode for bool { fn encode(&self, ty: &MySqlTypeInfo, out: &mut MySqlOutput<'_>) -> encode::Result<()> {