-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
137 lines (117 loc) · 4.42 KB
/
index.js
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
#!/usr/bin/env node
const commandLineArgs = require("command-line-args")
const commandLineUsage = require("command-line-usage")
const fs = require("fs")
const jq = require("node-jq")
const parser = require("pg-query-parser")
const readline = require("readline")
const R = require("ramda")
function buildSubqueryAsts(selectStmt, subqueries) {
if (selectStmt.withClause !== undefined) {
const ctes = selectStmt.withClause.WithClause.ctes
const recursive = selectStmt.withClause.WithClause.recursive !== undefined &&
selectStmt.withClause.WithClause.recursive
const injectWithClause = R.evolve({SelectStmt: R.merge(R.__, {withClause: {WithClause: {ctes, recursive}}})})
const subAsts = R.map(injectWithClause, subqueries)
const r = R.range(1, R.length(ctes) + 1)
const makePartialQueryAst = R.pipe(
R.take(R.__, ctes)
, cteSubset => {
const allFields = [{
ResTarget: {
val: {ColumnRef: {fields: [{"A_Star": {}}]}}
}
}]
const fromClause = [{RangeVar: {relname: R.last(cteSubset).CommonTableExpr.ctename}}]
return {
SelectStmt: {
withClause: {WithClause: {ctes: cteSubset, recursive}}
, targetList: allFields
, fromClause: fromClause
, op: 0
}
}
}
);
return R.concat(R.map(makePartialQueryAst, r), subAsts)
}
else {
return subqueries
}
}
function transformQueryAst(ast, query) {
//console.log(JSON.stringify(ast.query[0].SelectStmt, null, 2))
//console.log("Getting subqueries from", JSON.stringify(R.pick(["fromClause"], ast.query[0].SelectStmt)))
jq.run("[ .. | .subquery? | select(. != null) | select(length > 0) ]"
, R.pick(["fromClause"], ast.query[0].SelectStmt)
, { input: "json", output: "json" })
.then((subqueries) => {
const subqueryAsts = buildSubqueryAsts(ast.query[0].SelectStmt, subqueries)
const fullQueryList = R.append(query, R.map(R.pipe(Array, parser.deparse), subqueryAsts))
console.log(fullQueryList.join(";\n"))
})
}
function getQuery(options) {
if (options.command !== undefined) {
return Promise.resolve(options.command)
}
else {
return new Promise((resolve, reject) => {
let q = ""
const rl = readline.createInterface({
input: options.file !== undefined ? fs.createReadStream(options.file) : process.stdin
, output: process.stdout
, terminal: false
})
rl.on("line", line => q += line + "\n")
rl.on("close", () => resolve(q))
})
}
}
const cmdLineDefinitions = [
{ name: "ast", type: Boolean, description: "Output source query AST in JSON format instead of SQL statements"}
, { name: "command", alias: "c", type: String, description: "SQL command to process" }
, { name: "file", alias: "f", type: String, description: "File name to read SQL command from" }
, { name: "interactive", alias: "i", type: Boolean, description: "Read SQL from stdin" }
]
const helpSections = [
{
header: "pgdebug",
content: "Transforms input SQL query into a series of queries returning rows for each CTE and each subquery in the FROM clause"
},
{
header: "Options",
optionList: cmdLineDefinitions
}
]
// ACTION BEGINS HERE
const options = commandLineArgs(cmdLineDefinitions)
if (R.isEmpty(options)) {
console.log(commandLineUsage(helpSections))
process.exit(1)
return
}
getQuery(options).then(q => {
if (R.isEmpty(R.trim(q))) {
console.error("No query supplied")
process.exit(1)
return
}
const ast = parser.parse(q)
if (options.ast) {
console.log(JSON.stringify(ast, null, 2))
}
else {
if (ast.error !== undefined) {
console.error("Parsing error: ", JSON.stringify(ast.error))
process.exit(1)
return
}
else if (ast.query[0].SelectStmt === undefined) {
console.error("Can only process SELECT statements")
process.exit(1)
return
}
transformQueryAst(ast, q)
}
})