Adding functions in SQL mode doesn't work

I use the latest code mirror v4.7. In mode/sql/sql.js I wanted to add some SQL functions to be highlighted for example: datediff which is a MySQL SQL function. I changed (highlighted in bold) the MIME definition of x-mysql:

CodeMirror.defineMIME(“text/x-mysql”, {
name: “sql”,
client: set(“charset clear connect edit ego exit go help nopager notee nowarning pager print prompt quit rehash source status system tee”),
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 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 diagnostics 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 get 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 install 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 soname 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 uninstall 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”),
functions: set(“datediff”),
builtin: set(“bool boolean bit blob decimal double 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”),
atoms: set(“false true null unknown”),
operatorChars: /^[*+-%<>!=&|^]/,
dateSQL: set(“date time timestamp”),
support: set(“ODBCdotTable decimallessFloat zerolessFloat binaryNumber hexNumber doubleQuote nCharCast charsetCast commentHash commentSpaceRequired”),
hooks: {
“@”: hookVar,
“`”: hookIdentifier,
“\”: hookClient
}
});

It doesn’t gets highlighted when I use this query:

-- Test SQL Query
SELECT datediff(leverdatum,besteldatum) 
FROM bestelling 
WHERE bedrag>400

The rest of the SQL code does gets highlighted!
I tested on Win7 x64 in FF33 and IE11.

(Sorry for the layout of the first code snippet: it doesn’t seem to work when using the preformatted text button)

The functions property was mentioned in a comment, but not supported in the code. I’ve removed the misleading comment.

Ok thanks for the reply. That explains why :smile:
How do I extend the x-mysql with SQL functions like datediff etc… What is the best way to do this? Just adding it to keywords? But I like to keep my changes/additions seperate so I can easily update codemirror. What are best practices?

Adding them to keywords or builtin is probably a decent solution. You can extract the defineMIME call that defines the mysql mode from sql.js (though you’ll also have to copy a few small utilities that it uses, like sqlKeywords and set), and call it from your own code, after the SQL mode has loaded, to override the default mysql definition.

Found the CodeMirror.extendMode function to do this.