-
Notifications
You must be signed in to change notification settings - Fork 35
/
pscan.js
executable file
·223 lines (180 loc) · 4.39 KB
/
pscan.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
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
#!/usr/bin/env node
const fs = require('fs');
const assert = require('assert');
const lines = fs.readFileSync(process.argv[2], {encoding:'utf8'}).split("\n");
function getRecordStream(lineList)
{
var recs = [];
const blankRx = /^\s*$/;
lineList.forEach(function(line) {
if (blankRx.test(line))
return;
try {
const record = JSON.parse(line);
recs.push(record);
} catch (e) {
console.error("Invalid line: ", line);
console.error(e);
}
});
return recs;
}
function parseRecordStream(recs)
{
var stk = [];
var ret = null;
recs.forEach(function(current) {
if (current.op && current.op == 'DEFINE-COL') {
var objColumn = {
name: current.name,
flags: current.flags,
attr: [],
};
while (stk.length > 0) {
const rec = stk.pop();
if ('attr' in rec)
objColumn.attr.push(rec);
else if ('type' in rec)
objColumn.type = rec.type;
else if (rec.op && rec.op == 'START-COL')
break;
else {
assert.fail('unhandled rec: ' + JSON.stringify(rec));
}
}
stk.push(objColumn);
} else if (current.op && current.op == 'CREATE-TABLE') {
var objTable = {
verb: 'CREATE_TABLE',
name: current.name,
temp: current.temp,
if_n_exists: current.if_n_exists,
columns: [],
};
var wantCols = current.n_cols;
while (wantCols-- > 0) {
const col = stk.pop();
assert(col !== undefined);
objTable.columns.push(col);
}
stk.push(objTable);
} else if (current.op && current.op == 'DROP-TABLE') {
var objTab = {
verb: 'DROP_TABLE',
temp: current.temp,
if_exists: current.if_exists,
tables: [],
};
var wantTabs = current.n_tables;
while (wantTabs-- > 0) {
const tab = stk.pop();
assert(tab !== undefined);
objTab.tables.push(tab.name);
}
stk.push(objTab);
} else if (current.op && current.op == 'INSERT') {
var objInsert = {
verb: 'INSERT',
tbl_name: current.tbl_name,
opts: current.opts,
values: [],
columns: [],
};
var wantVals = current.n_vals;
while (wantVals-- > 0) {
const val = stk.pop();
assert(val !== undefined);
objInsert.values.unshift(val.values);
}
var wantCols = current.n_cols;
assert(wantCols <= 1);
if (wantCols > 0) {
const val = stk.pop();
assert(val !== undefined);
objInsert.columns = val;
}
stk.push(objInsert);
} else if ('INSERT-COLS' in current) {
var arrCols = [];
var wantVals = current['INSERT-COLS'];
while (wantVals-- > 0) {
const val = stk.pop();
assert(val !== undefined);
arrCols.unshift(val['COLUMN']);
}
stk.push(arrCols);
} else if (current.op && current.op == 'CREATE-DB') {
var objDb = {
verb: 'CREATE_DATABASE',
name: current.name,
if_ne: current.if_ne,
};
stk.push(objDb);
} else if (current.op && current.op == 'DROP-DB') {
var objDb = {
verb: 'DROP_DATABASE',
name: current.name,
if_exists: current.if_exists,
};
stk.push(objDb);
} else if (current.op && current.op == 'SELECT') {
var objSelect = {
verb: 'SELECT',
opts: current.opts,
tables: [],
exprs: [],
};
var wantTabs = current.n_tbl_ref;
while (wantTabs-- > 0) {
const tab = stk.pop();
assert(tab !== undefined);
objSelect.tables.push(tab.name);
}
var wantExpr = current.n_expr;
while (wantExpr-- > 0) {
const an_expr = stk.pop();
assert(an_expr !== undefined);
objSelect.exprs.push(an_expr);
}
stk.push(objSelect);
} else if (current.op && current.op == 'STMT') {
const stmt = stk.pop();
assert(stk.length === 0);
ret = stmt;
} else if ('INT/NUMBER' in current) {
const objVal = {
type: 'number',
val: current['INT/NUMBER'],
};
stk.push(objVal);
} else if ('STRING' in current) {
const objVal = {
type: 'string',
val: current.STRING,
};
stk.push(objVal);
} else if ('VALUES' in current) {
const objVal = {
type: 'value_list',
values: [],
};
var wantVals = current.VALUES;
while (wantVals-- > 0) {
const val = stk.pop();
assert(val !== undefined);
objVal.values.unshift(val);
}
stk.push(objVal);
} else if ('result' in current) {
// EOF; do nothing
} else {
// console.log("PUSH", current);
stk.push(current);
}
});
return ret;
}
const recs = getRecordStream(lines);
// console.dir(recs);
const stmt = parseRecordStream(recs);
console.log(JSON.stringify(stmt, null, 2));