Move code
This commit is contained in:
parent
5069f92001
commit
981a5f731d
@ -2,6 +2,7 @@ mod delete;
|
|||||||
mod executor;
|
mod executor;
|
||||||
mod insert;
|
mod insert;
|
||||||
mod paginator;
|
mod paginator;
|
||||||
|
mod query;
|
||||||
mod select;
|
mod select;
|
||||||
mod update;
|
mod update;
|
||||||
|
|
||||||
@ -9,10 +10,11 @@ pub use delete::*;
|
|||||||
pub use executor::*;
|
pub use executor::*;
|
||||||
pub use insert::*;
|
pub use insert::*;
|
||||||
pub use paginator::*;
|
pub use paginator::*;
|
||||||
|
pub use query::*;
|
||||||
pub use select::*;
|
pub use select::*;
|
||||||
pub use update::*;
|
pub use update::*;
|
||||||
|
|
||||||
use crate::{DatabaseConnection, QueryResult, Statement, TypeErr};
|
use crate::{DatabaseConnection, Statement};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use std::{error::Error, fmt};
|
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!();
|
sea_query::sea_query_driver_mysql!();
|
||||||
use sea_query_driver_mysql::bind_query;
|
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;
|
pub struct SqlxMySqlConnector;
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ mod insert;
|
|||||||
mod join;
|
mod join;
|
||||||
#[cfg(feature = "with-json")]
|
#[cfg(feature = "with-json")]
|
||||||
mod json;
|
mod json;
|
||||||
mod result;
|
|
||||||
mod select;
|
mod select;
|
||||||
mod traits;
|
mod traits;
|
||||||
mod update;
|
mod update;
|
||||||
@ -17,7 +16,6 @@ pub use insert::*;
|
|||||||
pub use join::*;
|
pub use join::*;
|
||||||
#[cfg(feature = "with-json")]
|
#[cfg(feature = "with-json")]
|
||||||
pub use json::*;
|
pub use json::*;
|
||||||
pub use result::*;
|
|
||||||
pub use select::*;
|
pub use select::*;
|
||||||
pub use traits::*;
|
pub use traits::*;
|
||||||
pub use update::*;
|
pub use update::*;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user