mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-24 09:38:29 +02:00
Print table/col/val when row not found
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
use crate::connection_or_tx::ConnectionOrTx;
|
use crate::connection_or_tx::ConnectionOrTx;
|
||||||
|
use crate::error::Error::ModelNotFound;
|
||||||
|
use crate::error::Result;
|
||||||
use crate::models::{AnyModel, UpsertModelInfo};
|
use crate::models::{AnyModel, UpsertModelInfo};
|
||||||
use crate::util::{ModelChangeEvent, ModelPayload, UpdateSource};
|
use crate::util::{ModelChangeEvent, ModelPayload, UpdateSource};
|
||||||
use log::error;
|
use log::error;
|
||||||
@@ -8,6 +10,7 @@ use sea_query::{
|
|||||||
SqliteQueryBuilder,
|
SqliteQueryBuilder,
|
||||||
};
|
};
|
||||||
use sea_query_rusqlite::RusqliteBinder;
|
use sea_query_rusqlite::RusqliteBinder;
|
||||||
|
use std::fmt::Debug;
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
|
|
||||||
pub struct DbContext<'a> {
|
pub struct DbContext<'a> {
|
||||||
@@ -18,19 +21,33 @@ pub struct DbContext<'a> {
|
|||||||
impl<'a> DbContext<'a> {
|
impl<'a> DbContext<'a> {
|
||||||
pub(crate) fn find_one<'s, M>(
|
pub(crate) fn find_one<'s, M>(
|
||||||
&self,
|
&self,
|
||||||
col: impl IntoColumnRef,
|
col: impl IntoColumnRef + IntoIden + Clone,
|
||||||
value: impl Into<SimpleExpr>,
|
value: impl Into<SimpleExpr> + Debug,
|
||||||
) -> crate::error::Result<M>
|
) -> Result<M>
|
||||||
where
|
where
|
||||||
M: Into<AnyModel> + Clone + UpsertModelInfo,
|
M: Into<AnyModel> + Clone + UpsertModelInfo,
|
||||||
{
|
{
|
||||||
|
let value_debug = format!("{:?}", value);
|
||||||
|
|
||||||
|
let value_expr = value.into();
|
||||||
let (sql, params) = Query::select()
|
let (sql, params) = Query::select()
|
||||||
.from(M::table_name())
|
.from(M::table_name())
|
||||||
.column(Asterisk)
|
.column(Asterisk)
|
||||||
.cond_where(Expr::col(col).eq(value))
|
.cond_where(Expr::col(col.clone()).eq(value_expr))
|
||||||
.build_rusqlite(SqliteQueryBuilder);
|
.build_rusqlite(SqliteQueryBuilder);
|
||||||
let mut stmt = self.conn.prepare(sql.as_str()).expect("Failed to prepare query");
|
let mut stmt = self.conn.prepare(sql.as_str()).expect("Failed to prepare query");
|
||||||
Ok(stmt.query_row(&*params.as_params(), M::from_row)?)
|
match stmt.query_row(&*params.as_params(), M::from_row) {
|
||||||
|
Ok(result) => Ok(result),
|
||||||
|
Err(rusqlite::Error::QueryReturnedNoRows) => {
|
||||||
|
Err(ModelNotFound(format!(
|
||||||
|
r#"table "{}" {} == {}"#,
|
||||||
|
M::table_name().into_iden().to_string(),
|
||||||
|
col.into_iden().to_string(),
|
||||||
|
value_debug
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
Err(e) => Err(crate::error::Error::SqlError(e)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn find_optional<'s, M>(
|
pub(crate) fn find_optional<'s, M>(
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ pub struct Settings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for Settings {
|
impl UpsertModelInfo for Settings {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
SettingsIden::Table
|
SettingsIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,7 +252,7 @@ pub struct Workspace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for Workspace {
|
impl UpsertModelInfo for Workspace {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
WorkspaceIden::Table
|
WorkspaceIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -355,7 +355,7 @@ pub struct WorkspaceMeta {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for WorkspaceMeta {
|
impl UpsertModelInfo for WorkspaceMeta {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
WorkspaceMetaIden::Table
|
WorkspaceMetaIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -456,7 +456,7 @@ pub struct CookieJar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for CookieJar {
|
impl UpsertModelInfo for CookieJar {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
CookieJarIden::Table
|
CookieJarIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -535,7 +535,7 @@ pub struct Environment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for Environment {
|
impl UpsertModelInfo for Environment {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
EnvironmentIden::Table
|
EnvironmentIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -655,7 +655,7 @@ pub struct Folder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for Folder {
|
impl UpsertModelInfo for Folder {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
FolderIden::Table
|
FolderIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -786,7 +786,7 @@ pub struct HttpRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for HttpRequest {
|
impl UpsertModelInfo for HttpRequest {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
HttpRequestIden::Table
|
HttpRequestIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -913,7 +913,7 @@ pub struct WebsocketConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for WebsocketConnection {
|
impl UpsertModelInfo for WebsocketConnection {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
WebsocketConnectionIden::Table
|
WebsocketConnectionIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1027,7 +1027,7 @@ pub struct WebsocketRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for WebsocketRequest {
|
impl UpsertModelInfo for WebsocketRequest {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
WebsocketRequestIden::Table
|
WebsocketRequestIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1152,7 +1152,7 @@ pub struct WebsocketEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for WebsocketEvent {
|
impl UpsertModelInfo for WebsocketEvent {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
WebsocketEventIden::Table
|
WebsocketEventIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1269,7 +1269,7 @@ pub struct HttpResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for HttpResponse {
|
impl UpsertModelInfo for HttpResponse {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
HttpResponseIden::Table
|
HttpResponseIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1377,7 +1377,7 @@ pub struct GraphQlIntrospection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for GraphQlIntrospection {
|
impl UpsertModelInfo for GraphQlIntrospection {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
GraphQlIntrospectionIden::Table
|
GraphQlIntrospectionIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1461,7 +1461,7 @@ pub struct GrpcRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for GrpcRequest {
|
impl UpsertModelInfo for GrpcRequest {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
GrpcRequestIden::Table
|
GrpcRequestIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1588,7 +1588,7 @@ pub struct GrpcConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for GrpcConnection {
|
impl UpsertModelInfo for GrpcConnection {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
GrpcConnectionIden::Table
|
GrpcConnectionIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1708,7 +1708,7 @@ pub struct GrpcEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for GrpcEvent {
|
impl UpsertModelInfo for GrpcEvent {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
GrpcEventIden::Table
|
GrpcEventIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1799,7 +1799,7 @@ pub struct Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for Plugin {
|
impl UpsertModelInfo for Plugin {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
PluginIden::Table
|
PluginIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1881,7 +1881,7 @@ pub struct SyncState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for SyncState {
|
impl UpsertModelInfo for SyncState {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
SyncStateIden::Table
|
SyncStateIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1964,7 +1964,7 @@ pub struct KeyValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UpsertModelInfo for KeyValue {
|
impl UpsertModelInfo for KeyValue {
|
||||||
fn table_name() -> impl IntoTableRef + Debug {
|
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||||
KeyValueIden::Table
|
KeyValueIden::Table
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2181,7 +2181,7 @@ impl AnyModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait UpsertModelInfo {
|
pub trait UpsertModelInfo {
|
||||||
fn table_name() -> impl IntoTableRef + Debug;
|
fn table_name() -> impl IntoTableRef + IntoIden;
|
||||||
fn id_column() -> impl IntoIden + Eq + Clone;
|
fn id_column() -> impl IntoIden + Eq + Clone;
|
||||||
fn generate_id() -> String;
|
fn generate_id() -> String;
|
||||||
fn order_by() -> (impl IntoColumnRef, Order);
|
fn order_by() -> (impl IntoColumnRef, Order);
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ impl<'a> DbContext<'a> {
|
|||||||
return self.resolve_auth_for_folder(&folder);
|
return self.resolve_auth_for_folder(&folder);
|
||||||
}
|
}
|
||||||
|
|
||||||
let workspace = self.get_workspace("invalid")?;
|
let workspace = self.get_workspace(&http_request.workspace_id)?;
|
||||||
Ok(self.resolve_auth_for_workspace(&workspace))
|
Ok(self.resolve_auth_for_workspace(&workspace))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user