Move code
This commit is contained in:
parent
5069f92001
commit
981a5f731d
@ -2,6 +2,7 @@ mod delete;
|
||||
mod executor;
|
||||
mod insert;
|
||||
mod paginator;
|
||||
mod query;
|
||||
mod select;
|
||||
mod update;
|
||||
|
||||
@ -9,10 +10,11 @@ pub use delete::*;
|
||||
pub use executor::*;
|
||||
pub use insert::*;
|
||||
pub use paginator::*;
|
||||
pub use query::*;
|
||||
pub use select::*;
|
||||
pub use update::*;
|
||||
|
||||
use crate::{DatabaseConnection, QueryResult, Statement, TypeErr};
|
||||
use crate::{DatabaseConnection, Statement};
|
||||
use async_trait::async_trait;
|
||||
use std::{error::Error, fmt};
|
||||
|
||||
|
94
src/connector/query.rs
Normal file
94
src/connector/query.rs
Normal file
@ -0,0 +1,94 @@
|
||||
use sqlx::mysql::MySqlRow;
|
||||
use std::{error::Error, fmt};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct QueryResult {
|
||||
pub(crate) row: QueryResultRow,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum QueryResultRow {
|
||||
SqlxMySql(MySqlRow),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TypeErr;
|
||||
|
||||
pub trait TryGetable {
|
||||
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TypeErr>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
// TryGetable //
|
||||
|
||||
macro_rules! try_getable {
|
||||
( $type: ty ) => {
|
||||
impl TryGetable for $type {
|
||||
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TypeErr> {
|
||||
let column = format!("{}{}", pre, col);
|
||||
match &res.row {
|
||||
QueryResultRow::SqlxMySql(row) => {
|
||||
use sqlx::Row;
|
||||
Ok(row.try_get(column.as_str())?)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryGetable for Option<$type> {
|
||||
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TypeErr> {
|
||||
let column = format!("{}{}", pre, col);
|
||||
match &res.row {
|
||||
QueryResultRow::SqlxMySql(row) => {
|
||||
use sqlx::Row;
|
||||
match row.try_get(column.as_str()) {
|
||||
Ok(v) => Ok(Some(v)),
|
||||
Err(_) => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
try_getable!(bool);
|
||||
try_getable!(i8);
|
||||
try_getable!(i16);
|
||||
try_getable!(i32);
|
||||
try_getable!(i64);
|
||||
try_getable!(u8);
|
||||
try_getable!(u16);
|
||||
try_getable!(u32);
|
||||
try_getable!(u64);
|
||||
try_getable!(f32);
|
||||
try_getable!(f64);
|
||||
try_getable!(String);
|
||||
|
||||
// QueryResult //
|
||||
|
||||
impl QueryResult {
|
||||
pub fn try_get<T>(&self, pre: &str, col: &str) -> Result<T, TypeErr>
|
||||
where
|
||||
T: TryGetable,
|
||||
{
|
||||
T::try_get(self, pre, col)
|
||||
}
|
||||
}
|
||||
|
||||
// TypeErr //
|
||||
|
||||
impl Error for TypeErr {}
|
||||
|
||||
impl fmt::Display for TypeErr {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<sqlx::Error> for TypeErr {
|
||||
fn from(_: sqlx::Error) -> TypeErr {
|
||||
TypeErr
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ use sqlx::{
|
||||
sea_query::sea_query_driver_mysql!();
|
||||
use sea_query_driver_mysql::bind_query;
|
||||
|
||||
use crate::{connector::*, debug_print, query::*, DatabaseConnection, Statement};
|
||||
use crate::{connector::*, debug_print, DatabaseConnection, Statement};
|
||||
|
||||
pub struct SqlxMySqlConnector;
|
||||
|
||||
|
@ -5,7 +5,6 @@ mod insert;
|
||||
mod join;
|
||||
#[cfg(feature = "with-json")]
|
||||
mod json;
|
||||
mod result;
|
||||
mod select;
|
||||
mod traits;
|
||||
mod update;
|
||||
@ -17,7 +16,6 @@ pub use insert::*;
|
||||
pub use join::*;
|
||||
#[cfg(feature = "with-json")]
|
||||
pub use json::*;
|
||||
pub use result::*;
|
||||
pub use select::*;
|
||||
pub use traits::*;
|
||||
pub use update::*;
|
||||
|
Loading…
x
Reference in New Issue
Block a user