Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ts_generator/sql_parser/expressions/translate_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tests/mysql_query_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
66 changes: 66 additions & 0 deletions tests/postgres_query_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading