mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-29 21:00:54 +00:00
feat(mysql): impl Type for bool
This commit is contained in:
parent
d8131d3b3d
commit
b6b24cd927
@ -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>>,
|
||||
}
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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<()> {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user