Skip to content

Commit

Permalink
[sql mode] Support backslash and escape constants in pgsql
Browse files Browse the repository at this point in the history
  • Loading branch information
adityatoshniwal authored and marijnh committed Dec 9, 2019
1 parent 3ace5da commit 3e6aafd
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions mode/sql/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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";
};
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e6aafd

Please sign in to comment.