Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjing committed Aug 7, 2024
1 parent d4441e1 commit 50e183f
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 86 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
[env]
RUST_LOG = "info,ultimate=debug,ultimate_common=debug"

[alias]
check-all = "check --all-features"
clippy-all = "clippy --all-features -- -D warnings"
# 设置 rustc 编译器参数
# [build]
# rustflags = ["--cfg", "uuid_unstable"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ Rust 实用工具库,微服务融合……
- [crates](crates/): 工具库、组件库,可以包含对第 3 方服务的依赖。
- [ultimates](ultimates/): ultimate 融合库。比如:数据库、消息系统等,可以放置类似 `spring-boot` 一样的 **starter**

```sh
cargo clippy-all && cargo check-all```
1 change: 0 additions & 1 deletion examples/db-example/src/role/role_bmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use ultimate_db::{base::DbBmc, generate_common_bmc_fns};

use super::model::{RoleEntity, RoleFilter, RoleForCreate, RoleForUpdate};

#[allow(unused)]
pub struct RoleBmc;
impl DbBmc for RoleBmc {
const TABLE: &'static str = "role";
Expand Down
1 change: 1 addition & 0 deletions ultimates/ultimate-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ sqlx.workspace = true
sea-query-binder.workspace = true
sea-query.workspace = true
modql.workspace = true
utoipa = { workspace = true, optional = true }

[dev-dependencies]
anyhow.workspace = true
165 changes: 85 additions & 80 deletions ultimates/ultimate-db/src/base/macro_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,100 +3,105 @@
/// code for the custom implementations.
#[macro_export]
macro_rules! generate_common_bmc_fns {
(
Bmc: $struct_name:ident,
Entity: $entity:ty,
$(ForCreate: $for_create:ty,)?
$(ForUpdate: $for_update:ty,)?
$(Filter: $filter:ty,)?
) => {
(
Bmc: $struct_name:ident,
Entity: $entity:ty,
$(ForCreate: $for_create:ty,)?
$(ForUpdate: $for_update:ty,)?
$(Filter: $filter:ty,)?
) => {
impl $struct_name {
$(
pub async fn create(
mm: &ultimate_db::ModelManager,
entity_c: $for_create,
) -> ultimate_db::Result<i64> {
ultimate_db::base::create::<Self, _>(mm, entity_c).await
}
$(
pub async fn create(
mm: &ultimate_db::ModelManager,
entity_c: $for_create,
) -> ultimate_db::Result<i64> {
ultimate_db::base::create::<Self, _>(mm, entity_c).await
}

pub async fn create_many(
mm: &ultimate_db::ModelManager,
entity_c: Vec<$for_create>,
) -> ultimate_db::Result<Vec<i64>> {
ultimate_db::base::create_many::<Self, _>(mm, entity_c).await
}
)?
pub async fn create_many(
mm: &ultimate_db::ModelManager,
entity_c: Vec<$for_create>,
) -> ultimate_db::Result<Vec<i64>> {
ultimate_db::base::create_many::<Self, _>(mm, entity_c).await
}
)?

$(
pub async fn insert(
mm: &ultimate_db::ModelManager,
entity_c: $for_create,
) -> ultimate_db::Result<()> {
ultimate_db::base::insert::<Self, _>(mm, entity_c).await
}
$(
pub async fn insert(
mm: &ultimate_db::ModelManager,
entity_c: $for_create,
) -> ultimate_db::Result<()> {
ultimate_db::base::insert::<Self, _>(mm, entity_c).await
}

pub async fn insert_many(
mm: &ultimate_db::ModelManager,
entity_c: Vec<$for_create>,
) -> ultimate_db::Result<u64> {
ultimate_db::base::insert_many::<Self, _>(mm, entity_c).await
}
)?
pub async fn insert_many(
mm: &ultimate_db::ModelManager,
entity_c: Vec<$for_create>,
) -> ultimate_db::Result<u64> {
ultimate_db::base::insert_many::<Self, _>(mm, entity_c).await
}
)?

pub async fn get_by_id(
mm: &ultimate_db::ModelManager,
id: ultimate_db::Id,
mm: &ultimate_db::ModelManager,
id: impl Into<ultimate_db::Id>,
) -> ultimate_db::Result<$entity> {
ultimate_db::base::get_by_id::<Self, _>(mm, id).await
ultimate_db::base::get_by_id::<Self, _>(mm, id.into()).await
}

$(
pub async fn find(
mm: &ultimate_db::ModelManager,
filter: $filter,
) -> ultimate_db::Result<Option<$entity>> {
ultimate_db::base::find::<Self, _, _>(mm, filter).await
}
$(
pub async fn find(
mm: &ultimate_db::ModelManager,
filter: $filter,
) -> ultimate_db::Result<Option<$entity>> {
ultimate_db::base::find::<Self, _, _>(mm, filter).await
}

pub async fn list(
mm: &ultimate_db::ModelManager,
filter: Option<$filter>,
list_options: Option<modql::filter::ListOptions>,
) -> ultimate_db::Result<Vec<$entity>> {
ultimate_db::base::list::<Self, _, _>(mm, filter, list_options).await
}
pub async fn list(
mm: &ultimate_db::ModelManager,
filter: Vec<$filter>,
list_options: Option<modql::filter::ListOptions>,
) -> ultimate_db::Result<Vec<$entity>> {
ultimate_db::base::list::<Self, _, _>(mm, Some(filter), list_options).await
}

pub async fn count(
mm: &ultimate_db::ModelManager,
filter: Option<$filter>,
) -> ultimate_db::Result<i64> {
ultimate_db::base::count::<Self, _>(mm, filter).await
}
)?
pub async fn count(
mm: &ultimate_db::ModelManager,
filter: Vec<$filter>,
) -> ultimate_db::Result<i64> {
ultimate_db::base::count::<Self, _>(mm, Some(filter)).await
}
)?

$(
pub async fn update(
mm: &ultimate_db::ModelManager,
id: ultimate_db::Id,
entity_u: $for_update,
) -> ultimate_db::Result<()> {
ultimate_db::base::update::<Self, _>(mm, id, entity_u).await
}
)?
$(
pub async fn update(
mm: &ultimate_db::ModelManager,
id: impl Into<ultimate_db::Id>,
entity_u: $for_update,
) -> ultimate_db::Result<()> {
ultimate_db::base::update::<Self, _>(mm, id.into(), entity_u).await
}
)?

pub async fn delete(
mm: &ultimate_db::ModelManager,
id: ultimate_db::Id,
) -> ultimate_db::Result<()> {
ultimate_db::base::delete::<Self>(mm, id).await
}
pub async fn delete(
mm: &ultimate_db::ModelManager,
id: impl Into<ultimate_db::Id>,
) -> ultimate_db::Result<()> {
ultimate_db::base::delete::<Self>(mm, id.into()).await
}

pub async fn delete_many(
mm: &ultimate_db::ModelManager,
ids: Vec<ultimate_db::Id>,
) -> ultimate_db::Result<u64> {
ultimate_db::base::delete_many::<Self>(mm, ids).await
}
pub async fn delete_many<V, I>(
mm: &ultimate_db::ModelManager,
ids: I,
) -> ultimate_db::Result<u64>
where
V: Into<ultimate_db::Id>,
I: IntoIterator<Item = V>,
{
let ids = ids.into_iter().map(|v| v.into()).collect();
ultimate_db::base::delete_many::<Self>(mm, ids).await
}
}
};
}
78 changes: 77 additions & 1 deletion ultimates/ultimate-db/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use derive_more::derive::Display;
use modql::{field::HasSeaFields, filter::FilterNode};
use modql::{
field::HasSeaFields,
filter::{FilterNode, ListOptions, OrderBys},
};
use sea_query::SimpleExpr;
use serde::{Deserialize, Serialize};
use sqlx::{postgres::PgRow, FromRow};
Expand Down Expand Up @@ -63,6 +66,79 @@ impl From<&str> for Id {
}
}

pub fn to_vec_id<V, I>(ids: I) -> Vec<Id>
where
V: Into<Id>,
I: IntoIterator<Item = V>,
{
ids.into_iter().map(|v| v.into()).collect()
}

#[derive(Serialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct Page {
pub page: i64,
pub page_size: i64,
pub total_size: i64,
pub total_page: i64,
}

