|
| 1 | +# @pgsql/quotes |
| 2 | + |
| 3 | +<p align="center" width="100%"> |
| 4 | + <img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" /> |
| 5 | +</p> |
| 6 | + |
| 7 | + |
| 8 | +<p align="center" width="100%"> |
| 9 | + <a href="https://github.com/constructive-io/pgsql-parser/actions/workflows/run-tests.yaml"> |
| 10 | + <img height="20" src="https://github.com/constructive-io/pgsql-parser/actions/workflows/run-tests.yaml/badge.svg" /> |
| 11 | + </a> |
| 12 | + <a href="https://github.com/constructive-io/pgsql-parser/blob/main/LICENSE-MIT"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a> |
| 13 | +</p> |
| 14 | + |
| 15 | + |
| 16 | +PostgreSQL identifier quoting and keyword classification utilities. A faithful TypeScript port of PostgreSQL's `quote_identifier()` from `ruleutils.c`, with full keyword classification from `kwlist.h`. |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +```bash |
| 21 | +npm install @pgsql/quotes |
| 22 | +``` |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +### Quoting Identifiers |
| 27 | + |
| 28 | +```typescript |
| 29 | +import { QuoteUtils } from '@pgsql/quotes'; |
| 30 | + |
| 31 | +// Simple identifiers are not quoted |
| 32 | +QuoteUtils.quoteIdentifier('my_table'); // 'my_table' |
| 33 | + |
| 34 | +// Reserved keywords are quoted |
| 35 | +QuoteUtils.quoteIdentifier('select'); // '"select"' |
| 36 | +QuoteUtils.quoteIdentifier('table'); // '"table"' |
| 37 | + |
| 38 | +// Unreserved keywords are not quoted |
| 39 | +QuoteUtils.quoteIdentifier('schema'); // 'schema' |
| 40 | + |
| 41 | +// Identifiers with uppercase or special chars are quoted |
| 42 | +QuoteUtils.quoteIdentifier('MyTable'); // '"MyTable"' |
| 43 | +QuoteUtils.quoteIdentifier('my-table'); // '"my-table"' |
| 44 | + |
| 45 | +// Embedded double quotes are escaped |
| 46 | +QuoteUtils.quoteIdentifier('a"b'); // '"a""b"' |
| 47 | +``` |
| 48 | + |
| 49 | +### Qualified Names |
| 50 | + |
| 51 | +```typescript |
| 52 | +import { QuoteUtils } from '@pgsql/quotes'; |
| 53 | + |
| 54 | +// Schema-qualified names |
| 55 | +QuoteUtils.quoteQualifiedIdentifier('public', 'my_table'); |
| 56 | +// 'public.my_table' |
| 57 | + |
| 58 | +// Dotted names (first part strict, rest relaxed) |
| 59 | +QuoteUtils.quoteDottedName(['public', 'my_table']); |
| 60 | +// 'public.my_table' |
| 61 | + |
| 62 | +// Keywords after dot don't need quoting (PostgreSQL grammar rule) |
| 63 | +QuoteUtils.quoteDottedName(['select', 'select']); |
| 64 | +// '"select".select' |
| 65 | +``` |
| 66 | + |
| 67 | +### Type Names |
| 68 | + |
| 69 | +```typescript |
| 70 | +import { QuoteUtils } from '@pgsql/quotes'; |
| 71 | + |
| 72 | +// Type names allow col_name and type_func_name keywords unquoted |
| 73 | +QuoteUtils.quoteIdentifierTypeName('json'); // 'json' |
| 74 | +QuoteUtils.quoteIdentifierTypeName('integer'); // 'integer' |
| 75 | +QuoteUtils.quoteIdentifierTypeName('boolean'); // 'boolean' |
| 76 | + |
| 77 | +// Only reserved keywords are quoted in type position |
| 78 | +QuoteUtils.quoteIdentifierTypeName('select'); // '"select"' |
| 79 | + |
| 80 | +// Schema-qualified type names |
| 81 | +QuoteUtils.quoteTypeDottedName(['public', 'json']); // 'public.json' |
| 82 | +``` |
| 83 | + |
| 84 | +### String Escaping |
| 85 | + |
| 86 | +```typescript |
| 87 | +import { QuoteUtils } from '@pgsql/quotes'; |
| 88 | + |
| 89 | +// Escape string literals |
| 90 | +QuoteUtils.escape('hello'); // "'hello'" |
| 91 | +QuoteUtils.escape("it's"); // "'it''s'" |
| 92 | + |
| 93 | +// E-string formatting (auto-detects need for E prefix) |
| 94 | +QuoteUtils.formatEString('a\\b'); // "E'a\\\\b'" |
| 95 | +QuoteUtils.formatEString('hello'); // "'hello'" |
| 96 | +``` |
| 97 | + |
| 98 | +### Keyword Classification |
| 99 | + |
| 100 | +```typescript |
| 101 | +import { keywordKindOf } from '@pgsql/quotes'; |
| 102 | +import type { KeywordKind } from '@pgsql/quotes'; |
| 103 | + |
| 104 | +keywordKindOf('select'); // 'RESERVED_KEYWORD' |
| 105 | +keywordKindOf('schema'); // 'UNRESERVED_KEYWORD' |
| 106 | +keywordKindOf('json'); // 'COL_NAME_KEYWORD' |
| 107 | +keywordKindOf('join'); // 'TYPE_FUNC_NAME_KEYWORD' |
| 108 | +keywordKindOf('foo'); // 'NO_KEYWORD' |
| 109 | +``` |
| 110 | + |
| 111 | +### Raw Keyword Sets |
| 112 | + |
| 113 | +```typescript |
| 114 | +import { |
| 115 | + RESERVED_KEYWORDS, |
| 116 | + UNRESERVED_KEYWORDS, |
| 117 | + COL_NAME_KEYWORDS, |
| 118 | + TYPE_FUNC_NAME_KEYWORDS, |
| 119 | +} from '@pgsql/quotes'; |
| 120 | + |
| 121 | +RESERVED_KEYWORDS.has('select'); // true |
| 122 | +COL_NAME_KEYWORDS.has('json'); // true |
| 123 | +``` |
| 124 | + |
| 125 | +## API |
| 126 | + |
| 127 | +### QuoteUtils |
| 128 | + |
| 129 | +| Method | Description | |
| 130 | +|--------|-------------| |
| 131 | +| `escape(literal)` | Wraps a string in single quotes, escaping embedded quotes | |
| 132 | +| `escapeEString(value)` | Escapes backslashes and single quotes for E-string literals | |
| 133 | +| `formatEString(value)` | Auto-detects and formats E-prefixed string literals | |
| 134 | +| `needsEscapePrefix(value)` | Checks if a value needs E-prefix escaping | |
| 135 | +| `quoteIdentifier(ident)` | Quotes an identifier if needed (port of PG's `quote_identifier`) | |
| 136 | +| `quoteIdentifierAfterDot(ident)` | Quotes for lexical reasons only (post-dot position) | |
| 137 | +| `quoteDottedName(parts)` | Quotes a multi-part dotted name (e.g., `schema.table`) | |
| 138 | +| `quoteQualifiedIdentifier(qualifier, ident)` | Quotes a two-part qualified name | |
| 139 | +| `quoteIdentifierTypeName(ident)` | Quotes an identifier in type-name context | |
| 140 | +| `quoteTypeDottedName(parts)` | Quotes a multi-part dotted type name | |
| 141 | + |
| 142 | +### keywordKindOf(word) |
| 143 | + |
| 144 | +Returns the keyword classification for a given word. Case-insensitive. |
| 145 | + |
| 146 | +Returns one of: `'NO_KEYWORD'`, `'UNRESERVED_KEYWORD'`, `'COL_NAME_KEYWORD'`, `'TYPE_FUNC_NAME_KEYWORD'`, `'RESERVED_KEYWORD'`. |
| 147 | + |
| 148 | +## Updating Keywords |
| 149 | + |
| 150 | +To regenerate the keyword list from a PostgreSQL source tree: |
| 151 | + |
| 152 | +```bash |
| 153 | +npm run keywords -- ~/path/to/postgres/src/include/parser/kwlist.h |
| 154 | +``` |
| 155 | + |
| 156 | +This parses PostgreSQL's `kwlist.h` and regenerates `src/kwlist.ts`. |
0 commit comments