Skip to content

Commit

Permalink
avoid aliasFor for casts
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Sep 11, 2024
1 parent 48b7f5f commit 67a4c5e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
21 changes: 12 additions & 9 deletions packages/typegen/src/query/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export const getAliasInfo = (statement: pgsqlAST.Statement): AliasInfo[] => {
}))
.statement(statement)

const availableTables = lodash.uniqBy(allTableReferences, JSON.stringify)
const availableTables = lodash.uniqBy(allTableReferences, t => JSON.stringify(t))

const aliasGroups = lodash.groupBy(availableTables, t => t.referredToAs)

Expand All @@ -241,35 +241,38 @@ export const getAliasInfo = (statement: pgsqlAST.Statement): AliasInfo[] => {
`Some aliases are duplicated, this is too confusing. ${JSON.stringify({aliasGroups})}`,
)

return statement.columns.reduce<AliasInfo[]>((mappings, {expr, alias}) => {
return statement.columns.flatMap<AliasInfo>(columnAliasInfo)

function columnAliasInfo({expr, alias}: pgsqlAST.SelectedColumn): AliasInfo | [] {
if (expr.type === 'cast') {
expr = expr.operand
const info = columnAliasInfo({expr: expr.operand, alias: alias})
return {...info, aliasFor: null}
}

if (expr.type === 'ref') {
const matchingTables = availableTables.filter(t => expr.table?.name === t.referredToAs).map(t => t.table)
return mappings.concat({
return {
queryColumn: alias?.name ?? expr.name,
aliasFor: expr.name,
tablesColumnCouldBeFrom:
matchingTables.length === 0 && availableTables.length === 1 ? [availableTables[0].table] : matchingTables,
hasNullableJoin: undefined !== expr.table && nullableJoins.includes(expr.table.name),
})
}
}

if (expr.type === 'call') {
return mappings.concat({
return {
queryColumn: alias?.name ?? expr.function.name,
aliasFor: null,
tablesColumnCouldBeFrom: [],
hasNullableJoin: false,
} satisfies AliasInfo)
}
}

// console.dir({expr}, {depth: null})

return mappings
}, [])
return []
}
}

export const suggestedTags = ({tables, columns}: ReturnType<typeof sqlTablesAndColumns>): string[] => {
Expand Down
8 changes: 4 additions & 4 deletions packages/typegen/test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ test('write types', async () => {
sql\`select t1.id from test_table t1 join test_table t2 on t1.id = t2.n\`,
sql\`select jb->'foo'->>'bar' from test_table\`,
sql\`select n::numeric from test_table\`,
sql\`select n::numeric::text from test_table\`,
sql\`select n::text from test_table\`,
sql\`select * from (values (1, 'one'), (2, 'two')) as vals (num, letter)\`,
sql\`select t from (select id from test_table) t\`,
sql\`
Expand Down Expand Up @@ -118,7 +118,7 @@ test('write types', async () => {
sql<queries.TestTable_id>\`select t1.id from test_table t1 join test_table t2 on t1.id = t2.n\`,
sql<queries.Column>\`select jb->'foo'->>'bar' from test_table\`,
sql<queries.TestTable_n>\`select n::numeric from test_table\`,
sql<queries.TestTable_76>\`select n::numeric::text from test_table\`,
sql<queries.TestTable_76>\`select n::text from test_table\`,
sql<queries.Num_letter>\`select * from (values (1, 'one'), (2, 'two')) as vals (num, letter)\`,
sql<queries.T>\`select t from (select id from test_table) t\`,
sql<queries.TestTable_tAliased1_tNnAliased>\`
Expand Down Expand Up @@ -279,11 +279,11 @@ test('write types', async () => {
/** - query: \`select n::numeric from test_table\` */
export interface TestTable_n {
/** column: \`public.test_table.n\`, regtype: \`integer\` */
/** regtype: \`numeric\` */
n: number | null
}
/** - query: \`select n::numeric::text from test_table\` */
/** - query: \`select n::text from test_table\` */
export interface TestTable_76 {
/** regtype: \`text\` */
n: string | null
Expand Down

0 comments on commit 67a4c5e

Please sign in to comment.