pgsql-deparser 17.12.2 → 17.13.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/README.md +1 -1
- package/deparser.d.ts +0 -2
- package/deparser.js +179 -223
- package/esm/deparser.js +179 -223
- package/esm/kwlist.js +531 -0
- package/esm/utils/quote-utils.js +77 -46
- package/kwlist.d.ts +16 -0
- package/kwlist.js +535 -0
- package/package.json +4 -3
- package/utils/quote-utils.d.ts +23 -2
- package/utils/quote-utils.js +77 -46
package/esm/kwlist.js
ADDED
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated from PostgreSQL kwlist.h
|
|
4
|
+
* DO NOT EDIT BY HAND.
|
|
5
|
+
*/
|
|
6
|
+
export const kwlist = {
|
|
7
|
+
UNRESERVED_KEYWORD: [
|
|
8
|
+
"abort",
|
|
9
|
+
"absent",
|
|
10
|
+
"absolute",
|
|
11
|
+
"access",
|
|
12
|
+
"action",
|
|
13
|
+
"add",
|
|
14
|
+
"admin",
|
|
15
|
+
"after",
|
|
16
|
+
"aggregate",
|
|
17
|
+
"also",
|
|
18
|
+
"alter",
|
|
19
|
+
"always",
|
|
20
|
+
"asensitive",
|
|
21
|
+
"assertion",
|
|
22
|
+
"assignment",
|
|
23
|
+
"at",
|
|
24
|
+
"atomic",
|
|
25
|
+
"attach",
|
|
26
|
+
"attribute",
|
|
27
|
+
"backward",
|
|
28
|
+
"before",
|
|
29
|
+
"begin",
|
|
30
|
+
"breadth",
|
|
31
|
+
"by",
|
|
32
|
+
"cache",
|
|
33
|
+
"call",
|
|
34
|
+
"called",
|
|
35
|
+
"cascade",
|
|
36
|
+
"cascaded",
|
|
37
|
+
"catalog",
|
|
38
|
+
"chain",
|
|
39
|
+
"characteristics",
|
|
40
|
+
"checkpoint",
|
|
41
|
+
"class",
|
|
42
|
+
"close",
|
|
43
|
+
"cluster",
|
|
44
|
+
"columns",
|
|
45
|
+
"comment",
|
|
46
|
+
"comments",
|
|
47
|
+
"commit",
|
|
48
|
+
"committed",
|
|
49
|
+
"compression",
|
|
50
|
+
"conditional",
|
|
51
|
+
"configuration",
|
|
52
|
+
"conflict",
|
|
53
|
+
"connection",
|
|
54
|
+
"constraints",
|
|
55
|
+
"content",
|
|
56
|
+
"continue",
|
|
57
|
+
"conversion",
|
|
58
|
+
"copy",
|
|
59
|
+
"cost",
|
|
60
|
+
"csv",
|
|
61
|
+
"cube",
|
|
62
|
+
"current",
|
|
63
|
+
"cursor",
|
|
64
|
+
"cycle",
|
|
65
|
+
"data",
|
|
66
|
+
"database",
|
|
67
|
+
"day",
|
|
68
|
+
"deallocate",
|
|
69
|
+
"declare",
|
|
70
|
+
"defaults",
|
|
71
|
+
"deferred",
|
|
72
|
+
"definer",
|
|
73
|
+
"delete",
|
|
74
|
+
"delimiter",
|
|
75
|
+
"delimiters",
|
|
76
|
+
"depends",
|
|
77
|
+
"depth",
|
|
78
|
+
"detach",
|
|
79
|
+
"dictionary",
|
|
80
|
+
"disable",
|
|
81
|
+
"discard",
|
|
82
|
+
"document",
|
|
83
|
+
"domain",
|
|
84
|
+
"double",
|
|
85
|
+
"drop",
|
|
86
|
+
"each",
|
|
87
|
+
"empty",
|
|
88
|
+
"enable",
|
|
89
|
+
"encoding",
|
|
90
|
+
"encrypted",
|
|
91
|
+
"enforced",
|
|
92
|
+
"enum",
|
|
93
|
+
"error",
|
|
94
|
+
"escape",
|
|
95
|
+
"event",
|
|
96
|
+
"exclude",
|
|
97
|
+
"excluding",
|
|
98
|
+
"exclusive",
|
|
99
|
+
"execute",
|
|
100
|
+
"explain",
|
|
101
|
+
"expression",
|
|
102
|
+
"extension",
|
|
103
|
+
"external",
|
|
104
|
+
"family",
|
|
105
|
+
"filter",
|
|
106
|
+
"finalize",
|
|
107
|
+
"first",
|
|
108
|
+
"following",
|
|
109
|
+
"force",
|
|
110
|
+
"format",
|
|
111
|
+
"forward",
|
|
112
|
+
"function",
|
|
113
|
+
"functions",
|
|
114
|
+
"generated",
|
|
115
|
+
"global",
|
|
116
|
+
"granted",
|
|
117
|
+
"groups",
|
|
118
|
+
"handler",
|
|
119
|
+
"header",
|
|
120
|
+
"hold",
|
|
121
|
+
"hour",
|
|
122
|
+
"identity",
|
|
123
|
+
"if",
|
|
124
|
+
"ignore",
|
|
125
|
+
"immediate",
|
|
126
|
+
"immutable",
|
|
127
|
+
"implicit",
|
|
128
|
+
"import",
|
|
129
|
+
"include",
|
|
130
|
+
"including",
|
|
131
|
+
"increment",
|
|
132
|
+
"indent",
|
|
133
|
+
"index",
|
|
134
|
+
"indexes",
|
|
135
|
+
"inherit",
|
|
136
|
+
"inherits",
|
|
137
|
+
"inline",
|
|
138
|
+
"input",
|
|
139
|
+
"insensitive",
|
|
140
|
+
"insert",
|
|
141
|
+
"instead",
|
|
142
|
+
"invoker",
|
|
143
|
+
"isolation",
|
|
144
|
+
"keep",
|
|
145
|
+
"key",
|
|
146
|
+
"keys",
|
|
147
|
+
"label",
|
|
148
|
+
"language",
|
|
149
|
+
"large",
|
|
150
|
+
"last",
|
|
151
|
+
"leakproof",
|
|
152
|
+
"level",
|
|
153
|
+
"listen",
|
|
154
|
+
"load",
|
|
155
|
+
"local",
|
|
156
|
+
"location",
|
|
157
|
+
"lock",
|
|
158
|
+
"locked",
|
|
159
|
+
"logged",
|
|
160
|
+
"lsn",
|
|
161
|
+
"mapping",
|
|
162
|
+
"match",
|
|
163
|
+
"matched",
|
|
164
|
+
"materialized",
|
|
165
|
+
"maxvalue",
|
|
166
|
+
"merge",
|
|
167
|
+
"method",
|
|
168
|
+
"minute",
|
|
169
|
+
"minvalue",
|
|
170
|
+
"mode",
|
|
171
|
+
"month",
|
|
172
|
+
"move",
|
|
173
|
+
"name",
|
|
174
|
+
"names",
|
|
175
|
+
"nested",
|
|
176
|
+
"new",
|
|
177
|
+
"next",
|
|
178
|
+
"nfc",
|
|
179
|
+
"nfd",
|
|
180
|
+
"nfkc",
|
|
181
|
+
"nfkd",
|
|
182
|
+
"no",
|
|
183
|
+
"normalized",
|
|
184
|
+
"nothing",
|
|
185
|
+
"notify",
|
|
186
|
+
"nowait",
|
|
187
|
+
"nulls",
|
|
188
|
+
"object",
|
|
189
|
+
"objects",
|
|
190
|
+
"of",
|
|
191
|
+
"off",
|
|
192
|
+
"oids",
|
|
193
|
+
"old",
|
|
194
|
+
"omit",
|
|
195
|
+
"operator",
|
|
196
|
+
"option",
|
|
197
|
+
"options",
|
|
198
|
+
"ordinality",
|
|
199
|
+
"others",
|
|
200
|
+
"over",
|
|
201
|
+
"overriding",
|
|
202
|
+
"owned",
|
|
203
|
+
"owner",
|
|
204
|
+
"parallel",
|
|
205
|
+
"parameter",
|
|
206
|
+
"parser",
|
|
207
|
+
"partial",
|
|
208
|
+
"partition",
|
|
209
|
+
"partitions",
|
|
210
|
+
"passing",
|
|
211
|
+
"password",
|
|
212
|
+
"path",
|
|
213
|
+
"period",
|
|
214
|
+
"plan",
|
|
215
|
+
"plans",
|
|
216
|
+
"policy",
|
|
217
|
+
"preceding",
|
|
218
|
+
"prepare",
|
|
219
|
+
"prepared",
|
|
220
|
+
"preserve",
|
|
221
|
+
"prior",
|
|
222
|
+
"privileges",
|
|
223
|
+
"procedural",
|
|
224
|
+
"procedure",
|
|
225
|
+
"procedures",
|
|
226
|
+
"program",
|
|
227
|
+
"publication",
|
|
228
|
+
"quote",
|
|
229
|
+
"quotes",
|
|
230
|
+
"range",
|
|
231
|
+
"read",
|
|
232
|
+
"reassign",
|
|
233
|
+
"recursive",
|
|
234
|
+
"ref",
|
|
235
|
+
"referencing",
|
|
236
|
+
"refresh",
|
|
237
|
+
"reindex",
|
|
238
|
+
"relative",
|
|
239
|
+
"release",
|
|
240
|
+
"rename",
|
|
241
|
+
"repeatable",
|
|
242
|
+
"replace",
|
|
243
|
+
"replica",
|
|
244
|
+
"reset",
|
|
245
|
+
"respect",
|
|
246
|
+
"restart",
|
|
247
|
+
"restrict",
|
|
248
|
+
"return",
|
|
249
|
+
"returns",
|
|
250
|
+
"revoke",
|
|
251
|
+
"role",
|
|
252
|
+
"rollback",
|
|
253
|
+
"rollup",
|
|
254
|
+
"routine",
|
|
255
|
+
"routines",
|
|
256
|
+
"rows",
|
|
257
|
+
"rule",
|
|
258
|
+
"savepoint",
|
|
259
|
+
"scalar",
|
|
260
|
+
"schema",
|
|
261
|
+
"schemas",
|
|
262
|
+
"scroll",
|
|
263
|
+
"search",
|
|
264
|
+
"second",
|
|
265
|
+
"security",
|
|
266
|
+
"sequence",
|
|
267
|
+
"sequences",
|
|
268
|
+
"serializable",
|
|
269
|
+
"server",
|
|
270
|
+
"session",
|
|
271
|
+
"set",
|
|
272
|
+
"sets",
|
|
273
|
+
"share",
|
|
274
|
+
"show",
|
|
275
|
+
"simple",
|
|
276
|
+
"skip",
|
|
277
|
+
"snapshot",
|
|
278
|
+
"source",
|
|
279
|
+
"split",
|
|
280
|
+
"sql",
|
|
281
|
+
"stable",
|
|
282
|
+
"standalone",
|
|
283
|
+
"start",
|
|
284
|
+
"statement",
|
|
285
|
+
"statistics",
|
|
286
|
+
"stdin",
|
|
287
|
+
"stdout",
|
|
288
|
+
"storage",
|
|
289
|
+
"stored",
|
|
290
|
+
"strict",
|
|
291
|
+
"string",
|
|
292
|
+
"strip",
|
|
293
|
+
"subscription",
|
|
294
|
+
"support",
|
|
295
|
+
"sysid",
|
|
296
|
+
"system",
|
|
297
|
+
"tables",
|
|
298
|
+
"tablespace",
|
|
299
|
+
"target",
|
|
300
|
+
"temp",
|
|
301
|
+
"template",
|
|
302
|
+
"temporary",
|
|
303
|
+
"text",
|
|
304
|
+
"ties",
|
|
305
|
+
"transaction",
|
|
306
|
+
"transform",
|
|
307
|
+
"trigger",
|
|
308
|
+
"truncate",
|
|
309
|
+
"trusted",
|
|
310
|
+
"type",
|
|
311
|
+
"types",
|
|
312
|
+
"uescape",
|
|
313
|
+
"unbounded",
|
|
314
|
+
"uncommitted",
|
|
315
|
+
"unconditional",
|
|
316
|
+
"unencrypted",
|
|
317
|
+
"unknown",
|
|
318
|
+
"unlisten",
|
|
319
|
+
"unlogged",
|
|
320
|
+
"until",
|
|
321
|
+
"update",
|
|
322
|
+
"vacuum",
|
|
323
|
+
"valid",
|
|
324
|
+
"validate",
|
|
325
|
+
"validator",
|
|
326
|
+
"value",
|
|
327
|
+
"varying",
|
|
328
|
+
"version",
|
|
329
|
+
"view",
|
|
330
|
+
"views",
|
|
331
|
+
"virtual",
|
|
332
|
+
"volatile",
|
|
333
|
+
"wait",
|
|
334
|
+
"whitespace",
|
|
335
|
+
"within",
|
|
336
|
+
"without",
|
|
337
|
+
"work",
|
|
338
|
+
"wrapper",
|
|
339
|
+
"write",
|
|
340
|
+
"xml",
|
|
341
|
+
"year",
|
|
342
|
+
"yes",
|
|
343
|
+
"zone"
|
|
344
|
+
],
|
|
345
|
+
RESERVED_KEYWORD: [
|
|
346
|
+
"all",
|
|
347
|
+
"analyse",
|
|
348
|
+
"analyze",
|
|
349
|
+
"and",
|
|
350
|
+
"any",
|
|
351
|
+
"array",
|
|
352
|
+
"as",
|
|
353
|
+
"asc",
|
|
354
|
+
"asymmetric",
|
|
355
|
+
"both",
|
|
356
|
+
"case",
|
|
357
|
+
"cast",
|
|
358
|
+
"check",
|
|
359
|
+
"collate",
|
|
360
|
+
"column",
|
|
361
|
+
"constraint",
|
|
362
|
+
"create",
|
|
363
|
+
"current_catalog",
|
|
364
|
+
"current_date",
|
|
365
|
+
"current_role",
|
|
366
|
+
"current_time",
|
|
367
|
+
"current_timestamp",
|
|
368
|
+
"current_user",
|
|
369
|
+
"default",
|
|
370
|
+
"deferrable",
|
|
371
|
+
"desc",
|
|
372
|
+
"distinct",
|
|
373
|
+
"do",
|
|
374
|
+
"else",
|
|
375
|
+
"end",
|
|
376
|
+
"except",
|
|
377
|
+
"false",
|
|
378
|
+
"fetch",
|
|
379
|
+
"for",
|
|
380
|
+
"foreign",
|
|
381
|
+
"from",
|
|
382
|
+
"grant",
|
|
383
|
+
"group",
|
|
384
|
+
"having",
|
|
385
|
+
"in",
|
|
386
|
+
"initially",
|
|
387
|
+
"intersect",
|
|
388
|
+
"into",
|
|
389
|
+
"lateral",
|
|
390
|
+
"leading",
|
|
391
|
+
"limit",
|
|
392
|
+
"localtime",
|
|
393
|
+
"localtimestamp",
|
|
394
|
+
"not",
|
|
395
|
+
"null",
|
|
396
|
+
"offset",
|
|
397
|
+
"on",
|
|
398
|
+
"only",
|
|
399
|
+
"or",
|
|
400
|
+
"order",
|
|
401
|
+
"placing",
|
|
402
|
+
"primary",
|
|
403
|
+
"references",
|
|
404
|
+
"returning",
|
|
405
|
+
"select",
|
|
406
|
+
"session_user",
|
|
407
|
+
"some",
|
|
408
|
+
"symmetric",
|
|
409
|
+
"system_user",
|
|
410
|
+
"table",
|
|
411
|
+
"then",
|
|
412
|
+
"to",
|
|
413
|
+
"trailing",
|
|
414
|
+
"true",
|
|
415
|
+
"union",
|
|
416
|
+
"unique",
|
|
417
|
+
"user",
|
|
418
|
+
"using",
|
|
419
|
+
"variadic",
|
|
420
|
+
"when",
|
|
421
|
+
"where",
|
|
422
|
+
"window",
|
|
423
|
+
"with"
|
|
424
|
+
],
|
|
425
|
+
TYPE_FUNC_NAME_KEYWORD: [
|
|
426
|
+
"authorization",
|
|
427
|
+
"binary",
|
|
428
|
+
"collation",
|
|
429
|
+
"concurrently",
|
|
430
|
+
"cross",
|
|
431
|
+
"current_schema",
|
|
432
|
+
"freeze",
|
|
433
|
+
"full",
|
|
434
|
+
"ilike",
|
|
435
|
+
"inner",
|
|
436
|
+
"is",
|
|
437
|
+
"isnull",
|
|
438
|
+
"join",
|
|
439
|
+
"left",
|
|
440
|
+
"like",
|
|
441
|
+
"natural",
|
|
442
|
+
"notnull",
|
|
443
|
+
"outer",
|
|
444
|
+
"overlaps",
|
|
445
|
+
"right",
|
|
446
|
+
"similar",
|
|
447
|
+
"tablesample",
|
|
448
|
+
"verbose"
|
|
449
|
+
],
|
|
450
|
+
COL_NAME_KEYWORD: [
|
|
451
|
+
"between",
|
|
452
|
+
"bigint",
|
|
453
|
+
"bit",
|
|
454
|
+
"boolean",
|
|
455
|
+
"char",
|
|
456
|
+
"character",
|
|
457
|
+
"coalesce",
|
|
458
|
+
"dec",
|
|
459
|
+
"decimal",
|
|
460
|
+
"exists",
|
|
461
|
+
"extract",
|
|
462
|
+
"float",
|
|
463
|
+
"greatest",
|
|
464
|
+
"grouping",
|
|
465
|
+
"inout",
|
|
466
|
+
"int",
|
|
467
|
+
"integer",
|
|
468
|
+
"interval",
|
|
469
|
+
"json",
|
|
470
|
+
"json_array",
|
|
471
|
+
"json_arrayagg",
|
|
472
|
+
"json_exists",
|
|
473
|
+
"json_object",
|
|
474
|
+
"json_objectagg",
|
|
475
|
+
"json_query",
|
|
476
|
+
"json_scalar",
|
|
477
|
+
"json_serialize",
|
|
478
|
+
"json_table",
|
|
479
|
+
"json_value",
|
|
480
|
+
"least",
|
|
481
|
+
"merge_action",
|
|
482
|
+
"national",
|
|
483
|
+
"nchar",
|
|
484
|
+
"none",
|
|
485
|
+
"normalize",
|
|
486
|
+
"nullif",
|
|
487
|
+
"numeric",
|
|
488
|
+
"out",
|
|
489
|
+
"overlay",
|
|
490
|
+
"position",
|
|
491
|
+
"precision",
|
|
492
|
+
"real",
|
|
493
|
+
"row",
|
|
494
|
+
"setof",
|
|
495
|
+
"smallint",
|
|
496
|
+
"substring",
|
|
497
|
+
"time",
|
|
498
|
+
"timestamp",
|
|
499
|
+
"treat",
|
|
500
|
+
"trim",
|
|
501
|
+
"values",
|
|
502
|
+
"varchar",
|
|
503
|
+
"xmlattributes",
|
|
504
|
+
"xmlconcat",
|
|
505
|
+
"xmlelement",
|
|
506
|
+
"xmlexists",
|
|
507
|
+
"xmlforest",
|
|
508
|
+
"xmlnamespaces",
|
|
509
|
+
"xmlparse",
|
|
510
|
+
"xmlpi",
|
|
511
|
+
"xmlroot",
|
|
512
|
+
"xmlserialize",
|
|
513
|
+
"xmltable"
|
|
514
|
+
]
|
|
515
|
+
};
|
|
516
|
+
export const RESERVED_KEYWORDS = new Set(kwlist.RESERVED_KEYWORD ?? []);
|
|
517
|
+
export const UNRESERVED_KEYWORDS = new Set(kwlist.UNRESERVED_KEYWORD ?? []);
|
|
518
|
+
export const COL_NAME_KEYWORDS = new Set(kwlist.COL_NAME_KEYWORD ?? []);
|
|
519
|
+
export const TYPE_FUNC_NAME_KEYWORDS = new Set(kwlist.TYPE_FUNC_NAME_KEYWORD ?? []);
|
|
520
|
+
export function keywordKindOf(word) {
|
|
521
|
+
const w = word.toLowerCase();
|
|
522
|
+
if (RESERVED_KEYWORDS.has(w))
|
|
523
|
+
return "RESERVED_KEYWORD";
|
|
524
|
+
if (TYPE_FUNC_NAME_KEYWORDS.has(w))
|
|
525
|
+
return "TYPE_FUNC_NAME_KEYWORD";
|
|
526
|
+
if (COL_NAME_KEYWORDS.has(w))
|
|
527
|
+
return "COL_NAME_KEYWORD";
|
|
528
|
+
if (UNRESERVED_KEYWORDS.has(w))
|
|
529
|
+
return "UNRESERVED_KEYWORD";
|
|
530
|
+
return "NO_KEYWORD";
|
|
531
|
+
}
|
package/esm/utils/quote-utils.js
CHANGED
|
@@ -1,50 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
'all', 'analyse', 'analyze', 'and', 'any', 'array', 'as', 'asc', 'asymmetric',
|
|
3
|
-
'authorization', 'binary', 'both', 'case', 'cast', 'check', 'collate', 'collation',
|
|
4
|
-
'column', 'concurrently', 'constraint', 'create', 'cross', 'current_catalog',
|
|
5
|
-
'current_date', 'current_role', 'current_schema', 'current_time', 'current_timestamp',
|
|
6
|
-
'current_user', 'default', 'deferrable', 'desc', 'distinct', 'do', 'else', 'end',
|
|
7
|
-
'except', 'false', 'fetch', 'for', 'foreign', 'freeze', 'from', 'full', 'grant',
|
|
8
|
-
'group', 'having', 'ilike', 'in', 'initially', 'inner', 'intersect', 'into', 'is',
|
|
9
|
-
'isnull', 'join', 'lateral', 'leading', 'left', 'like', 'limit', 'localtime',
|
|
10
|
-
'localtimestamp', 'natural', 'not', 'notnull', 'null', 'offset', 'on', 'only',
|
|
11
|
-
'or', 'order', 'outer', 'overlaps', 'placing', 'primary', 'references', 'returning',
|
|
12
|
-
'right', 'select', 'session_user', 'similar', 'some', 'symmetric', 'table', 'tablesample',
|
|
13
|
-
'then', 'to', 'trailing', 'true', 'union', 'unique', 'user', 'using', 'variadic',
|
|
14
|
-
'verbose', 'when', 'where', 'window', 'with'
|
|
15
|
-
]);
|
|
1
|
+
import { keywordKindOf } from '../kwlist';
|
|
16
2
|
export class QuoteUtils {
|
|
17
|
-
static needsQuotes(value) {
|
|
18
|
-
if (!value || typeof value !== 'string') {
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
const lowerValue = value.toLowerCase();
|
|
22
|
-
if (RESERVED_WORDS.has(lowerValue)) {
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
if (!/^[a-z_][a-z0-9_$]*$/i.test(value)) {
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
if (value !== value.toLowerCase()) {
|
|
29
|
-
return true;
|
|
30
|
-
}
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
static quote(value) {
|
|
34
|
-
if (value == null) {
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
if (Array.isArray(value)) {
|
|
38
|
-
return value.map(v => this.quote(v));
|
|
39
|
-
}
|
|
40
|
-
if (typeof value !== 'string') {
|
|
41
|
-
return value;
|
|
42
|
-
}
|
|
43
|
-
if (this.needsQuotes(value)) {
|
|
44
|
-
return `"${value}"`;
|
|
45
|
-
}
|
|
46
|
-
return value;
|
|
47
|
-
}
|
|
48
3
|
static escape(literal) {
|
|
49
4
|
return `'${literal.replace(/'/g, "''")}'`;
|
|
50
5
|
}
|
|
@@ -78,4 +33,80 @@ export class QuoteUtils {
|
|
|
78
33
|
// unless it's a raw \x... bytea-style literal.
|
|
79
34
|
return !/^\\x[0-9a-fA-F]+$/i.test(value) && value.includes('\\');
|
|
80
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Quote an identifier only if needed
|
|
38
|
+
*
|
|
39
|
+
* This is a TypeScript port of PostgreSQL's quote_identifier() function from ruleutils.c
|
|
40
|
+
* https://github.com/postgres/postgres/blob/fab5cd3dd1323f9e66efeb676c4bb212ff340204/src/backend/utils/adt/ruleutils.c#L13055-L13137
|
|
41
|
+
*
|
|
42
|
+
* Can avoid quoting if ident starts with a lowercase letter or underscore
|
|
43
|
+
* and contains only lowercase letters, digits, and underscores, *and* is
|
|
44
|
+
* not any SQL keyword. Otherwise, supply quotes.
|
|
45
|
+
*
|
|
46
|
+
* When quotes are needed, embedded double quotes are properly escaped as "".
|
|
47
|
+
*/
|
|
48
|
+
static quoteIdentifier(ident) {
|
|
49
|
+
if (!ident)
|
|
50
|
+
return ident;
|
|
51
|
+
let nquotes = 0;
|
|
52
|
+
let safe = true;
|
|
53
|
+
// Check first character: must be lowercase letter or underscore
|
|
54
|
+
const firstChar = ident[0];
|
|
55
|
+
if (!((firstChar >= 'a' && firstChar <= 'z') || firstChar === '_')) {
|
|
56
|
+
safe = false;
|
|
57
|
+
}
|
|
58
|
+
// Check all characters
|
|
59
|
+
for (let i = 0; i < ident.length; i++) {
|
|
60
|
+
const ch = ident[i];
|
|
61
|
+
if ((ch >= 'a' && ch <= 'z') ||
|
|
62
|
+
(ch >= '0' && ch <= '9') ||
|
|
63
|
+
(ch === '_')) {
|
|
64
|
+
// okay
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
safe = false;
|
|
68
|
+
if (ch === '"') {
|
|
69
|
+
nquotes++;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (safe) {
|
|
74
|
+
// Check for keyword. We quote keywords except for unreserved ones.
|
|
75
|
+
// (In some cases we could avoid quoting a col_name or type_func_name
|
|
76
|
+
// keyword, but it seems much harder than it's worth to tell that.)
|
|
77
|
+
const kwKind = keywordKindOf(ident);
|
|
78
|
+
if (kwKind !== 'NO_KEYWORD' && kwKind !== 'UNRESERVED_KEYWORD') {
|
|
79
|
+
safe = false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (safe) {
|
|
83
|
+
return ident; // no change needed
|
|
84
|
+
}
|
|
85
|
+
// Build quoted identifier with escaped embedded quotes
|
|
86
|
+
let result = '"';
|
|
87
|
+
for (let i = 0; i < ident.length; i++) {
|
|
88
|
+
const ch = ident[i];
|
|
89
|
+
if (ch === '"') {
|
|
90
|
+
result += '"'; // escape " as ""
|
|
91
|
+
}
|
|
92
|
+
result += ch;
|
|
93
|
+
}
|
|
94
|
+
result += '"';
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Quote a possibly-qualified identifier
|
|
99
|
+
*
|
|
100
|
+
* This is a TypeScript port of PostgreSQL's quote_qualified_identifier() function from ruleutils.c
|
|
101
|
+
* https://github.com/postgres/postgres/blob/fab5cd3dd1323f9e66efeb676c4bb212ff340204/src/backend/utils/adt/ruleutils.c#L13139-L13156
|
|
102
|
+
*
|
|
103
|
+
* Return a name of the form qualifier.ident, or just ident if qualifier
|
|
104
|
+
* is null/undefined, quoting each component if necessary.
|
|
105
|
+
*/
|
|
106
|
+
static quoteQualifiedIdentifier(qualifier, ident) {
|
|
107
|
+
if (qualifier) {
|
|
108
|
+
return `${QuoteUtils.quoteIdentifier(qualifier)}.${QuoteUtils.quoteIdentifier(ident)}`;
|
|
109
|
+
}
|
|
110
|
+
return QuoteUtils.quoteIdentifier(ident);
|
|
111
|
+
}
|
|
81
112
|
}
|
package/kwlist.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generated from PostgreSQL kwlist.h
|
|
3
|
+
* DO NOT EDIT BY HAND.
|
|
4
|
+
*/
|
|
5
|
+
export type KeywordKind = "NO_KEYWORD" | "UNRESERVED_KEYWORD" | "COL_NAME_KEYWORD" | "TYPE_FUNC_NAME_KEYWORD" | "RESERVED_KEYWORD";
|
|
6
|
+
export declare const kwlist: {
|
|
7
|
+
readonly UNRESERVED_KEYWORD: readonly ["abort", "absent", "absolute", "access", "action", "add", "admin", "after", "aggregate", "also", "alter", "always", "asensitive", "assertion", "assignment", "at", "atomic", "attach", "attribute", "backward", "before", "begin", "breadth", "by", "cache", "call", "called", "cascade", "cascaded", "catalog", "chain", "characteristics", "checkpoint", "class", "close", "cluster", "columns", "comment", "comments", "commit", "committed", "compression", "conditional", "configuration", "conflict", "connection", "constraints", "content", "continue", "conversion", "copy", "cost", "csv", "cube", "current", "cursor", "cycle", "data", "database", "day", "deallocate", "declare", "defaults", "deferred", "definer", "delete", "delimiter", "delimiters", "depends", "depth", "detach", "dictionary", "disable", "discard", "document", "domain", "double", "drop", "each", "empty", "enable", "encoding", "encrypted", "enforced", "enum", "error", "escape", "event", "exclude", "excluding", "exclusive", "execute", "explain", "expression", "extension", "external", "family", "filter", "finalize", "first", "following", "force", "format", "forward", "function", "functions", "generated", "global", "granted", "groups", "handler", "header", "hold", "hour", "identity", "if", "ignore", "immediate", "immutable", "implicit", "import", "include", "including", "increment", "indent", "index", "indexes", "inherit", "inherits", "inline", "input", "insensitive", "insert", "instead", "invoker", "isolation", "keep", "key", "keys", "label", "language", "large", "last", "leakproof", "level", "listen", "load", "local", "location", "lock", "locked", "logged", "lsn", "mapping", "match", "matched", "materialized", "maxvalue", "merge", "method", "minute", "minvalue", "mode", "month", "move", "name", "names", "nested", "new", "next", "nfc", "nfd", "nfkc", "nfkd", "no", "normalized", "nothing", "notify", "nowait", "nulls", "object", "objects", "of", "off", "oids", "old", "omit", "operator", "option", "options", "ordinality", "others", "over", "overriding", "owned", "owner", "parallel", "parameter", "parser", "partial", "partition", "partitions", "passing", "password", "path", "period", "plan", "plans", "policy", "preceding", "prepare", "prepared", "preserve", "prior", "privileges", "procedural", "procedure", "procedures", "program", "publication", "quote", "quotes", "range", "read", "reassign", "recursive", "ref", "referencing", "refresh", "reindex", "relative", "release", "rename", "repeatable", "replace", "replica", "reset", "respect", "restart", "restrict", "return", "returns", "revoke", "role", "rollback", "rollup", "routine", "routines", "rows", "rule", "savepoint", "scalar", "schema", "schemas", "scroll", "search", "second", "security", "sequence", "sequences", "serializable", "server", "session", "set", "sets", "share", "show", "simple", "skip", "snapshot", "source", "split", "sql", "stable", "standalone", "start", "statement", "statistics", "stdin", "stdout", "storage", "stored", "strict", "string", "strip", "subscription", "support", "sysid", "system", "tables", "tablespace", "target", "temp", "template", "temporary", "text", "ties", "transaction", "transform", "trigger", "truncate", "trusted", "type", "types", "uescape", "unbounded", "uncommitted", "unconditional", "unencrypted", "unknown", "unlisten", "unlogged", "until", "update", "vacuum", "valid", "validate", "validator", "value", "varying", "version", "view", "views", "virtual", "volatile", "wait", "whitespace", "within", "without", "work", "wrapper", "write", "xml", "year", "yes", "zone"];
|
|
8
|
+
readonly RESERVED_KEYWORD: readonly ["all", "analyse", "analyze", "and", "any", "array", "as", "asc", "asymmetric", "both", "case", "cast", "check", "collate", "column", "constraint", "create", "current_catalog", "current_date", "current_role", "current_time", "current_timestamp", "current_user", "default", "deferrable", "desc", "distinct", "do", "else", "end", "except", "false", "fetch", "for", "foreign", "from", "grant", "group", "having", "in", "initially", "intersect", "into", "lateral", "leading", "limit", "localtime", "localtimestamp", "not", "null", "offset", "on", "only", "or", "order", "placing", "primary", "references", "returning", "select", "session_user", "some", "symmetric", "system_user", "table", "then", "to", "trailing", "true", "union", "unique", "user", "using", "variadic", "when", "where", "window", "with"];
|
|
9
|
+
readonly TYPE_FUNC_NAME_KEYWORD: readonly ["authorization", "binary", "collation", "concurrently", "cross", "current_schema", "freeze", "full", "ilike", "inner", "is", "isnull", "join", "left", "like", "natural", "notnull", "outer", "overlaps", "right", "similar", "tablesample", "verbose"];
|
|
10
|
+
readonly COL_NAME_KEYWORD: readonly ["between", "bigint", "bit", "boolean", "char", "character", "coalesce", "dec", "decimal", "exists", "extract", "float", "greatest", "grouping", "inout", "int", "integer", "interval", "json", "json_array", "json_arrayagg", "json_exists", "json_object", "json_objectagg", "json_query", "json_scalar", "json_serialize", "json_table", "json_value", "least", "merge_action", "national", "nchar", "none", "normalize", "nullif", "numeric", "out", "overlay", "position", "precision", "real", "row", "setof", "smallint", "substring", "time", "timestamp", "treat", "trim", "values", "varchar", "xmlattributes", "xmlconcat", "xmlelement", "xmlexists", "xmlforest", "xmlnamespaces", "xmlparse", "xmlpi", "xmlroot", "xmlserialize", "xmltable"];
|
|
11
|
+
};
|
|
12
|
+
export declare const RESERVED_KEYWORDS: Set<string>;
|
|
13
|
+
export declare const UNRESERVED_KEYWORDS: Set<string>;
|
|
14
|
+
export declare const COL_NAME_KEYWORDS: Set<string>;
|
|
15
|
+
export declare const TYPE_FUNC_NAME_KEYWORDS: Set<string>;
|
|
16
|
+
export declare function keywordKindOf(word: string): KeywordKind;
|