From 3e6aafd236ffe1ea76c252e1b264a9fb7b15a8e7 Mon Sep 17 00:00:00 2001 From: Aditya Toshniwal Date: Mon, 9 Dec 2019 19:25:42 +0530 Subject: [PATCH] [sql mode] Support backslash and escape constants in pgsql Add support for escape constant as mentioned here: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS-ESCAPE --- mode/sql/sql.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/mode/sql/sql.js b/mode/sql/sql.js index 35902bbab1..6f3e328593 100644 --- a/mode/sql/sql.js +++ b/mode/sql/sql.js @@ -65,6 +65,12 @@ CodeMirror.defineMode("sql", function(config, parserConfig) { // charset casting: _utf8'str', N'str', n'str' // ref: http://dev.mysql.com/doc/refman/5.5/en/string-literals.html return "keyword"; + } else if (support.escapeConstant && (ch == "e" || ch == "E") + && (stream.peek() == "'" || (stream.peek() == '"' && support.doubleQuote))) { + // escape constant: E'str', e'str' + // ref: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS-ESCAPE + state.tokenize = tokenLiteral(stream.peek(), true); + return "keyword"; } else if (support.commentSlashSlash && ch == "/" && stream.eat("/")) { // 1-line comment stream.skipToEnd(); @@ -122,15 +128,19 @@ CodeMirror.defineMode("sql", function(config, parserConfig) { } // 'string', with char specified in quote escaped by '\' - function tokenLiteral(quote) { + function tokenLiteral(quote, withEscapeConst) { return function(stream, state) { var escaped = false, ch; + /* eat the first quote in case of escape constant */ + if(withEscapeConst) { + ch = stream.eat(quote) + } while ((ch = stream.next()) != null) { if (ch == quote && !escaped) { state.tokenize = tokenBase; break; } - escaped = backslashStringEscapes && !escaped && ch == "\\"; + escaped = (backslashStringEscapes || withEscapeConst) && !escaped && ch == "\\"; } return "string"; }; @@ -413,8 +423,9 @@ CodeMirror.defineMode("sql", function(config, parserConfig) { builtin: set("bigint int8 bigserial serial8 bit varying varbit boolean bool box bytea character char varchar cidr circle date double precision float8 inet integer int int4 interval json jsonb line lseg macaddr macaddr8 money numeric decimal path pg_lsn point polygon real float4 smallint int2 smallserial serial2 serial serial4 text time without zone with timetz timestamp timestamptz tsquery tsvector txid_snapshot uuid xml"), atoms: set("false true null unknown"), operatorChars: /^[*\/+\-%<>!=&|^\/#@?~]/, + backslashStringEscapes: false, dateSQL: set("date time timestamp"), - support: set("ODBCdotTable decimallessFloat zerolessFloat binaryNumber hexNumber nCharCast charsetCast") + support: set("ODBCdotTable decimallessFloat zerolessFloat binaryNumber hexNumber nCharCast charsetCast escapeConstant") }); // Google's SQL-like query language, GQL