-
Notifications
You must be signed in to change notification settings - Fork 8
CTE support #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CTE support #251
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,14 +64,25 @@ pub async fn translate_wildcard_expr( | |
| ts_query: &mut TsQuery, | ||
| db_conn: &DBConn, | ||
| ) -> Result<(), TsGeneratorError> { | ||
| let table_with_joins = get_all_table_names_from_select(select)?; | ||
| let table_names = get_all_table_names_from_select(select)?; | ||
|
|
||
| if table_with_joins.len() > 1 { | ||
| // Check if the table is a CTE or table-valued function registered in table_valued_function_columns. | ||
| // CTEs are processed before the main query body and their columns are stored there. | ||
| for table_name in &table_names { | ||
| if let Some(tvf_columns) = ts_query.table_valued_function_columns.get(table_name).cloned() { | ||
| for (col_name, ts_type) in tvf_columns { | ||
| ts_query.result.insert(col_name, vec![ts_type]); | ||
| } | ||
| return Ok(()); | ||
| } | ||
| } | ||
|
|
||
| if table_names.len() > 1 { | ||
| warning!("Impossible to calculate appropriate field names of a wildcard query with multiple tables. Please use explicit field names instead. Query: {}", select.to_string()); | ||
| } | ||
|
Comment on lines
+71
to
82
|
||
|
|
||
| let table_with_joins = table_with_joins.iter().map(|s| s.as_ref()).collect(); | ||
| let all_fields = DB_SCHEMA.lock().await.fetch_table(&table_with_joins, db_conn).await; | ||
| let table_refs = table_names.iter().map(|s| s.as_ref()).collect(); | ||
| let all_fields = DB_SCHEMA.lock().await.fetch_table(&table_refs, db_conn).await; | ||
|
|
||
| if let Some(all_fields) = all_fields { | ||
| for key in all_fields.keys() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| export type SimpleCteParams = []; | ||
|
|
||
| export interface ISimpleCteResult { | ||
| id: number; | ||
| name: string; | ||
| } | ||
|
|
||
| export interface ISimpleCteQuery { | ||
| params: SimpleCteParams; | ||
| result: ISimpleCteResult; | ||
| } | ||
|
|
||
| export type RankWithCteParams = []; | ||
|
|
||
| export interface IRankWithCteResult { | ||
| id: number; | ||
| name: string; | ||
| rk: any; | ||
| } | ||
|
|
||
| export interface IRankWithCteQuery { | ||
| params: RankWithCteParams; | ||
| result: IRankWithCteResult; | ||
| } | ||
|
|
||
| export type MultipleCtesParams = []; | ||
|
|
||
| export interface IMultipleCtesResult { | ||
| id: number; | ||
| name: string; | ||
| } | ||
|
|
||
| export interface IMultipleCtesQuery { | ||
| params: MultipleCtesParams; | ||
| result: IMultipleCtesResult; | ||
| } | ||
|
|
||
| export type CteWithWildcardParams = []; | ||
|
|
||
| export interface ICteWithWildcardResult { | ||
| id: number; | ||
| name: string; | ||
| } | ||
|
|
||
| export interface ICteWithWildcardQuery { | ||
| params: CteWithWildcardParams; | ||
| result: ICteWithWildcardResult; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| export type SimpleCteParams = []; | ||
|
|
||
| export interface ISimpleCteResult { | ||
| id: number; | ||
| name: string; | ||
| } | ||
|
|
||
| export interface ISimpleCteQuery { | ||
| params: SimpleCteParams; | ||
| result: ISimpleCteResult; | ||
| } | ||
|
|
||
| export type RankWithCteParams = []; | ||
|
|
||
| export interface IRankWithCteResult { | ||
| id: number; | ||
| name: string; | ||
| rk: any; | ||
| } | ||
|
|
||
| export interface IRankWithCteQuery { | ||
| params: RankWithCteParams; | ||
| result: IRankWithCteResult; | ||
| } | ||
|
|
||
| export type MultipleCtesParams = []; | ||
|
|
||
| export interface IMultipleCtesResult { | ||
| id: number; | ||
| name: string; | ||
| } | ||
|
|
||
| export interface IMultipleCtesQuery { | ||
| params: MultipleCtesParams; | ||
| result: IMultipleCtesResult; | ||
| } | ||
|
|
||
| export type CteWithWildcardParams = []; | ||
|
|
||
| export interface ICteWithWildcardResult { | ||
| id: number; | ||
| name: string; | ||
| } | ||
|
|
||
| export interface ICteWithWildcardQuery { | ||
| params: CteWithWildcardParams; | ||
| result: ICteWithWildcardResult; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { sql } from 'sqlx-ts' | ||
|
|
||
| // Simple CTE with explicit column selection | ||
| const simpleCte = sql` | ||
| -- @name: simple cte | ||
| WITH filtered_items AS ( | ||
| SELECT id, name FROM items | ||
| ) | ||
| SELECT id, name FROM filtered_items | ||
| ` | ||
|
|
||
| // CTE with window function (RANK) — original issue #104 | ||
| const rankWithCte = sql` | ||
| -- @name: rank with cte | ||
| WITH ranked_items AS ( | ||
| SELECT | ||
| id, | ||
| name, | ||
| rarity, | ||
| RANK() OVER (PARTITION BY rarity ORDER BY id) AS rk | ||
| FROM items | ||
| ) | ||
| SELECT id, name, rk FROM ranked_items WHERE rk = 1 | ||
| ` | ||
|
|
||
| // Multiple CTEs | ||
| const multipleCtes = sql` | ||
| -- @name: multiple ctes | ||
| WITH | ||
| popular AS ( | ||
| SELECT id, name FROM items WHERE id > 10 | ||
| ), | ||
| rare AS ( | ||
| SELECT id, name FROM items WHERE rarity = 'rare' | ||
| ) | ||
| SELECT id, name FROM popular | ||
| ` | ||
|
|
||
| // CTE with wildcard in outer query | ||
| const cteWithWildcard = sql` | ||
| -- @name: cte with wildcard | ||
| WITH base AS ( | ||
| SELECT id, name FROM items | ||
| ) | ||
| SELECT * FROM base | ||
| ` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This parameter is named
cte_columns, but call sites pass&ts_query.table_valued_function_columns, andtranslate_queryalso stores CTE columns intable_valued_function_columns. This naming mismatch makes the API harder to understand and maintain. Consider renaming the parameter (and related comments/locals) to something likevirtual_table_columns(or splitting CTE vs TVF maps if they’re intended to be distinct concepts).