Skip to content

Commit 10ac5e6

Browse files
committed
throw error on the impoissble cases
1 parent d8a3639 commit 10ac5e6

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

src/ts_generator/sql_parser/expressions/translate_expr.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,20 @@ pub async fn get_sql_query_param(
124124
single_table_name: &Option<&str>,
125125
table_with_joins: &Option<Vec<TableWithJoins>>,
126126
db_conn: &DBConn,
127-
) -> Option<(TsFieldType, bool, Option<String>)> {
127+
) -> Result<Option<(TsFieldType, bool, Option<String>)>, TsGeneratorError> {
128128
let table_name: Option<String>;
129129

130130
if table_with_joins.is_some() {
131131
table_name = translate_table_from_expr(table_with_joins, &left.clone()).ok();
132132
} else if single_table_name.is_some() {
133133
table_name = single_table_name.map(|x| x.to_string());
134134
} else {
135-
error!(
136-
"Failed to infer table name while processing WHERE clause. Expression: {}",
137-
left
138-
);
139-
return None;
135+
return Err(TsGeneratorError::TableNameInferenceFailedInWhere {
136+
query: format!(
137+
"Failed to infer table name while processing WHERE clause. Expression: {}. Ensure your query has a FROM clause or table reference.",
138+
left
139+
),
140+
});
140141
}
141142

142143
let column_name = translate_column_name_expr(left);
@@ -148,33 +149,32 @@ pub async fn get_sql_query_param(
148149
match (column_name, expr_placeholder, table_name) {
149150
(Some(column_name), Some(expr_placeholder), Some(table_name)) => {
150151
let table_names = vec![table_name.as_str()];
151-
let columns = DB_SCHEMA.lock().await.fetch_table(&table_names, db_conn).await;
152-
153-
if columns.is_none() {
154-
error!(
155-
"Table '{}' not found in database schema. Check that the table exists and is accessible.",
156-
table_name
157-
);
158-
return None;
159-
}
160-
161-
let columns = columns.unwrap();
152+
let columns = DB_SCHEMA
153+
.lock()
154+
.await
155+
.fetch_table(&table_names, db_conn)
156+
.await
157+
.ok_or_else(|| TsGeneratorError::TableNotFoundInSchema {
158+
table: table_name.clone(),
159+
})?;
162160

163161
// get column and return TsFieldType
164-
let column = columns.get(column_name.as_str());
165-
if column.is_none() {
162+
let column = columns.get(column_name.as_str()).ok_or_else(|| {
166163
let available_columns = columns.keys().map(|k| k.as_str()).collect::<Vec<_>>().join(", ");
167-
error!(
168-
"Column '{}' not found in table '{}'. Available columns: {}",
169-
column_name, table_name, available_columns
170-
);
171-
return None;
172-
}
164+
TsGeneratorError::ColumnNotFoundInTable {
165+
column: column_name.clone(),
166+
table: table_name.clone(),
167+
available_columns,
168+
}
169+
})?;
173170

174-
let column = column.unwrap();
175-
Some((column.field_type.to_owned(), column.is_nullable, Some(expr_placeholder)))
171+
Ok(Some((
172+
column.field_type.to_owned(),
173+
column.is_nullable,
174+
Some(expr_placeholder),
175+
)))
176176
}
177-
_ => None,
177+
_ => Ok(None),
178178
}
179179
}
180180

@@ -315,7 +315,7 @@ pub async fn translate_expr(
315315
// OPERATORS START //
316316
/////////////////////
317317
Expr::BinaryOp { left, op: _, right } => {
318-
let param = get_sql_query_param(left, right, single_table_name, table_with_joins, db_conn).await;
318+
let param = get_sql_query_param(left, right, single_table_name, table_with_joins, db_conn).await?;
319319
if let Some((value, is_nullable, index)) = param {
320320
let _ = ts_query.insert_param(&value, &is_nullable, &index);
321321
Ok(())
@@ -358,7 +358,7 @@ pub async fn translate_expr(
358358
table_with_joins,
359359
db_conn,
360360
)
361-
.await;
361+
.await?;
362362

363363
if let Some((value, is_nullable, index)) = result {
364364
let array_item = TsFieldType::Array(Box::new(value));
@@ -386,8 +386,8 @@ pub async fn translate_expr(
386386
low,
387387
high,
388388
} => {
389-
let low = get_sql_query_param(expr, low, single_table_name, table_with_joins, db_conn).await;
390-
let high = get_sql_query_param(expr, high, single_table_name, table_with_joins, db_conn).await;
389+
let low = get_sql_query_param(expr, low, single_table_name, table_with_joins, db_conn).await?;
390+
let high = get_sql_query_param(expr, high, single_table_name, table_with_joins, db_conn).await?;
391391
if let Some((value, is_nullable, placeholder)) = low {
392392
ts_query.insert_param(&value, &is_nullable, &placeholder)?;
393393
}

0 commit comments

Comments
 (0)