Merge pull request #1018 from BastiDood/str-to-sql-refactor

refactor(types): improve readability of `impl ToSql for &str`
This commit is contained in:
Steven Fackler 2023-04-07 10:19:26 -04:00 committed by GitHub
commit 8b9b5d0388
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1012,28 +1012,20 @@ impl ToSql for Vec<u8> {
impl<'a> ToSql for &'a str { impl<'a> ToSql for &'a str {
fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> { fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
match *ty { match ty.name() {
ref ty if ty.name() == "ltree" => types::ltree_to_sql(self, w), "ltree" => types::ltree_to_sql(self, w),
ref ty if ty.name() == "lquery" => types::lquery_to_sql(self, w), "lquery" => types::lquery_to_sql(self, w),
ref ty if ty.name() == "ltxtquery" => types::ltxtquery_to_sql(self, w), "ltxtquery" => types::ltxtquery_to_sql(self, w),
_ => types::text_to_sql(self, w), _ => types::text_to_sql(self, w),
} }
Ok(IsNull::No) Ok(IsNull::No)
} }
fn accepts(ty: &Type) -> bool { fn accepts(ty: &Type) -> bool {
match *ty { matches!(
Type::VARCHAR | Type::TEXT | Type::BPCHAR | Type::NAME | Type::UNKNOWN => true, *ty,
ref ty Type::VARCHAR | Type::TEXT | Type::BPCHAR | Type::NAME | Type::UNKNOWN
if (ty.name() == "citext" ) || matches!(ty.name(), "citext" | "ltree" | "lquery" | "ltxtquery")
|| ty.name() == "ltree"
|| ty.name() == "lquery"
|| ty.name() == "ltxtquery") =>
{
true
}
_ => false,
}
} }
to_sql_checked!(); to_sql_checked!();