diff --git a/src/ts_generator/sql_parser/expressions/translate_expr.rs b/src/ts_generator/sql_parser/expressions/translate_expr.rs index 894fe3ab..6e9a207f 100644 --- a/src/ts_generator/sql_parser/expressions/translate_expr.rs +++ b/src/ts_generator/sql_parser/expressions/translate_expr.rs @@ -420,7 +420,7 @@ pub async fn translate_expr( negated: _, } => { // You do not need an alias as we are processing a subquery within the WHERE clause - translate_query(ts_query, &None, subquery, db_conn, None, false).await?; + translate_query(ts_query, table_with_joins, subquery, db_conn, None, false).await?; Ok(()) } Expr::Between { @@ -654,7 +654,7 @@ pub async fn translate_expr( } => ts_query.insert_result(alias, &[TsFieldType::Unknown], is_selection, false, expr_for_logging), Expr::Exists { subquery, negated: _ } => { ts_query.insert_result(alias, &[TsFieldType::Boolean], is_selection, false, expr_for_logging)?; - translate_query(ts_query, &None, subquery, db_conn, alias, false).await + translate_query(ts_query, table_with_joins, subquery, db_conn, alias, false).await } // Note: ListAgg and ArrayAgg were removed in sqlparser 0.59.0 // They are now represented as Function variants diff --git a/tests/mysql_query_parameters.rs b/tests/mysql_query_parameters.rs index 679a9d18..7fdf0077 100644 --- a/tests/mysql_query_parameters.rs +++ b/tests/mysql_query_parameters.rs @@ -308,6 +308,28 @@ export interface ISomeQueryResult { name: string; } +export interface ISomeQueryQuery { + params: SomeQueryParams; + result: ISomeQueryResult; +} +"#); + + #[rustfmt::skip] +run_test!(should_pick_query_params_from_correlated_exists_subquery, TestConfig::new("mysql", true, None, None), + +//// TS query //// +r#" +const someQuery = sql`SELECT c.id AS id FROM characters c WHERE EXISTS (SELECT 1 FROM character_quests cq WHERE cq.character_id = c.id AND cq.status = ?) AND c.level > ?`; +"#, + +//// Generated TS interfaces //// +r#" +export type SomeQueryParams = [string | null, number | null]; + +export interface ISomeQueryResult { + id: number; +} + export interface ISomeQueryQuery { params: SomeQueryParams; result: ISomeQueryResult; diff --git a/tests/postgres_query_parameters.rs b/tests/postgres_query_parameters.rs index 6b2503ba..5d46522c 100644 --- a/tests/postgres_query_parameters.rs +++ b/tests/postgres_query_parameters.rs @@ -122,6 +122,72 @@ export interface ISomeQueryResult { quantity: number | null; } +export interface ISomeQueryQuery { + params: SomeQueryParams; + result: ISomeQueryResult; +} +"#); + + #[rustfmt::skip] +run_test!(should_pick_query_params_from_correlated_exists_subquery, TestConfig::new("postgres", true, None, None), + +//// TS query //// +r#" +const someQuery = sql`SELECT c.id AS id FROM characters c WHERE EXISTS (SELECT 1 FROM character_quests cq WHERE cq.character_id = c.id AND cq.status = $1) AND c.level > $2`; +"#, + +//// Generated TS interfaces //// +r#" +export type SomeQueryParams = [string | null, number | null]; + +export interface ISomeQueryResult { + id: number; +} + +export interface ISomeQueryQuery { + params: SomeQueryParams; + result: ISomeQueryResult; +} +"#); + + #[rustfmt::skip] +run_test!(should_pick_query_params_from_correlated_exists_subquery_referencing_unaliased_table, TestConfig::new("postgres", true, None, None), + +//// TS query //// +r#" +const someQuery = sql`SELECT id FROM characters WHERE EXISTS (SELECT 1 FROM character_quests cq WHERE cq.character_id = characters.id AND cq.status = $1)`; +"#, + +//// Generated TS interfaces //// +r#" +export type SomeQueryParams = [string | null]; + +export interface ISomeQueryResult { + id: number; +} + +export interface ISomeQueryQuery { + params: SomeQueryParams; + result: ISomeQueryResult; +} +"#); + + #[rustfmt::skip] +run_test!(should_pick_query_params_from_correlated_in_subquery, TestConfig::new("postgres", true, None, None), + +//// TS query //// +r#" +const someQuery = sql`SELECT c.id AS id FROM characters c WHERE c.id IN (SELECT cq.character_id FROM character_quests cq WHERE cq.character_id = c.id AND cq.status = $1)`; +"#, + +//// Generated TS interfaces //// +r#" +export type SomeQueryParams = [string | null]; + +export interface ISomeQueryResult { + id: number; +} + export interface ISomeQueryQuery { params: SomeQueryParams; result: ISomeQueryResult;