mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-02 15:25:32 +00:00
refactor(macros): adapt to the 0.4.x core refactor
This commit is contained in:
parent
eaa7fba9d4
commit
a54b1267f6
@ -21,6 +21,20 @@ impl MySqlTypeInfo {
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn __type_feature_gate(&self) -> Option<&'static str> {
|
||||
match self.r#type {
|
||||
ColumnType::Date | ColumnType::Time | ColumnType::Timestamp | ColumnType::Datetime => {
|
||||
Some("time")
|
||||
}
|
||||
|
||||
ColumnType::Json => Some("json"),
|
||||
ColumnType::NewDecimal => Some("bigdecimal"),
|
||||
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_column(column: &ColumnDefinition) -> Option<Self> {
|
||||
if column.r#type == ColumnType::Null {
|
||||
None
|
||||
|
@ -147,6 +147,48 @@ impl PgTypeInfo {
|
||||
self.0.kind()
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn __type_feature_gate(&self) -> Option<&'static str> {
|
||||
if [
|
||||
PgTypeInfo::DATE,
|
||||
PgTypeInfo::TIME,
|
||||
PgTypeInfo::TIMESTAMP,
|
||||
PgTypeInfo::TIMESTAMPTZ,
|
||||
PgTypeInfo::DATE_ARRAY,
|
||||
PgTypeInfo::TIME_ARRAY,
|
||||
PgTypeInfo::TIMESTAMP_ARRAY,
|
||||
PgTypeInfo::TIMESTAMPTZ_ARRAY,
|
||||
]
|
||||
.contains(self)
|
||||
{
|
||||
Some("time")
|
||||
} else if [PgTypeInfo::UUID, PgTypeInfo::UUID_ARRAY].contains(self) {
|
||||
Some("uuid")
|
||||
} else if [
|
||||
PgTypeInfo::JSON,
|
||||
PgTypeInfo::JSONB,
|
||||
PgTypeInfo::JSON_ARRAY,
|
||||
PgTypeInfo::JSONB_ARRAY,
|
||||
]
|
||||
.contains(self)
|
||||
{
|
||||
Some("json")
|
||||
} else if [
|
||||
PgTypeInfo::CIDR,
|
||||
PgTypeInfo::INET,
|
||||
PgTypeInfo::CIDR_ARRAY,
|
||||
PgTypeInfo::INET_ARRAY,
|
||||
]
|
||||
.contains(self)
|
||||
{
|
||||
Some("ipnetwork")
|
||||
} else if [PgTypeInfo::NUMERIC, PgTypeInfo::NUMERIC_ARRAY].contains(self) {
|
||||
Some("bigdecimal")
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a `PgTypeInfo` from a type name.
|
||||
///
|
||||
/// The OID for the type will be fetched from Postgres on use of
|
||||
|
@ -53,7 +53,7 @@ macro_rules! impl_database_ext {
|
||||
)*
|
||||
$(
|
||||
$(#[$meta])?
|
||||
_ if sqlx_core::types::TypeInfo::compatible(&<$ty as sqlx_core::types::Type<$database>>::type_info(), &info) => Some(input_ty!($ty $(, $input)?)),
|
||||
_ if <$ty as sqlx_core::decode::Decode<$database>>::accepts(&info) => Some(input_ty!($ty $(, $input)?)),
|
||||
)*
|
||||
_ => None
|
||||
}
|
||||
@ -67,13 +67,13 @@ macro_rules! impl_database_ext {
|
||||
)*
|
||||
$(
|
||||
$(#[$meta])?
|
||||
_ if sqlx_core::types::TypeInfo::compatible(&<$ty as sqlx_core::types::Type<$database>>::type_info(), &info) => return Some(stringify!($ty)),
|
||||
_ if <$ty as sqlx_core::decode::Decode<$database>>::accepts(&info) => return Some(stringify!($ty)),
|
||||
)*
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
fn get_feature_gate($ty_info: &Self::TypeInfo) -> Option<&'static str> {
|
||||
fn get_feature_gate($name: &Self::TypeInfo) -> Option<&'static str> {
|
||||
$get_gate
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ impl_database_ext! {
|
||||
sqlx::types::BigDecimal,
|
||||
},
|
||||
ParamChecking::Weak,
|
||||
feature-types: info => info.type_feature_gate(),
|
||||
feature-types: info => info.__type_feature_gate(),
|
||||
row = sqlx::mysql::MySqlRow,
|
||||
name = "MySQL/MariaDB"
|
||||
name = "MySQL"
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ impl_database_ext! {
|
||||
|
||||
},
|
||||
ParamChecking::Strong,
|
||||
feature-types: info => info.type_feature_gate(),
|
||||
feature-types: info => info.__type_feature_gate(),
|
||||
row = sqlx::postgres::PgRow,
|
||||
name = "PostgreSQL"
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ pub fn quote_args<DB: DatabaseExt>(
|
||||
|
||||
let args_check = if input.checked && DB::PARAM_CHECKING == ParamChecking::Strong {
|
||||
describe
|
||||
.param_types
|
||||
.params
|
||||
.iter()
|
||||
.zip(input.arg_names.iter().zip(&input.arg_exprs))
|
||||
.enumerate()
|
||||
|
@ -17,32 +17,22 @@ pub struct RustColumn {
|
||||
struct DisplayColumn<'a> {
|
||||
// zero-based index, converted to 1-based number
|
||||
idx: usize,
|
||||
name: Option<&'a str>,
|
||||
name: &'a str,
|
||||
}
|
||||
|
||||
impl Display for DisplayColumn<'_> {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
let num = self.idx + 1;
|
||||
|
||||
if let Some(name) = self.name {
|
||||
write!(f, "column #{} ({:?})", num, name)
|
||||
} else {
|
||||
write!(f, "column #{}", num)
|
||||
}
|
||||
write!(f, "column #{} ({:?})", self.idx + 1, self.name)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn columns_to_rust<DB: DatabaseExt>(describe: &Describe<DB>) -> crate::Result<Vec<RustColumn>> {
|
||||
describe
|
||||
.result_columns
|
||||
.columns
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, column)| -> crate::Result<_> {
|
||||
let name = column
|
||||
.name
|
||||
.as_deref()
|
||||
.ok_or_else(|| format!("column at position {} must have a name", i))?;
|
||||
|
||||
let name = &*column.name;
|
||||
let ident = parse_ident(name)?;
|
||||
|
||||
let mut type_ = if let Some(type_info) = &column.type_info {
|
||||
@ -57,7 +47,7 @@ pub fn columns_to_rust<DB: DatabaseExt>(describe: &Describe<DB>) -> crate::Resul
|
||||
feat = feature_gate,
|
||||
col = DisplayColumn {
|
||||
idx: i,
|
||||
name: column.name.as_deref()
|
||||
name: &*column.name
|
||||
}
|
||||
)
|
||||
} else {
|
||||
@ -66,7 +56,7 @@ pub fn columns_to_rust<DB: DatabaseExt>(describe: &Describe<DB>) -> crate::Resul
|
||||
ty = type_info,
|
||||
col = DisplayColumn {
|
||||
idx: i,
|
||||
name: column.name.as_deref()
|
||||
name: &*column.name
|
||||
}
|
||||
)
|
||||
};
|
||||
@ -82,14 +72,14 @@ pub fn columns_to_rust<DB: DatabaseExt>(describe: &Describe<DB>) -> crate::Resul
|
||||
this can happen for columns that are the result of an expression",
|
||||
col = DisplayColumn {
|
||||
idx: i,
|
||||
name: column.name.as_deref()
|
||||
name: &*column.name
|
||||
}
|
||||
),
|
||||
)
|
||||
.to_compile_error()
|
||||
};
|
||||
|
||||
if !column.non_null.unwrap_or(false) {
|
||||
if !column.not_null.unwrap_or(false) {
|
||||
type_ = quote! { Option<#type_> };
|
||||
}
|
||||
|
||||
|
1
sqlx-macros/src/query_macros/input.rs
Normal file
1
sqlx-macros/src/query_macros/input.rs
Normal file
@ -0,0 +1 @@
|
||||
|
1
sqlx-macros/src/query_macros/mod.rs
Normal file
1
sqlx-macros/src/query_macros/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
|
1
sqlx-macros/src/query_macros/query.rs
Normal file
1
sqlx-macros/src/query_macros/query.rs
Normal file
@ -0,0 +1 @@
|
||||
|
@ -12,7 +12,7 @@ impl<T> ResultExt<T> for crate::Result<T> {
|
||||
|
||||
impl<T> ResultExt<T> for crate::Result<Option<T>> {
|
||||
fn try_unwrap_optional(self) -> crate::Result<T> {
|
||||
self?.ok_or_else(|| UnexpectedNullError.into())
|
||||
self?.ok_or_else(|| crate::Error::Decode(UnexpectedNullError.into()))
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user