pgsql-deparser 17.12.2 → 17.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,53 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.QuoteUtils = void 0;
4
- const RESERVED_WORDS = new Set([
5
- 'all', 'analyse', 'analyze', 'and', 'any', 'array', 'as', 'asc', 'asymmetric',
6
- 'authorization', 'binary', 'both', 'case', 'cast', 'check', 'collate', 'collation',
7
- 'column', 'concurrently', 'constraint', 'create', 'cross', 'current_catalog',
8
- 'current_date', 'current_role', 'current_schema', 'current_time', 'current_timestamp',
9
- 'current_user', 'default', 'deferrable', 'desc', 'distinct', 'do', 'else', 'end',
10
- 'except', 'false', 'fetch', 'for', 'foreign', 'freeze', 'from', 'full', 'grant',
11
- 'group', 'having', 'ilike', 'in', 'initially', 'inner', 'intersect', 'into', 'is',
12
- 'isnull', 'join', 'lateral', 'leading', 'left', 'like', 'limit', 'localtime',
13
- 'localtimestamp', 'natural', 'not', 'notnull', 'null', 'offset', 'on', 'only',
14
- 'or', 'order', 'outer', 'overlaps', 'placing', 'primary', 'references', 'returning',
15
- 'right', 'select', 'session_user', 'similar', 'some', 'symmetric', 'table', 'tablesample',
16
- 'then', 'to', 'trailing', 'true', 'union', 'unique', 'user', 'using', 'variadic',
17
- 'verbose', 'when', 'where', 'window', 'with'
18
- ]);
4
+ const kwlist_1 = require("../kwlist");
19
5
  class QuoteUtils {
20
- static needsQuotes(value) {
21
- if (!value || typeof value !== 'string') {
22
- return false;
23
- }
24
- const lowerValue = value.toLowerCase();
25
- if (RESERVED_WORDS.has(lowerValue)) {
26
- return true;
27
- }
28
- if (!/^[a-z_][a-z0-9_$]*$/i.test(value)) {
29
- return true;
30
- }
31
- if (value !== value.toLowerCase()) {
32
- return true;
33
- }
34
- return false;
35
- }
36
- static quote(value) {
37
- if (value == null) {
38
- return null;
39
- }
40
- if (Array.isArray(value)) {
41
- return value.map(v => this.quote(v));
42
- }
43
- if (typeof value !== 'string') {
44
- return value;
45
- }
46
- if (this.needsQuotes(value)) {
47
- return `"${value}"`;
48
- }
49
- return value;
50
- }
51
6
  static escape(literal) {
52
7
  return `'${literal.replace(/'/g, "''")}'`;
53
8
  }
@@ -81,5 +36,140 @@ class QuoteUtils {
81
36
  // unless it's a raw \x... bytea-style literal.
82
37
  return !/^\\x[0-9a-fA-F]+$/i.test(value) && value.includes('\\');
83
38
  }
39
+ /**
40
+ * Quote an identifier only if needed
41
+ *
42
+ * This is a TypeScript port of PostgreSQL's quote_identifier() function from ruleutils.c
43
+ * https://github.com/postgres/postgres/blob/fab5cd3dd1323f9e66efeb676c4bb212ff340204/src/backend/utils/adt/ruleutils.c#L13055-L13137
44
+ *
45
+ * Can avoid quoting if ident starts with a lowercase letter or underscore
46
+ * and contains only lowercase letters, digits, and underscores, *and* is
47
+ * not any SQL keyword. Otherwise, supply quotes.
48
+ *
49
+ * When quotes are needed, embedded double quotes are properly escaped as "".
50
+ */
51
+ static quoteIdentifier(ident) {
52
+ if (!ident)
53
+ return ident;
54
+ let safe = true;
55
+ // Check first character: must be lowercase letter or underscore
56
+ const firstChar = ident[0];
57
+ if (!((firstChar >= 'a' && firstChar <= 'z') || firstChar === '_')) {
58
+ safe = false;
59
+ }
60
+ // Check all characters
61
+ for (let i = 0; i < ident.length; i++) {
62
+ const ch = ident[i];
63
+ if ((ch >= 'a' && ch <= 'z') ||
64
+ (ch >= '0' && ch <= '9') ||
65
+ (ch === '_')) {
66
+ // okay
67
+ }
68
+ else {
69
+ safe = false;
70
+ }
71
+ }
72
+ if (safe) {
73
+ // Check for keyword. We quote keywords except for unreserved ones.
74
+ // (In some cases we could avoid quoting a col_name or type_func_name
75
+ // keyword, but it seems much harder than it's worth to tell that.)
76
+ const kwKind = (0, kwlist_1.keywordKindOf)(ident);
77
+ if (kwKind !== 'NO_KEYWORD' && kwKind !== 'UNRESERVED_KEYWORD') {
78
+ safe = false;
79
+ }
80
+ }
81
+ if (safe) {
82
+ return ident; // no change needed
83
+ }
84
+ // Build quoted identifier with escaped embedded quotes
85
+ let result = '"';
86
+ for (let i = 0; i < ident.length; i++) {
87
+ const ch = ident[i];
88
+ if (ch === '"') {
89
+ result += '"'; // escape " as ""
90
+ }
91
+ result += ch;
92
+ }
93
+ result += '"';
94
+ return result;
95
+ }
96
+ /**
97
+ * Quote an identifier that appears after a dot in a qualified name.
98
+ *
99
+ * In PostgreSQL's grammar, identifiers that appear after a dot (e.g., schema.name,
100
+ * table.column) are in a more permissive position that accepts all keyword categories
101
+ * including RESERVED_KEYWORD. This means we only need to quote for lexical reasons
102
+ * (uppercase, special characters, leading digits) not for keyword reasons.
103
+ *
104
+ * Empirically verified: `myschema.select`, `myschema.float`, `t.from` all parse
105
+ * successfully in PostgreSQL without quotes.
106
+ */
107
+ static quoteIdentifierAfterDot(ident) {
108
+ if (!ident)
109
+ return ident;
110
+ let safe = true;
111
+ const firstChar = ident[0];
112
+ if (!((firstChar >= 'a' && firstChar <= 'z') || firstChar === '_')) {
113
+ safe = false;
114
+ }
115
+ for (let i = 0; i < ident.length; i++) {
116
+ const ch = ident[i];
117
+ if ((ch >= 'a' && ch <= 'z') ||
118
+ (ch >= '0' && ch <= '9') ||
119
+ (ch === '_')) {
120
+ // okay
121
+ }
122
+ else {
123
+ safe = false;
124
+ }
125
+ }
126
+ if (safe) {
127
+ return ident;
128
+ }
129
+ let result = '"';
130
+ for (let i = 0; i < ident.length; i++) {
131
+ const ch = ident[i];
132
+ if (ch === '"') {
133
+ result += '"';
134
+ }
135
+ result += ch;
136
+ }
137
+ result += '"';
138
+ return result;
139
+ }
140
+ /**
141
+ * Quote a dotted name (e.g., schema.table, catalog.schema.table).
142
+ *
143
+ * The first part uses strict quoting (keywords are quoted), while subsequent
144
+ * parts use relaxed quoting (keywords allowed, only quote for lexical reasons).
145
+ *
146
+ * This reflects PostgreSQL's grammar where the first identifier in a statement
147
+ * may conflict with keywords, but identifiers after a dot are in a more
148
+ * permissive position.
149
+ */
150
+ static quoteDottedName(parts) {
151
+ if (!parts || parts.length === 0)
152
+ return '';
153
+ if (parts.length === 1) {
154
+ return QuoteUtils.quoteIdentifier(parts[0]);
155
+ }
156
+ return parts.map((part, index) => index === 0 ? QuoteUtils.quoteIdentifier(part) : QuoteUtils.quoteIdentifierAfterDot(part)).join('.');
157
+ }
158
+ /**
159
+ * Quote a possibly-qualified identifier
160
+ *
161
+ * This is inspired by PostgreSQL's quote_qualified_identifier() function from ruleutils.c
162
+ * but uses relaxed quoting for the tail component since PostgreSQL's grammar accepts
163
+ * all keywords in qualified name positions.
164
+ *
165
+ * Return a name of the form qualifier.ident, or just ident if qualifier
166
+ * is null/undefined, quoting each component if necessary.
167
+ */
168
+ static quoteQualifiedIdentifier(qualifier, ident) {
169
+ if (qualifier) {
170
+ return `${QuoteUtils.quoteIdentifier(qualifier)}.${QuoteUtils.quoteIdentifierAfterDot(ident)}`;
171
+ }
172
+ return QuoteUtils.quoteIdentifier(ident);
173
+ }
84
174
  }
85
175
  exports.QuoteUtils = QuoteUtils;