blob: 49b53313c90d805a68a24de43d3034447510bf16 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim syntax file
2" Language: mysql
3" Maintainer: Kenneth J. Pronovici <pronovic@ieee.org>
Bram Moolenaar071d4272004-06-13 20:20:40 +00004" Filenames: *.mysql
Yinzuo Jiang6e375752024-07-21 09:27:27 +02005" URL: ftp://cedar-solutions.com/software/mysql.vim (https://github.com/pronovic/vim-syntax/blob/master/mysql.vim)
6" Note: The definitions below are taken from the mysql user manual as of April 2002, for version 3.23 and have been updated
7" in July 2024 with the docs for version 8.4
8" Last Change: 2016 Apr 11
9" 2024-07-21: update MySQL functions as of MySQL 8.4 (by Vim Project)
10"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020012" quit when a syntax file was already loaded
13if exists("b:current_syntax")
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 finish
15endif
16
17" Always ignore case
18syn case ignore
19
20" General keywords which don't fall into other categories
Bram Moolenaar03413f42016-04-12 21:07:15 +020021syn keyword mysqlKeyword action add after aggregate all alter as asc auto_increment avg_row_length
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022syn keyword mysqlKeyword both by
23syn keyword mysqlKeyword cascade change character check checksum column columns comment constraint create cross
24syn keyword mysqlKeyword current_date current_time current_timestamp
25syn keyword mysqlKeyword data database databases day day_hour day_minute day_second
26syn keyword mysqlKeyword default delayed delay_key_write delete desc describe distinct distinctrow drop
27syn keyword mysqlKeyword enclosed escape escaped explain
28syn keyword mysqlKeyword fields file first flush for foreign from full function
29syn keyword mysqlKeyword global grant grants group
30syn keyword mysqlKeyword having heap high_priority hosts hour hour_minute hour_second
31syn keyword mysqlKeyword identified ignore index infile inner insert insert_id into isam
32syn keyword mysqlKeyword join
Bram Moolenaar03413f42016-04-12 21:07:15 +020033syn keyword mysqlKeyword key keys kill last_insert_id leading left limit lines load local lock logs long
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034syn keyword mysqlKeyword low_priority
35syn keyword mysqlKeyword match max_rows middleint min_rows minute minute_second modify month myisam
36syn keyword mysqlKeyword natural no
37syn keyword mysqlKeyword on optimize option optionally order outer outfile
38syn keyword mysqlKeyword pack_keys partial password primary privileges procedure process processlist
Bram Moolenaar5c736222010-01-06 20:54:52 +010039syn keyword mysqlKeyword read references reload rename replace restrict returns revoke right row rows
Bram Moolenaar8c8de832008-06-24 22:58:06 +000040syn keyword mysqlKeyword second select show shutdown soname sql_big_result sql_big_selects sql_big_tables sql_log_off
41syn keyword mysqlKeyword sql_log_update sql_low_priority_updates sql_select_limit sql_small_result sql_warnings starting
42syn keyword mysqlKeyword status straight_join string
43syn keyword mysqlKeyword table tables temporary terminated to trailing type
44syn keyword mysqlKeyword unique unlock unsigned update usage use using
45syn keyword mysqlKeyword values varbinary variables varying
46syn keyword mysqlKeyword where with write
47syn keyword mysqlKeyword year_month
48syn keyword mysqlKeyword zerofill
Bram Moolenaar071d4272004-06-13 20:20:40 +000049
50" Special values
Bram Moolenaar8c8de832008-06-24 22:58:06 +000051syn keyword mysqlSpecial false null true
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53" Strings (single- and double-quote)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000054syn region mysqlString start=+"+ skip=+\\\\\|\\"+ end=+"+
55syn region mysqlString start=+'+ skip=+\\\\\|\\'+ end=+'+
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
57" Numbers and hexidecimal values
Bram Moolenaar8c8de832008-06-24 22:58:06 +000058syn match mysqlNumber "-\=\<[0-9]*\>"
59syn match mysqlNumber "-\=\<[0-9]*\.[0-9]*\>"
Bram Moolenaar5c736222010-01-06 20:54:52 +010060syn match mysqlNumber "-\=\<[0-9][0-9]*e[+-]\=[0-9]*\>"
Bram Moolenaar8c8de832008-06-24 22:58:06 +000061syn match mysqlNumber "-\=\<[0-9]*\.[0-9]*e[+-]\=[0-9]*\>"
62syn match mysqlNumber "\<0x[abcdefABCDEF0-9]*\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
64" User variables
Bram Moolenaar00a927d2010-05-14 23:24:24 +020065syn match mysqlVariable "@\a*[A-Za-z0-9]*\([._]*[A-Za-z0-9]\)*"
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
Bram Moolenaar03413f42016-04-12 21:07:15 +020067" Escaped column names
68syn match mysqlEscaped "`[^`]*`"
69
Bram Moolenaar071d4272004-06-13 20:20:40 +000070" Comments (c-style, mysql-style and modified sql-style)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000071syn region mysqlComment start="/\*" end="\*/"
72syn match mysqlComment "#.*"
73syn match mysqlComment "--\_s.*"
Bram Moolenaar071d4272004-06-13 20:20:40 +000074syn sync ccomment mysqlComment
75
76" Column types
77"
78" This gets a bit ugly. There are two different problems we have to
79" deal with.
80"
Bram Moolenaar00a927d2010-05-14 23:24:24 +020081" The first problem is that some keywords like 'float' can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +000082" both with and without specifiers, i.e. 'float', 'float(1)' and
83" 'float(@var)' are all valid. We have to account for this and we
84" also have to make sure that garbage like floatn or float_(1) is not
85" highlighted.
86"
87" The second problem is that some of these keywords are included in
88" function names. For instance, year() is part of the name of the
89" dayofyear() function, and the dec keyword (no parenthesis) is part of
Bram Moolenaar03413f42016-04-12 21:07:15 +020090" the name of the decode() function.
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
Bram Moolenaar03413f42016-04-12 21:07:15 +020092syn keyword mysqlType tinyint smallint mediumint int integer bigint
93syn keyword mysqlType date datetime time bit bool
Bram Moolenaar8c8de832008-06-24 22:58:06 +000094syn keyword mysqlType tinytext mediumtext longtext text
95syn keyword mysqlType tinyblob mediumblob longblob blob
Bram Moolenaar03413f42016-04-12 21:07:15 +020096syn region mysqlType start="float\W" end="."me=s-1
97syn region mysqlType start="float$" end="."me=s-1
Yinzuo Jiang6e375752024-07-21 09:27:27 +020098syn region mysqlType start="\<float(" end=")" contains=mysqlNumber,mysqlVariable
Bram Moolenaar8c8de832008-06-24 22:58:06 +000099syn region mysqlType start="double\W" end="."me=s-1
100syn region mysqlType start="double$" end="."me=s-1
Yinzuo Jiang6e375752024-07-21 09:27:27 +0200101syn region mysqlType start="\<double(" end=")" contains=mysqlNumber,mysqlVariable
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000102syn region mysqlType start="double precision\W" end="."me=s-1
103syn region mysqlType start="double precision$" end="."me=s-1
104syn region mysqlType start="double precision(" end=")" contains=mysqlNumber,mysqlVariable
105syn region mysqlType start="real\W" end="."me=s-1
106syn region mysqlType start="real$" end="."me=s-1
Yinzuo Jiang6e375752024-07-21 09:27:27 +0200107syn region mysqlType start="\<real(" end=")" contains=mysqlNumber,mysqlVariable
108syn region mysqlType start="\<numeric(" end=")" contains=mysqlNumber,mysqlVariable
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000109syn region mysqlType start="dec\W" end="."me=s-1
110syn region mysqlType start="dec$" end="."me=s-1
Yinzuo Jiang6e375752024-07-21 09:27:27 +0200111syn region mysqlType start="\<dec(" end=")" contains=mysqlNumber,mysqlVariable
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000112syn region mysqlType start="decimal\W" end="."me=s-1
113syn region mysqlType start="decimal$" end="."me=s-1
Yinzuo Jiang6e375752024-07-21 09:27:27 +0200114syn region mysqlType start="\<decimal(" end=")" contains=mysqlNumber,mysqlVariable
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115syn region mysqlType start="\Wtimestamp\W" end="."me=s-1
116syn region mysqlType start="\Wtimestamp$" end="."me=s-1
117syn region mysqlType start="\Wtimestamp(" end=")" contains=mysqlNumber,mysqlVariable
118syn region mysqlType start="^timestamp\W" end="."me=s-1
119syn region mysqlType start="^timestamp$" end="."me=s-1
120syn region mysqlType start="^timestamp(" end=")" contains=mysqlNumber,mysqlVariable
121syn region mysqlType start="\Wyear(" end=")" contains=mysqlNumber,mysqlVariable
122syn region mysqlType start="^year(" end=")" contains=mysqlNumber,mysqlVariable
Yinzuo Jiang6e375752024-07-21 09:27:27 +0200123syn region mysqlType start="\<char(" end=")" contains=mysqlNumber,mysqlVariable
124syn region mysqlType start="\<varchar(" end=")" contains=mysqlNumber,mysqlVariable
125syn region mysqlType start="\<enum(" end=")" contains=mysqlString,mysqlVariable
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000126syn region mysqlType start="\Wset(" end=")" contains=mysqlString,mysqlVariable
127syn region mysqlType start="^set(" end=")" contains=mysqlString,mysqlVariable
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129" Logical, string and numeric operators
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000130syn keyword mysqlOperator between not and or is in like regexp rlike binary exists
Yinzuo Jiang6e375752024-07-21 09:27:27 +0200131syn region mysqlOperatorFunction start="\<isnull(" end=")" contains=ALL
132syn region mysqlOperatorFunction start="\<coalesce(" end=")" contains=ALL
133syn region mysqlOperatorFunction start="\<interval(" end=")" contains=ALL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Yinzuo Jiang6e375752024-07-21 09:27:27 +0200135" Flow control functions
136" https://docs.oracle.com/cd/E17952_01/mysql-8.4-en/flow-control-functions.html
137syn keyword mysqlFlowLabel case when then else end
138syn region mysqlFlowFunction start="\<ifnull(" end=")" contains=ALL
139syn region mysqlFlowFunction start="\<nullif(" end=")" contains=ALL
140syn region mysqlFlowFunction start="\<if(" end=")" contains=ALL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
Yinzuo Jiang6e375752024-07-21 09:27:27 +0200142" Window functions
143" https://docs.oracle.com/cd/E17952_01/mysql-8.4-en/window-functions-usage.html
144syn keyword mysqlWindowKeyword over partition window
145" https://docs.oracle.com/cd/E17952_01/mysql-8.4-en/window-function-descriptions.html
146syn region mysqlWindowFunction start="\<cume_dist(" end=")" contains=ALL
147syn region mysqlWindowFunction start="\<dense_rank(" end=")" contains=ALL
148syn region mysqlWindowFunction start="\<first_value(" end=")" contains=ALL
149syn region mysqlWindowFunction start="\<lag(" end=")" contains=ALL
150syn region mysqlWindowFunction start="\<last_value(" end=")" contains=ALL
151syn region mysqlWindowFunction start="\<lead(" end=")" contains=ALL
152syn region mysqlWindowFunction start="\<nth_value(" end=")" contains=ALL
153syn region mysqlWindowFunction start="\<ntile(" end=")" contains=ALL
154syn region mysqlWindowFunction start="\<percent_rank(" end=")" contains=ALL
155syn region mysqlWindowFunction start="\<rank(" end=")" contains=ALL
156syn region mysqlWindowFunction start="\<row_number(" end=")" contains=ALL
157
158" General functions
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159"
160" I'm leery of just defining keywords for functions, since according to the MySQL manual:
161"
Bram Moolenaar03413f42016-04-12 21:07:15 +0200162" Function names do not clash with table or column names. For example, ABS is a
163" valid column name. The only restriction is that for a function call, no spaces
164" are allowed between the function name and the `(' that follows it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165"
Bram Moolenaar03413f42016-04-12 21:07:15 +0200166" This means that if I want to highlight function names properly, I have to use a
167" region to define them, not just a keyword. This will probably cause the syntax file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168" to load more slowly, but at least it will be 'correct'.
169
Yinzuo Jiang6e375752024-07-21 09:27:27 +0200170syn region mysqlFunction start="\<abs(" end=")" contains=ALL
171syn region mysqlFunction start="\<acos(" end=")" contains=ALL
172syn region mysqlFunction start="\<adddate(" end=")" contains=ALL
173syn region mysqlFunction start="\<ascii(" end=")" contains=ALL
174syn region mysqlFunction start="\<asin(" end=")" contains=ALL
175syn region mysqlFunction start="\<atan(" end=")" contains=ALL
176syn region mysqlFunction start="\<atan2(" end=")" contains=ALL
177syn region mysqlFunction start="\<avg(" end=")" contains=ALL
178syn region mysqlFunction start="\<benchmark(" end=")" contains=ALL
179syn region mysqlFunction start="\<bin(" end=")" contains=ALL
180syn region mysqlFunction start="\<bit_and(" end=")" contains=ALL
181syn region mysqlFunction start="\<bit_count(" end=")" contains=ALL
182syn region mysqlFunction start="\<bit_or(" end=")" contains=ALL
183syn region mysqlFunction start="\<ceiling(" end=")" contains=ALL
184syn region mysqlFunction start="\<character_length(" end=")" contains=ALL
185syn region mysqlFunction start="\<char_length(" end=")" contains=ALL
186syn region mysqlFunction start="\<concat(" end=")" contains=ALL
187syn region mysqlFunction start="\<concat_ws(" end=")" contains=ALL
188syn region mysqlFunction start="\<connection_id(" end=")" contains=ALL
189syn region mysqlFunction start="\<conv(" end=")" contains=ALL
190syn region mysqlFunction start="\<cos(" end=")" contains=ALL
191syn region mysqlFunction start="\<cot(" end=")" contains=ALL
192syn region mysqlFunction start="\<count(" end=")" contains=ALL
193syn region mysqlFunction start="\<curdate(" end=")" contains=ALL
194syn region mysqlFunction start="\<curtime(" end=")" contains=ALL
195syn region mysqlFunction start="\<date_add(" end=")" contains=ALL
196syn region mysqlFunction start="\<date_format(" end=")" contains=ALL
197syn region mysqlFunction start="\<date_sub(" end=")" contains=ALL
198syn region mysqlFunction start="\<dayname(" end=")" contains=ALL
199syn region mysqlFunction start="\<dayofmonth(" end=")" contains=ALL
200syn region mysqlFunction start="\<dayofweek(" end=")" contains=ALL
201syn region mysqlFunction start="\<dayofyear(" end=")" contains=ALL
202syn region mysqlFunction start="\<decode(" end=")" contains=ALL
203syn region mysqlFunction start="\<degrees(" end=")" contains=ALL
204syn region mysqlFunction start="\<elt(" end=")" contains=ALL
205syn region mysqlFunction start="\<encode(" end=")" contains=ALL
206syn region mysqlFunction start="\<encrypt(" end=")" contains=ALL
207syn region mysqlFunction start="\<exp(" end=")" contains=ALL
208syn region mysqlFunction start="\<export_set(" end=")" contains=ALL
209syn region mysqlFunction start="\<extract(" end=")" contains=ALL
210syn region mysqlFunction start="\<field(" end=")" contains=ALL
211syn region mysqlFunction start="\<find_in_set(" end=")" contains=ALL
212syn region mysqlFunction start="\<floor(" end=")" contains=ALL
213syn region mysqlFunction start="\<format(" end=")" contains=ALL
214syn region mysqlFunction start="\<from_days(" end=")" contains=ALL
215syn region mysqlFunction start="\<from_unixtime(" end=")" contains=ALL
216syn region mysqlFunction start="\<get_lock(" end=")" contains=ALL
217syn region mysqlFunction start="\<greatest(" end=")" contains=ALL
218syn region mysqlFunction start="\<group_unique_users(" end=")" contains=ALL
219syn region mysqlFunction start="\<hex(" end=")" contains=ALL
220syn region mysqlFunction start="\<inet_aton(" end=")" contains=ALL
221syn region mysqlFunction start="\<inet_ntoa(" end=")" contains=ALL
222syn region mysqlFunction start="\<instr(" end=")" contains=ALL
223syn region mysqlFunction start="\<lcase(" end=")" contains=ALL
224syn region mysqlFunction start="\<least(" end=")" contains=ALL
225syn region mysqlFunction start="\<length(" end=")" contains=ALL
226syn region mysqlFunction start="\<load_file(" end=")" contains=ALL
227syn region mysqlFunction start="\<locate(" end=")" contains=ALL
228syn region mysqlFunction start="\<log(" end=")" contains=ALL
229syn region mysqlFunction start="\<log10(" end=")" contains=ALL
230syn region mysqlFunction start="\<lower(" end=")" contains=ALL
231syn region mysqlFunction start="\<lpad(" end=")" contains=ALL
232syn region mysqlFunction start="\<ltrim(" end=")" contains=ALL
233syn region mysqlFunction start="\<make_set(" end=")" contains=ALL
234syn region mysqlFunction start="\<master_pos_wait(" end=")" contains=ALL
235syn region mysqlFunction start="\<max(" end=")" contains=ALL
236syn region mysqlFunction start="\<md5(" end=")" contains=ALL
237syn region mysqlFunction start="\<mid(" end=")" contains=ALL
238syn region mysqlFunction start="\<min(" end=")" contains=ALL
239syn region mysqlFunction start="\<mod(" end=")" contains=ALL
240syn region mysqlFunction start="\<monthname(" end=")" contains=ALL
241syn region mysqlFunction start="\<now(" end=")" contains=ALL
242syn region mysqlFunction start="\<oct(" end=")" contains=ALL
243syn region mysqlFunction start="\<octet_length(" end=")" contains=ALL
244syn region mysqlFunction start="\<ord(" end=")" contains=ALL
245syn region mysqlFunction start="\<period_add(" end=")" contains=ALL
246syn region mysqlFunction start="\<period_diff(" end=")" contains=ALL
247syn region mysqlFunction start="\<pi(" end=")" contains=ALL
248syn region mysqlFunction start="\<position(" end=")" contains=ALL
249syn region mysqlFunction start="\<pow(" end=")" contains=ALL
250syn region mysqlFunction start="\<power(" end=")" contains=ALL
251syn region mysqlFunction start="\<quarter(" end=")" contains=ALL
252syn region mysqlFunction start="\<radians(" end=")" contains=ALL
253syn region mysqlFunction start="\<rand(" end=")" contains=ALL
254syn region mysqlFunction start="\<release_lock(" end=")" contains=ALL
255syn region mysqlFunction start="\<repeat(" end=")" contains=ALL
256syn region mysqlFunction start="\<reverse(" end=")" contains=ALL
257syn region mysqlFunction start="\<round(" end=")" contains=ALL
258syn region mysqlFunction start="\<rpad(" end=")" contains=ALL
259syn region mysqlFunction start="\<rtrim(" end=")" contains=ALL
260syn region mysqlFunction start="\<sec_to_time(" end=")" contains=ALL
261syn region mysqlFunction start="\<session_user(" end=")" contains=ALL
262syn region mysqlFunction start="\<sign(" end=")" contains=ALL
263syn region mysqlFunction start="\<sin(" end=")" contains=ALL
264syn region mysqlFunction start="\<soundex(" end=")" contains=ALL
265syn region mysqlFunction start="\<space(" end=")" contains=ALL
266syn region mysqlFunction start="\<sqrt(" end=")" contains=ALL
267syn region mysqlFunction start="\<std(" end=")" contains=ALL
268syn region mysqlFunction start="\<stddev(" end=")" contains=ALL
269syn region mysqlFunction start="\<strcmp(" end=")" contains=ALL
270syn region mysqlFunction start="\<subdate(" end=")" contains=ALL
271syn region mysqlFunction start="\<substring(" end=")" contains=ALL
272syn region mysqlFunction start="\<substring_index(" end=")" contains=ALL
273syn region mysqlFunction start="\<subtime(" end=")" contains=ALL
274syn region mysqlFunction start="\<sum(" end=")" contains=ALL
275syn region mysqlFunction start="\<sysdate(" end=")" contains=ALL
276syn region mysqlFunction start="\<system_user(" end=")" contains=ALL
277syn region mysqlFunction start="\<tan(" end=")" contains=ALL
278syn region mysqlFunction start="\<time_format(" end=")" contains=ALL
279syn region mysqlFunction start="\<time_to_sec(" end=")" contains=ALL
280syn region mysqlFunction start="\<to_days(" end=")" contains=ALL
281syn region mysqlFunction start="\<trim(" end=")" contains=ALL
282syn region mysqlFunction start="\<ucase(" end=")" contains=ALL
283syn region mysqlFunction start="\<unique_users(" end=")" contains=ALL
284syn region mysqlFunction start="\<unix_timestamp(" end=")" contains=ALL
285syn region mysqlFunction start="\<upper(" end=")" contains=ALL
286syn region mysqlFunction start="\<user(" end=")" contains=ALL
287syn region mysqlFunction start="\<version(" end=")" contains=ALL
288syn region mysqlFunction start="\<week(" end=")" contains=ALL
289syn region mysqlFunction start="\<weekday(" end=")" contains=ALL
290syn region mysqlFunction start="\<yearweek(" end=")" contains=ALL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291
292" Define the default highlighting.
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200293" Only when an item doesn't have highlighting yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294
Yinzuo Jiang6e375752024-07-21 09:27:27 +0200295hi def link mysqlKeyword Keyword
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200296hi def link mysqlSpecial Special
297hi def link mysqlString String
298hi def link mysqlNumber Number
299hi def link mysqlVariable Identifier
300hi def link mysqlComment Comment
301hi def link mysqlType Type
Yinzuo Jiang6e375752024-07-21 09:27:27 +0200302hi def link mysqlOperator Operator
303hi def link mysqlOperatorFunction Function
304hi def link mysqlFlowFunction Function
305hi def link mysqlFlowLabel Label
306hi def link mysqlWindowFunction Function
307hi def link mysqlWindowKeyword Keyword
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200308hi def link mysqlFunction Function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310
311let b:current_syntax = "mysql"
312