impl Page {
pub fn new(page: &Pagination, total_size: i64) -> Self {
let total_page = if total_size == 0 { 0 } else { (total_size + page.page_size - 1) / page.page_size };
Self { page: page.page, page_size: page.page_size, total_size, total_page }
}
}

#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct Pagination {
#[serde(default = "default_page")]
pub page: i64,

#[serde(default = "default_page_size")]
pub page_size: i64,

pub order_bys: Option<OrderBys>,
}

// pub static DEFAULT_PAGE_INFO: LazyLock<PageInfo> = LazyLock::new(|| PageInfo::default());

impl Pagination {
pub fn offset(&self) -> i64 {
if self.page < 2 {
return 0;
}
self.page_size * (self.page - 1)
}
}

impl Default for Pagination {
fn default() -> Self {
Self { page: default_page(), page_size: default_page_size(), order_bys: Default::default() }
}
}

impl From<Pagination> for ListOptions {
fn from(value: Pagination) -> Self {
ListOptions { limit: Some(value.page_size), offset: Some(value.offset()), order_bys: value.order_bys }
}
}

impl From<&Pagination> for ListOptions {
fn from(value: &Pagination) -> Self {
ListOptions { limit: Some(value.page_size), offset: Some(value.offset()), order_bys: value.order_bys.clone() }
}
}

fn default_page() -> i64 {
1
}

fn default_page_size() -> i64 {
20
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
6 changes: 2 additions & 4 deletions ultimates/ultimate/src/error/data_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,13 @@ impl From<DataError> for tonic::Status {
fn from(value: DataError) -> Self {
match value {
DataError::BizError { code, msg } => make_tonic_status(code, msg),
DataError::Unauthorized => tonic::Status::unauthenticated("Unauthorized"),
DataError::SecurityError(_) => tonic::Status::unauthenticated("Token error"),
DataError::DbErr(ex) => tonic::Status::from_error(ex.into()),
DataError::UltimateCommonError(ex) => tonic::Status::from_error(ex.into()),
DataError::AnyhowError(ex) => tonic::Status::from_error(ex.into()),
DataError::SystemTimeError(ex) => tonic::Status::from_error(ex.into()),
DataError::ParseIntError(ex) => tonic::Status::from_error(ex.into()),
DataError::GrpcTransportError(ex) => tonic::Status::from_error(ex.into()),
DataError::IoError(e) => tonic::Status::internal(e.to_string()),
DataError::JsonError(ex) => tonic::Status::from_error(ex.into()),
DataError::GrpcTransportError(ex) => tonic::Status::from_error(ex.into()),
}
}
}
Expand Down

0 comments on commit 50e183f

Please sign in to comment.