Skip to content

Commit b1b1dd5

Browse files
santecmarijnh
authored andcommitted
Improve Mode SQL
* mysql & mariadb: ** .21 syntax ** .table_name syntax ** added keywords * MIMEs define their operatorChars * MIMEs have a support prop * use spaces as tab in index.html for consistency * some return false -> null * more comments
1 parent 1960b0d commit b1b1dd5

File tree

2 files changed

+85
-57
lines changed

2 files changed

+85
-57
lines changed

mode/sql/index.html

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
11
<!doctype html>
22
<html>
3-
<head>
4-
<meta charset="utf-8">
5-
<title>SQL Mode for CodeMirror</title>
6-
<link rel="stylesheet" href="../../lib/codemirror.css" />
7-
<script src="../../lib/codemirror.js"></script>
8-
<script src="sql.js"></script>
9-
<style>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>SQL Mode for CodeMirror</title>
6+
<link rel="stylesheet" href="../../lib/codemirror.css" />
7+
<script src="../../lib/codemirror.js"></script>
8+
<script src="../../lib/codemirror.js"></script>
9+
<script src="../../lib/util/continuelist.js"></script>
10+
<script src="sql.js"></script>
11+
<style>
1012
.CodeMirror {
11-
border-top: 1px solid black;
12-
border-bottom: 1px solid black;
13+
border-top: 1px solid black;
14+
border-bottom: 1px solid black;
1315
}
14-
</style>
15-
<link rel="stylesheet" href="../../doc/docs.css">
16-
<script>
16+
</style>
17+
<link rel="stylesheet" href="../../doc/docs.css">
18+
<script>
1719
var init = function() {
18-
var mime = 'text/x-mariadb';
19-
20-
// get mime type
21-
if (window.location.href.indexOf('mime=') > -1) {
22-
mime = window.location.href.substr(window.location.href.indexOf('mime=') + 5);
23-
}
24-
25-
window.editor = CodeMirror.fromTextArea(document.getElementById('code'), {
26-
mode: mime,
27-
indentWithTabs: true,
28-
smartIndent: true,
29-
lineNumbers: true,
30-
matchBrackets : true,
31-
autofocus: true
32-
});
20+
var mime = 'text/x-mariadb';
21+
22+
// get mime type
23+
if (window.location.href.indexOf('mime=') > -1) {
24+
mime = window.location.href.substr(window.location.href.indexOf('mime=') + 5);
25+
}
26+
27+
window.editor = CodeMirror.fromTextArea(document.getElementById('code'), {
28+
mode: mime,
29+
indentWithTabs: true,
30+
smartIndent: true,
31+
lineNumbers: true,
32+
matchBrackets : true,
33+
autofocus: true
34+
});
3335
};
34-
</script>
35-
</head>
36-
<body onload="init();">
37-
<h1>SQL Mode for CodeMirror</h1>
38-
<form>
39-
<textarea id="code" name="code">-- SQL Mode for CodeMirror
36+
</script>
37+
</head>
38+
<body onload="init();">
39+
<h1>SQL Mode for CodeMirror</h1>
40+
<form>
41+
<textarea id="code" name="code">-- SQL Mode for CodeMirror
4042
SELECT SQL_NO_CACHE DISTINCT
4143
@var1 AS `val1`, @'val2', @global.'sql_mode',
42-
1.1 AS `float_val`, 0.09e3 AS `int_with_esp`,
44+
1.1 AS `float_val`, .14 AS `another_float`, 0.09e3 AS `int_with_esp`,
4345
0xFA5 AS `hex`, x'fa5' AS `hex2`, 0b101 AS `bin`, b'101' AS `bin2`,
44-
DATE '1994-01-01' AS `sql_date`, { T "1994-01-01" } AS `odbc_date`,
46+
DATE '1994-01-01' AS `sql_date`, { T "1994-01-01" } AS `odbc_date`,
4547
'myString', UNKNOWN
4648
FROM DUAL
4749
-- space needed after '--'
@@ -50,12 +52,17 @@ <h1>SQL Mode for CodeMirror</h1>
5052
comment! */
5153
LIMIT 1 OFFSET 0;
5254
</textarea>
53-
</form>
54-
<p><strong>MIME types defined:</strong>
55-
<code><a href="?mime=text/x-sql">text/x-sql</a></code>,
56-
<code><a href="?mime=text/x-mysql">text/x-mysql</a></code>,
57-
<code><a href="?mime=text/x-mariadb">text/x-mariadb</a></code>,
58-
<code><a href="?mime=text/x-plsql">text/x-plsql</a></code>.
59-
</p>
60-
</body>
55+
</form>
56+
<p><strong>MIME types defined:</strong>
57+
<code><a href="?mime=text/x-sql">text/x-sql</a></code>,
58+
<code><a href="?mime=text/x-mysql">text/x-mysql</a></code>,
59+
<code><a href="?mime=text/x-mariadb">text/x-mariadb</a></code>,
60+
<code><a href="?mime=text/x-plsql">text/x-plsql</a></code>.
61+
</p>
62+
<p>
63+
<strong>Tests:</strong>
64+
<a href="../../test/index.html#sql_*">normal</a>,
65+
<a href="../../test/index.html#verbose,sql_*">verbose</a>.
66+
</p>
67+
</body>
6168
</html>

