From 0e8d4fc84c24fe4285711038f7673aa6d6be0466 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Sun, 22 Jun 2025 22:20:05 -0700 Subject: [PATCH] updates for #127 --- CLI.md | 195 ------------------ __fixtures__/generated/generated.json | 1 + __fixtures__/kitchen-sink/misc/issues.sql | 3 + .../kitchen-sink/misc-issues.test.ts | 3 +- packages/deparser/src/deparser.ts | 8 +- 5 files changed, 13 insertions(+), 197 deletions(-) delete mode 100644 CLI.md diff --git a/CLI.md b/CLI.md deleted file mode 100644 index 49943b14..00000000 --- a/CLI.md +++ /dev/null @@ -1,195 +0,0 @@ -# CLI Consolidation Plan - -## Overview - -Consolidate all CLI functionality from `@launchql/proto-cli`, `pgsql-parser`, and add deparser capabilities into a unified `@pgsql/cli` package. - -## Current State - -### 1. `@launchql/proto-cli` (packages/cli) -- **Binary**: `pg-proto-parser` -- **Commands**: - - `codegen`: Generate TypeScript from protobuf files - - `protogen`: Download and process proto files - - `runtime-schema`: Generate runtime schema for AST nodes -- **Dependencies**: Uses `inquirerer` for interactive CLI, `minimist` for arg parsing - -### 2. `pgsql-parser` (packages/parser) -- **Binary**: `pgsql-parser` -- **Functionality**: Parse SQL files and output JSON AST -- **Options**: - - `--pl`: Parse PL/pgSQL functions only -- **Dependencies**: Uses `minimist` directly in cli.js - -### 3. `deparser` (packages/deparser) -- **No CLI**: Currently no CLI interface -- **Functionality**: Convert AST back to SQL - -## New CLI Structure: `@pgsql/cli` - -### Package Changes - -1. **Rename**: `@launchql/proto-cli` → `@pgsql/cli` -2. **Binary**: `pgsql` (shorter, cleaner) -3. **Remove minimist** from parser and deparser packages -4. **Centralize** all CLI logic in the cli package - -### Command Structure - -``` -pgsql [options] - -Commands: - parse Parse SQL to AST - deparse Convert AST to SQL - proto-gen Generate TypeScript from protobuf (formerly codegen) - proto-fetch Download and process proto files (formerly protogen) - runtime-schema Generate runtime schema for AST nodes - -Global Options: - -h, --help Show help - -v, --version Show version -``` - -### Command Details - -#### 1. `pgsql parse` -```bash -pgsql parse [options] - -Options: - -o, --output Output to file instead of stdout - -f, --format Output format: json, pretty (default: pretty) - --pl Parse as PL/pgSQL function only - --clean Clean the AST tree (remove location info) - -h, --help Show help -``` - -#### 2. `pgsql deparse` -```bash -pgsql deparse [options] - -Options: - -o, --output Output to file instead of stdout - -i, --input Input JSON file (or use stdin) - --format SQL formatting options - -h, --help Show help -``` - -#### 3. `pgsql proto-gen` -```bash -pgsql proto-gen --inFile --outDir [options] - -Options: - --inFile Input .proto file (required) - --outDir Output directory (required) - --enums Generate TypeScript enums - --enums-json Generate JSON enum mappings - --types Generate TypeScript interfaces - --utils Generate utility functions - --ast-helpers Generate AST helper methods - --wrapped-helpers Generate wrapped AST helpers - --optional Make all fields optional - --keep-case Keep original field casing - --remove-undefined Remove UNDEFINED enum at position 0 - -h, --help Show help -``` - -#### 4. `pgsql proto-fetch` -```bash -pgsql proto-fetch [options] - -Options: - --url Proto file URL to download - --inFile Where to save the proto file - --outFile Generated JS output file - --replace-pkg Original package name to replace - --with-pkg New package name - -h, --help Show help -``` - -#### 5. `pgsql runtime-schema` -```bash -pgsql runtime-schema --inFile --outDir [options] - -Options: - --inFile Input .proto file (required) - --outDir Output directory (required) - --format Output format: json, typescript (default: json) - --filename Output filename (default: runtime-schema) - -h, --help Show help -``` - -## Implementation Steps - -### Phase 1: Setup New Package -1. Copy `packages/cli` to new location (keep history) -2. Update package.json: - - Name: `@pgsql/cli` - - Binary: `pgsql` - - Update dependencies -3. Remove `minimist` dependency from parser/deparser packages - -### Phase 2: Implement Core Commands -1. Create new command structure without `inquirerer` for non-interactive mode -2. Implement `parse` command: - - Move logic from parser/src/cli.js - - Add output options - - Add format options -3. Implement `deparse` command: - - Import deparser functionality - - Add file I/O handling - - Add formatting options - -### Phase 3: Refactor Proto Commands -1. Rename `codegen` → `proto-gen` -2. Rename `protogen` → `proto-fetch` -3. Update command options to use consistent naming -4. Add new options for wrapped helpers - -### Phase 4: Improve Help System -1. Create detailed help for each command -2. Add examples to help text -3. Create man page style documentation - -### Phase 5: Testing & Documentation -1. Add comprehensive tests for all commands -2. Update README with new command structure -3. Create migration guide from old CLIs -4. Add shell completion scripts - -## Benefits - -1. **Single Entry Point**: One CLI tool for all PostgreSQL AST operations -2. **Consistent Interface**: Unified command structure and options -3. **Better Discoverability**: Clear command hierarchy with good help -4. **Reduced Dependencies**: Remove minimist from individual packages -5. **Extensibility**: Easy to add new commands in the future - -## Migration Guide - -### For `pgsql-parser` users: -```bash -# Old -pgsql-parser file.sql - -# New -pgsql parse file.sql -``` - -### For `pg-proto-parser` users: -```bash -# Old -pg-proto-parser codegen --inFile pg_query.proto --outDir out - -# New -pgsql proto-gen --inFile pg_query.proto --outDir out -``` - -## Future Enhancements - -1. **Interactive Mode**: Keep inquirerer for interactive command selection -2. **Config Files**: Support `.pgsqlrc` configuration files -3. **Plugins**: Allow extending with custom commands -4. **Watch Mode**: Auto-regenerate on file changes -5. **Batch Processing**: Process multiple files at once \ No newline at end of file diff --git a/__fixtures__/generated/generated.json b/__fixtures__/generated/generated.json index 9f569975..1ceb9e38 100644 --- a/__fixtures__/generated/generated.json +++ b/__fixtures__/generated/generated.json @@ -21095,6 +21095,7 @@ "misc/issues-9.sql": "CREATE TABLE \"Album\"\n(\n \"AlbumId\" INT NOT NULL,\n \"Title\" VARCHAR(160) NOT NULL,\n \"ArtistId\" INT NOT NULL,\n CONSTRAINT \"PK_Album\" PRIMARY KEY (\"AlbumId\")\n)", "misc/issues-10.sql": "CREATE INDEX \"existing_undispatched_message\" ON public.messages USING btree (\"context_id\", context_type, notification_name, \"to\", user_id)", "misc/issues-11.sql": "COMMENT ON COLUMN \"foo\".\"whatever\" IS $$\nSomething blah, this data may have chars like '\\n' and '\\r' in it.\n$$", + "misc/issues-12.sql": "SELECT * from foo.bar.baz", "misc/inflection-1.sql": "CREATE SCHEMA inflection", "misc/inflection-2.sql": "GRANT USAGE ON SCHEMA inflection TO PUBLIC", "misc/inflection-3.sql": "ALTER DEFAULT PRIVILEGES IN SCHEMA inflection \n GRANT EXECUTE ON FUNCTIONS TO PUBLIC", diff --git a/__fixtures__/kitchen-sink/misc/issues.sql b/__fixtures__/kitchen-sink/misc/issues.sql index 076611af..d53b2a07 100644 --- a/__fixtures__/kitchen-sink/misc/issues.sql +++ b/__fixtures__/kitchen-sink/misc/issues.sql @@ -54,3 +54,6 @@ CREATE INDEX "existing_undispatched_message" ON public.messages USING btree ("co COMMENT ON COLUMN "foo"."whatever" IS $$ Something blah, this data may have chars like '\n' and '\r' in it. $$; + +-- https://github.com/launchql/pgsql-parser/issues/127 +SELECT * from foo.bar.baz; \ No newline at end of file diff --git a/packages/deparser/__tests__/kitchen-sink/misc-issues.test.ts b/packages/deparser/__tests__/kitchen-sink/misc-issues.test.ts index d8008b69..ab6c7ea5 100644 --- a/packages/deparser/__tests__/kitchen-sink/misc-issues.test.ts +++ b/packages/deparser/__tests__/kitchen-sink/misc-issues.test.ts @@ -14,6 +14,7 @@ it('misc-issues', async () => { "misc/issues-8.sql", "misc/issues-9.sql", "misc/issues-10.sql", - "misc/issues-11.sql" + "misc/issues-11.sql", + "misc/issues-12.sql" ]); }); diff --git a/packages/deparser/src/deparser.ts b/packages/deparser/src/deparser.ts index 3c336fda..eaa883fb 100644 --- a/packages/deparser/src/deparser.ts +++ b/packages/deparser/src/deparser.ts @@ -1577,7 +1577,13 @@ export class Deparser implements DeparserVisitor { } let tableName = ''; - if (node.schemaname) { + if (node.catalogname) { + tableName = QuoteUtils.quote(node.catalogname); + if (node.schemaname) { + tableName += '.' + QuoteUtils.quote(node.schemaname); + } + tableName += '.' + QuoteUtils.quote(node.relname); + } else if (node.schemaname) { tableName = QuoteUtils.quote(node.schemaname) + '.' + QuoteUtils.quote(node.relname); } else { tableName = QuoteUtils.quote(node.relname);