1 | /**
|
---|
2 | * @param {string} value
|
---|
3 | * @returns {RegExp}
|
---|
4 | * */
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * @param {RegExp | string } re
|
---|
8 | * @returns {string}
|
---|
9 | */
|
---|
10 | function source(re) {
|
---|
11 | if (!re) return null;
|
---|
12 | if (typeof re === "string") return re;
|
---|
13 |
|
---|
14 | return re.source;
|
---|
15 | }
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * @param {...(RegExp | string) } args
|
---|
19 | * @returns {string}
|
---|
20 | */
|
---|
21 | function concat(...args) {
|
---|
22 | const joined = args.map((x) => source(x)).join("");
|
---|
23 | return joined;
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Any of the passed expresssions may match
|
---|
28 | *
|
---|
29 | * Creates a huge this | this | that | that match
|
---|
30 | * @param {(RegExp | string)[] } args
|
---|
31 | * @returns {string}
|
---|
32 | */
|
---|
33 | function either(...args) {
|
---|
34 | const joined = '(' + args.map((x) => source(x)).join("|") + ")";
|
---|
35 | return joined;
|
---|
36 | }
|
---|
37 |
|
---|
38 | /*
|
---|
39 | Language: SQL
|
---|
40 | Website: https://en.wikipedia.org/wiki/SQL
|
---|
41 | Category: common, database
|
---|
42 | */
|
---|
43 |
|
---|
44 | function sql(hljs) {
|
---|
45 | const COMMENT_MODE = hljs.COMMENT('--', '$');
|
---|
46 | const STRING = {
|
---|
47 | className: 'string',
|
---|
48 | variants: [
|
---|
49 | {
|
---|
50 | begin: /'/,
|
---|
51 | end: /'/,
|
---|
52 | contains: [
|
---|
53 | {begin: /''/ }
|
---|
54 | ]
|
---|
55 | }
|
---|
56 | ]
|
---|
57 | };
|
---|
58 | const QUOTED_IDENTIFIER = {
|
---|
59 | begin: /"/,
|
---|
60 | end: /"/,
|
---|
61 | contains: [ { begin: /""/ } ]
|
---|
62 | };
|
---|
63 |
|
---|
64 | const LITERALS = [
|
---|
65 | "true",
|
---|
66 | "false",
|
---|
67 | // Not sure it's correct to call NULL literal, and clauses like IS [NOT] NULL look strange that way.
|
---|
68 | // "null",
|
---|
69 | "unknown"
|
---|
70 | ];
|
---|
71 |
|
---|
72 | const MULTI_WORD_TYPES = [
|
---|
73 | "double precision",
|
---|
74 | "large object",
|
---|
75 | "with timezone",
|
---|
76 | "without timezone"
|
---|
77 | ];
|
---|
78 |
|
---|
79 | const TYPES = [
|
---|
80 | 'bigint',
|
---|
81 | 'binary',
|
---|
82 | 'blob',
|
---|
83 | 'boolean',
|
---|
84 | 'char',
|
---|
85 | 'character',
|
---|
86 | 'clob',
|
---|
87 | 'date',
|
---|
88 | 'dec',
|
---|
89 | 'decfloat',
|
---|
90 | 'decimal',
|
---|
91 | 'float',
|
---|
92 | 'int',
|
---|
93 | 'integer',
|
---|
94 | 'interval',
|
---|
95 | 'nchar',
|
---|
96 | 'nclob',
|
---|
97 | 'national',
|
---|
98 | 'numeric',
|
---|
99 | 'real',
|
---|
100 | 'row',
|
---|
101 | 'smallint',
|
---|
102 | 'time',
|
---|
103 | 'timestamp',
|
---|
104 | 'varchar',
|
---|
105 | 'varying', // modifier (character varying)
|
---|
106 | 'varbinary'
|
---|
107 | ];
|
---|
108 |
|
---|
109 | const NON_RESERVED_WORDS = [
|
---|
110 | "add",
|
---|
111 | "asc",
|
---|
112 | "collation",
|
---|
113 | "desc",
|
---|
114 | "final",
|
---|
115 | "first",
|
---|
116 | "last",
|
---|
117 | "view"
|
---|
118 | ];
|
---|
119 |
|
---|
120 | // https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#reserved-word
|
---|
121 | const RESERVED_WORDS = [
|
---|
122 | "abs",
|
---|
123 | "acos",
|
---|
124 | "all",
|
---|
125 | "allocate",
|
---|
126 | "alter",
|
---|
127 | "and",
|
---|
128 | "any",
|
---|
129 | "are",
|
---|
130 | "array",
|
---|
131 | "array_agg",
|
---|
132 | "array_max_cardinality",
|
---|
133 | "as",
|
---|
134 | "asensitive",
|
---|
135 | "asin",
|
---|
136 | "asymmetric",
|
---|
137 | "at",
|
---|
138 | "atan",
|
---|
139 | "atomic",
|
---|
140 | "authorization",
|
---|
141 | "avg",
|
---|
142 | "begin",
|
---|
143 | "begin_frame",
|
---|
144 | "begin_partition",
|
---|
145 | "between",
|
---|
146 | "bigint",
|
---|
147 | "binary",
|
---|
148 | "blob",
|
---|
149 | "boolean",
|
---|
150 | "both",
|
---|
151 | "by",
|
---|
152 | "call",
|
---|
153 | "called",
|
---|
154 | "cardinality",
|
---|
155 | "cascaded",
|
---|
156 | "case",
|
---|
157 | "cast",
|
---|
158 | "ceil",
|
---|
159 | "ceiling",
|
---|
160 | "char",
|
---|
161 | "char_length",
|
---|
162 | "character",
|
---|
163 | "character_length",
|
---|
164 | "check",
|
---|
165 | "classifier",
|
---|
166 | "clob",
|
---|
167 | "close",
|
---|
168 | "coalesce",
|
---|
169 | "collate",
|
---|
170 | "collect",
|
---|
171 | "column",
|
---|
172 | "commit",
|
---|
173 | "condition",
|
---|
174 | "connect",
|
---|
175 | "constraint",
|
---|
176 | "contains",
|
---|
177 | "convert",
|
---|
178 | "copy",
|
---|
179 | "corr",
|
---|
180 | "corresponding",
|
---|
181 | "cos",
|
---|
182 | "cosh",
|
---|
183 | "count",
|
---|
184 | "covar_pop",
|
---|
185 | "covar_samp",
|
---|
186 | "create",
|
---|
187 | "cross",
|
---|
188 | "cube",
|
---|
189 | "cume_dist",
|
---|
190 | "current",
|
---|
191 | "current_catalog",
|
---|
192 | "current_date",
|
---|
193 | "current_default_transform_group",
|
---|
194 | "current_path",
|
---|
195 | "current_role",
|
---|
196 | "current_row",
|
---|
197 | "current_schema",
|
---|
198 | "current_time",
|
---|
199 | "current_timestamp",
|
---|
200 | "current_path",
|
---|
201 | "current_role",
|
---|
202 | "current_transform_group_for_type",
|
---|
203 | "current_user",
|
---|
204 | "cursor",
|
---|
205 | "cycle",
|
---|
206 | "date",
|
---|
207 | "day",
|
---|
208 | "deallocate",
|
---|
209 | "dec",
|
---|
210 | "decimal",
|
---|
211 | "decfloat",
|
---|
212 | "declare",
|
---|
213 | "default",
|
---|
214 | "define",
|
---|
215 | "delete",
|
---|
216 | "dense_rank",
|
---|
217 | "deref",
|
---|
218 | "describe",
|
---|
219 | "deterministic",
|
---|
220 | "disconnect",
|
---|
221 | "distinct",
|
---|
222 | "double",
|
---|
223 | "drop",
|
---|
224 | "dynamic",
|
---|
225 | "each",
|
---|
226 | "element",
|
---|
227 | "else",
|
---|
228 | "empty",
|
---|
229 | "end",
|
---|
230 | "end_frame",
|
---|
231 | "end_partition",
|
---|
232 | "end-exec",
|
---|
233 | "equals",
|
---|
234 | "escape",
|
---|
235 | "every",
|
---|
236 | "except",
|
---|
237 | "exec",
|
---|
238 | "execute",
|
---|
239 | "exists",
|
---|
240 | "exp",
|
---|
241 | "external",
|
---|
242 | "extract",
|
---|
243 | "false",
|
---|
244 | "fetch",
|
---|
245 | "filter",
|
---|
246 | "first_value",
|
---|
247 | "float",
|
---|
248 | "floor",
|
---|
249 | "for",
|
---|
250 | "foreign",
|
---|
251 | "frame_row",
|
---|
252 | "free",
|
---|
253 | "from",
|
---|
254 | "full",
|
---|
255 | "function",
|
---|
256 | "fusion",
|
---|
257 | "get",
|
---|
258 | "global",
|
---|
259 | "grant",
|
---|
260 | "group",
|
---|
261 | "grouping",
|
---|
262 | "groups",
|
---|
263 | "having",
|
---|
264 | "hold",
|
---|
265 | "hour",
|
---|
266 | "identity",
|
---|
267 | "in",
|
---|
268 | "indicator",
|
---|
269 | "initial",
|
---|
270 | "inner",
|
---|
271 | "inout",
|
---|
272 | "insensitive",
|
---|
273 | "insert",
|
---|
274 | "int",
|
---|
275 | "integer",
|
---|
276 | "intersect",
|
---|
277 | "intersection",
|
---|
278 | "interval",
|
---|
279 | "into",
|
---|
280 | "is",
|
---|
281 | "join",
|
---|
282 | "json_array",
|
---|
283 | "json_arrayagg",
|
---|
284 | "json_exists",
|
---|
285 | "json_object",
|
---|
286 | "json_objectagg",
|
---|
287 | "json_query",
|
---|
288 | "json_table",
|
---|
289 | "json_table_primitive",
|
---|
290 | "json_value",
|
---|
291 | "lag",
|
---|
292 | "language",
|
---|
293 | "large",
|
---|
294 | "last_value",
|
---|
295 | "lateral",
|
---|
296 | "lead",
|
---|
297 | "leading",
|
---|
298 | "left",
|
---|
299 | "like",
|
---|
300 | "like_regex",
|
---|
301 | "listagg",
|
---|
302 | "ln",
|
---|
303 | "local",
|
---|
304 | "localtime",
|
---|
305 | "localtimestamp",
|
---|
306 | "log",
|
---|
307 | "log10",
|
---|
308 | "lower",
|
---|
309 | "match",
|
---|
310 | "match_number",
|
---|
311 | "match_recognize",
|
---|
312 | "matches",
|
---|
313 | "max",
|
---|
314 | "member",
|
---|
315 | "merge",
|
---|
316 | "method",
|
---|
317 | "min",
|
---|
318 | "minute",
|
---|
319 | "mod",
|
---|
320 | "modifies",
|
---|
321 | "module",
|
---|
322 | "month",
|
---|
323 | "multiset",
|
---|
324 | "national",
|
---|
325 | "natural",
|
---|
326 | "nchar",
|
---|
327 | "nclob",
|
---|
328 | "new",
|
---|
329 | "no",
|
---|
330 | "none",
|
---|
331 | "normalize",
|
---|
332 | "not",
|
---|
333 | "nth_value",
|
---|
334 | "ntile",
|
---|
335 | "null",
|
---|
336 | "nullif",
|
---|
337 | "numeric",
|
---|
338 | "octet_length",
|
---|
339 | "occurrences_regex",
|
---|
340 | "of",
|
---|
341 | "offset",
|
---|
342 | "old",
|
---|
343 | "omit",
|
---|
344 | "on",
|
---|
345 | "one",
|
---|
346 | "only",
|
---|
347 | "open",
|
---|
348 | "or",
|
---|
349 | "order",
|
---|
350 | "out",
|
---|
351 | "outer",
|
---|
352 | "over",
|
---|
353 | "overlaps",
|
---|
354 | "overlay",
|
---|
355 | "parameter",
|
---|
356 | "partition",
|
---|
357 | "pattern",
|
---|
358 | "per",
|
---|
359 | "percent",
|
---|
360 | "percent_rank",
|
---|
361 | "percentile_cont",
|
---|
362 | "percentile_disc",
|
---|
363 | "period",
|
---|
364 | "portion",
|
---|
365 | "position",
|
---|
366 | "position_regex",
|
---|
367 | "power",
|
---|
368 | "precedes",
|
---|
369 | "precision",
|
---|
370 | "prepare",
|
---|
371 | "primary",
|
---|
372 | "procedure",
|
---|
373 | "ptf",
|
---|
374 | "range",
|
---|
375 | "rank",
|
---|
376 | "reads",
|
---|
377 | "real",
|
---|
378 | "recursive",
|
---|
379 | "ref",
|
---|
380 | "references",
|
---|
381 | "referencing",
|
---|
382 | "regr_avgx",
|
---|
383 | "regr_avgy",
|
---|
384 | "regr_count",
|
---|
385 | "regr_intercept",
|
---|
386 | "regr_r2",
|
---|
387 | "regr_slope",
|
---|
388 | "regr_sxx",
|
---|
389 | "regr_sxy",
|
---|
390 | "regr_syy",
|
---|
391 | "release",
|
---|
392 | "result",
|
---|
393 | "return",
|
---|
394 | "returns",
|
---|
395 | "revoke",
|
---|
396 | "right",
|
---|
397 | "rollback",
|
---|
398 | "rollup",
|
---|
399 | "row",
|
---|
400 | "row_number",
|
---|
401 | "rows",
|
---|
402 | "running",
|
---|
403 | "savepoint",
|
---|
404 | "scope",
|
---|
405 | "scroll",
|
---|
406 | "search",
|
---|
407 | "second",
|
---|
408 | "seek",
|
---|
409 | "select",
|
---|
410 | "sensitive",
|
---|
411 | "session_user",
|
---|
412 | "set",
|
---|
413 | "show",
|
---|
414 | "similar",
|
---|
415 | "sin",
|
---|
416 | "sinh",
|
---|
417 | "skip",
|
---|
418 | "smallint",
|
---|
419 | "some",
|
---|
420 | "specific",
|
---|
421 | "specifictype",
|
---|
422 | "sql",
|
---|
423 | "sqlexception",
|
---|
424 | "sqlstate",
|
---|
425 | "sqlwarning",
|
---|
426 | "sqrt",
|
---|
427 | "start",
|
---|
428 | "static",
|
---|
429 | "stddev_pop",
|
---|
430 | "stddev_samp",
|
---|
431 | "submultiset",
|
---|
432 | "subset",
|
---|
433 | "substring",
|
---|
434 | "substring_regex",
|
---|
435 | "succeeds",
|
---|
436 | "sum",
|
---|
437 | "symmetric",
|
---|
438 | "system",
|
---|
439 | "system_time",
|
---|
440 | "system_user",
|
---|
441 | "table",
|
---|
442 | "tablesample",
|
---|
443 | "tan",
|
---|
444 | "tanh",
|
---|
445 | "then",
|
---|
446 | "time",
|
---|
447 | "timestamp",
|
---|
448 | "timezone_hour",
|
---|
449 | "timezone_minute",
|
---|
450 | "to",
|
---|
451 | "trailing",
|
---|
452 | "translate",
|
---|
453 | "translate_regex",
|
---|
454 | "translation",
|
---|
455 | "treat",
|
---|
456 | "trigger",
|
---|
457 | "trim",
|
---|
458 | "trim_array",
|
---|
459 | "true",
|
---|
460 | "truncate",
|
---|
461 | "uescape",
|
---|
462 | "union",
|
---|
463 | "unique",
|
---|
464 | "unknown",
|
---|
465 | "unnest",
|
---|
466 | "update ",
|
---|
467 | "upper",
|
---|
468 | "user",
|
---|
469 | "using",
|
---|
470 | "value",
|
---|
471 | "values",
|
---|
472 | "value_of",
|
---|
473 | "var_pop",
|
---|
474 | "var_samp",
|
---|
475 | "varbinary",
|
---|
476 | "varchar",
|
---|
477 | "varying",
|
---|
478 | "versioning",
|
---|
479 | "when",
|
---|
480 | "whenever",
|
---|
481 | "where",
|
---|
482 | "width_bucket",
|
---|
483 | "window",
|
---|
484 | "with",
|
---|
485 | "within",
|
---|
486 | "without",
|
---|
487 | "year",
|
---|
488 | ];
|
---|
489 |
|
---|
490 | // these are reserved words we have identified to be functions
|
---|
491 | // and should only be highlighted in a dispatch-like context
|
---|
492 | // ie, array_agg(...), etc.
|
---|
493 | const RESERVED_FUNCTIONS = [
|
---|
494 | "abs",
|
---|
495 | "acos",
|
---|
496 | "array_agg",
|
---|
497 | "asin",
|
---|
498 | "atan",
|
---|
499 | "avg",
|
---|
500 | "cast",
|
---|
501 | "ceil",
|
---|
502 | "ceiling",
|
---|
503 | "coalesce",
|
---|
504 | "corr",
|
---|
505 | "cos",
|
---|
506 | "cosh",
|
---|
507 | "count",
|
---|
508 | "covar_pop",
|
---|
509 | "covar_samp",
|
---|
510 | "cume_dist",
|
---|
511 | "dense_rank",
|
---|
512 | "deref",
|
---|
513 | "element",
|
---|
514 | "exp",
|
---|
515 | "extract",
|
---|
516 | "first_value",
|
---|
517 | "floor",
|
---|
518 | "json_array",
|
---|
519 | "json_arrayagg",
|
---|
520 | "json_exists",
|
---|
521 | "json_object",
|
---|
522 | "json_objectagg",
|
---|
523 | "json_query",
|
---|
524 | "json_table",
|
---|
525 | "json_table_primitive",
|
---|
526 | "json_value",
|
---|
527 | "lag",
|
---|
528 | "last_value",
|
---|
529 | "lead",
|
---|
530 | "listagg",
|
---|
531 | "ln",
|
---|
532 | "log",
|
---|
533 | "log10",
|
---|
534 | "lower",
|
---|
535 | "max",
|
---|
536 | "min",
|
---|
537 | "mod",
|
---|
538 | "nth_value",
|
---|
539 | "ntile",
|
---|
540 | "nullif",
|
---|
541 | "percent_rank",
|
---|
542 | "percentile_cont",
|
---|
543 | "percentile_disc",
|
---|
544 | "position",
|
---|
545 | "position_regex",
|
---|
546 | "power",
|
---|
547 | "rank",
|
---|
548 | "regr_avgx",
|
---|
549 | "regr_avgy",
|
---|
550 | "regr_count",
|
---|
551 | "regr_intercept",
|
---|
552 | "regr_r2",
|
---|
553 | "regr_slope",
|
---|
554 | "regr_sxx",
|
---|
555 | "regr_sxy",
|
---|
556 | "regr_syy",
|
---|
557 | "row_number",
|
---|
558 | "sin",
|
---|
559 | "sinh",
|
---|
560 | "sqrt",
|
---|
561 | "stddev_pop",
|
---|
562 | "stddev_samp",
|
---|
563 | "substring",
|
---|
564 | "substring_regex",
|
---|
565 | "sum",
|
---|
566 | "tan",
|
---|
567 | "tanh",
|
---|
568 | "translate",
|
---|
569 | "translate_regex",
|
---|
570 | "treat",
|
---|
571 | "trim",
|
---|
572 | "trim_array",
|
---|
573 | "unnest",
|
---|
574 | "upper",
|
---|
575 | "value_of",
|
---|
576 | "var_pop",
|
---|
577 | "var_samp",
|
---|
578 | "width_bucket",
|
---|
579 | ];
|
---|
580 |
|
---|
581 | // these functions can
|
---|
582 | const POSSIBLE_WITHOUT_PARENS = [
|
---|
583 | "current_catalog",
|
---|
584 | "current_date",
|
---|
585 | "current_default_transform_group",
|
---|
586 | "current_path",
|
---|
587 | "current_role",
|
---|
588 | "current_schema",
|
---|
589 | "current_transform_group_for_type",
|
---|
590 | "current_user",
|
---|
591 | "session_user",
|
---|
592 | "system_time",
|
---|
593 | "system_user",
|
---|
594 | "current_time",
|
---|
595 | "localtime",
|
---|
596 | "current_timestamp",
|
---|
597 | "localtimestamp"
|
---|
598 | ];
|
---|
599 |
|
---|
600 | // those exist to boost relevance making these very
|
---|
601 | // "SQL like" keyword combos worth +1 extra relevance
|
---|
602 | const COMBOS = [
|
---|
603 | "create table",
|
---|
604 | "insert into",
|
---|
605 | "primary key",
|
---|
606 | "foreign key",
|
---|
607 | "not null",
|
---|
608 | "alter table",
|
---|
609 | "add constraint",
|
---|
610 | "grouping sets",
|
---|
611 | "on overflow",
|
---|
612 | "character set",
|
---|
613 | "respect nulls",
|
---|
614 | "ignore nulls",
|
---|
615 | "nulls first",
|
---|
616 | "nulls last",
|
---|
617 | "depth first",
|
---|
618 | "breadth first"
|
---|
619 | ];
|
---|
620 |
|
---|
621 | const FUNCTIONS = RESERVED_FUNCTIONS;
|
---|
622 |
|
---|
623 | const KEYWORDS = [...RESERVED_WORDS, ...NON_RESERVED_WORDS].filter((keyword) => {
|
---|
624 | return !RESERVED_FUNCTIONS.includes(keyword);
|
---|
625 | });
|
---|
626 |
|
---|
627 | const VARIABLE = {
|
---|
628 | className: "variable",
|
---|
629 | begin: /@[a-z0-9]+/,
|
---|
630 | };
|
---|
631 |
|
---|
632 | const OPERATOR = {
|
---|
633 | className: "operator",
|
---|
634 | begin: /[-+*/=%^~]|&&?|\|\|?|!=?|<(?:=>?|<|>)?|>[>=]?/,
|
---|
635 | relevance: 0,
|
---|
636 | };
|
---|
637 |
|
---|
638 | const FUNCTION_CALL = {
|
---|
639 | begin: concat(/\b/, either(...FUNCTIONS), /\s*\(/),
|
---|
640 | keywords: {
|
---|
641 | built_in: FUNCTIONS
|
---|
642 | }
|
---|
643 | };
|
---|
644 |
|
---|
645 | // keywords with less than 3 letters are reduced in relevancy
|
---|
646 | function reduceRelevancy(list, {exceptions, when} = {}) {
|
---|
647 | const qualifyFn = when;
|
---|
648 | exceptions = exceptions || [];
|
---|
649 | return list.map((item) => {
|
---|
650 | if (item.match(/\|\d+$/) || exceptions.includes(item)) {
|
---|
651 | return item;
|
---|
652 | } else if (qualifyFn(item)) {
|
---|
653 | return `${item}|0`;
|
---|
654 | } else {
|
---|
655 | return item;
|
---|
656 | }
|
---|
657 | });
|
---|
658 | }
|
---|
659 |
|
---|
660 | return {
|
---|
661 | name: 'SQL',
|
---|
662 | case_insensitive: true,
|
---|
663 | // does not include {} or HTML tags `</`
|
---|
664 | illegal: /[{}]|<\//,
|
---|
665 | keywords: {
|
---|
666 | $pattern: /\b[\w\.]+/,
|
---|
667 | keyword:
|
---|
668 | reduceRelevancy(KEYWORDS, { when: (x) => x.length < 3 }),
|
---|
669 | literal: LITERALS,
|
---|
670 | type: TYPES,
|
---|
671 | built_in: POSSIBLE_WITHOUT_PARENS
|
---|
672 | },
|
---|
673 | contains: [
|
---|
674 | {
|
---|
675 | begin: either(...COMBOS),
|
---|
676 | keywords: {
|
---|
677 | $pattern: /[\w\.]+/,
|
---|
678 | keyword: KEYWORDS.concat(COMBOS),
|
---|
679 | literal: LITERALS,
|
---|
680 | type: TYPES
|
---|
681 | },
|
---|
682 | },
|
---|
683 | {
|
---|
684 | className: "type",
|
---|
685 | begin: either(...MULTI_WORD_TYPES)
|
---|
686 | },
|
---|
687 | FUNCTION_CALL,
|
---|
688 | VARIABLE,
|
---|
689 | STRING,
|
---|
690 | QUOTED_IDENTIFIER,
|
---|
691 | hljs.C_NUMBER_MODE,
|
---|
692 | hljs.C_BLOCK_COMMENT_MODE,
|
---|
693 | COMMENT_MODE,
|
---|
694 | OPERATOR
|
---|
695 | ]
|
---|
696 | };
|
---|
697 | }
|
---|
698 |
|
---|
699 | module.exports = sql;
|
---|