mode/sql/sql.js

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
55
atoms = parserConfig.atoms || {"false": true, "true": true, "null": true},
66
builtin = parserConfig.builtin || {},
77
keywords = parserConfig.keywords,
8-
operatorChars = /^[*+\-%<>!=&|~^]/,
8+
operatorChars = parserConfig.operatorChars || /^[*+\-%<>!=&|~^]/,
9+
support = parserConfig.support || {},
910
hooks = parserConfig.hooks || {},
1011
dateSQL = parserConfig.dateSQL || {"date" : true, "time" : true, "timestamp" : true};
11-
12+
1213
function tokenBase(stream, state) {
1314
var ch = stream.next();
1415

16+
// call hooks from the mime type
1517
if (hooks[ch]) {
1618
var result = hooks[ch](stream, state);
1719
if (result !== false) return result;
@@ -36,7 +38,8 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
3638
// strings
3739
state.tokenize = tokenLiteral(ch);
3840
return state.tokenize(stream, state);
39-
} else if (/^[\(\),\.;\[\]]/.test(ch)) {
41+
} else if (/^[\(\),\;\[\]]/.test(ch)) {
42+
// no highlightning
4043
return null;
4144
} else if (ch == "#" || (ch == "-" && stream.eat("-") && stream.eat(" "))) {
4245
// 1-line comments
@@ -46,9 +49,19 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
4649
// multi-line comments
4750
state.tokenize = tokenComment;
4851
return state.tokenize(stream, state);
52+
} else if (ch == ".") {
53+
// .1 for 0.1
54+
if (stream.match(/^[0-9eE]+/) && support.zerolessFloat == true) {
55+
return "number";
56+
}
57+
// .table_name (ODBC)
58+
if (stream.match(/^[a-zA-Z_]+/) && support.ODBCdotTable == true) {
59+
return "variable-2";
60+
}
4961
} else if (operatorChars.test(ch)) {
62+
// operators
5063
stream.eatWhile(operatorChars);
51-
return false;
64+
return null;
5265
} else if (ch == '{' &&
5366
(stream.match(/^( )*(d|D|t|T|ts|TS)( )*'[^']*'( )*}/) || stream.match(/^( )*(d|D|t|T|ts|TS)( )*"[^"]*"( )*}/))) {
5467
// dates (weird ODBC syntax)
@@ -63,10 +76,11 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
6376
if (builtin.hasOwnProperty(word)) return "builtin";
6477
if (keywords.hasOwnProperty(word)) return "keyword";
6578
if (client.hasOwnProperty(word)) return "string-2";
66-
return "variable";
79+
return null;
6780
}
6881
}
6982

83+
// 'string', with char specified in quote escaped by '\'
7084
function tokenLiteral(quote) {
7185
return function(stream, state) {
7286
var escaped = false, ch;
@@ -149,15 +163,15 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
149163

150164
(function() {
151165
"use strict";
152-
166+
167+
// `identifier`
153168
function hookIdentifier(stream) {
154169
var escaped = false, ch;
155-
156170
while ((ch = stream.next()) != null) {
157171
if (ch == "`" && !escaped) return "variable-2";
158172
escaped = !escaped && ch == "`";
159173
}
160-
return false;
174+
return null;
161175
}
162176

163177
// variable token
@@ -182,13 +196,13 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
182196
} else if (stream.match(/^[0-9a-zA-Z$\.\_]+/)) {
183197
return "variable-2";
184198
}
185-
return false;
199+
return null;
186200
};
187201

188202
// short client keyword token
189203
function hookClient(stream) {
190204
// \g, etc
191-
return stream.match(/^[a-zA-Z]\b/) ? "variable-2" : false;
205+
return stream.match(/^[a-zA-Z]\b/) ? "variable-2" : null;
192206
}
193207

194208
var sqlKeywords = "alter and as asc between by count create delete desc distinct drop from having in insert into is join like not on or order select set table union update values where ";
@@ -204,16 +218,20 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
204218
keywords: set(sqlKeywords + "begin"),
205219
builtin: set("bool boolean bit blob enum long longblob longtext medium mediumblob mediumint mediumtext time timestamp tinyblob tinyint tinytext text bigint int int1 int2 int3 int4 int8 integer float float4 float8 double char varbinary varchar varcharacter precision real date datetime year unsigned signed decimal numeric"),
206220
atoms: set("false true null unknown"),
207-
dateSQL: set("date time timestamp")
221+
operatorChars: /^[*+\-%<>!=]/,
222+
dateSQL: set("date time timestamp"),
223+
support: set("ODBCdotTable")
208224
});
209225

210226
CodeMirror.defineMIME("text/x-mysql", {
211227
name: "sql",
212228
client: set("charset clear connect edit ego exit go help nopager notee nowarning pager print prompt quit rehash source status system tee"),
213229
keywords: set(sqlKeywords + "accessible action add after algorithm all analyze asensitive at authors auto_increment autocommit avg avg_row_length before binary binlog both btree cache call cascade cascaded case catalog_name chain change changed character check checkpoint checksum class_origin client_statistics close coalesce code collate collation collations column columns comment commit committed completion concurrent condition connection consistent constraint contains continue contributors convert cross current_date current_time current_timestamp current_user cursor data database databases day_hour day_microsecond day_minute day_second deallocate dec declare default delay_key_write delayed delimiter des_key_file describe deterministic dev_pop dev_samp deviance directory disable discard distinctrow div dual dumpfile each elseif enable enclosed end ends engine engines enum errors escape escaped even event events every execute exists exit explain extended fast fetch field fields first flush for force foreign found_rows full fulltext function general global grant grants group groupby_concat handler hash help high_priority hosts hour_microsecond hour_minute hour_second if ignore ignore_server_ids import index index_statistics infile inner innodb inout insensitive insert_method interval invoker isolation iterate key keys kill language last leading leave left level limit linear lines list load local localtime localtimestamp lock logs low_priority master master_heartbeat_period master_ssl_verify_server_cert masters match max max_rows maxvalue message_text middleint migrate min min_rows minute_microsecond minute_second mod mode modifies modify mutex mysql_errno natural next no no_write_to_binlog offline offset one online open optimize option optionally out outer outfile pack_keys parser partition partitions password phase plugin plugins prepare preserve prev primary privileges procedure processlist profile profiles purge query quick range read read_write reads real rebuild recover references regexp relaylog release remove rename reorganize repair repeatable replace require resignal restrict resume return returns revoke right rlike rollback rollup row row_format rtree savepoint schedule schema schema_name schemas second_microsecond security sensitive separator serializable server session share show signal slave slow smallint snapshot spatial specific sql sql_big_result sql_buffer_result sql_cache sql_calc_found_rows sql_no_cache sql_small_result sqlexception sqlstate sqlwarning ssl start starting starts status std stddev stddev_pop stddev_samp storage straight_join subclass_origin sum suspend table_name table_statistics tables tablespace temporary terminated to trailing transaction trigger triggers truncate uncommitted undo unique unlock upgrade usage use use_frm user user_resources user_statistics using utc_date utc_time utc_timestamp value variables varying view views warnings when while with work write xa xor year_month zerofill begin do then else loop repeat"),
214-
builtin: set("bool boolean bit blob enum long longblob longtext medium mediumblob mediumint mediumtext time timestamp tinyblob tinyint tinytext text bigint int int1 int2 int3 int4 int8 integer float float4 float8 double char varbinary varchar varcharacter precision date datetime year unsigned signed decimal numeric"),
230+
builtin: set("bool boolean bit blob decimal double enum float long longblob longtext medium mediumblob mediumint mediumtext time timestamp tinyblob tinyint tinytext text bigint int int1 int2 int3 int4 int8 integer float float4 float8 double char varbinary varchar varcharacter precision date datetime year unsigned signed numeric"),
215231
atoms: set("false true null unknown"),
232+
operatorChars: /^[*+\-%<>!=&|^]/,
216233
dateSQL: set("date time timestamp"),
234+
support: set("ODBCdotTable zerolessFloat"),
217235
hooks: {
218236
"@": hookVar,
219237
"`": hookIdentifier,
@@ -225,9 +243,11 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
225243
name: "sql",
226244
client: set("charset clear connect edit ego exit go help nopager notee nowarning pager print prompt quit rehash source status system tee"),
227245
keywords: set(sqlKeywords + "accessible action add after algorithm all always analyze asensitive at authors auto_increment autocommit avg avg_row_length before binary binlog both btree cache call cascade cascaded case catalog_name chain change changed character check checkpoint checksum class_origin client_statistics close coalesce code collate collation collations column columns comment commit committed completion concurrent condition connection consistent constraint contains continue contributors convert cross current_date current_time current_timestamp current_user cursor data database databases day_hour day_microsecond day_minute day_second deallocate dec declare default delay_key_write delayed delimiter des_key_file describe deterministic dev_pop dev_samp deviance directory disable discard distinctrow div dual dumpfile each elseif enable enclosed end ends engine engines enum errors escape escaped even event events every execute exists exit explain extended fast fetch field fields first flush for force foreign found_rows full fulltext function general generated global grant grants group groupby_concat handler hash help high_priority hosts hour_microsecond hour_minute hour_second if ignore ignore_server_ids import index index_statistics infile inner innodb inout insensitive insert_method interval invoker isolation iterate key keys kill language last leading leave left level limit linear lines list load local localtime localtimestamp lock logs low_priority master master_heartbeat_period master_ssl_verify_server_cert masters match max max_rows maxvalue message_text middleint migrate min min_rows minute_microsecond minute_second mod mode modifies modify mutex mysql_errno natural next no no_write_to_binlog offline offset one online open optimize option optionally out outer outfile pack_keys parser partition partitions password persistent phase plugin plugins prepare preserve prev primary privileges procedure processlist profile profiles purge query quick range read read_write reads real rebuild recover references regexp relaylog release remove rename reorganize repair repeatable replace require resignal restrict resume return returns revoke right rlike rollback rollup row row_format rtree savepoint schedule schema schema_name schemas second_microsecond security sensitive separator serializable server session share show signal slave slow smallint snapshot spatial specific sql sql_big_result sql_buffer_result sql_cache sql_calc_found_rows sql_no_cache sql_small_result sqlexception sqlstate sqlwarning ssl start starting starts status std stddev stddev_pop stddev_samp storage straight_join subclass_origin sum suspend table_name table_statistics tables tablespace temporary terminated to trailing transaction trigger triggers truncate uncommitted undo unique unlock upgrade usage use use_frm user user_resources user_statistics using utc_date utc_time utc_timestamp value variables varying view views virtual warnings when while with work write xa xor year_month zerofill begin do then else loop repeat"),
228-
builtin: set("bool boolean bit blob enum long longblob longtext medium mediumblob mediumint mediumtext time timestamp tinyblob tinyint tinytext text bigint int int1 int2 int3 int4 int8 integer float float4 float8 double char varbinary varchar varcharacter precision date datetime year unsigned signed decimal numeric"),
246+
builtin: set("bool boolean bit blob decimal double enum float long longblob longtext medium mediumblob mediumint mediumtext time timestamp tinyblob tinyint tinytext text bigint int int1 int2 int3 int4 int8 integer float float4 float8 double char varbinary varchar varcharacter precision date datetime year unsigned signed numeric"),
229247
atoms: set("false true null unknown"),
248+
operatorChars: /^[*+\-%<>!=&|^]/,
230249
dateSQL: set("date time timestamp"),
250+
support: set("ODBCdotTable zerolessFloat"),
231251
hooks: {
232252
"@": hookVar,
233253
"`": hookIdentifier,
@@ -242,6 +262,7 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
242262
keywords: set("abort accept access add all alter and any array arraylen as asc assert assign at attributes audit authorization avg base_table begin between binary_integer body boolean by case cast char char_base check close cluster clusters colauth column comment commit compress connect connected constant constraint crash create current currval cursor data_base database date dba deallocate debugoff debugon decimal declare default definition delay delete desc digits dispose distinct do drop else elsif enable end entry escape exception exception_init exchange exclusive exists exit external fast fetch file for force form from function generic goto grant group having identified if immediate in increment index indexes indicator initial initrans insert interface intersect into is key level library like limited local lock log logging long loop master maxextents maxtrans member minextents minus mislabel mode modify multiset new next no noaudit nocompress nologging noparallel not nowait number_base object of off offline on online only open option or order out package parallel partition pctfree pctincrease pctused pls_integer positive positiven pragma primary prior private privileges procedure public raise range raw read rebuild record ref references refresh release rename replace resource restrict return returning reverse revoke rollback row rowid rowlabel rownum rows run savepoint schema segment select separate session set share snapshot some space split sql start statement storage subtype successful synonym tabauth table tables tablespace task terminate then to trigger truncate type union unique unlimited unrecoverable unusable update use using validate value values variable view views when whenever where while with work"),
243263
functions: set("abs acos add_months ascii asin atan atan2 average bfilename ceil chartorowid chr concat convert cos cosh count decode deref dual dump dup_val_on_index empty error exp false floor found glb greatest hextoraw initcap instr instrb isopen last_day least lenght lenghtb ln lower lpad ltrim lub make_ref max min mod months_between new_time next_day nextval nls_charset_decl_len nls_charset_id nls_charset_name nls_initcap nls_lower nls_sort nls_upper nlssort no_data_found notfound null nvl others power rawtohex reftohex round rowcount rowidtochar rpad rtrim sign sin sinh soundex sqlcode sqlerrm sqrt stddev substr substrb sum sysdate tan tanh to_char to_date to_label to_multi_byte to_number to_single_byte translate true trunc uid upper user userenv variance vsize"),
244264
builtin: set("bfile blob character clob dec float int integer mlslabel natural naturaln nchar nclob number numeric nvarchar2 real rowtype signtype smallint string varchar varchar2"),
245-
dateSQL: set("date time timestamp"),
265+
operatorChars: /^[*+\-%<>!=~]/,
266+
dateSQL: set("date time timestamp")
246267
});
247268
}());

0 commit comments

Comments
 (0)