Skip to content
This repository was archived by the owner on Jul 15, 2021. It is now read-only.

Commit 6880085

Browse files
committed
Ability to construct single JSON object. Refs #27
Ability to construct a single statement list node containing all the statements parsed by the stream transform. Uses a second transform available as createStitcher(). This transform separates each statement AST with a comma and then wraps the entire list in a statement list node: ``` json { "type": "statement", "variant": "list", "statement": [] } ```
1 parent 23a7828 commit 6880085

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/streaming.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import { parse, SyntaxError as PegSyntaxError } from './parser';
55
import { Tracer } from './tracer';
6-
import { SqliteParserTransform } from './streaming';
6+
import { SqliteParserTransform, SingleNodeTransform } from './streaming';
77

88
export default function sqliteParser(source, options, callback) {
99
const t = Tracer();
@@ -44,5 +44,9 @@ sqliteParser['createParser'] = function () {
4444
return new SqliteParserTransform();
4545
};
4646

47+
sqliteParser['createStitcher'] = function () {
48+
return new SingleNodeTransform();
49+
};
50+
4751
sqliteParser['NAME'] = 'sqlite-parser';
4852
sqliteParser['VERSION'] = '@@VERSION';

src/streaming.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class SqliteParserTransform extends Transform {
3434
if (nextAst != null) {
3535
let serialized;
3636
try {
37-
serialized = JSON.stringify(nextAst);
37+
serialized = JSON.stringify(nextAst, null, 2);
3838
} catch (e) {
3939
// Serialize error
4040
return callback(e);
@@ -56,3 +56,26 @@ export class SqliteParserTransform extends Transform {
5656
callback();
5757
}
5858
}
59+
60+
export class SingleNodeTransform extends Transform {
61+
constructor(options) {
62+
super(options);
63+
this.push(`{\n "type": "statement",\n "variant": "list",\n "statement": [\n `);
64+
this.queries = 0;
65+
}
66+
67+
_transform(data, encoding, callback) {
68+
data = data.toString();
69+
if (this.queries !== 0) {
70+
data = `,\n${data}`;
71+
}
72+
this.queries += 1;
73+
this.push(data.replace(/\n/g, '\n '));
74+
callback();
75+
}
76+
77+
_flush(callback) {
78+
this.push(`\n ]\n}`);
79+
callback();
80+
}
81+
}

0 commit comments

Comments
 (0)