Skip to content

Commit 28f4fa3

Browse files
committed
updates for async
1 parent 8d42485 commit 28f4fa3

File tree

14 files changed

+294
-457
lines changed

14 files changed

+294
-457
lines changed

packages/deparser/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@pgsql/deparser",
2+
"name": "pgsql-deparser",
33
"version": "17.0.0",
44
"author": "Dan Lynch <[email protected]>",
55
"description": "PostgreSQL AST Deparser",
@@ -45,7 +45,7 @@
4545
"database"
4646
],
4747
"devDependencies": {
48-
"@pgsql/parser": "^17.0.0"
48+
"libpg-query": "17.3.0"
4949
},
5050
"dependencies": {
5151
"@pgsql/types": "^17.0.0"

packages/deparser/scripts/make-fixtures-ast.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import * as path from 'path';
33
import * as fs from 'fs';
44
import { sync as globSync } from 'glob';
5-
import { parse, deparse as deparseSync } from '@pgsql/parser';
5+
import { parse } from 'libpg-query';
66
import { ParseResult, RawStmt } from '@pgsql/types';
77
import { cleanTree } from '../src/utils';
88

@@ -18,25 +18,31 @@ function ensureDir(dir: string) {
1818
ensureDir(OUT_DIR);
1919

2020
const fixtures = globSync(`${FIXTURE_DIR}/**/*.sql`);
21-
fixtures.forEach((fixturePath) => {
22-
const relPath = path.relative(FIXTURE_DIR, fixturePath);
23-
const sql = fs.readFileSync(fixturePath, 'utf-8');
24-
let statements: ParseResult;
25-
try {
26-
statements = parse(sql);
27-
} catch (err: any) {
28-
console.error(`Failed to parse ${relPath}:`, err);
29-
return;
21+
22+
async function main() {
23+
for (const fixturePath of fixtures) {
24+
const relPath = path.relative(FIXTURE_DIR, fixturePath);
25+
const sql = fs.readFileSync(fixturePath, 'utf-8');
26+
let statements: ParseResult;
27+
try {
28+
statements = await parse(sql);
29+
} catch (err: any) {
30+
console.error(`Failed to parse ${relPath}:`, err);
31+
continue;
32+
}
33+
34+
for (let idx = 0; idx < statements.stmts.length; idx++) {
35+
const stmt = statements.stmts[idx];
36+
const outDir = path.join(OUT_DIR, path.dirname(relPath));
37+
ensureDir(outDir);
38+
const base = path.basename(relPath, '.sql');
39+
const outName = `${base}-${idx + 1}.sql`;
40+
const outPath = path.join(outDir, outName);
41+
42+
fs.writeFileSync(outPath, JSON.stringify(cleanTree(stmt.stmt), null, 2));
43+
console.log(`Generated ${outPath}`);
44+
}
3045
}
46+
}
3147

32-
statements.stmts.forEach((stmt: RawStmt, idx: number) => {
33-
const outDir = path.join(OUT_DIR, path.dirname(relPath));
34-
ensureDir(outDir);
35-
const base = path.basename(relPath, '.sql');
36-
const outName = `${base}-${idx + 1}.sql`;
37-
const outPath = path.join(outDir, outName);
38-
39-
fs.writeFileSync(outPath, JSON.stringify(cleanTree(stmt.stmt), null, 2));
40-
console.log(`Generated ${outPath}`);
41-
});
42-
});
48+
main().catch(console.error);

packages/deparser/scripts/make-fixtures-sql.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import * as path from 'path';
33
import * as fs from 'fs';
44
import { sync as globSync } from 'glob';
5-
import { parse, deparse as deparseSync } from '@pgsql/parser';
5+
import { parse, deparse } from 'libpg-query';
66
import { ParseResult, RawStmt } from '@pgsql/types';
77

88
const FIXTURE_DIR = path.join(__dirname, '../../../__fixtures__/legacy');
@@ -17,34 +17,40 @@ function ensureDir(dir: string) {
1717
ensureDir(OUT_DIR);
1818

1919
const fixtures = globSync(`${FIXTURE_DIR}/**/*.sql`);
20-
fixtures.forEach((fixturePath) => {
21-
const relPath = path.relative(FIXTURE_DIR, fixturePath);
22-
const sql = fs.readFileSync(fixturePath, 'utf-8');
23-
let statements: ParseResult;
24-
try {
25-
statements = parse(sql);
26-
} catch (err: any) {
27-
console.error(`Failed to parse ${relPath}:`, err);
28-
return;
29-
}
3020

31-
statements.stmts.forEach((stmt: RawStmt, idx: number) => {
32-
const outDir = path.join(OUT_DIR, path.dirname(relPath));
33-
ensureDir(outDir);
34-
const base = path.basename(relPath, '.sql');
35-
const outName = `${base}-${idx + 1}.sql`;
36-
const outPath = path.join(outDir, outName);
37-
let deparsedSql: string;
21+
async function main() {
22+
for (const fixturePath of fixtures) {
23+
const relPath = path.relative(FIXTURE_DIR, fixturePath);
24+
const sql = fs.readFileSync(fixturePath, 'utf-8');
25+
let statements: ParseResult;
3826
try {
39-
deparsedSql = deparseSync({
40-
version: 170000,
41-
stmts: [stmt]
42-
});
27+
statements = await parse(sql);
4328
} catch (err: any) {
44-
console.error(`Failed to deparse statement ${idx + 1} in ${relPath}:`, err);
45-
return;
29+
console.error(`Failed to parse ${relPath}:`, err);
30+
continue;
31+
}
32+
33+
for (let idx = 0; idx < statements.stmts.length; idx++) {
34+
const stmt = statements.stmts[idx];
35+
const outDir = path.join(OUT_DIR, path.dirname(relPath));
36+
ensureDir(outDir);
37+
const base = path.basename(relPath, '.sql');
38+
const outName = `${base}-${idx + 1}.sql`;
39+
const outPath = path.join(outDir, outName);
40+
let deparsedSql: string;
41+
try {
42+
deparsedSql = await deparse({
43+
version: 170000,
44+
stmts: [stmt]
45+
});
46+
} catch (err: any) {
47+
console.error(`Failed to deparse statement ${idx + 1} in ${relPath}:`, err);
48+
continue;
49+
}
50+
fs.writeFileSync(outPath, deparsedSql);
51+
console.log(`Generated ${outPath}`);
4652
}
47-
fs.writeFileSync(outPath, deparsedSql);
48-
console.log(`Generated ${outPath}`);
49-
});
50-
});
53+
}
54+
}
55+
56+
main().catch(console.error);

packages/deparser/scripts/make-fixtures.ts

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import * as path from 'path';
33
import * as fs from 'fs';
44
import { sync as globSync } from 'glob';
5-
import { parse, deparse as deparseSync } from '@pgsql/parser';
5+
import { parse, deparse } from 'libpg-query';
66
import { ParseResult, RawStmt } from '@pgsql/types';
77

88
const FIXTURE_DIR = path.join(__dirname, '../../../__fixtures__/kitchen-sink');
@@ -18,32 +18,39 @@ ensureDir(OUT_DIR);
1818

1919
const fixtures = globSync(`${FIXTURE_DIR}/**/*.sql`);
2020

21-
// Collect deparsed SQL in a single JSON
22-
const results: Record<string, string> = {};
23-
fixtures.forEach((fixturePath) => {
24-
const relPath = path.relative(FIXTURE_DIR, fixturePath);
25-
const sql = fs.readFileSync(fixturePath, 'utf-8');
26-
let parseResult: ParseResult;
27-
try {
28-
parseResult = parse(sql);
29-
} catch (err: any) {
30-
console.error(`Failed to parse ${relPath}:`, err);
31-
return;
32-
}
33-
parseResult.stmts.forEach((stmt: RawStmt, idx: number) => {
34-
let deparsedSql: string;
21+
async function main() {
22+
// Collect deparsed SQL in a single JSON
23+
const results: Record<string, string> = {};
24+
25+
for (const fixturePath of fixtures) {
26+
const relPath = path.relative(FIXTURE_DIR, fixturePath);
27+
const sql = fs.readFileSync(fixturePath, 'utf-8');
28+
let parseResult: ParseResult;
3529
try {
36-
deparsedSql = deparseSync({ version: 170000, stmts: [stmt] });
30+
parseResult = await parse(sql);
3731
} catch (err: any) {
38-
console.error(`Failed to deparse statement ${idx + 1} in ${relPath}:`, err);
39-
return;
32+
console.error(`Failed to parse ${relPath}:`, err);
33+
continue;
34+
}
35+
36+
for (let idx = 0; idx < parseResult.stmts.length; idx++) {
37+
const stmt = parseResult.stmts[idx];
38+
let deparsedSql: string;
39+
try {
40+
deparsedSql = await deparse({ version: 170000, stmts: [stmt] });
41+
} catch (err: any) {
42+
console.error(`Failed to deparse statement ${idx + 1} in ${relPath}:`, err);
43+
continue;
44+
}
45+
const key = `${relPath.replace(/\.sql$/, '')}-${idx + 1}.sql`;
46+
results[key] = deparsedSql;
4047
}
41-
const key = `${relPath.replace(/\.sql$/, '')}-${idx + 1}.sql`;
42-
results[key] = deparsedSql;
43-
});
44-
});
48+
}
49+
50+
// Write aggregated JSON to output file
51+
const outputFile = path.join(OUT_DIR, 'generated.json');
52+
fs.writeFileSync(outputFile, JSON.stringify(results, null, 2));
53+
console.log(`Wrote JSON to ${outputFile}`);
54+
}
4555

46-
// Write aggregated JSON to output file
47-
const outputFile = path.join(OUT_DIR, 'generated.json');
48-
fs.writeFileSync(outputFile, JSON.stringify(results, null, 2));
49-
console.log(`Wrote JSON to ${outputFile}`);
56+
main().catch(console.error);

packages/deparser/scripts/make-kitchen-sink.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
import * as path from 'path';
33
import * as fs from 'fs';
44
import { sync as globSync } from 'glob';
5-
import { parse, deparse as deparseSync } from '@pgsql/parser';
5+
import { parse } from 'libpg-query';
66
import { ParseResult, RawStmt } from '@pgsql/types';
7-
import { cleanTree } from '../src/utils';
87

98
const FIXTURE_DIR = path.join(__dirname, '../../../__fixtures__/kitchen-sink');
109
const OUT_DIR = path.join(__dirname, '../__tests__/kitchen-sink');
@@ -22,30 +21,35 @@ const generateTestFile = (name: string, tests: string[]) => {
2221
import { FixtureTestUtils } from '../../test-utils';
2322
const fixtures = new FixtureTestUtils();
2423
25-
it('${name}', () => {
26-
fixtures.runFixtureTests(${JSON.stringify(tests, null, 2)});
24+
it('${name}', async () => {
25+
await fixtures.runFixtureTests(${JSON.stringify(tests, null, 2)});
2726
});
2827
`
2928
};
3029

3130
const fixtures = globSync(`${FIXTURE_DIR}/**/*.sql`);
32-
fixtures.forEach((fixturePath) => {
33-
const relPath = path.relative(FIXTURE_DIR, fixturePath);
34-
const sql = fs.readFileSync(fixturePath, 'utf-8');
35-
let statements: ParseResult;
36-
try {
37-
statements = parse(sql);
38-
} catch (err: any) {
39-
console.error(`Failed to parse ${relPath}:`, err);
40-
return;
41-
}
4231

43-
const names = statements.stmts.map((stmt: RawStmt, idx: number) => {
44-
const outName = `${relPath.replace(/\.sql$/, '')}-${idx + 1}.sql`;
45-
return outName;
46-
});
32+
async function main() {
33+
for (const fixturePath of fixtures) {
34+
const relPath = path.relative(FIXTURE_DIR, fixturePath);
35+
const sql = fs.readFileSync(fixturePath, 'utf-8');
36+
let statements: ParseResult;
37+
try {
38+
statements = await parse(sql);
39+
} catch (err: any) {
40+
console.error(`Failed to parse ${relPath}:`, err);
41+
continue;
42+
}
43+
44+
const names = statements.stmts.map((stmt: RawStmt, idx: number) => {
45+
const outName = `${relPath.replace(/\.sql$/, '')}-${idx + 1}.sql`;
46+
return outName;
47+
});
48+
49+
const testFileName = relPath.replace(/\//g, '-').replace(/\.sql$/, '');
50+
const testFile = generateTestFile(testFileName, names);
51+
fs.writeFileSync(path.join(OUT_DIR, `${testFileName}.test.ts`), testFile);
52+
}
53+
}
4754

48-
const testFileName = relPath.replace(/\//g, '-').replace(/\.sql$/, '');
49-
const testFile = generateTestFile(testFileName, names);
50-
fs.writeFileSync(path.join(OUT_DIR, `${testFileName}.test.ts`), testFile);
51-
});
55+
main().catch(console.error);

packages/deparser/src/utils/list-utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Node } from '@pgsql/types';
2-
31
export class ListUtils {
42
static unwrapList(obj: any): any[] {
53
if (obj === undefined || obj === null) {

packages/deparser/test-utils/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { parse } from '@pgsql/parser';
1+
import { parse } from 'libpg-query';
22
import { deparse } from '../src';
3-
import { cleanTree, cleanLines } from '../src/utils';
3+
import { cleanTree } from '../src/utils';
44
import { readFileSync } from 'fs';
55
import * as path from 'path';
66
import { expect } from '@jest/globals';
@@ -81,9 +81,9 @@ export class TestUtils {
8181
console.error(errMessage.join('\n'));
8282
}
8383

84-
tryParse(sql: string) {
84+
async tryParse(sql: string) {
8585
try {
86-
return parse(sql);
86+
return await parse(sql);
8787
} catch (err: any) {
8888
if (err.cursorPosition) {
8989
this.printErrorMessage(sql, err.cursorPosition);
@@ -92,17 +92,17 @@ export class TestUtils {
9292
}
9393
}
9494

95-
expectAstMatch(testName: string, sql: string) {
95+
async expectAstMatch(testName: string, sql: string) {
9696
let tree: any;
9797
try {
9898
tree = this.tryParse(sql);
9999
if (tree.stmts) {
100-
tree.stmts.forEach((stmt: any) => {
100+
tree.stmts.forEach(async (stmt: any) => {
101101
if (stmt.stmt) {
102102
const outSql = deparse(stmt.stmt);
103103
let reparsed;
104104
try {
105-
reparsed = parse(outSql);
105+
reparsed = await parse(outSql);
106106
} catch (parseErr) {
107107
throw createParseError(
108108
'INVALID_DEPARSED_SQL',
@@ -205,14 +205,14 @@ export class FixtureTestUtils extends TestUtils {
205205
);
206206
}
207207

208-
runFixtureTests(filters: string[]) {
208+
async runFixtureTests(filters: string[]) {
209209
if (filters.length === 0) {
210210
console.log('no filters provided, skipping tests.');
211211
return;
212212
}
213-
this.getTestEntries(filters).forEach(([relativePath, sql]) => {
213+
this.getTestEntries(filters).forEach(async ([relativePath, sql]) => {
214214
try {
215-
this.expectAstMatch(relativePath, sql);
215+
await this.expectAstMatch(relativePath, sql);
216216
} catch (err) {
217217
throw err;
218218
}

packages/enums/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@pgsql/enums",
2+
"name": "pgsql-enums",
33
"version": "17.0.0",
44
"author": "Dan Lynch <[email protected]>",
55
"description": "PostgreSQL AST enums for pgsql-parser",

0 commit comments

Comments
 (0)