refactor(macros): adapt to the 0.4.x core refactor

This commit is contained in:
Ryan Leckey 2020-05-26 04:43:18 -07:00
parent eaa7fba9d4
commit a54b1267f6
No known key found for this signature in database
GPG Key ID: BBDFC5595030E7D3
11 changed files with 75 additions and 26 deletions

View File

@ -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

View File

@ -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

View File

@ -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
}
}

View File

@ -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"
}

View File

@ -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"
}

View File

@ -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()

View File

@ -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_> };
}

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -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()))
}
}