mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-04-10 22:10:36 +00:00
fix all nits
This commit is contained in:
@@ -18,5 +18,6 @@ tokio = { version = "0.2.0-alpha.4" }
|
||||
url = "2.1.0"
|
||||
|
||||
[features]
|
||||
mariadb = ["sqlx/mariadb"]
|
||||
postgres = ["sqlx/postgres"]
|
||||
uuid = ["sqlx/uuid"]
|
||||
|
||||
11
sqlx-macros/src/backend/mariadb.rs
Normal file
11
sqlx-macros/src/backend/mariadb.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
impl_backend_ext! {
|
||||
sqlx::MariaDb {
|
||||
bool,
|
||||
String | &str,
|
||||
i16,
|
||||
i32,
|
||||
i64,
|
||||
f32,
|
||||
f64
|
||||
}
|
||||
}
|
||||
52
sqlx-macros/src/backend/mod.rs
Normal file
52
sqlx-macros/src/backend/mod.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use sqlx::Backend;
|
||||
|
||||
pub trait BackendExt: Backend {
|
||||
fn param_type_for_id(id: &Self::TypeId) -> Option<&'static str>;
|
||||
|
||||
fn return_type_for_id(id: &Self::TypeId) -> Option<&'static str>;
|
||||
}
|
||||
|
||||
macro_rules! impl_backend_ext {
|
||||
($backend:ty { $($(#[$meta:meta])? $ty:ty $(| $borrowed:ty)?),* }) => {
|
||||
impl $crate::backend::BackendExt for $backend {
|
||||
fn param_type_for_id(id: &Self::TypeId) -> Option<&'static str> {
|
||||
use sqlx::types::TypeMetadata;
|
||||
|
||||
match () {
|
||||
$(
|
||||
$(#[$meta])?
|
||||
_ if <$backend as sqlx::types::HasSqlType<$ty>>::metadata().type_id_eq(id) => Some(borrowed_ty!($ty $(, $borrowed)?)),
|
||||
)*
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
fn return_type_for_id(id: &Self::TypeId) -> Option<&'static str> {
|
||||
use sqlx::types::TypeMetadata;
|
||||
|
||||
match () {
|
||||
$(
|
||||
$(#[$meta])?
|
||||
_ if <$backend as sqlx::types::HasSqlType<$ty>>::metadata().type_id_eq(id) => return Some(stringify!($ty)),
|
||||
)*
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! borrowed_ty {
|
||||
($ty:ty, $borrowed:ty) => {
|
||||
stringify!($borrowed)
|
||||
};
|
||||
($ty:ty) => {
|
||||
stringify!($ty)
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(feature = "postgres")]
|
||||
mod postgres;
|
||||
|
||||
#[cfg(feature = "mariadb")]
|
||||
mod mariadb;
|
||||
13
sqlx-macros/src/backend/postgres.rs
Normal file
13
sqlx-macros/src/backend/postgres.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
impl_backend_ext! {
|
||||
sqlx::Postgres {
|
||||
bool,
|
||||
String | &str,
|
||||
i16,
|
||||
i32,
|
||||
i64,
|
||||
f32,
|
||||
f64,
|
||||
#[cfg(feature = "uuid")]
|
||||
sqlx::types::Uuid
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,10 @@ use url::Url;
|
||||
type Error = Box<dyn std::error::Error>;
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
mod backend;
|
||||
|
||||
use backend::BackendExt;
|
||||
|
||||
struct MacroInput {
|
||||
sql: String,
|
||||
sql_span: Span,
|
||||
@@ -110,7 +114,7 @@ async fn process_sql(input: MacroInput) -> Result<TokenStream> {
|
||||
}
|
||||
}
|
||||
|
||||
async fn process_sql_with<DB: sqlx::Backend>(
|
||||
async fn process_sql_with<DB: BackendExt>(
|
||||
input: MacroInput,
|
||||
conn: sqlx::Connection<DB>,
|
||||
) -> Result<TokenStream>
|
||||
@@ -144,7 +148,7 @@ where
|
||||
get_type_override(expr)
|
||||
.or_else(|| {
|
||||
Some(
|
||||
<DB as sqlx::HasTypeMetadata>::param_type_for_id(type_)?
|
||||
<DB as BackendExt>::param_type_for_id(type_)?
|
||||
.parse::<proc_macro2::TokenStream>()
|
||||
.unwrap(),
|
||||
)
|
||||
@@ -157,12 +161,10 @@ where
|
||||
.result_fields
|
||||
.iter()
|
||||
.map(|column| {
|
||||
Ok(
|
||||
<DB as sqlx::HasTypeMetadata>::return_type_for_id(&column.type_id)
|
||||
.ok_or_else(|| format!("unknown type ID: {}", &column.type_id))?
|
||||
.parse::<proc_macro2::TokenStream>()
|
||||
.unwrap(),
|
||||
)
|
||||
Ok(<DB as BackendExt>::return_type_for_id(&column.type_id)
|
||||
.ok_or_else(|| format!("unknown type ID: {}", &column.type_id))?
|
||||
.parse::<proc_macro2::TokenStream>()
|
||||
.unwrap())
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
|
||||
@@ -64,9 +64,6 @@ pub mod postgres;
|
||||
#[doc(inline)]
|
||||
pub use self::postgres::Postgres;
|
||||
|
||||
#[cfg(feature = "uuid")]
|
||||
pub use uuid::Uuid;
|
||||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
// These types allow the `sqlx_macros::sql!()` macro to polymorphically compare a
|
||||
|
||||
@@ -16,42 +16,12 @@ pub struct MariaDbTypeMetadata {
|
||||
}
|
||||
|
||||
impl HasTypeMetadata for MariaDb {
|
||||
type TypeId = u8;
|
||||
type TypeMetadata = MariaDbTypeMetadata;
|
||||
|
||||
fn param_type_for_id(id: &Self::TypeId) -> Option<&'static str> {
|
||||
Some(match FieldType(*id) {
|
||||
FieldType::MYSQL_TYPE_TINY => "i8",
|
||||
FieldType::MYSQL_TYPE_SHORT => "i16",
|
||||
FieldType::MYSQL_TYPE_LONG => "i32",
|
||||
FieldType::MYSQL_TYPE_LONGLONG => "i64",
|
||||
FieldType::MYSQL_TYPE_VAR_STRING => "&str",
|
||||
FieldType::MYSQL_TYPE_FLOAT => "f32",
|
||||
FieldType::MYSQL_TYPE_DOUBLE => "f64",
|
||||
FieldType::MYSQL_TYPE_BLOB => "&[u8]",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
fn return_type_for_id(id: &Self::TypeId) -> Option<&'static str> {
|
||||
Some(match FieldType(*id) {
|
||||
FieldType::MYSQL_TYPE_TINY => "i8",
|
||||
FieldType::MYSQL_TYPE_SHORT => "i16",
|
||||
FieldType::MYSQL_TYPE_LONG => "i32",
|
||||
FieldType::MYSQL_TYPE_LONGLONG => "i64",
|
||||
FieldType::MYSQL_TYPE_VAR_STRING => "String",
|
||||
FieldType::MYSQL_TYPE_FLOAT => "f32",
|
||||
FieldType::MYSQL_TYPE_DOUBLE => "f64",
|
||||
FieldType::MYSQL_TYPE_BLOB => "Vec<u8>",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeMetadata for MariaDbTypeMetadata {
|
||||
type TypeId = u8;
|
||||
}
|
||||
|
||||
fn type_id(&self) -> &Self::TypeId {
|
||||
&self.field_type.0
|
||||
impl TypeMetadata<u8> for MariaDbTypeMetadata {
|
||||
fn type_id_eq(&self, other: &u8) -> bool {
|
||||
&self.field_type.0 == other
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,56 +57,10 @@ pub struct PostgresTypeMetadata {
|
||||
impl HasTypeMetadata for Postgres {
|
||||
type TypeId = u32;
|
||||
type TypeMetadata = PostgresTypeMetadata;
|
||||
|
||||
fn param_type_for_id(id: &Self::TypeId) -> Option<&'static str> {
|
||||
Some(match id {
|
||||
16 => "bool",
|
||||
1000 => "&[bool]",
|
||||
25 => "&str",
|
||||
1009 => "&[&str]",
|
||||
21 => "i16",
|
||||
1005 => "&[i16]",
|
||||
23 => "i32",
|
||||
1007 => "&[i32]",
|
||||
20 => "i64",
|
||||
1016 => "&[i64]",
|
||||
700 => "f32",
|
||||
1021 => "&[f32]",
|
||||
701 => "f64",
|
||||
1022 => "&[f64]",
|
||||
2950 => "sqlx::Uuid",
|
||||
2951 => "&[sqlx::Uuid]",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
fn return_type_for_id(id: &Self::TypeId) -> Option<&'static str> {
|
||||
Some(match id {
|
||||
16 => "bool",
|
||||
1000 => "Vec<bool>",
|
||||
25 => "String",
|
||||
1009 => "Vec<String>",
|
||||
21 => "i16",
|
||||
1005 => "Vec<i16>",
|
||||
23 => "i32",
|
||||
1007 => "Vec<i32>",
|
||||
20 => "i64",
|
||||
1016 => "Vec<i64>",
|
||||
700 => "f32",
|
||||
1021 => "Vec<f32>",
|
||||
701 => "f64",
|
||||
1022 => "Vec<f64>",
|
||||
2950 => "sqlx::Uuid",
|
||||
2951 => "Vec<sqlx::Uuid>",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeMetadata for PostgresTypeMetadata {
|
||||
type TypeId = u32;
|
||||
|
||||
fn type_id(&self) -> &u32 {
|
||||
&self.oid
|
||||
impl TypeMetadata<u32> for PostgresTypeMetadata {
|
||||
fn type_id_eq(&self, other: &u32) -> bool {
|
||||
&self.oid == other || &self.array_oid == other
|
||||
}
|
||||
}
|
||||
|
||||
30
src/types.rs
30
src/types.rs
@@ -1,28 +1,24 @@
|
||||
#[cfg(feature = "uuid")]
|
||||
pub use uuid::Uuid;
|
||||
|
||||
/// Information about how a backend stores metadata about
|
||||
/// given SQL types.
|
||||
pub trait HasTypeMetadata {
|
||||
/// The actual type used to represent metadata.
|
||||
type TypeMetadata: TypeMetadata<TypeId = Self::TypeId>;
|
||||
type TypeMetadata: TypeMetadata<Self::TypeId>;
|
||||
|
||||
/// The Rust type of the type ID for the backend.
|
||||
/// The Rust type of type identifiers in `DESCRIBE` responses for the SQL backend.
|
||||
type TypeId: Eq;
|
||||
|
||||
/// UNSTABLE: for internal use only
|
||||
#[doc(hidden)]
|
||||
fn param_type_for_id(id: &Self::TypeId) -> Option<&'static str>;
|
||||
|
||||
/// UNSTABLE: for internal use only
|
||||
#[doc(hidden)]
|
||||
fn return_type_for_id(id: &Self::TypeId) -> Option<&'static str>;
|
||||
}
|
||||
|
||||
pub trait TypeMetadata {
|
||||
type TypeId: Eq;
|
||||
|
||||
fn type_id(&self) -> &Self::TypeId;
|
||||
fn type_id_eq(&self, id: &Self::TypeId) -> bool {
|
||||
self.type_id() == id
|
||||
}
|
||||
pub trait TypeMetadata<TypeId: Eq> {
|
||||
/// Return `true` if the given type ID is contained in this metadata.
|
||||
///
|
||||
/// What this means depends on the backend:
|
||||
///
|
||||
/// * For Postgres, this should return true if the type ID or array type ID matches.
|
||||
/// * For MySQL (and likely all other backends) this should just compare the type IDs.
|
||||
fn type_id_eq(&self, other: &TypeId) -> bool;
|
||||
}
|
||||
|
||||
/// Indicates that a SQL type exists for a backend and defines
|
||||
|
||||
Reference in New Issue
Block a user