feat(mysql): impl Type for bool

This commit is contained in:
Ryan Leckey 2021-02-23 16:11:10 -08:00
parent d8131d3b3d
commit b6b24cd927
No known key found for this signature in database
GPG Key ID: F8AA68C235AB08C9
3 changed files with 24 additions and 7 deletions

View File

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

View File

@ -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<Bytes> {
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> {
ByteString::try_from(self.as_shared_bytes()?).map_err(DecodeError::NotUtf8)
}
/// Decode this value into the target type.
pub fn decode<T: Decode<'r, MySql>>(self) -> DecodeResult<T> {
<T as Decode<'r, MySql>>::decode(self)

View File

@ -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<MySql> for bool {
fn type_id() -> MySqlTypeId {
<u8 as Type<MySql>>::type_id()
}
fn compatible(ty: &MySqlTypeInfo) -> bool {
<u8 as Type<MySql>>::compatible(ty)
}
}
impl Encode<MySql> for bool {
fn encode(&self, ty: &MySqlTypeInfo, out: &mut MySqlOutput<'_>) -> encode::Result<()> {