Skip to content

Commit

Permalink
rename (updated 12:35)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Sep 5, 2024
1 parent 5f987f6 commit 2fc2d83
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
10 changes: 5 additions & 5 deletions packages/typegen/src/query/analyze-select-statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {createHash} from 'crypto'
import * as lodash from 'lodash'
import {parse, toSql} from 'pgsql-ast-parser'
import {z} from 'zod'
import {aliasMappings, getASTModifiedToSingleSelect, ModifiedAST} from './parse'
import {getAliasInfo, getASTModifiedToSingleSelect, ModifiedAST} from './parse'

/**
* Returns a list of results that stem from a special query used to retrieve type information from the database.
Expand Down Expand Up @@ -44,14 +44,14 @@ export const analyzeSelectStatement = async (
const modifiedAst = getASTModifiedToSingleSelect(toSql.statement(statement))
const analyzed = await analyzeSelectStatement(tx, modifiedAst)

const statementAliasInfo = aliasMappings(statement)
const aliasInfoList = getAliasInfo(statement)
const aliasList = analyzed[0].column_aliases

const tempTableColumns = aliasList.map(aliasName => {
const found = statementAliasInfo.find(info => info.queryColumn === aliasName)
if (!found) throw new Error(`Alias ${aliasName} not found in statement`)
const aliasInfo = aliasInfoList.find(info => info.queryColumn === aliasName)
if (!aliasInfo) throw new Error(`Alias ${aliasName} not found in statement`)

const analyzedResult = analyzed.find(a => a.table_column_name === found.aliasFor)
const analyzedResult = analyzed.find(a => a.table_column_name === aliasInfo.aliasFor)
if (!analyzedResult) throw new Error(`Alias ${aliasName} not found in analyzed results`)

const def = `${aliasName} ${analyzedResult.underlying_data_type} ${analyzedResult.is_underlying_nullable === 'NO' ? 'not null' : ''}`
Expand Down
4 changes: 2 additions & 2 deletions packages/typegen/src/query/column-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {tryOrDefault} from '../util'
import {memoizeQueryFn} from '../utils/memoize'
import {SelectStatementAnalyzedColumn, analyzeSelectStatement} from './analyze-select-statement'
import {
aliasMappings,
getAliasInfo,
getASTModifiedToSingleSelect,
getSuggestedTags,
isParseable,
Expand Down Expand Up @@ -58,7 +58,7 @@ const getFieldInfo = (
// console.warn('using originalAst', {formattedQuery, originalSql})
}

const mappings = aliasMappings(ast)
const mappings = getAliasInfo(ast)

const relatedResults = mappings.flatMap(c =>
selectStatementColumns
Expand Down
27 changes: 23 additions & 4 deletions packages/typegen/src/query/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,29 @@ const expressionName = (ex: pgsqlAST.Expr): string | undefined => {
return undefined
}

export interface AliasMapping {
export interface AliasInfo {
/**
* The column name in the query,
* - e.g. in `select a as x from foo` this would be x
* - e.g. in `select a from foo` this would be a
*/
queryColumn: string
/**
* The column name in the query,
* - e.g. in `select a as x from foo` this would be a
*/
aliasFor: string
/**
* The table name(s) the column could be from,
* - e.g. in `select a from foo` this would be foo
* - e.g. in `select a from foo join bar on foo.id = bar.id` this would be foo and bar
*/
tablesColumnCouldBeFrom: string[]
/**
* Whether the column could be nullable via a join,
* - e.g. in `select a from foo` this would be false
* - e.g. in `select a from foo left join bar on foo.id = bar.id` this would be true
*/
hasNullableJoin: boolean
}
/**
Expand All @@ -217,7 +236,7 @@ export interface AliasMapping {
* list of `tablesColumnCouldBeFrom`. For simple queries like `select id from messages` it'll get sensible results, though, and those
* results can be used to look for non-nullability of columns.
*/
export const aliasMappings = (statement: pgsqlAST.Statement): AliasMapping[] => {
export const getAliasInfo = (statement: pgsqlAST.Statement): AliasInfo[] => {
assert.strictEqual(statement.type, 'select' as const)
assert.ok(statement.columns, `Can't get alias mappings from query with no columns`)

Expand Down Expand Up @@ -262,7 +281,7 @@ export const aliasMappings = (statement: pgsqlAST.Statement): AliasMapping[] =>
`Some aliases are duplicated, this is too confusing. ${JSON.stringify({aliasGroups})}`,
)

return statement.columns.reduce<AliasMapping[]>((mappings, {expr, alias}) => {
return statement.columns.reduce<AliasInfo[]>((mappings, {expr, alias}) => {
if (expr.type === 'ref') {
return mappings.concat({
queryColumn: alias?.name ?? expr.name,
Expand Down Expand Up @@ -294,7 +313,7 @@ export const suggestedTags = ({tables, columns}: ReturnType<typeof sqlTablesAndC

export const getSuggestedTags = lodash.flow(templateToValidSql, sqlTablesAndColumns, suggestedTags)

export const getAliasMappings = lodash.flow(getASTModifiedToSingleSelect, m => m.ast, aliasMappings)
export const getAliasMappings = lodash.flow(getASTModifiedToSingleSelect, m => m.ast, getAliasInfo)

export const removeSimpleComments = (sql: string) =>
sql
Expand Down

0 comments on commit 2fc2d83

Please sign in to comment.