Skip to content

Commit 3e6aafd

Browse files
adityatoshniwalmarijnh
authored andcommitted
[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
1 parent 3ace5da commit 3e6aafd

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

mode/sql/sql.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
6565
// charset casting: _utf8'str', N'str', n'str'
6666
// ref: http://dev.mysql.com/doc/refman/5.5/en/string-literals.html
6767
return "keyword";
68+
} else if (support.escapeConstant && (ch == "e" || ch == "E")
69+
&& (stream.peek() == "'" || (stream.peek() == '"' && support.doubleQuote))) {
70+
// escape constant: E'str', e'str'
71+
// ref: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS-ESCAPE
72+
state.tokenize = tokenLiteral(stream.peek(), true);
73+
return "keyword";
6874
} else if (support.commentSlashSlash && ch == "/" && stream.eat("/")) {
6975
// 1-line comment
7076
stream.skipToEnd();
@@ -122,15 +128,19 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
122128
}
123129

124130
// 'string', with char specified in quote escaped by '\'
125-
function tokenLiteral(quote) {
131+
function tokenLiteral(quote, withEscapeConst) {
126132
return function(stream, state) {
127133
var escaped = false, ch;
134+
/* eat the first quote in case of escape constant */
135+
if(withEscapeConst) {
136+
ch = stream.eat(quote)
137+
}
128138
while ((ch = stream.next()) != null) {
129139
if (ch == quote && !escaped) {
130140
state.tokenize = tokenBase;
131141
break;
132142
}
133-
escaped = backslashStringEscapes && !escaped && ch == "\\";
143+
escaped = (backslashStringEscapes || withEscapeConst) && !escaped && ch == "\\";
134144
}
135145
return "string";
136146
};
@@ -413,8 +423,9 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
413423
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"),
414424
atoms: set("false true null unknown"),
415425
operatorChars: /^[*\/+\-%<>!=&|^\/#@?~]/,
426+
backslashStringEscapes: false,
416427
dateSQL: set("date time timestamp"),
417-
support: set("ODBCdotTable decimallessFloat zerolessFloat binaryNumber hexNumber nCharCast charsetCast")
428+
support: set("ODBCdotTable decimallessFloat zerolessFloat binaryNumber hexNumber nCharCast charsetCast escapeConstant")
418429
});
419430

420431
// Google's SQL-like query language, GQL

0 commit comments

Comments
 (0)