mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-29 21:00:54 +00:00
feat(mysql): add MySqlRawValue
- renamed from MySqlValue (from master)
This commit is contained in:
parent
8925d2be0a
commit
6dfc91daf7
49
sqlx-mysql/src/value.rs
Normal file
49
sqlx-mysql/src/value.rs
Normal file
@ -0,0 +1,49 @@
|
||||
use std::str::from_utf8;
|
||||
|
||||
use bytes::Bytes;
|
||||
use sqlx_core::decode::{Error as DecodeError, Result as DecodeResult};
|
||||
use sqlx_core::{Decode, Runtime};
|
||||
|
||||
use crate::MySql;
|
||||
|
||||
/// The format of a raw SQL value for MySQL.
|
||||
///
|
||||
/// MySQL returns values in [`Text`] format for unprepared queries and in [`Binary`]
|
||||
/// format for prepared queries. There is no facility to request a different format.
|
||||
///
|
||||
#[derive(Debug, PartialEq, Copy, Clone)]
|
||||
pub enum MySqlRawValueFormat {
|
||||
Binary,
|
||||
Text,
|
||||
}
|
||||
|
||||
/// The raw representation of a SQL value for MySQL.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MySqlRawValue<'r> {
|
||||
value: Option<&'r Bytes>,
|
||||
format: MySqlRawValueFormat,
|
||||
}
|
||||
|
||||
// 'r: row
|
||||
impl<'r> MySqlRawValue<'r> {
|
||||
/// Returns the format of this value.
|
||||
pub const fn format(&self) -> MySqlRawValueFormat {
|
||||
self.format
|
||||
}
|
||||
|
||||
/// Returns the underlying byte view of this value as a byte slice.
|
||||
pub fn as_bytes(&self) -> DecodeResult<&'r [u8]> {
|
||||
self.value.map(|bytes| &**bytes).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))
|
||||
}
|
||||
|
||||
/// Decode this value into the target type.
|
||||
pub fn decode<T: Decode<'r, MySql, Rt>, Rt: Runtime>(self) -> DecodeResult<T> {
|
||||
<T as Decode<'r, MySql, Rt>>::decode(self)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user