-
Notifications
You must be signed in to change notification settings - Fork 8
/
parse-pg-query.ts
234 lines (198 loc) · 7.11 KB
/
parse-pg-query.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import * as QueryParser from "pg-query-parser"
import { augmentFileValidationError, augmentQuerySyntaxError } from "../errors"
import { ColumnReference, Query, SourceFile, TableReference, QuerySourceMapSpan } from "../types"
import { resolvePropertyTypes } from "../typescript/objectish"
import { placeholderColumnName } from "../utils"
import {
createQueryNodePath,
createQueryNodeSubpath,
getQueryPathParent,
traverseSubTree,
QueryNodePath
} from "./query-traversal"
import {
filterDuplicateTableRefs,
resolveColumnReference,
resolveResTarget
} from "./resolve-references"
import {
getNodeType,
isColumnRef,
isCommonTableExpr,
isParamRef,
isRelationRef,
isResTarget,
isSelectStmt
} from "./pg-node-types"
interface ExpressionSpreadTypes {
[paramID: number]: ReturnType<typeof resolvePropertyTypes> | null
}
interface QueryContext {
expressionSpreadTypes: ExpressionSpreadTypes
query: string
sourceFile: SourceFile
sourceMap: QuerySourceMapSpan[]
}
const $any = Symbol("any")
export const spreadTypeAny: ReturnType<typeof resolvePropertyTypes> = Object.defineProperty(
{},
$any,
{
enumerable: false,
value: true
}
)
const isPlaceholderSelect = (node: QueryParser.QueryNode<any>) =>
isSelectStmt(node) &&
!node.SelectStmt.fromClause &&
node.SelectStmt.targetList &&
node.SelectStmt.targetList.length === 1 &&
isResTarget(node.SelectStmt.targetList[0]) &&
isParamRef(node.SelectStmt.targetList[0].ResTarget.val)
const isSubquery = (node: QueryParser.QueryNode<any>) =>
getNodeType(node).endsWith("Stmt") && !isPlaceholderSelect(node)
const isReturningIntoParentQuery = (path: QueryNodePath<QueryParser.QueryNode<any>>) =>
["larg", "rarg", "selectStmt"].indexOf(path.parentPropKey) > -1
function getSubqueries(
statementPath: QueryNodePath<QueryParser.Query>
): Array<QueryNodePath<QueryParser.Query>> {
const subqueries: Array<QueryNodePath<QueryParser.Query>> = []
traverseSubTree(statementPath, (path, $cancelRecursion) => {
if (path.node === statementPath.node) return
if (isSubquery(path.node)) {
subqueries.push(path)
return $cancelRecursion
}
})
return subqueries
}
function getTableReferences(
statementPath: QueryNodePath<any>,
includeSubQueries: boolean
): Array<QueryNodePath<QueryParser.RelationRef>> {
const referencedTables: Array<QueryNodePath<QueryParser.RelationRef>> = []
traverseSubTree(statementPath, (path, $cancelRecursion) => {
const { node } = path
if (isRelationRef(node)) {
referencedTables.push(path)
} else if (!includeSubQueries) {
if (node !== statementPath.node && isSubquery(node)) return $cancelRecursion
}
})
return referencedTables
}
function getReferencedColumns(
statement: QueryNodePath<QueryParser.Query>,
spreadTypes: ExpressionSpreadTypes
): ColumnReference[] {
const referencedColumns: ColumnReference[] = []
traverseSubTree(statement, (path, $cancelRecursion) => {
const spreadType = isParamRef(path.node) ? spreadTypes[path.node.ParamRef.number] : null
if (path.node === statement.node) return
if (isSubquery(path.node)) return $cancelRecursion
if (isColumnRef(path.node)) {
const relationRefs = getTableReferences(statement, false)
const columnRef = resolveColumnReference(path, relationRefs)
if (columnRef && columnRef.columnName !== placeholderColumnName) {
referencedColumns.push(columnRef)
}
} else if (isResTarget(path.node) && path.node.ResTarget.name) {
const relationRefs = getTableReferences(statement, false)
const columnRef = resolveResTarget(path, relationRefs)
if (columnRef && columnRef.columnName !== placeholderColumnName) {
referencedColumns.push(columnRef)
}
} else if (spreadType) {
const relationRefs = getTableReferences(statement, false)
const tableRefsInScope: TableReference[] = relationRefs.map(ref => ({
as: ref.node.RangeVar.alias ? ref.node.RangeVar.alias.Alias.aliasname : undefined,
tableName: ref.node.RangeVar.relname,
path
}))
for (const spreadArgKey of Object.keys(spreadType)) {
referencedColumns.push({
tableRefsInScope: filterDuplicateTableRefs(tableRefsInScope),
columnName: spreadArgKey,
path
})
}
if (spreadType === spreadTypeAny || !spreadType) {
referencedColumns.push({
any: true,
tableRefsInScope: filterDuplicateTableRefs(tableRefsInScope),
columnName: "",
path
})
}
}
})
return referencedColumns
}
function getReturningColumns(statement: QueryNodePath<QueryParser.Query>): ColumnReference[] {
const body = (statement.node as any)[statement.type]
const relationRefs = getTableReferences(statement, false)
const { returningList = [], targetList = [] } = body
const resTargets: QueryParser.ResTarget[] = [...returningList, ...targetList].filter(
node => getNodeType(node) === "ResTarget"
)
return resTargets
.map(resTarget => {
const propKey = returningList.indexOf(resTarget) > -1 ? "returningList" : "targetList"
const resTargetPath = createQueryNodeSubpath(statement, resTarget, propKey)
return resolveResTarget(resTargetPath, relationRefs)
})
.filter((ref): ref is ColumnReference => ref !== null)
}
function instantiateQuery(path: QueryNodePath<QueryParser.Query>, context: QueryContext): Query {
const referencedColumns = getReferencedColumns(path, context.expressionSpreadTypes)
const referencedTables = getTableReferences(path, true).map(tableRef => ({
tableName: tableRef.node.RangeVar.relname,
path: tableRef
}))
const returnedColumns = getReturningColumns(path)
const subqueries: Query[] = getSubqueries(path).map(subqueryPath =>
instantiateQuery(subqueryPath, context)
)
const type = getNodeType(path.node)
.replace(/Stmt$/, "")
.toUpperCase()
const parent = getQueryPathParent(path)
const exposedAsTable =
parent && isCommonTableExpr(parent.node) ? parent.node.CommonTableExpr.ctename : undefined
return {
type,
path,
exposedAsTable,
referencedColumns,
referencedTables,
returnedColumns,
returnsIntoParentQuery: isReturningIntoParentQuery(path),
query: context.query,
sourceFile: context.sourceFile,
sourceMap: context.sourceMap,
subqueries
}
}
export function parsePostgresQuery(
queryString: string,
sourceFile: SourceFile,
sourceMap: QuerySourceMapSpan[],
spreadTypes: ExpressionSpreadTypes
): Query {
const context: QueryContext = {
expressionSpreadTypes: spreadTypes,
query: queryString,
sourceFile,
sourceMap
}
const result = QueryParser.parse(queryString)
if (result.error) {
const fakePath = createQueryNodePath({ SelectStmt: { op: 0 } }, [], "")
const query = instantiateQuery(fakePath, context)
const error = new Error(`Syntax error in SQL query.\nSubstituted query: ${queryString.trim()}`)
throw augmentFileValidationError(augmentQuerySyntaxError(error, result.error, query), query)
}
const parsedQuery = result.query[0]
const queryPath = createQueryNodePath(parsedQuery, [], "")
return instantiateQuery(queryPath, context)
}