### Install @pgsql/utils Source: https://github.com/constructive-io/pgsql-parser/blob/main/README.md Install the package for programmatic AST construction. ```bash npm install @pgsql/utils ``` -------------------------------- ### Install Dependencies and Build Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/transform/FIXING.md Install project dependencies and build the project using pnpm. ```bash pnpm pnpm build ``` -------------------------------- ### Install pg-proto-parser Source: https://github.com/constructive-io/pgsql-parser/blob/main/README.md Install the package for protobuf parsing and code generation. ```bash npm install pg-proto-parser ``` -------------------------------- ### Install and Build Packages Source: https://github.com/constructive-io/pgsql-parser/blob/main/AGENTS.md Standard commands to install dependencies, build all packages, run tests, and lint the codebase. ```bash pnpm install pnpm run build # builds all packages (includes code generation) pnpm run test # runs all package tests pnpm run lint # lints all packages ``` -------------------------------- ### Install plpgsql-parser Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-parser/README.md Install the package using npm. This is the first step before using any of its functionalities. ```bash npm install plpgsql-parser ``` -------------------------------- ### Installation Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-deparser/README.md Install the plpgsql-deparser package using npm. ```APIDOC ## Installation ```bash npm install plpgsql-deparser ``` ``` -------------------------------- ### Install @pgsql/transform Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/transform/README.md Install the package using npm. ```bash npm install @pgsql/transform ``` -------------------------------- ### Install @pgsql/traverse Source: https://github.com/constructive-io/pgsql-parser/blob/main/README.md Install the package for programmatic AST visiting and traversal. ```bash npm install @pgsql/traverse ``` -------------------------------- ### Install @pgsql/quotes Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/quotes/README.md Install the package using npm. ```bash npm install @pgsql/quotes ``` -------------------------------- ### Install Dependencies and Build Source: https://github.com/constructive-io/pgsql-parser/blob/main/README.md Use pnpm to install all project dependencies and build all packages within the monorepo. ```bash pnpm install pnpm build ``` -------------------------------- ### Install pgsql-parser Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/parser/README.md Install the pgsql-parser package using npm. ```sh npm install pgsql-parser ``` -------------------------------- ### Integration Example Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/traverse/README.md Demonstrates how to integrate the traversal utilities with the `pgsql-parser` for parsing SQL and visiting AST nodes. ```APIDOC ## Integration with pgsql-parser This package is designed to work seamlessly with the pgsql-parser ecosystem: ```typescript import { parse } from 'pgsql-parser'; import { visit } from '@pgsql/traverse'; const sql = 'SELECT name, email FROM users WHERE age > 18'; const ast = await parse(sql); const visitor = { RangeVar: (node) => { console.log('Table:', node.relname); }, ColumnRef: (node) => { console.log('Column:', node.fields?.[0]?.String?.sval); } }; visit(ast, visitor); ``` ``` -------------------------------- ### Install plpgsql-parse Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-parse/README.md Install the package using npm. ```sh npm install plpgsql-parse ``` -------------------------------- ### Install and Build Monorepo Source: https://github.com/constructive-io/pgsql-parser/blob/main/PUBLISH.md Installs all dependencies and builds all packages in the correct order using Lerna. This is a prerequisite before publishing. ```bash pnpm install && pnpm build ``` -------------------------------- ### Install plpgsql-deparser Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-deparser/README.md Install the package using npm. ```bash npm install plpgsql-deparser ``` -------------------------------- ### Install Dependencies with pnpm Source: https://github.com/constructive-io/pgsql-parser/blob/main/DEVELOPMENT.md Run this command in the project root to install all necessary dependencies for the monorepo. ```bash pnpm install ``` -------------------------------- ### Add SQL Fixture Example Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-deparser/AGENTS.md Example of a PL/pgSQL function definition to be added to fixture files. ```sql -- Test N: Description of what this tests CREATE FUNCTION test_example(p_input text, OUT result text) LANGUAGE plpgsql AS $$ BEGIN result := p_input; RETURN; END$$; ``` -------------------------------- ### Install pg-ast Package Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pg-ast/README.md Use this npm command to add the pg-ast library to your project. ```bash npm install pg-ast ``` -------------------------------- ### Install pgsql-parse Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/parse/README.md Install the package using npm. This command adds the library to your project dependencies. ```sh npm install pgsql-parse ``` -------------------------------- ### Install pgsql-deparser Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/deparser/README.md Install the latest version of pgsql-deparser using npm. ```sh npm install pgsql-deparser ``` -------------------------------- ### Quick Start: Download and Process Proto File Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pgsql-cli/README.md Fetch a PostgreSQL protobuf file from a URL and process it using `pgsql proto-fetch`. Specify the URL, input file name, and output file name for the generated JavaScript. ```bash pgsql proto-fetch --url https://raw.githubusercontent.com/pganalyze/libpg_query/16-latest/protobuf/pg_query.proto --inFile pg_query.proto --outFile pg_query.js ``` -------------------------------- ### Install @pgsql/cli Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pgsql-cli/README.md Install the @pgsql/cli globally using npm. This command makes the `pgsql` executable available in your terminal. ```bash npm install -g @pgsql/cli ``` -------------------------------- ### Commit Fixture Files Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-deparser/AGENTS.md Example git commands to add and commit fixture-related files. ```bash git add __fixtures__/plpgsql/plpgsql_deparser_fixes.sql git add __fixtures__/plpgsql-generated/generated.json git add packages/plpgsql-deparser/__tests__/deparser-fixes.test.ts git add packages/plpgsql-deparser/__tests__/__snapshots__/deparser-fixes.test.ts.snap git commit -m "test: add fixtures for [description]" ``` -------------------------------- ### Install pgsql-types Package Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pgsql-types/README.md Use npm to install the pgsql-types package. This package provides narrowed TypeScript type definitions for PostgreSQL AST nodes. ```bash npm install pgsql-types ``` -------------------------------- ### Deparse Custom SELECT Node Example Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/deparser/DEPARSER_USAGE.md This example demonstrates how to create a custom Abstract Syntax Tree (AST) node for a SELECT statement and then deparse it into an SQL string using the 'pgsql-deparser' library. ```typescript // Creating a custom SELECT node const customSelect = { SelectStmt: { targetList: [{ ResTarget: { val: { ColumnRef: { fields: [{ String: { sval: '*' } }] } } } }], fromClause: [{ RangeVar: { relname: 'users', inh: true, relpersistence: 'p' } }] } }; const sql = await deparse(customSelect); // Output: "SELECT * FROM users" ``` -------------------------------- ### Quick Start: Parse SQL to AST Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pgsql-cli/README.md Use the `pgsql parse` command to convert a SQL file into its Abstract Syntax Tree representation. The output is printed to the console by default. ```bash pgsql parse query.sql ``` -------------------------------- ### Integrate Parsing, Transformation, and Deparsing Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/transform/README.md This example demonstrates a full workflow: parsing SQL with a specific older version, transforming the resulting AST to the latest version using PG13ToPG17Transformer, and then deparsing the transformed AST back into SQL using the latest grammar. ```typescript import { parse } from '@pgsql/parser'; import { deparse } from 'pgsql-deparser'; import { PG13ToPG17Transformer } from '@pgsql/transform'; // Parse with older version const pg13Ast = await parse('SELECT * FROM users', { version: 13 }); // Transform to latest const transformer = new PG13ToPG17Transformer(); const pg17Ast = transformer.transform(pg13Ast); // Deparse with latest grammar const sql = await deparse(pg17Ast); ``` -------------------------------- ### Initialize Runtime Schema Generator Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/proto-parser/src/runtime-schema/README.md Instantiate the RuntimeSchemaGenerator with the protobuf root. This is the basic setup for generating node specifications. ```typescript import { RuntimeSchemaGenerator } from './generator'; import { NodeSpec } from './types'; const generator = new RuntimeSchemaGenerator(protoRoot); const nodeSpecs: NodeSpec[] = generator.generateNodeSpecs(); ``` -------------------------------- ### Parse and Deparse SQL Example Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/deparser/DEPARSER_USAGE.md Use this to parse an SQL string into an AST and then deparse the AST back into an SQL string. Ensure both 'pgsql-parser' and 'pgsql-deparser' are imported. ```typescript import { deparse } from 'pgsql-deparser'; import { parse } from 'pgsql-parser'; // Parse SQL const sql = 'SELECT * FROM users; INSERT INTO logs (action) VALUES ($1);'; const parseResult = await parse(sql); // Deparse back to SQL const regeneratedSql = await deparse(parseResult); console.log(regeneratedSql); // Output: "SELECT * FROM users;\n\nINSERT INTO logs (action) VALUES ($1);" ``` -------------------------------- ### Quick Start: Generate TypeScript from Protobuf Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pgsql-cli/README.md Generate TypeScript definitions from PostgreSQL protobuf files using the `pgsql proto-gen` command. Specify input proto files and an output directory. Use `--types` and `--enums` flags to include type and enum definitions. ```bash pgsql proto-gen --inFile pg_query.proto --outDir out --types --enums ``` -------------------------------- ### Test Transformer by Comparing PG Versions Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/transform/DEBUGGING.md This example demonstrates how to test an AST transformer by parsing SQL with a source version (e.g., PG13), transforming it to a target version (e.g., PG14), and comparing the result with the actual PG14 parsed output. ```javascript import { parse } from '@pgsql/parser'; import { ASTTransformer } from '../dist/index.js'; async function testTransformer() { const transformer = new ASTTransformer(); const sql = `ALTER TABLE test ADD CONSTRAINT name CHECK (col = 'value')`; // Parse with correct versions const pg13Result = await parse(sql, 13); const pg14Result = await parse(sql, 14); // Transform PG13 to PG14 const astToTransform = JSON.parse(JSON.stringify(pg13Result.result)); if (astToTransform.stmts && Array.isArray(astToTransform.stmts)) { astToTransform.stmts = astToTransform.stmts.map((stmtWrapper) => { if (stmtWrapper.stmt) { const transformedStmt = transformer.transform(stmtWrapper.stmt, 13, 14); return { ...stmtWrapper, stmt: transformedStmt }; } return stmtWrapper; }); } astToTransform.version = pg14Result.result.version; // Compare transformed result with actual PG14 result const expectedStr = JSON.stringify(pg14Result.result, null, 2); const transformedStr = JSON.stringify(astToTransform, null, 2); if (expectedStr === transformedStr) { console.log('✅ Transformation successful'); } else { console.log('❌ Transformation failed'); } } ``` -------------------------------- ### Pretty Formatting Example: Custom Characters Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/deparser/DEPARSER_USAGE.md Demonstrates using custom newline and tab characters for pretty-formatted SQL output, such as Windows line endings and 4-space indentation. ```typescript const options = { pretty: true, newline: '\r\n', // Windows line endings tab: ' ' // 4-space indentation }; const sql = deparse(parseResult, options); ``` -------------------------------- ### Integrate pgsql-parser with @pgsql/traverse Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/traverse/README.md This example demonstrates how to parse a SQL query using `pgsql-parser` and then traverse the resulting AST using the `visit` function from `@pgsql/traverse`. It logs table names and column names encountered during traversal. ```typescript import { parse } from 'pgsql-parser'; import { visit } from '@pgsql/traverse'; const sql = 'SELECT name, email FROM users WHERE age > 18'; const ast = await parse(sql); const visitor = { RangeVar: (node) => { console.log('Table:', node.relname); }, ColumnRef: (node) => { console.log('Column:', node.fields?.[0]?.String?.sval); } }; visit(ast, visitor); ``` -------------------------------- ### Pretty Formatting Example: Basic SELECT Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/deparser/DEPARSER_USAGE.md Compares the output of deparsing a SELECT statement with and without pretty formatting enabled. ```typescript // Without pretty formatting const sql1 = deparse(selectAst, { pretty: false }); // Output: "SELECT id, name, email FROM users WHERE active = true;" // With pretty formatting const sql2 = deparse(selectAst, { pretty: true }); // Output: // SELECT // id, // name, // email // FROM users // WHERE // active = true; ``` -------------------------------- ### Full SQL + PL/pgSQL Parsing and Deparsing Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-deparser/README.md Demonstrates a full round-trip parsing and deparsing process for SQL statements containing PL/pgSQL functions. This example requires loading the module and uses the `parse` and `deparseSync` functions. ```typescript import { parse, deparseSync, loadModule } from 'plpgsql-parser'; await loadModule(); const parsed = parse(` CREATE FUNCTION my_func() RETURNS void LANGUAGE plpgsql AS $$ BEGIN RAISE NOTICE 'Hello'; END; $$; `); // Full round-trip: parses SQL + PL/pgSQL, deparses back to complete SQL const sql = deparseSync(parsed); ``` -------------------------------- ### Initialize Direct AST Transformer Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/transform/README.md Use direct transformers for optimized performance when the source and target versions are known. This example shows transforming from PG13 to PG17. ```typescript import { PG13ToPG17Transformer } from '@pgsql/transform'; const transformer = new PG13ToPG17Transformer(); const pg17Ast = transformer.transform(pg13Ast); ``` -------------------------------- ### Walk PL/pgSQL AST Nodes Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-parser/README.md Utilize the `walk` function to traverse the AST of PL/pgSQL nodes. This example demonstrates visiting block statements and return statements within a function. ```typescript import { parse, walk, loadModule } from 'plpgsql-parser'; import type { PLpgSQLVisitor } from 'plpgsql-parser'; await loadModule(); const parsed = parse(` CREATE FUNCTION get_user(p_id int) RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN (SELECT name FROM users WHERE id = p_id); END; $$; `); // Visit PL/pgSQL nodes const visitor: PLpgSQLVisitor = { PLpgSQL_stmt_block: (path) => { console.log('Found block at path:', path.path); }, PLpgSQL_stmt_return: (path) => { console.log('Found return statement'); }, }; walk(parsed.functions[0].plpgsql.hydrated, visitor); ``` -------------------------------- ### Create Select Statement AST with WHERE Clause Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pg-ast/README.md This example shows how to construct a more complex SELECT statement AST, including multiple columns, a WHERE clause with a comparison, and uses 'pgsql-deparser' to convert the AST to a SQL string. ```typescript import * as t from 'pg-ast'; import { SelectStmt } from '@pgsql/types'; import { deparse } from 'pgsql-deparser'; const query: { SelectStmt: SelectStmt } = t.nodes.selectStmt({ targetList: [ t.nodes.resTarget({ val: t.nodes.columnRef({ fields: [t.nodes.string({ sval: 'name' })] }) }), t.nodes.resTarget({ val: t.nodes.columnRef({ fields: [t.nodes.string({ sval: 'email' })] }) }) ], fromClause: [ t.nodes.rangeVar({ relname: 'users', inh: true, relpersistence: 'p' }) ], whereClause: t.nodes.aExpr({ kind: 'AEXPR_OP', name: [t.nodes.string({ sval: '>' })], lexpr: t.nodes.columnRef({ fields: [t.nodes.string({ sval: 'age' })] }), rexpr: t.nodes.aConst({ ival: t.ast.integer({ ival: 18 }) }) }), limitOption: 'LIMIT_OPTION_DEFAULT', op: 'SETOP_NONE' }); await deparse(createStmt); // SELECT name, email FROM users WHERE age > 18 ``` -------------------------------- ### Basic Usage Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-parser/README.md Demonstrates parsing SQL with PL/pgSQL functions and accessing the hydrated AST. ```typescript import { parse, loadModule } from 'plpgsql-parser'; // Initialize the WASM module await loadModule(); // Parse SQL with PL/pgSQL functions - auto-detects and hydrates const result = parse(` CREATE FUNCTION my_func(p_id int) RETURNS void LANGUAGE plpgsql AS $$ BEGIN RAISE NOTICE 'Hello %', p_id; END; $$; `); console.log(result.functions.length); // 1 console.log(result.functions[0].plpgsql.hydrated); // Hydrated AST ``` -------------------------------- ### Build Individual Package Source: https://github.com/constructive-io/pgsql-parser/blob/main/README.md Navigate to a specific package directory and run the build script for that package. ```bash cd packages/parser npm run build ``` -------------------------------- ### Navigate and Run Tests Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/transform/FIXING.md Navigate to the transform package directory and run tests using pnpm. ```bash cd packages/transform pnpm test ``` -------------------------------- ### Download and Process Proto File Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pgsql-cli/README.md Fetches a proto file from a given URL, saves it locally, and processes it for use. Requires input and output file paths. ```bash pgsql proto-fetch \ --url https://raw.githubusercontent.com/pganalyze/libpg_query/16-latest/protobuf/pg_query.proto \ --inFile pg_query.proto \ --outFile pg_query.js ``` -------------------------------- ### Migrate from pg-proto-parser CLI Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pgsql-cli/README.md Illustrates the command mapping from the older `pg-proto-parser` tool to the new `pgsql proto-gen` command. ```bash # Old pg-proto-parser codegen --inFile pg_query.proto --outDir out # New pgsql proto-gen --inFile pg_query.proto --outDir out ``` -------------------------------- ### Install pgsql-deparser for PostgreSQL 13-16 Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/deparser/README.md Install version-specific packages of pgsql-deparser for compatibility with PostgreSQL versions 13 through 16. The recommended version is PG17. ```sh npm install pgsql-deparser@pg13 # PostgreSQL 13 ``` ```sh npm install pgsql-deparser@pg14 # PostgreSQL 14 ``` ```sh npm install pgsql-deparser@pg15 # PostgreSQL 15 ``` ```sh npm install pgsql-deparser@pg16 # PostgreSQL 16 ``` -------------------------------- ### With Options Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-deparser/README.md Illustrates how to customize the deparsing process by providing options to the `PLpgSQLDeparser` constructor, such as indentation and keyword casing. ```APIDOC ## With Options ```typescript import { PLpgSQLDeparser } from 'plpgsql-deparser'; const deparser = new PLpgSQLDeparser({ indent: ' ', // 4 spaces instead of default 2 newline: '\n', // newline character uppercase: false, // lowercase keywords }); const deparsed = deparser.deparseResult(parseResult); ``` ``` -------------------------------- ### Add Snapshot Test Case Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-deparser/AGENTS.md Example of an explicit test case with snapshot assertions for deparser fixes. ```typescript it('should handle [description]', async () => { const sql = `CREATE FUNCTION test_example(...) LANGUAGE plpgsql AS $$ BEGIN -- your test case END$$`; await testUtils.expectAstMatch('description', sql); const parsed = parsePlPgSQLSync(sql) as unknown as PLpgSQLParseResult; const deparsed = deparseSync(parsed); expect(deparsed).toMatchSnapshot(); // Add specific assertions expect(deparsed).toContain('expected output'); }); ``` -------------------------------- ### Deparse a Single Function Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-deparser/README.md Provides an example of deparsing a single function body AST using the static `deparseFunction` method. ```APIDOC ## Deparse a Single Function ```typescript import { PLpgSQLDeparser } from 'plpgsql-deparser'; // If you have just the function body AST const funcBody = parseResult.plpgsql_funcs[0].PLpgSQL_function; const deparsed = PLpgSQLDeparser.deparseFunction(funcBody); ``` ``` -------------------------------- ### Display CLI Help for Runtime Schema Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/proto-parser/src/runtime-schema/README.md Command to display help information for the runtime-schema subcommand of the pg-proto-parser CLI tool. ```bash pg-proto-parser runtime-schema --help ``` -------------------------------- ### Build AST with Types Source: https://github.com/constructive-io/pgsql-parser/blob/main/README.md Construct an AST programmatically using TypeScript types for validation. This example demonstrates building a `SelectStmt` AST. ```typescript import { deparse } from 'pgsql-deparser'; import { SelectStmt } from '@pgsql/types'; const stmt: { SelectStmt: SelectStmt } = { SelectStmt: { targetList: [ { ResTarget: { val: { ColumnRef: { fields: [{ A_Star: {} }] } } } } ], fromClause: [ { RangeVar: { relname: 'some_table', inh: true, relpersistence: 'p' } } ], limitOption: 'LIMIT_OPTION_DEFAULT', op: 'SETOP_NONE' } }; await deparse(stmt); ``` -------------------------------- ### Publish All Parser Versions Source: https://github.com/constructive-io/pgsql-parser/blob/main/PUBLISH.md Automates the preparation, building, and publishing of all parser versions. It loops through version directories, builds each package, and publishes it using the npm tag defined in the configuration. ```bash cd packages/parser npm run prepare-versions for version in versions/*/; do cd "$version" npm run build npm run publish:pkg # Uses the npmTag from config cd .. done ``` -------------------------------- ### Transform SQL with PL/pgSQL Functions Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-parser/README.md Use the `transformSync` function for a synchronous parse-modify-deparse pipeline. This example renames a function within the SQL script. ```typescript // Transform API for parse -> modify -> deparse pipeline const output = transformSync(sql, (ctx) => { // Modify the function name ctx.functions[0].stmt.funcname[0].String.sval = 'renamed_func'; }); ``` -------------------------------- ### Running Specific Tests with pnpm Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/deparser/TESTING.md Use this command to isolate and test a specific failing test case. Ensure you have pnpm installed and configured for the project. ```bash pnpm test --testNamePattern="specific-test" ``` -------------------------------- ### Example Skipped Test Entry Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/transform/FIXING.md This is the format for entries in transformer-errors.ts, detailing skipped tests. It includes Postgres versions, the test case file, and a description of the issue. ```typescript [16, 17, "pretty/misc-5.sql", "16-17 transformer fails WITH clause TypeCast prefix issue: transformer adds pg_catalog prefix to JSON types when expected output has none"] ``` -------------------------------- ### pgsql proto-fetch Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pgsql-cli/README.md Downloads PostgreSQL protobuf files from a URL and processes them. ```APIDOC ## pgsql proto-fetch ### Description Download and process proto files from URLs. ### Method CLI Command ### Endpoint N/A (CLI Tool) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Options | Option | Description | Default | |---------------------|------------------------------------------------------|------------| | `--url` | URL of the proto file to download | | `--inFile` | Path to save the downloaded proto file | | `--outFile` | Path to save the processed JavaScript file | | `-h, --help` | Show help | | ### Request Example ```bash pgsql proto-fetch --url https://raw.githubusercontent.com/pganalyze/libpg_query/16-latest/protobuf/pg_query.proto --inFile pg_query.proto --outFile pg_query.js ``` ### Response #### Success Response (200) - **Processed JavaScript** (file) - The processed JavaScript file containing definitions. #### Response Example N/A (Generates files) ``` -------------------------------- ### Deparse AST to SQL using pgsql-deparser Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/parser/README.md Use the `pgsql-deparser` module for pure TypeScript AST-to-SQL conversion. This example constructs an AST for a SELECT statement and then deparses it. ```typescript import * as t from '@pgsql/utils'; import { RangeVar, SelectStmt } from '@pgsql/types'; import { deparse } from 'pgsql-deparser'; // This could have been obtained from any JSON or AST, not necessarily @pgsql/utils const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({ targetList: [ t.nodes.resTarget({ val: t.nodes.columnRef({ fields: [t.nodes.aStar()] }) }) ], fromClause: [ t.nodes.rangeVar({ relname: 'some_table', inh: true, relpersistence: 'p' }) ], limitOption: 'LIMIT_OPTION_DEFAULT', op: 'SETOP_NONE' }); // Modify the AST if needed (stmt.SelectStmt.fromClause[0] as {RangeVar: RangeVar}).RangeVar.relname = 'another_table'; // Deparse the modified AST back to a SQL string console.log(await deparse(stmt)); // Output: SELECT * FROM another_table ``` -------------------------------- ### Run Test Suite Command Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-deparser/AGENTS.md Command to run the test suite for verifying fixtures. ```bash cd packages/plpgsql-deparser pnpm test ``` -------------------------------- ### Prepare Parser Versions Source: https://github.com/constructive-io/pgsql-parser/blob/main/PUBLISH.md Prepares the parser package for multi-version publishing by generating version-specific directories, package.json, tsconfig.json, and source files based on the configuration. ```bash cd packages/parser npm run prepare-versions ``` -------------------------------- ### Migrate from pgsql-parser CLI Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pgsql-cli/README.md Shows the new command structure for parsing SQL files, replacing the old `pgsql-parser` command. ```bash # Old pgsql-parser file.sql pgsql-parser file.sql --pl # New pgsql parse file.sql pgsql parse file.sql --pl ``` -------------------------------- ### Modify SQL AST and Deparse Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/parser/README.md Programmatically modify a SQL query's AST and then deparse it back into a SQL string. This example changes the table name in a SELECT statement. ```javascript import { parse, deparse } from 'pgsql-parser'; const stmts = await parse('SELECT * FROM test_table'); // Assuming the structure of stmts is known and matches the expected type stmts[0].RawStmt.stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table'; console.log(await deparse(stmts)); // SELECT * FROM "another_table" ``` -------------------------------- ### Generate Keyword List from PostgreSQL kwlist.h Source: https://github.com/constructive-io/pgsql-parser/blob/main/AGENTS.md Generates a list of keywords by parsing the PostgreSQL `kwlist.h` file. This script is used in the quotes package. ```bash npm run keywords ``` -------------------------------- ### Deparse AST to SQL in TypeScript Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/deparser/README.md Use the `deparseSync` function from `pgsql-deparser` to convert a PostgreSQL AST into an SQL string. This example demonstrates creating an AST using `@pgsql/utils` and then deparsing it. ```ts import * as t from '@pgsql/utils'; import { RangeVar, SelectStmt } from '@pgsql/types'; import { deparseSync as deparse } from 'pgsql-deparser'; // This could have been obtained from any JSON or AST, not necessarily @pgsql/utils const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({ targetList: [ t.nodes.resTarget({ val: t.nodes.columnRef({ fields: [t.nodes.aStar()] }) }) ], fromClause: [ t.nodes.rangeVar({ relname: 'some_table', inh: true, relpersistence: 'p' }) ], limitOption: 'LIMIT_OPTION_DEFAULT', op: 'SETOP_NONE' }); // Modify the AST if needed (stmt.SelectStmt.fromClause[0] as {RangeVar: RangeVar}).RangeVar.relname = 'another_table'; // Deparse the modified AST back to a SQL string console.log(deparse(stmt)); // Output: SELECT * FROM another_table ``` -------------------------------- ### Sync Parse and Deparse SQL with Initialization Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/parser/README.md Use sync methods `parseSync` and `deparseSync` after explicitly initializing the module with `loadModule()`. Recommended only when necessary. ```typescript import { loadModule, parseSync, deparseSync } from 'pgsql-parser'; // Initialize first (required for sync methods) await loadModule(); // Now safe to use sync methods const stmts = parseSync('SELECT * FROM test_table'); const sql = deparseSync(stmts); ``` -------------------------------- ### Run Local Tests for Debugging Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/transform/DEBUGGING.md During development, always run tests locally using commands like `pnpm test __tests__/kitchen-sink/13-14`. Avoid using CI logs or commands to ensure faster feedback loops. ```bash pnpm test __tests__/kitchen-sink/13-14 ``` -------------------------------- ### Generate JSON AST for Select Statement Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pg-ast/README.md Example demonstrating how to create a JSON AST for a SELECT statement with a single column from a table. This utilizes helper methods from 'pg-ast' and 'pgsql-deparser' for output. ```typescript import * as t from 'pg-ast'; import { SelectStmt } from '@pgsql/types'; import { deparse } from 'pgsql-deparser'; const selectStmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({ targetList: [ t.nodes.resTarget({ val: t.nodes.columnRef({ fields: [ t.nodes.aStar() ] }) }) ], fromClause: [ t.nodes.rangeVar({ relname: 'some_amazing_table', inh: true, relpersistence: 'p' }) ], limitOption: 'LIMIT_OPTION_DEFAULT', op: 'SETOP_NONE' }); console.log(selectStmt); // Output: { "SelectStmt": { "targetList": [ { "ResTarget": { "val": { "ColumnRef": { "fields": [ { "A_Star": {} } ] } } } } ], "fromClause": [ { "RangeVar": { "relname": "some_amazing_table", "inh": true, "relpersistence": "p" } } ], "limitOption": "LIMIT_OPTION_DEFAULT", "op": "SETOP_NONE" } } console.log(await deparse(stmt)) // Output: SELECT * FROM some_amazing_table ``` -------------------------------- ### Process Existing Proto File with Package Replacement Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pgsql-cli/README.md Processes a local proto file and allows for replacing package names, useful for managing dependencies. ```bash pgsql proto-fetch \ --inFile pg_query.proto \ --outFile pg_query.js \ --replace-pkg "protobufjs/minimal" \ --with-pkg "@custom/protobufjs" ``` -------------------------------- ### Transform Specific AST Nodes (WITH and WHERE clauses) Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/transform/src/transformers-direct/README.md This example focuses on transforming specific clauses within a parsed AST, such as a WITH clause or a WHERE clause. It assumes the AST has already been parsed and the relevant transformer is available. ```typescript // Transform just a WITH clause const selectStmt = pg14Ast.stmts[0].stmt.SelectStmt; const withClause = selectStmt.withClause; const transformedWith = transformer.transform(withClause); // Transform a WHERE clause const whereClause = selectStmt.whereClause; const transformedWhere = transformer.transform(whereClause); ``` -------------------------------- ### Generate AST Helpers from Protobuf Source: https://github.com/constructive-io/pgsql-parser/blob/main/AGENTS.md Use this script to generate Abstract Syntax Tree (AST) helper functions from protobuf definitions. This is part of the code generation process. ```bash npm run build:proto ``` -------------------------------- ### Quick Start: Deparse AST to SQL Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pgsql-cli/README.md Use the `pgsql deparse` command to convert an AST (typically from a JSON file) back into a SQL query. This is useful for reconstructing SQL from its AST representation. ```bash pgsql deparse ast.json ``` -------------------------------- ### Transform Complex Queries to PG17 Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/transform/src/transformers-direct/README.md This example shows how to parse a complex SQL query with a WITH clause, ORDER BY, and LIMIT, and then transform its AST from PG14 to PG17. It requires importing the appropriate transformer for the source version. ```typescript const complexSQL = " WITH active_users AS ( SELECT * FROM users WHERE active = true ) SELECT id, name FROM active_users ORDER BY name LIMIT 10 "; const pg14Parser = new Parser({ version: 14 }); const pg14Ast = await pg14Parser.parse(complexSQL); const transformer = new PG14ToPG17Transformer(); const pg17Ast = transformer.transform(pg14Ast); ``` -------------------------------- ### Building the Project with pnpm Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/deparser/TESTING.md Always run this command after making code changes to build the project before running tests. This ensures that your tests are executed against the latest compiled code. ```bash pnpm build ``` -------------------------------- ### Correctly Await Async Parser Method Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/transform/DEBUGGING.md The `parse()` method of `@pgsql/parser` is asynchronous and returns a Promise. Always use `await` or `.then()` to get the AST structure; otherwise, transformers will receive empty objects. ```javascript const { Parser } = require('@pgsql/parser'); const parser = new Parser({version: 13}); const result = await parser.parse(sql); // With await ``` ```javascript // ❌ WRONG - returns unresolved Promise, not AST const { Parser } = require('@pgsql/parser'); const parser = new Parser({version: 13}); const result = parser.parse(sql); // Missing await! ``` -------------------------------- ### PL/pgSQL Parsing and Deparsing Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-parse/README.md Demonstrates how to use the `parseSync` and `deparseSync` functions to parse PL/pgSQL code while preserving comments, and then deparse it back into SQL. ```APIDOC ## PL/pgSQL Parsing and Deparsing API ### Description This section details the core functions for parsing and deparsing PL/pgSQL code with comment preservation. ### Functions - `loadModule()`: Asynchronously loads the necessary WASM module for parsing and deparsing. - `parseSync(sql: string)`: Synchronously parses the input SQL string, returning an object containing enhanced AST information and function-specific comment data. - `deparseSync(result: ParseResult)`: Synchronously deparses the parsed result back into a SQL string, preserving outer SQL comments/whitespace and inner PL/pgSQL comments. ### Parameters #### `parseSync` Parameters - **sql** (string) - Required - The PL/pgSQL code to parse. #### `deparseSync` Parameters - **result** (object) - Required - The result object obtained from `parseSync`. - **enhanced** (object) - Contains outer SQL comments and whitespace. - **functions** (array) - Contains comment data for each PL/pgSQL function. ### Request Example (Parsing) ```typescript import { parseSync, deparseSync, loadModule } from 'plpgsql-parse'; await loadModule(); const result = await parseSync(` -- Create a counter function CREATE FUNCTION get_count() RETURNS int LANGUAGE plpgsql AS $$ BEGIN -- Count active users RETURN (SELECT count(*) FROM users WHERE active); END; $$; `); console.log(result.enhanced); console.log(result.functions); ``` ### Response Example (Parsing Result Structure) ```json { "enhanced": { /* ... outer SQL comments and whitespace ... */ }, "functions": [ { "body": "-- Count active users\n RETURN (SELECT count(*) FROM users WHERE active);", "comments": [ { "line": 3, "text": "-- Count active users" } ] } ] } ``` ### Request Example (Deparsing) ```typescript const sql = deparseSync(result); console.log(sql); ``` ### Response Example (Deparsed SQL) ```sql -- Create a counter function CREATE FUNCTION get_count() RETURNS int LANGUAGE plpgsql AS $$ BEGIN -- Count active users RETURN (SELECT count(*) FROM users WHERE active); END; $$; ``` ``` -------------------------------- ### Parse Protobuf and Generate Files Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/proto-parser/README.md Use this to parse protobuf files and generate TypeScript and JSON output. Ensure `inFile` and `outDir` are correctly specified. ```javascript import { PgProtoParser } from 'pg-proto-parser'; // Create PgProtoParser const parser = new PgProtoParser(inFile, { outDir }); // Generate TypeScript and JSON files await parser.write(); ``` -------------------------------- ### Basic Usage Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/plpgsql-deparser/README.md Demonstrates how to parse a PL/pgSQL function and then deparse its body back to a SQL string using the asynchronous `deparse` function. ```APIDOC ## Basic Usage ```typescript import { parsePlPgSQL } from '@libpg-query/parser'; import { deparse, PLpgSQLDeparser } from 'plpgsql-deparser'; // Parse a PL/pgSQL function const funcSql = ` CREATE OR REPLACE FUNCTION test_func() RETURNS INTEGER AS $$ DECLARE sum int := 0; BEGIN FOR n IN 1..10 LOOP sum := sum + n; END LOOP; RETURN sum; END; $$ LANGUAGE plpgsql; `; const parseResult = await parsePlPgSQL(funcSql); // Deparse the function body const deparsed = await deparse(parseResult); console.log(deparsed); ``` ``` -------------------------------- ### Basic Import and Parse with @pgsql/parser Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/transform/DEBUGGING.md Import the parse function and PgParser class from '@pgsql/parser'. Use 'parse' with a SQL string and version number, or instantiate PgParser with version options for parsing. ```javascript import { parse, PgParser } from '@pgsql/parser'; // Parse with specific version const result15 = await parse('SELECT 1+1 as sum', 15); console.log(result15); // { version: 15, result: { version: 150007, stmts: [...] } } // Using PgParser class const parser = new PgParser({version: 16}); const result16 = await parser.parse('SELECT * FROM users'); ``` -------------------------------- ### Root Project Scripts Source: https://github.com/constructive-io/pgsql-parser/blob/main/AGENTS.md Commands for managing the entire monorepo, including building, cleaning, testing, linting, updating dependencies, and version bumping. ```bash pnpm -r run build ``` ```bash pnpm -r run clean ``` ```bash pnpm -r run test ``` ```bash pnpm -r run lint ``` ```bash pnpm up -r -i -L ``` ```bash ts-node scripts/bump-versions.ts ``` ```bash makage update-workspace ``` -------------------------------- ### Run Individual Test Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/transform/FIXING.md Execute a specific test file to reproduce or verify a fix. This command targets a particular test case within the kitchen-sink suite. ```bash pnpm test __tests__/kitchen-sink/16-17/pretty-misc.test.ts ``` -------------------------------- ### Prepare Deparser Versions Source: https://github.com/constructive-io/pgsql-parser/blob/main/PUBLISH.md Prepares the deparser package for multi-version publishing by running a series of scripts to clean up types, organize transformers, and generate version-specific deparsers. ```bash cd packages/deparser npm run prepare-versions ``` -------------------------------- ### Run Linter with pnpm Source: https://github.com/constructive-io/pgsql-parser/blob/main/DEVELOPMENT.md Use this command to check code quality across all packages. ```bash pnpm lint ``` -------------------------------- ### Generate Runtime Schema via CLI Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/proto-parser/src/runtime-schema/README.md Command-line interface command to generate runtime schemas. Specify input proto file and output directory. ```bash pg-proto-parser runtime-schema --inFile --outDir ``` -------------------------------- ### Bump pgsql-parser and pgsql-deparser Versions Source: https://github.com/constructive-io/pgsql-parser/blob/main/AGENTS.md An interactive command-line interface (CLI) tool to bump the versions of `pgsql-parser` and `pgsql-deparser` for different PostgreSQL versions. ```bash npm run bump-versions ``` -------------------------------- ### Per-Package Standard Scripts Source: https://github.com/constructive-io/pgsql-parser/blob/main/AGENTS.md Common scripts available in each package for building, cleaning, linting, and testing. ```bash tsc && tsc -p tsconfig.esm.json ``` ```bash makage clean dist ``` ```bash makage assets ``` ```bash eslint . --fix ``` ```bash jest ``` ```bash jest --watch ``` ```bash npm run build ``` -------------------------------- ### Configure and Run PgProtoParser with Runtime Schema Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/proto-parser/src/runtime-schema/README.md Configure the PgProtoParser to enable and customize runtime schema generation. This includes setting the output directory and filename for the schema. ```typescript import { PgProtoParser } from '../parser'; const options = { outDir: './output', runtimeSchema: { enabled: true, filename: 'ast-schema', format: 'json' // or 'typescript' } }; const parser = new PgProtoParser('./proto/pg_query.proto', options); parser.write(); ``` -------------------------------- ### Run Tests in Watch Mode for a Specific Package Source: https://github.com/constructive-io/pgsql-parser/blob/main/DEVELOPMENT.md Navigate to a package directory and use this command to run its tests automatically when files change. Alternatively, use `pnpm test` for a single run. ```bash cd packages/deparser pnpm test:watch ``` -------------------------------- ### Generate JSON Runtime Schema for AST Nodes Source: https://github.com/constructive-io/pgsql-parser/blob/main/packages/pgsql-cli/README.md Generates a JSON runtime schema for AST nodes from a .proto file. Requires input proto file and output directory. Defaults to JSON format. ```bash pgsql runtime-schema --inFile pg_query.proto --outDir out ```