pgsql-deparser 17.17.1 → 17.18.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.
- package/deparser.js +196 -196
- package/esm/deparser.js +1 -1
- package/esm/index.js +1 -0
- package/index.d.ts +1 -0
- package/index.js +3 -1
- package/package.json +4 -4
- package/esm/kwlist.js +0 -531
- package/esm/utils/quote-utils.js +0 -240
- package/kwlist.d.ts +0 -16
- package/kwlist.js +0 -535
- package/utils/quote-utils.d.ts +0 -87
- package/utils/quote-utils.js +0 -244
package/utils/quote-utils.js
DELETED
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.QuoteUtils = void 0;
|
|
4
|
-
const kwlist_1 = require("../kwlist");
|
|
5
|
-
class QuoteUtils {
|
|
6
|
-
static escape(literal) {
|
|
7
|
-
return `'${literal.replace(/'/g, "''")}'`;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Escapes a string value for use in E-prefixed string literals
|
|
11
|
-
* Handles both backslashes and single quotes properly
|
|
12
|
-
*/
|
|
13
|
-
static escapeEString(value) {
|
|
14
|
-
return value.replace(/\\/g, '\\\\').replace(/'/g, "''");
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Formats a string as an E-prefixed string literal with proper escaping
|
|
18
|
-
* This wraps the complete E-prefix logic including detection and formatting
|
|
19
|
-
*/
|
|
20
|
-
static formatEString(value) {
|
|
21
|
-
const needsEscape = QuoteUtils.needsEscapePrefix(value);
|
|
22
|
-
if (needsEscape) {
|
|
23
|
-
const escapedValue = QuoteUtils.escapeEString(value);
|
|
24
|
-
return `E'${escapedValue}'`;
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
return QuoteUtils.escape(value);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Determines if a string value needs E-prefix for escaped string literals
|
|
32
|
-
* Detects backslash escape sequences that require E-prefix in PostgreSQL
|
|
33
|
-
*/
|
|
34
|
-
static needsEscapePrefix(value) {
|
|
35
|
-
// Always use E'' if the string contains any backslashes,
|
|
36
|
-
// unless it's a raw \x... bytea-style literal.
|
|
37
|
-
return !/^\\x[0-9a-fA-F]+$/i.test(value) && value.includes('\\');
|
|
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
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Quote an identifier that appears as a type name.
|
|
176
|
-
*
|
|
177
|
-
* Type names in PostgreSQL have a less strict quoting policy than standalone identifiers.
|
|
178
|
-
* In type positions, COL_NAME_KEYWORD and TYPE_FUNC_NAME_KEYWORD are allowed unquoted
|
|
179
|
-
* (e.g., 'json', 'int', 'boolean', 'interval'). Only RESERVED_KEYWORD must be quoted.
|
|
180
|
-
*
|
|
181
|
-
* This is different from:
|
|
182
|
-
* - quoteIdentifier(): quotes all keywords except UNRESERVED_KEYWORD
|
|
183
|
-
* - quoteIdentifierAfterDot(): only quotes for lexical reasons (no keyword checking)
|
|
184
|
-
*
|
|
185
|
-
* Type names still need quoting for lexical reasons (uppercase, special chars, etc.).
|
|
186
|
-
*/
|
|
187
|
-
static quoteIdentifierTypeName(ident) {
|
|
188
|
-
if (!ident)
|
|
189
|
-
return ident;
|
|
190
|
-
let safe = true;
|
|
191
|
-
// Check first character: must be lowercase letter or underscore
|
|
192
|
-
const firstChar = ident[0];
|
|
193
|
-
if (!((firstChar >= 'a' && firstChar <= 'z') || firstChar === '_')) {
|
|
194
|
-
safe = false;
|
|
195
|
-
}
|
|
196
|
-
// Check all characters
|
|
197
|
-
for (let i = 0; i < ident.length; i++) {
|
|
198
|
-
const ch = ident[i];
|
|
199
|
-
if ((ch >= 'a' && ch <= 'z') ||
|
|
200
|
-
(ch >= '0' && ch <= '9') ||
|
|
201
|
-
(ch === '_')) {
|
|
202
|
-
// okay
|
|
203
|
-
}
|
|
204
|
-
else {
|
|
205
|
-
safe = false;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
if (safe) {
|
|
209
|
-
// For type names, only quote RESERVED_KEYWORD
|
|
210
|
-
// COL_NAME_KEYWORD and TYPE_FUNC_NAME_KEYWORD are allowed unquoted in type positions
|
|
211
|
-
const kwKind = (0, kwlist_1.keywordKindOf)(ident);
|
|
212
|
-
if (kwKind === 'RESERVED_KEYWORD') {
|
|
213
|
-
safe = false;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
if (safe) {
|
|
217
|
-
return ident; // no change needed
|
|
218
|
-
}
|
|
219
|
-
// Build quoted identifier with escaped embedded quotes
|
|
220
|
-
let result = '"';
|
|
221
|
-
for (let i = 0; i < ident.length; i++) {
|
|
222
|
-
const ch = ident[i];
|
|
223
|
-
if (ch === '"') {
|
|
224
|
-
result += '"'; // escape " as ""
|
|
225
|
-
}
|
|
226
|
-
result += ch;
|
|
227
|
-
}
|
|
228
|
-
result += '"';
|
|
229
|
-
return result;
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Quote a dotted type name (e.g., schema.typename).
|
|
233
|
-
*
|
|
234
|
-
* For type names, we use type-name quoting for all parts since the entire
|
|
235
|
-
* qualified name is in a type context. This allows keywords like 'json',
|
|
236
|
-
* 'int', 'boolean' to remain unquoted in user-defined schema-qualified types.
|
|
237
|
-
*/
|
|
238
|
-
static quoteTypeDottedName(parts) {
|
|
239
|
-
if (!parts || parts.length === 0)
|
|
240
|
-
return '';
|
|
241
|
-
return parts.map(part => QuoteUtils.quoteIdentifierTypeName(part)).join('.');
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
exports.QuoteUtils = QuoteUtils;
|