pgsql.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. module.exports = function(hljs) {
  2. var COMMENT_MODE = hljs.COMMENT('--', '$');
  3. var UNQUOTED_IDENT = '[a-zA-Z_][a-zA-Z_0-9$]*';
  4. var DOLLAR_STRING = '\\$([a-zA-Z_]?|[a-zA-Z_][a-zA-Z_0-9]*)\\$';
  5. var LABEL = '<<\\s*' + UNQUOTED_IDENT + '\\s*>>';
  6. var SQL_KW =
  7. // https://www.postgresql.org/docs/11/static/sql-keywords-appendix.html
  8. // https://www.postgresql.org/docs/11/static/sql-commands.html
  9. // SQL commands (starting words)
  10. 'ABORT ALTER ANALYZE BEGIN CALL CHECKPOINT|10 CLOSE CLUSTER COMMENT COMMIT COPY CREATE DEALLOCATE DECLARE ' +
  11. 'DELETE DISCARD DO DROP END EXECUTE EXPLAIN FETCH GRANT IMPORT INSERT LISTEN LOAD LOCK MOVE NOTIFY ' +
  12. 'PREPARE REASSIGN|10 REFRESH REINDEX RELEASE RESET REVOKE ROLLBACK SAVEPOINT SECURITY SELECT SET SHOW ' +
  13. 'START TRUNCATE UNLISTEN|10 UPDATE VACUUM|10 VALUES ' +
  14. // SQL commands (others)
  15. 'AGGREGATE COLLATION CONVERSION|10 DATABASE DEFAULT PRIVILEGES DOMAIN TRIGGER EXTENSION FOREIGN ' +
  16. 'WRAPPER|10 TABLE FUNCTION GROUP LANGUAGE LARGE OBJECT MATERIALIZED VIEW OPERATOR CLASS ' +
  17. 'FAMILY POLICY PUBLICATION|10 ROLE RULE SCHEMA SEQUENCE SERVER STATISTICS SUBSCRIPTION SYSTEM ' +
  18. 'TABLESPACE CONFIGURATION DICTIONARY PARSER TEMPLATE TYPE USER MAPPING PREPARED ACCESS ' +
  19. 'METHOD CAST AS TRANSFORM TRANSACTION OWNED TO INTO SESSION AUTHORIZATION ' +
  20. 'INDEX PROCEDURE ASSERTION ' +
  21. // additional reserved key words
  22. 'ALL ANALYSE AND ANY ARRAY ASC ASYMMETRIC|10 BOTH CASE CHECK ' +
  23. 'COLLATE COLUMN CONCURRENTLY|10 CONSTRAINT CROSS ' +
  24. 'DEFERRABLE RANGE ' +
  25. 'DESC DISTINCT ELSE EXCEPT FOR FREEZE|10 FROM FULL HAVING ' +
  26. 'ILIKE IN INITIALLY INNER INTERSECT IS ISNULL JOIN LATERAL LEADING LIKE LIMIT ' +
  27. 'NATURAL NOT NOTNULL NULL OFFSET ON ONLY OR ORDER OUTER OVERLAPS PLACING PRIMARY ' +
  28. 'REFERENCES RETURNING SIMILAR SOME SYMMETRIC TABLESAMPLE THEN ' +
  29. 'TRAILING UNION UNIQUE USING VARIADIC|10 VERBOSE WHEN WHERE WINDOW WITH ' +
  30. // some of non-reserved (which are used in clauses or as PL/pgSQL keyword)
  31. 'BY RETURNS INOUT OUT SETOF|10 IF STRICT CURRENT CONTINUE OWNER LOCATION OVER PARTITION WITHIN ' +
  32. 'BETWEEN ESCAPE EXTERNAL INVOKER DEFINER WORK RENAME VERSION CONNECTION CONNECT ' +
  33. 'TABLES TEMP TEMPORARY FUNCTIONS SEQUENCES TYPES SCHEMAS OPTION CASCADE RESTRICT ADD ADMIN ' +
  34. 'EXISTS VALID VALIDATE ENABLE DISABLE REPLICA|10 ALWAYS PASSING COLUMNS PATH ' +
  35. 'REF VALUE OVERRIDING IMMUTABLE STABLE VOLATILE BEFORE AFTER EACH ROW PROCEDURAL ' +
  36. 'ROUTINE NO HANDLER VALIDATOR OPTIONS STORAGE OIDS|10 WITHOUT INHERIT DEPENDS CALLED ' +
  37. 'INPUT LEAKPROOF|10 COST ROWS NOWAIT SEARCH UNTIL ENCRYPTED|10 PASSWORD CONFLICT|10 ' +
  38. 'INSTEAD INHERITS CHARACTERISTICS WRITE CURSOR ALSO STATEMENT SHARE EXCLUSIVE INLINE ' +
  39. 'ISOLATION REPEATABLE READ COMMITTED SERIALIZABLE UNCOMMITTED LOCAL GLOBAL SQL PROCEDURES ' +
  40. 'RECURSIVE SNAPSHOT ROLLUP CUBE TRUSTED|10 INCLUDE FOLLOWING PRECEDING UNBOUNDED RANGE GROUPS ' +
  41. 'UNENCRYPTED|10 SYSID FORMAT DELIMITER HEADER QUOTE ENCODING FILTER OFF ' +
  42. // some parameters of VACUUM/ANALYZE/EXPLAIN
  43. 'FORCE_QUOTE FORCE_NOT_NULL FORCE_NULL COSTS BUFFERS TIMING SUMMARY DISABLE_PAGE_SKIPPING ' +
  44. //
  45. 'RESTART CYCLE GENERATED IDENTITY DEFERRED IMMEDIATE LEVEL LOGGED UNLOGGED ' +
  46. 'OF NOTHING NONE EXCLUDE ATTRIBUTE ' +
  47. // from GRANT (not keywords actually)
  48. 'USAGE ROUTINES ' +
  49. // actually literals, but look better this way (due to IS TRUE, IS FALSE, ISNULL etc)
  50. 'TRUE FALSE NAN INFINITY ';
  51. var ROLE_ATTRS = // only those not in keywrods already
  52. 'SUPERUSER NOSUPERUSER CREATEDB NOCREATEDB CREATEROLE NOCREATEROLE INHERIT NOINHERIT ' +
  53. 'LOGIN NOLOGIN REPLICATION NOREPLICATION BYPASSRLS NOBYPASSRLS ';
  54. var PLPGSQL_KW =
  55. 'ALIAS BEGIN CONSTANT DECLARE END EXCEPTION RETURN PERFORM|10 RAISE GET DIAGNOSTICS ' +
  56. 'STACKED|10 FOREACH LOOP ELSIF EXIT WHILE REVERSE SLICE DEBUG LOG INFO NOTICE WARNING ASSERT ' +
  57. 'OPEN ';
  58. var TYPES =
  59. // https://www.postgresql.org/docs/11/static/datatype.html
  60. 'BIGINT INT8 BIGSERIAL SERIAL8 BIT VARYING VARBIT BOOLEAN BOOL BOX BYTEA CHARACTER CHAR VARCHAR ' +
  61. 'CIDR CIRCLE DATE DOUBLE PRECISION FLOAT8 FLOAT INET INTEGER INT INT4 INTERVAL JSON JSONB LINE LSEG|10 ' +
  62. 'MACADDR MACADDR8 MONEY NUMERIC DEC DECIMAL PATH POINT POLYGON REAL FLOAT4 SMALLINT INT2 ' +
  63. 'SMALLSERIAL|10 SERIAL2|10 SERIAL|10 SERIAL4|10 TEXT TIME ZONE TIMETZ|10 TIMESTAMP TIMESTAMPTZ|10 TSQUERY|10 TSVECTOR|10 ' +
  64. 'TXID_SNAPSHOT|10 UUID XML NATIONAL NCHAR ' +
  65. 'INT4RANGE|10 INT8RANGE|10 NUMRANGE|10 TSRANGE|10 TSTZRANGE|10 DATERANGE|10 ' +
  66. // pseudotypes
  67. 'ANYELEMENT ANYARRAY ANYNONARRAY ANYENUM ANYRANGE CSTRING INTERNAL ' +
  68. 'RECORD PG_DDL_COMMAND VOID UNKNOWN OPAQUE REFCURSOR ' +
  69. // spec. type
  70. 'NAME ' +
  71. // OID-types
  72. 'OID REGPROC|10 REGPROCEDURE|10 REGOPER|10 REGOPERATOR|10 REGCLASS|10 REGTYPE|10 REGROLE|10 ' +
  73. 'REGNAMESPACE|10 REGCONFIG|10 REGDICTIONARY|10 ';// +
  74. // some types from standard extensions
  75. 'HSTORE|10 LO LTREE|10 ';
  76. var TYPES_RE =
  77. TYPES.trim()
  78. .split(' ')
  79. .map( function(val) { return val.split('|')[0]; } )
  80. .join('|');
  81. var SQL_BI =
  82. 'CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURRENT_CATALOG|10 CURRENT_DATE LOCALTIME LOCALTIMESTAMP ' +
  83. 'CURRENT_ROLE|10 CURRENT_SCHEMA|10 SESSION_USER PUBLIC ';
  84. var PLPGSQL_BI =
  85. 'FOUND NEW OLD TG_NAME|10 TG_WHEN|10 TG_LEVEL|10 TG_OP|10 TG_RELID|10 TG_RELNAME|10 ' +
  86. 'TG_TABLE_NAME|10 TG_TABLE_SCHEMA|10 TG_NARGS|10 TG_ARGV|10 TG_EVENT|10 TG_TAG|10 ' +
  87. // get diagnostics
  88. 'ROW_COUNT RESULT_OID|10 PG_CONTEXT|10 RETURNED_SQLSTATE COLUMN_NAME CONSTRAINT_NAME ' +
  89. 'PG_DATATYPE_NAME|10 MESSAGE_TEXT TABLE_NAME SCHEMA_NAME PG_EXCEPTION_DETAIL|10 ' +
  90. 'PG_EXCEPTION_HINT|10 PG_EXCEPTION_CONTEXT|10 ';
  91. var PLPGSQL_EXCEPTIONS =
  92. // exceptions https://www.postgresql.org/docs/current/static/errcodes-appendix.html
  93. 'SQLSTATE SQLERRM|10 ' +
  94. 'SUCCESSFUL_COMPLETION WARNING DYNAMIC_RESULT_SETS_RETURNED IMPLICIT_ZERO_BIT_PADDING ' +
  95. 'NULL_VALUE_ELIMINATED_IN_SET_FUNCTION PRIVILEGE_NOT_GRANTED PRIVILEGE_NOT_REVOKED ' +
  96. 'STRING_DATA_RIGHT_TRUNCATION DEPRECATED_FEATURE NO_DATA NO_ADDITIONAL_DYNAMIC_RESULT_SETS_RETURNED ' +
  97. 'SQL_STATEMENT_NOT_YET_COMPLETE CONNECTION_EXCEPTION CONNECTION_DOES_NOT_EXIST CONNECTION_FAILURE ' +
  98. 'SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION SQLSERVER_REJECTED_ESTABLISHMENT_OF_SQLCONNECTION ' +
  99. 'TRANSACTION_RESOLUTION_UNKNOWN PROTOCOL_VIOLATION TRIGGERED_ACTION_EXCEPTION FEATURE_NOT_SUPPORTED ' +
  100. 'INVALID_TRANSACTION_INITIATION LOCATOR_EXCEPTION INVALID_LOCATOR_SPECIFICATION INVALID_GRANTOR ' +
  101. 'INVALID_GRANT_OPERATION INVALID_ROLE_SPECIFICATION DIAGNOSTICS_EXCEPTION ' +
  102. 'STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER CASE_NOT_FOUND CARDINALITY_VIOLATION ' +
  103. 'DATA_EXCEPTION ARRAY_SUBSCRIPT_ERROR CHARACTER_NOT_IN_REPERTOIRE DATETIME_FIELD_OVERFLOW ' +
  104. 'DIVISION_BY_ZERO ERROR_IN_ASSIGNMENT ESCAPE_CHARACTER_CONFLICT INDICATOR_OVERFLOW ' +
  105. 'INTERVAL_FIELD_OVERFLOW INVALID_ARGUMENT_FOR_LOGARITHM INVALID_ARGUMENT_FOR_NTILE_FUNCTION ' +
  106. 'INVALID_ARGUMENT_FOR_NTH_VALUE_FUNCTION INVALID_ARGUMENT_FOR_POWER_FUNCTION ' +
  107. 'INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION INVALID_CHARACTER_VALUE_FOR_CAST ' +
  108. 'INVALID_DATETIME_FORMAT INVALID_ESCAPE_CHARACTER INVALID_ESCAPE_OCTET INVALID_ESCAPE_SEQUENCE ' +
  109. 'NONSTANDARD_USE_OF_ESCAPE_CHARACTER INVALID_INDICATOR_PARAMETER_VALUE INVALID_PARAMETER_VALUE ' +
  110. 'INVALID_REGULAR_EXPRESSION INVALID_ROW_COUNT_IN_LIMIT_CLAUSE ' +
  111. 'INVALID_ROW_COUNT_IN_RESULT_OFFSET_CLAUSE INVALID_TABLESAMPLE_ARGUMENT INVALID_TABLESAMPLE_REPEAT ' +
  112. 'INVALID_TIME_ZONE_DISPLACEMENT_VALUE INVALID_USE_OF_ESCAPE_CHARACTER MOST_SPECIFIC_TYPE_MISMATCH ' +
  113. 'NULL_VALUE_NOT_ALLOWED NULL_VALUE_NO_INDICATOR_PARAMETER NUMERIC_VALUE_OUT_OF_RANGE ' +
  114. 'SEQUENCE_GENERATOR_LIMIT_EXCEEDED STRING_DATA_LENGTH_MISMATCH STRING_DATA_RIGHT_TRUNCATION ' +
  115. 'SUBSTRING_ERROR TRIM_ERROR UNTERMINATED_C_STRING ZERO_LENGTH_CHARACTER_STRING ' +
  116. 'FLOATING_POINT_EXCEPTION INVALID_TEXT_REPRESENTATION INVALID_BINARY_REPRESENTATION ' +
  117. 'BAD_COPY_FILE_FORMAT UNTRANSLATABLE_CHARACTER NOT_AN_XML_DOCUMENT INVALID_XML_DOCUMENT ' +
  118. 'INVALID_XML_CONTENT INVALID_XML_COMMENT INVALID_XML_PROCESSING_INSTRUCTION ' +
  119. 'INTEGRITY_CONSTRAINT_VIOLATION RESTRICT_VIOLATION NOT_NULL_VIOLATION FOREIGN_KEY_VIOLATION ' +
  120. 'UNIQUE_VIOLATION CHECK_VIOLATION EXCLUSION_VIOLATION INVALID_CURSOR_STATE ' +
  121. 'INVALID_TRANSACTION_STATE ACTIVE_SQL_TRANSACTION BRANCH_TRANSACTION_ALREADY_ACTIVE ' +
  122. 'HELD_CURSOR_REQUIRES_SAME_ISOLATION_LEVEL INAPPROPRIATE_ACCESS_MODE_FOR_BRANCH_TRANSACTION ' +
  123. 'INAPPROPRIATE_ISOLATION_LEVEL_FOR_BRANCH_TRANSACTION ' +
  124. 'NO_ACTIVE_SQL_TRANSACTION_FOR_BRANCH_TRANSACTION READ_ONLY_SQL_TRANSACTION ' +
  125. 'SCHEMA_AND_DATA_STATEMENT_MIXING_NOT_SUPPORTED NO_ACTIVE_SQL_TRANSACTION ' +
  126. 'IN_FAILED_SQL_TRANSACTION IDLE_IN_TRANSACTION_SESSION_TIMEOUT INVALID_SQL_STATEMENT_NAME ' +
  127. 'TRIGGERED_DATA_CHANGE_VIOLATION INVALID_AUTHORIZATION_SPECIFICATION INVALID_PASSWORD ' +
  128. 'DEPENDENT_PRIVILEGE_DESCRIPTORS_STILL_EXIST DEPENDENT_OBJECTS_STILL_EXIST ' +
  129. 'INVALID_TRANSACTION_TERMINATION SQL_ROUTINE_EXCEPTION FUNCTION_EXECUTED_NO_RETURN_STATEMENT ' +
  130. 'MODIFYING_SQL_DATA_NOT_PERMITTED PROHIBITED_SQL_STATEMENT_ATTEMPTED ' +
  131. 'READING_SQL_DATA_NOT_PERMITTED INVALID_CURSOR_NAME EXTERNAL_ROUTINE_EXCEPTION ' +
  132. 'CONTAINING_SQL_NOT_PERMITTED MODIFYING_SQL_DATA_NOT_PERMITTED ' +
  133. 'PROHIBITED_SQL_STATEMENT_ATTEMPTED READING_SQL_DATA_NOT_PERMITTED ' +
  134. 'EXTERNAL_ROUTINE_INVOCATION_EXCEPTION INVALID_SQLSTATE_RETURNED NULL_VALUE_NOT_ALLOWED ' +
  135. 'TRIGGER_PROTOCOL_VIOLATED SRF_PROTOCOL_VIOLATED EVENT_TRIGGER_PROTOCOL_VIOLATED ' +
  136. 'SAVEPOINT_EXCEPTION INVALID_SAVEPOINT_SPECIFICATION INVALID_CATALOG_NAME ' +
  137. 'INVALID_SCHEMA_NAME TRANSACTION_ROLLBACK TRANSACTION_INTEGRITY_CONSTRAINT_VIOLATION ' +
  138. 'SERIALIZATION_FAILURE STATEMENT_COMPLETION_UNKNOWN DEADLOCK_DETECTED ' +
  139. 'SYNTAX_ERROR_OR_ACCESS_RULE_VIOLATION SYNTAX_ERROR INSUFFICIENT_PRIVILEGE CANNOT_COERCE ' +
  140. 'GROUPING_ERROR WINDOWING_ERROR INVALID_RECURSION INVALID_FOREIGN_KEY INVALID_NAME ' +
  141. 'NAME_TOO_LONG RESERVED_NAME DATATYPE_MISMATCH INDETERMINATE_DATATYPE COLLATION_MISMATCH ' +
  142. 'INDETERMINATE_COLLATION WRONG_OBJECT_TYPE GENERATED_ALWAYS UNDEFINED_COLUMN ' +
  143. 'UNDEFINED_FUNCTION UNDEFINED_TABLE UNDEFINED_PARAMETER UNDEFINED_OBJECT ' +
  144. 'DUPLICATE_COLUMN DUPLICATE_CURSOR DUPLICATE_DATABASE DUPLICATE_FUNCTION ' +
  145. 'DUPLICATE_PREPARED_STATEMENT DUPLICATE_SCHEMA DUPLICATE_TABLE DUPLICATE_ALIAS ' +
  146. 'DUPLICATE_OBJECT AMBIGUOUS_COLUMN AMBIGUOUS_FUNCTION AMBIGUOUS_PARAMETER AMBIGUOUS_ALIAS ' +
  147. 'INVALID_COLUMN_REFERENCE INVALID_COLUMN_DEFINITION INVALID_CURSOR_DEFINITION ' +
  148. 'INVALID_DATABASE_DEFINITION INVALID_FUNCTION_DEFINITION ' +
  149. 'INVALID_PREPARED_STATEMENT_DEFINITION INVALID_SCHEMA_DEFINITION INVALID_TABLE_DEFINITION ' +
  150. 'INVALID_OBJECT_DEFINITION WITH_CHECK_OPTION_VIOLATION INSUFFICIENT_RESOURCES DISK_FULL ' +
  151. 'OUT_OF_MEMORY TOO_MANY_CONNECTIONS CONFIGURATION_LIMIT_EXCEEDED PROGRAM_LIMIT_EXCEEDED ' +
  152. 'STATEMENT_TOO_COMPLEX TOO_MANY_COLUMNS TOO_MANY_ARGUMENTS OBJECT_NOT_IN_PREREQUISITE_STATE ' +
  153. 'OBJECT_IN_USE CANT_CHANGE_RUNTIME_PARAM LOCK_NOT_AVAILABLE OPERATOR_INTERVENTION ' +
  154. 'QUERY_CANCELED ADMIN_SHUTDOWN CRASH_SHUTDOWN CANNOT_CONNECT_NOW DATABASE_DROPPED ' +
  155. 'SYSTEM_ERROR IO_ERROR UNDEFINED_FILE DUPLICATE_FILE SNAPSHOT_TOO_OLD CONFIG_FILE_ERROR ' +
  156. 'LOCK_FILE_EXISTS FDW_ERROR FDW_COLUMN_NAME_NOT_FOUND FDW_DYNAMIC_PARAMETER_VALUE_NEEDED ' +
  157. 'FDW_FUNCTION_SEQUENCE_ERROR FDW_INCONSISTENT_DESCRIPTOR_INFORMATION ' +
  158. 'FDW_INVALID_ATTRIBUTE_VALUE FDW_INVALID_COLUMN_NAME FDW_INVALID_COLUMN_NUMBER ' +
  159. 'FDW_INVALID_DATA_TYPE FDW_INVALID_DATA_TYPE_DESCRIPTORS ' +
  160. 'FDW_INVALID_DESCRIPTOR_FIELD_IDENTIFIER FDW_INVALID_HANDLE FDW_INVALID_OPTION_INDEX ' +
  161. 'FDW_INVALID_OPTION_NAME FDW_INVALID_STRING_LENGTH_OR_BUFFER_LENGTH ' +
  162. 'FDW_INVALID_STRING_FORMAT FDW_INVALID_USE_OF_NULL_POINTER FDW_TOO_MANY_HANDLES ' +
  163. 'FDW_OUT_OF_MEMORY FDW_NO_SCHEMAS FDW_OPTION_NAME_NOT_FOUND FDW_REPLY_HANDLE ' +
  164. 'FDW_SCHEMA_NOT_FOUND FDW_TABLE_NOT_FOUND FDW_UNABLE_TO_CREATE_EXECUTION ' +
  165. 'FDW_UNABLE_TO_CREATE_REPLY FDW_UNABLE_TO_ESTABLISH_CONNECTION PLPGSQL_ERROR ' +
  166. 'RAISE_EXCEPTION NO_DATA_FOUND TOO_MANY_ROWS ASSERT_FAILURE INTERNAL_ERROR DATA_CORRUPTED ' +
  167. 'INDEX_CORRUPTED ';
  168. var FUNCTIONS =
  169. // https://www.postgresql.org/docs/11/static/functions-aggregate.html
  170. 'ARRAY_AGG AVG BIT_AND BIT_OR BOOL_AND BOOL_OR COUNT EVERY JSON_AGG JSONB_AGG JSON_OBJECT_AGG ' +
  171. 'JSONB_OBJECT_AGG MAX MIN MODE STRING_AGG SUM XMLAGG ' +
  172. 'CORR COVAR_POP COVAR_SAMP REGR_AVGX REGR_AVGY REGR_COUNT REGR_INTERCEPT REGR_R2 REGR_SLOPE ' +
  173. 'REGR_SXX REGR_SXY REGR_SYY STDDEV STDDEV_POP STDDEV_SAMP VARIANCE VAR_POP VAR_SAMP ' +
  174. 'PERCENTILE_CONT PERCENTILE_DISC ' +
  175. // https://www.postgresql.org/docs/11/static/functions-window.html
  176. 'ROW_NUMBER RANK DENSE_RANK PERCENT_RANK CUME_DIST NTILE LAG LEAD FIRST_VALUE LAST_VALUE NTH_VALUE ' +
  177. // https://www.postgresql.org/docs/11/static/functions-comparison.html
  178. 'NUM_NONNULLS NUM_NULLS ' +
  179. // https://www.postgresql.org/docs/11/static/functions-math.html
  180. 'ABS CBRT CEIL CEILING DEGREES DIV EXP FLOOR LN LOG MOD PI POWER RADIANS ROUND SCALE SIGN SQRT ' +
  181. 'TRUNC WIDTH_BUCKET ' +
  182. 'RANDOM SETSEED ' +
  183. 'ACOS ACOSD ASIN ASIND ATAN ATAND ATAN2 ATAN2D COS COSD COT COTD SIN SIND TAN TAND ' +
  184. // https://www.postgresql.org/docs/11/static/functions-string.html
  185. 'BIT_LENGTH CHAR_LENGTH CHARACTER_LENGTH LOWER OCTET_LENGTH OVERLAY POSITION SUBSTRING TREAT TRIM UPPER ' +
  186. 'ASCII BTRIM CHR CONCAT CONCAT_WS CONVERT CONVERT_FROM CONVERT_TO DECODE ENCODE INITCAP' +
  187. 'LEFT LENGTH LPAD LTRIM MD5 PARSE_IDENT PG_CLIENT_ENCODING QUOTE_IDENT|10 QUOTE_LITERAL|10 ' +
  188. 'QUOTE_NULLABLE|10 REGEXP_MATCH REGEXP_MATCHES REGEXP_REPLACE REGEXP_SPLIT_TO_ARRAY ' +
  189. 'REGEXP_SPLIT_TO_TABLE REPEAT REPLACE REVERSE RIGHT RPAD RTRIM SPLIT_PART STRPOS SUBSTR ' +
  190. 'TO_ASCII TO_HEX TRANSLATE ' +
  191. // https://www.postgresql.org/docs/11/static/functions-binarystring.html
  192. 'OCTET_LENGTH GET_BIT GET_BYTE SET_BIT SET_BYTE ' +
  193. // https://www.postgresql.org/docs/11/static/functions-formatting.html
  194. 'TO_CHAR TO_DATE TO_NUMBER TO_TIMESTAMP ' +
  195. // https://www.postgresql.org/docs/11/static/functions-datetime.html
  196. 'AGE CLOCK_TIMESTAMP|10 DATE_PART DATE_TRUNC ISFINITE JUSTIFY_DAYS JUSTIFY_HOURS JUSTIFY_INTERVAL ' +
  197. 'MAKE_DATE MAKE_INTERVAL|10 MAKE_TIME MAKE_TIMESTAMP|10 MAKE_TIMESTAMPTZ|10 NOW STATEMENT_TIMESTAMP|10 ' +
  198. 'TIMEOFDAY TRANSACTION_TIMESTAMP|10 ' +
  199. // https://www.postgresql.org/docs/11/static/functions-enum.html
  200. 'ENUM_FIRST ENUM_LAST ENUM_RANGE ' +
  201. // https://www.postgresql.org/docs/11/static/functions-geometry.html
  202. 'AREA CENTER DIAMETER HEIGHT ISCLOSED ISOPEN NPOINTS PCLOSE POPEN RADIUS WIDTH ' +
  203. 'BOX BOUND_BOX CIRCLE LINE LSEG PATH POLYGON ' +
  204. // https://www.postgresql.org/docs/11/static/functions-net.html
  205. 'ABBREV BROADCAST HOST HOSTMASK MASKLEN NETMASK NETWORK SET_MASKLEN TEXT INET_SAME_FAMILY' +
  206. 'INET_MERGE MACADDR8_SET7BIT ' +
  207. // https://www.postgresql.org/docs/11/static/functions-textsearch.html
  208. 'ARRAY_TO_TSVECTOR GET_CURRENT_TS_CONFIG NUMNODE PLAINTO_TSQUERY PHRASETO_TSQUERY WEBSEARCH_TO_TSQUERY ' +
  209. 'QUERYTREE SETWEIGHT STRIP TO_TSQUERY TO_TSVECTOR JSON_TO_TSVECTOR JSONB_TO_TSVECTOR TS_DELETE ' +
  210. 'TS_FILTER TS_HEADLINE TS_RANK TS_RANK_CD TS_REWRITE TSQUERY_PHRASE TSVECTOR_TO_ARRAY ' +
  211. 'TSVECTOR_UPDATE_TRIGGER TSVECTOR_UPDATE_TRIGGER_COLUMN ' +
  212. // https://www.postgresql.org/docs/11/static/functions-xml.html
  213. 'XMLCOMMENT XMLCONCAT XMLELEMENT XMLFOREST XMLPI XMLROOT ' +
  214. 'XMLEXISTS XML_IS_WELL_FORMED XML_IS_WELL_FORMED_DOCUMENT XML_IS_WELL_FORMED_CONTENT ' +
  215. 'XPATH XPATH_EXISTS XMLTABLE XMLNAMESPACES ' +
  216. 'TABLE_TO_XML TABLE_TO_XMLSCHEMA TABLE_TO_XML_AND_XMLSCHEMA ' +
  217. 'QUERY_TO_XML QUERY_TO_XMLSCHEMA QUERY_TO_XML_AND_XMLSCHEMA ' +
  218. 'CURSOR_TO_XML CURSOR_TO_XMLSCHEMA ' +
  219. 'SCHEMA_TO_XML SCHEMA_TO_XMLSCHEMA SCHEMA_TO_XML_AND_XMLSCHEMA ' +
  220. 'DATABASE_TO_XML DATABASE_TO_XMLSCHEMA DATABASE_TO_XML_AND_XMLSCHEMA ' +
  221. 'XMLATTRIBUTES ' +
  222. // https://www.postgresql.org/docs/11/static/functions-json.html
  223. 'TO_JSON TO_JSONB ARRAY_TO_JSON ROW_TO_JSON JSON_BUILD_ARRAY JSONB_BUILD_ARRAY JSON_BUILD_OBJECT ' +
  224. 'JSONB_BUILD_OBJECT JSON_OBJECT JSONB_OBJECT JSON_ARRAY_LENGTH JSONB_ARRAY_LENGTH JSON_EACH ' +
  225. 'JSONB_EACH JSON_EACH_TEXT JSONB_EACH_TEXT JSON_EXTRACT_PATH JSONB_EXTRACT_PATH ' +
  226. 'JSON_OBJECT_KEYS JSONB_OBJECT_KEYS JSON_POPULATE_RECORD JSONB_POPULATE_RECORD JSON_POPULATE_RECORDSET ' +
  227. 'JSONB_POPULATE_RECORDSET JSON_ARRAY_ELEMENTS JSONB_ARRAY_ELEMENTS JSON_ARRAY_ELEMENTS_TEXT ' +
  228. 'JSONB_ARRAY_ELEMENTS_TEXT JSON_TYPEOF JSONB_TYPEOF JSON_TO_RECORD JSONB_TO_RECORD JSON_TO_RECORDSET ' +
  229. 'JSONB_TO_RECORDSET JSON_STRIP_NULLS JSONB_STRIP_NULLS JSONB_SET JSONB_INSERT JSONB_PRETTY ' +
  230. // https://www.postgresql.org/docs/11/static/functions-sequence.html
  231. 'CURRVAL LASTVAL NEXTVAL SETVAL ' +
  232. // https://www.postgresql.org/docs/11/static/functions-conditional.html
  233. 'COALESCE NULLIF GREATEST LEAST ' +
  234. // https://www.postgresql.org/docs/11/static/functions-array.html
  235. 'ARRAY_APPEND ARRAY_CAT ARRAY_NDIMS ARRAY_DIMS ARRAY_FILL ARRAY_LENGTH ARRAY_LOWER ARRAY_POSITION ' +
  236. 'ARRAY_POSITIONS ARRAY_PREPEND ARRAY_REMOVE ARRAY_REPLACE ARRAY_TO_STRING ARRAY_UPPER CARDINALITY ' +
  237. 'STRING_TO_ARRAY UNNEST ' +
  238. // https://www.postgresql.org/docs/11/static/functions-range.html
  239. 'ISEMPTY LOWER_INC UPPER_INC LOWER_INF UPPER_INF RANGE_MERGE ' +
  240. // https://www.postgresql.org/docs/11/static/functions-srf.html
  241. 'GENERATE_SERIES GENERATE_SUBSCRIPTS ' +
  242. // https://www.postgresql.org/docs/11/static/functions-info.html
  243. 'CURRENT_DATABASE CURRENT_QUERY CURRENT_SCHEMA|10 CURRENT_SCHEMAS|10 INET_CLIENT_ADDR INET_CLIENT_PORT ' +
  244. 'INET_SERVER_ADDR INET_SERVER_PORT ROW_SECURITY_ACTIVE FORMAT_TYPE ' +
  245. 'TO_REGCLASS TO_REGPROC TO_REGPROCEDURE TO_REGOPER TO_REGOPERATOR TO_REGTYPE TO_REGNAMESPACE TO_REGROLE ' +
  246. 'COL_DESCRIPTION OBJ_DESCRIPTION SHOBJ_DESCRIPTION ' +
  247. 'TXID_CURRENT TXID_CURRENT_IF_ASSIGNED TXID_CURRENT_SNAPSHOT TXID_SNAPSHOT_XIP TXID_SNAPSHOT_XMAX ' +
  248. 'TXID_SNAPSHOT_XMIN TXID_VISIBLE_IN_SNAPSHOT TXID_STATUS ' +
  249. // https://www.postgresql.org/docs/11/static/functions-admin.html
  250. 'CURRENT_SETTING SET_CONFIG BRIN_SUMMARIZE_NEW_VALUES BRIN_SUMMARIZE_RANGE BRIN_DESUMMARIZE_RANGE ' +
  251. 'GIN_CLEAN_PENDING_LIST ' +
  252. // https://www.postgresql.org/docs/11/static/functions-trigger.html
  253. 'SUPPRESS_REDUNDANT_UPDATES_TRIGGER ' +
  254. // ihttps://www.postgresql.org/docs/devel/static/lo-funcs.html
  255. 'LO_FROM_BYTEA LO_PUT LO_GET LO_CREAT LO_CREATE LO_UNLINK LO_IMPORT LO_EXPORT LOREAD LOWRITE ' +
  256. //
  257. 'GROUPING CAST ';
  258. var FUNCTIONS_RE =
  259. FUNCTIONS.trim()
  260. .split(' ')
  261. .map( function(val) { return val.split('|')[0]; } )
  262. .join('|');
  263. return {
  264. aliases: ['postgres','postgresql'],
  265. case_insensitive: true,
  266. keywords: {
  267. keyword:
  268. SQL_KW + PLPGSQL_KW + ROLE_ATTRS,
  269. built_in:
  270. SQL_BI + PLPGSQL_BI + PLPGSQL_EXCEPTIONS,
  271. },
  272. // Forbid some cunstructs from other languages to improve autodetect. In fact
  273. // "[a-z]:" is legal (as part of array slice), but improbabal.
  274. illegal: /:==|\W\s*\(\*|(^|\s)\$[a-z]|{{|[a-z]:\s*$|\.\.\.|TO:|DO:/,
  275. contains: [
  276. // special handling of some words, which are reserved only in some contexts
  277. {
  278. className: 'keyword',
  279. variants: [
  280. { begin: /\bTEXT\s*SEARCH\b/ },
  281. { begin: /\b(PRIMARY|FOREIGN|FOR(\s+NO)?)\s+KEY\b/ },
  282. { begin: /\bPARALLEL\s+(UNSAFE|RESTRICTED|SAFE)\b/ },
  283. { begin: /\bSTORAGE\s+(PLAIN|EXTERNAL|EXTENDED|MAIN)\b/ },
  284. { begin: /\bMATCH\s+(FULL|PARTIAL|SIMPLE)\b/ },
  285. { begin: /\bNULLS\s+(FIRST|LAST)\b/ },
  286. { begin: /\bEVENT\s+TRIGGER\b/ },
  287. { begin: /\b(MAPPING|OR)\s+REPLACE\b/ },
  288. { begin: /\b(FROM|TO)\s+(PROGRAM|STDIN|STDOUT)\b/ },
  289. { begin: /\b(SHARE|EXCLUSIVE)\s+MODE\b/ },
  290. { begin: /\b(LEFT|RIGHT)\s+(OUTER\s+)?JOIN\b/ },
  291. { begin: /\b(FETCH|MOVE)\s+(NEXT|PRIOR|FIRST|LAST|ABSOLUTE|RELATIVE|FORWARD|BACKWARD)\b/ },
  292. { begin: /\bPRESERVE\s+ROWS\b/ },
  293. { begin: /\bDISCARD\s+PLANS\b/ },
  294. { begin: /\bREFERENCING\s+(OLD|NEW)\b/ },
  295. { begin: /\bSKIP\s+LOCKED\b/ },
  296. { begin: /\bGROUPING\s+SETS\b/ },
  297. { begin: /\b(BINARY|INSENSITIVE|SCROLL|NO\s+SCROLL)\s+(CURSOR|FOR)\b/ },
  298. { begin: /\b(WITH|WITHOUT)\s+HOLD\b/ },
  299. { begin: /\bWITH\s+(CASCADED|LOCAL)\s+CHECK\s+OPTION\b/ },
  300. { begin: /\bEXCLUDE\s+(TIES|NO\s+OTHERS)\b/ },
  301. { begin: /\bFORMAT\s+(TEXT|XML|JSON|YAML)\b/ },
  302. { begin: /\bSET\s+((SESSION|LOCAL)\s+)?NAMES\b/ },
  303. { begin: /\bIS\s+(NOT\s+)?UNKNOWN\b/ },
  304. { begin: /\bSECURITY\s+LABEL\b/ },
  305. { begin: /\bSTANDALONE\s+(YES|NO|NO\s+VALUE)\b/ },
  306. { begin: /\bWITH\s+(NO\s+)?DATA\b/ },
  307. { begin: /\b(FOREIGN|SET)\s+DATA\b/ },
  308. { begin: /\bSET\s+(CATALOG|CONSTRAINTS)\b/ },
  309. { begin: /\b(WITH|FOR)\s+ORDINALITY\b/ },
  310. { begin: /\bIS\s+(NOT\s+)?DOCUMENT\b/ },
  311. { begin: /\bXML\s+OPTION\s+(DOCUMENT|CONTENT)\b/ },
  312. { begin: /\b(STRIP|PRESERVE)\s+WHITESPACE\b/ },
  313. { begin: /\bNO\s+(ACTION|MAXVALUE|MINVALUE)\b/ },
  314. { begin: /\bPARTITION\s+BY\s+(RANGE|LIST|HASH)\b/ },
  315. { begin: /\bAT\s+TIME\s+ZONE\b/ },
  316. { begin: /\bGRANTED\s+BY\b/ },
  317. { begin: /\bRETURN\s+(QUERY|NEXT)\b/ },
  318. { begin: /\b(ATTACH|DETACH)\s+PARTITION\b/ },
  319. { begin: /\bFORCE\s+ROW\s+LEVEL\s+SECURITY\b/ },
  320. { begin: /\b(INCLUDING|EXCLUDING)\s+(COMMENTS|CONSTRAINTS|DEFAULTS|IDENTITY|INDEXES|STATISTICS|STORAGE|ALL)\b/ },
  321. { begin: /\bAS\s+(ASSIGNMENT|IMPLICIT|PERMISSIVE|RESTRICTIVE|ENUM|RANGE)\b/ }
  322. ]
  323. },
  324. // functions named as keywords, followed by '('
  325. {
  326. begin: /\b(FORMAT|FAMILY|VERSION)\s*\(/,
  327. //keywords: { built_in: 'FORMAT FAMILY VERSION' }
  328. },
  329. // INCLUDE ( ... ) in index_parameters in CREATE TABLE
  330. {
  331. begin: /\bINCLUDE\s*\(/,
  332. keywords: 'INCLUDE'
  333. },
  334. // not highlight RANGE if not in frame_clause (not 100% correct, but seems satisfactory)
  335. {
  336. begin: /\bRANGE(?!\s*(BETWEEN|UNBOUNDED|CURRENT|[-0-9]+))/
  337. },
  338. // disable highlighting in commands CREATE AGGREGATE/COLLATION/DATABASE/OPERTOR/TEXT SEARCH .../TYPE
  339. // and in PL/pgSQL RAISE ... USING
  340. {
  341. begin: /\b(VERSION|OWNER|TEMPLATE|TABLESPACE|CONNECTION\s+LIMIT|PROCEDURE|RESTRICT|JOIN|PARSER|COPY|START|END|COLLATION|INPUT|ANALYZE|STORAGE|LIKE|DEFAULT|DELIMITER|ENCODING|COLUMN|CONSTRAINT|TABLE|SCHEMA)\s*=/
  342. },
  343. // PG_smth; HAS_some_PRIVILEGE
  344. {
  345. //className: 'built_in',
  346. begin: /\b(PG_\w+?|HAS_[A-Z_]+_PRIVILEGE)\b/,
  347. relevance: 10
  348. },
  349. // extract
  350. {
  351. begin: /\bEXTRACT\s*\(/,
  352. end: /\bFROM\b/,
  353. returnEnd: true,
  354. keywords: {
  355. //built_in: 'EXTRACT',
  356. type: 'CENTURY DAY DECADE DOW DOY EPOCH HOUR ISODOW ISOYEAR MICROSECONDS ' +
  357. 'MILLENNIUM MILLISECONDS MINUTE MONTH QUARTER SECOND TIMEZONE TIMEZONE_HOUR ' +
  358. 'TIMEZONE_MINUTE WEEK YEAR'
  359. }
  360. },
  361. // xmlelement, xmlpi - special NAME
  362. {
  363. begin: /\b(XMLELEMENT|XMLPI)\s*\(\s*NAME/,
  364. keywords: {
  365. //built_in: 'XMLELEMENT XMLPI',
  366. keyword: 'NAME'
  367. }
  368. },
  369. // xmlparse, xmlserialize
  370. {
  371. begin: /\b(XMLPARSE|XMLSERIALIZE)\s*\(\s*(DOCUMENT|CONTENT)/,
  372. keywords: {
  373. //built_in: 'XMLPARSE XMLSERIALIZE',
  374. keyword: 'DOCUMENT CONTENT'
  375. }
  376. },
  377. // Sequences. We actually skip everything between CACHE|INCREMENT|MAXVALUE|MINVALUE and
  378. // nearest following numeric constant. Without with trick we find a lot of "keywords"
  379. // in 'avrasm' autodetection test...
  380. {
  381. beginKeywords: 'CACHE INCREMENT MAXVALUE MINVALUE',
  382. end: hljs.C_NUMBER_RE,
  383. returnEnd: true,
  384. keywords: 'BY CACHE INCREMENT MAXVALUE MINVALUE'
  385. },
  386. // WITH|WITHOUT TIME ZONE as part of datatype
  387. {
  388. className: 'type',
  389. begin: /\b(WITH|WITHOUT)\s+TIME\s+ZONE\b/
  390. },
  391. // INTERVAL optional fields
  392. {
  393. className: 'type',
  394. begin: /\bINTERVAL\s+(YEAR|MONTH|DAY|HOUR|MINUTE|SECOND)(\s+TO\s+(MONTH|HOUR|MINUTE|SECOND))?\b/
  395. },
  396. // Pseudo-types which allowed only as return type
  397. {
  398. begin: /\bRETURNS\s+(LANGUAGE_HANDLER|TRIGGER|EVENT_TRIGGER|FDW_HANDLER|INDEX_AM_HANDLER|TSM_HANDLER)\b/,
  399. keywords: {
  400. keyword: 'RETURNS',
  401. type: 'LANGUAGE_HANDLER TRIGGER EVENT_TRIGGER FDW_HANDLER INDEX_AM_HANDLER TSM_HANDLER'
  402. }
  403. },
  404. // Known functions - only when followed by '('
  405. {
  406. begin: '\\b(' + FUNCTIONS_RE + ')\\s*\\('
  407. //keywords: { built_in: FUNCTIONS }
  408. },
  409. // Types
  410. {
  411. begin: '\\.(' + TYPES_RE + ')\\b' // prevent highlight as type, say, 'oid' in 'pgclass.oid'
  412. },
  413. {
  414. begin: '\\b(' + TYPES_RE + ')\\s+PATH\\b', // in XMLTABLE
  415. keywords: {
  416. keyword: 'PATH', // hopefully no one would use PATH type in XMLTABLE...
  417. type: TYPES.replace('PATH ','')
  418. }
  419. },
  420. {
  421. className: 'type',
  422. begin: '\\b(' + TYPES_RE + ')\\b'
  423. },
  424. // Strings, see https://www.postgresql.org/docs/11/static/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS
  425. {
  426. className: 'string',
  427. begin: '\'', end: '\'',
  428. contains: [{begin: '\'\''}]
  429. },
  430. {
  431. className: 'string',
  432. begin: '(e|E|u&|U&)\'', end: '\'',
  433. contains: [{begin: '\\\\.'}],
  434. relevance: 10
  435. },
  436. {
  437. begin: DOLLAR_STRING,
  438. endSameAsBegin: true,
  439. contains: [
  440. {
  441. // actually we want them all except SQL; listed are those with known implementations
  442. // and XML + JSON just in case
  443. subLanguage: ['pgsql','perl','python','tcl','r','lua','java','php','ruby','bash','scheme','xml','json'],
  444. endsWithParent: true
  445. }
  446. ]
  447. },
  448. // identifiers in quotes
  449. {
  450. begin: '"', end: '"',
  451. contains: [{begin: '""'}]
  452. },
  453. // numbers
  454. hljs.C_NUMBER_MODE,
  455. // comments
  456. hljs.C_BLOCK_COMMENT_MODE,
  457. COMMENT_MODE,
  458. // PL/pgSQL staff
  459. // %ROWTYPE, %TYPE, $n
  460. {
  461. className: 'meta',
  462. variants: [
  463. {begin: '%(ROW)?TYPE', relevance: 10}, // %TYPE, %ROWTYPE
  464. {begin: '\\$\\d+'}, // $n
  465. {begin: '^#\\w', end: '$'} // #compiler option
  466. ]
  467. },
  468. // <<labeles>>
  469. {
  470. className: 'symbol',
  471. begin: LABEL,
  472. relevance: 10
  473. }
  474. ]
  475. };
  476. };