diff --git a/Makefile.am b/Makefile.am index 15637bc..80d232c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -11,6 +11,7 @@ pkgconfig_DATA = libsqlpars.pc # noinst_PROGRAMS = $(TESTS) EXTRA_DIST = sql.y sql.l \ + pscan.js \ test-ok.sh test-fail.sh \ fail1.sql \ fail3.sql \ @@ -25,6 +26,7 @@ EXTRA_DIST = sql.y sql.l \ ok6.sql \ ok7.sql \ ok8.sql \ + ok9.sql \ tst-create-db.sql \ tst-create-table1.sql diff --git a/ok9.sql b/ok9.sql new file mode 100644 index 0000000..c2c1dbb --- /dev/null +++ b/ok9.sql @@ -0,0 +1,5 @@ +CREATE TEMPORARY TABLE IF NOT EXISTS mytab ( + i INT PRIMARY KEY, + t TEXT NOT NULL, + s VARCHAR(100) DEFAULT 'buzz' +); diff --git a/pscan.js b/pscan.js new file mode 100755 index 0000000..0525780 --- /dev/null +++ b/pscan.js @@ -0,0 +1,94 @@ +#!/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 (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, + n_cols: current.n_cols, + columns: [], + }; + var wantCols = objTable.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 == 'STMT') { + const stmt = stk.pop(); + assert(stk.length === 0); + + ret = stmt; + } 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) + "\n"); +