From f206d770ee0c345a8d655918cae4793eb71e3061 Mon Sep 17 00:00:00 2001 From: veypi Date: Mon, 2 Oct 2023 16:24:38 +0800 Subject: [PATCH] sqlx log --- oab/Cargo.toml | 2 +- oab/src/api/appuser.rs | 32 ++++++++++++++++---------------- oab/src/api/user.rs | 21 ++------------------- oab/src/cfg.rs | 8 +++++++- oab/src/result.rs | 1 - 5 files changed, 26 insertions(+), 38 deletions(-) diff --git a/oab/Cargo.toml b/oab/Cargo.toml index ed17b43..4703044 100644 --- a/oab/Cargo.toml +++ b/oab/Cargo.toml @@ -22,7 +22,7 @@ thiserror = "1.0" sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "mysql", "macros", "migrate", "chrono"] } sea-orm = { version = "^0.12.0", features = [ "sqlx-mysql", - "runtime-tokio-rustls", "macros", "debug-print", "with-chrono", + "runtime-tokio-rustls", "macros", "with-chrono", "with-json", "with-uuid" ] } diff --git a/oab/src/api/appuser.rs b/oab/src/api/appuser.rs index 87de5ce..9bf7a31 100644 --- a/oab/src/api/appuser.rs +++ b/oab/src/api/appuser.rs @@ -5,15 +5,11 @@ // Distributed under terms of the MIT license. // -use actix_web::{delete, get, post, web, Responder}; +use actix_web::{get, web, Responder}; use proc::access_read; -use serde::{Deserialize, Serialize}; -use tracing::info; +use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; -use crate::{ - models::{self, app_user}, - AppState, Error, Result, -}; +use crate::{models::app_user, AppState, Error, Result}; #[get("/app/{aid}/user/{uid}")] #[access_read("app")] @@ -22,24 +18,28 @@ pub async fn get( stat: web::Data, ) -> Result { let (mut aid, mut uid) = params.into_inner(); + let mut q = app_user::Entity::find(); if uid == "-" { uid = "".to_string(); + } else { + q = q.filter(app_user::Column::UserId.eq(uid.clone())); } if aid == "-" { aid = "".to_string(); + } else { + q = q.filter(app_user::Column::AppId.eq(aid.clone())); } - let sql = format!("select * from app_user where"); - info!("111|{}|{}|", aid, uid); if uid.is_empty() && aid.is_empty() { Err(Error::Missing("uid or aid".to_string())) } else { - let s = sqlx::query_as::<_, app_user::Model>( - "select * from app_user where app_id = ? and user_id = ?", - ) - .bind(aid) - .bind(uid) - .fetch_all(stat.sqlx()) - .await?; + let s: Vec = q.all(stat.db()).await?; + // let s = sqlx::query_as::<_, app_user::Model>( + // "select * from app_user where app_id = ? and user_id = ?", + // ) + // .bind(aid) + // .bind(uid) + // .fetch_all(stat.sqlx()) + // .await?; Ok(web::Json(s)) } } diff --git a/oab/src/api/user.rs b/oab/src/api/user.rs index de05e48..8e42068 100644 --- a/oab/src/api/user.rs +++ b/oab/src/api/user.rs @@ -37,24 +37,6 @@ pub async fn get(id: web::Path, stat: web::Data) -> Result) -> Result { let res: Vec = user::Entity::find().all(stat.db()).await?; - // let result = sqlx::query!( - // "select id,updated,created,username,nickname,email,icon,status, used, space from user", - // ) - // .map(|row| models::user::Model { - // id: row.id, - // created: row.created, - // updated: row.updated, - // username: row.username, - // nickname: row.nickname, - // email: row.email, - // status: row.status, - // used: row.used, - // space: row.space, - // icon: row.icon, - // ..Default::default() - // }) - // .fetch_all(CONFIG.sqlx()) - // .await?; Ok(web::Json(res)) } @@ -154,8 +136,9 @@ values ( ?, ?, ? ) }; if i == 0 { // let result: Vec = access::Entity::find().all(db).await?; + info!("asd"); let result = sqlx::query_as::<_, access::Model>( - "select access.name,access.rid,access.level from access, user_role, role WHERE user_role.user_id = ? && access.role_id=user_role.role_id && role.id=user_role.role_id && role.app_id = ?", + "select access.* from access, user_role, role WHERE user_role.user_id = ? && access.role_id=user_role.role_id && role.id=user_role.role_id && role.app_id = ?", ) .bind(&u.id) .bind(stat.uuid.clone()) diff --git a/oab/src/cfg.rs b/oab/src/cfg.rs index 63be27c..22b5153 100644 --- a/oab/src/cfg.rs +++ b/oab/src/cfg.rs @@ -11,13 +11,17 @@ use std::{ fs::File, io::{self, Read}, + str::FromStr, time::Duration, }; use clap::{Args, Parser, Subcommand}; use lazy_static::lazy_static; use sea_orm::{ConnectOptions, Database, DatabaseConnection}; -use sqlx::{mysql::MySqlPoolOptions, Pool}; +use sqlx::{ + mysql::{MySqlConnectOptions, MySqlPoolOptions}, + Pool, +}; use crate::Result; @@ -153,6 +157,7 @@ impl AppState { "mysql://{}:{}@{}/{}", self.db_user, self.db_pass, self.db_url, self.db_name ); + let p = MySqlPoolOptions::new() .max_connections(5) .connect_lazy(&url)?; @@ -170,6 +175,7 @@ impl AppState { .connect_timeout(Duration::from_secs(8)) .acquire_timeout(Duration::from_secs(8)) .idle_timeout(Duration::from_secs(8)) + .sqlx_logging(false) .max_lifetime(Duration::from_secs(8)); self._db = Some(Database::connect(opt).await?); diff --git a/oab/src/result.rs b/oab/src/result.rs index 0771916..6fd0f19 100644 --- a/oab/src/result.rs +++ b/oab/src/result.rs @@ -17,7 +17,6 @@ use thiserror::Error as ThisError; use tracing::info; pub type Result = std::result::Result; -pub type JsonResult = std::result::Result, Error>; #[derive(Serialize, Deserialize)] pub struct JsonResponse {