mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-29 21:00:54 +00:00
feat(mysql): expand MySqlTypeInfo, MySqlTypeId
This commit is contained in:
parent
d143a790af
commit
f348a8f2ac
@ -1,5 +1,7 @@
|
||||
use crate::protocol::{ColumnDefinition, ColumnFlags};
|
||||
|
||||
/// A unique identifier for a MySQL data type.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(
|
||||
any(feature = "offline", feature = "serde"),
|
||||
derive(serde::Serialize, serde::Deserialize)
|
||||
@ -9,18 +11,32 @@ pub struct MySqlTypeId(u8, u8);
|
||||
// a flag byte which has the highest bit set to indicate the type is unsigned
|
||||
const UNSIGNED: u8 = 0x80;
|
||||
|
||||
impl MySqlTypeId {
|
||||
pub(crate) const fn new(def: &ColumnDefinition) -> Self {
|
||||
Self(def.ty, if def.flags.contains(ColumnFlags::UNSIGNED) { UNSIGNED } else { 0 })
|
||||
}
|
||||
|
||||
pub(crate) const fn ty(self) -> u8 {
|
||||
self.0
|
||||
}
|
||||
|
||||
pub(crate) const fn flags(self) -> u8 {
|
||||
self.1
|
||||
}
|
||||
}
|
||||
|
||||
impl MySqlTypeId {
|
||||
/// Returns `true` if this is the `NULL` type.
|
||||
///
|
||||
/// For MySQL, this occurs in types from parameters or when `NULL` is
|
||||
/// directly used in an expression by itself, such as `SELECT NULL`.
|
||||
///
|
||||
pub const fn is_null(&self) -> bool {
|
||||
pub(crate) const fn is_null(&self) -> bool {
|
||||
matches!(*self, MySqlTypeId::NULL)
|
||||
}
|
||||
|
||||
/// Returns `true` if this is an integer data type.
|
||||
pub const fn is_integer(&self) -> bool {
|
||||
pub(crate) const fn is_integer(&self) -> bool {
|
||||
matches!(
|
||||
*self,
|
||||
MySqlTypeId::TINYINT
|
||||
@ -36,8 +52,13 @@ impl MySqlTypeId {
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns `true` if this is an unsigned data type.
|
||||
pub(crate) const fn is_unsigned(&self) -> bool {
|
||||
self.1 == UNSIGNED
|
||||
}
|
||||
|
||||
/// Returns the name for this MySQL data type.
|
||||
pub const fn name(&self) -> &'static str {
|
||||
pub(crate) const fn name(&self) -> &'static str {
|
||||
match *self {
|
||||
Self::NULL => "NULL",
|
||||
|
||||
|
||||
@ -1,4 +1,7 @@
|
||||
use crate::MySqlTypeId;
|
||||
use sqlx_core::TypeInfo;
|
||||
|
||||
use crate::protocol::ColumnDefinition;
|
||||
use crate::{MySql, MySqlTypeId};
|
||||
|
||||
/// Provides information about a MySQL type.
|
||||
#[derive(Debug, Clone)]
|
||||
@ -8,11 +11,16 @@ use crate::MySqlTypeId;
|
||||
)]
|
||||
pub struct MySqlTypeInfo {
|
||||
id: MySqlTypeId,
|
||||
flags: u16,
|
||||
charset: u8,
|
||||
charset: u16,
|
||||
|
||||
// [max_size] for integer types, this is (M) in BIT(M) or TINYINT(M)
|
||||
max_size: u8,
|
||||
max_size: u32,
|
||||
}
|
||||
|
||||
impl MySqlTypeInfo {
|
||||
pub(crate) const fn new(def: &ColumnDefinition) -> Self {
|
||||
Self { id: MySqlTypeId::new(def), charset: def.charset, max_size: def.max_size }
|
||||
}
|
||||
}
|
||||
|
||||
impl MySqlTypeInfo {
|
||||
@ -20,18 +28,20 @@ impl MySqlTypeInfo {
|
||||
pub const fn id(&self) -> MySqlTypeId {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if this is the `NULL` type.
|
||||
///
|
||||
/// For MySQL, this occurs in types from parameters or when `NULL` is
|
||||
/// directly used in an expression by itself, such as `SELECT NULL`.
|
||||
///
|
||||
pub const fn is_null(&self) -> bool {
|
||||
self.id().is_null()
|
||||
impl TypeInfo for MySqlTypeInfo {
|
||||
type Database = MySql;
|
||||
|
||||
fn id(&self) -> MySqlTypeId {
|
||||
self.id()
|
||||
}
|
||||
|
||||
/// Returns the name for this MySQL data type.
|
||||
pub const fn name(&self) -> &'static str {
|
||||
self.id().name()
|
||||
fn is_unknown(&self) -> bool {
|
||||
self.id.is_null()
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
self.id.name()
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user