blob: 5d07a5c40e0a9adad160791e56e306f79040eab1 [file] [log] [blame]
Bram Moolenaare2f98b92006-03-29 21:18:24 +00001*sql.txt* For Vim version 7.0c. Last change: Tue Mar 28 2006 9:33:14 PM
Bram Moolenaar1056d982006-03-09 22:37:52 +00002
3by David Fishburn
4
5This is a filetype plugin to work with SQL files.
6
7The Structured Query Language (SQL) is a standard which specifies statements
8that allow a user to interact with a relational database. Vim includes
9features for navigation, indentation and syntax highlighting.
10
111. Navigation |sql-navigation|
12 1.1 Matchit |sql-matchit|
13 1.2 Text Object Motions |sql-object-motions|
14 1.3 Predefined Object Motions |sql-predefined-objects|
15 1.4 Macros |sql-macros|
162. SQL Dialects |sql-dialects|
17 2.1 SQLSetType |SQLSetType|
18 2.2 SQL Dialect Default |sql-type-default|
193. Adding new SQL Dialects |sql-adding-dialects|
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000204. OMNI SQL Completion |sql-completion|
21 4.1 Static mode |sql-completion-static|
22 4.2 Dynamic mode |sql-completion-dynamic|
23 4.3 Tutorial |sql-completion-tutorial|
24 4.3.1 Complete Tables |sql-completion-tables|
25 4.3.2 Complete Columns |sql-completion-columns|
26 4.3.3 Complete Procedures |sql-completion-procedures|
27 4.3.4 Complete Views |sql-completion-views|
28 4.4 Completion Customization |sql-completion-customization|
29 4.5 Customizing Maps |sql-completion-maps|
Bram Moolenaar1056d982006-03-09 22:37:52 +000030
31==============================================================================
321. Navigation *sql-navigation*
33
34The SQL ftplugin provides a number of options to assist with file
35navigation.
36
37
381.1 Matchit *sql-matchit*
39-----------
40The matchit plugin (http://www.vim.org/scripts/script.php?script_id=39)
41provides many additional features and can be customized for different
42languages. The matchit plugin is configured by defining a local
43buffer variable, b:match_words. Pressing the % key while on various
44keywords will move the cursor to its match. For example, if the cursor
45is on an "if", pressing % will cycle between the "else", "elseif" and
46"end if" keywords.
47
48The following keywords are supported: >
49 if
50 elseif | elsif
51 else [if]
52 end if
53
54 [while condition] loop
55 leave
56 break
57 continue
58 exit
59 end loop
60
61 for
62 leave
63 break
64 continue
65 exit
66 end loop
67
68 do
69 statements
70 doend
71
72 case
73 when
74 when
75 default
76 end case
77
78 merge
79 when not matched
80 when matched
81
82 create[ or replace] procedure|function|event
83 returns
84<
85
861.2 Text Object Motions *sql-object-motions*
87-----------------------
88Vim has a number of predefined keys for working with text |object-motions|.
89This filetype plugin attempts to translate these keys to maps which make sense
90for the SQL language.
91
92The following |Normal| mode and |Visual| mode maps exist (when you edit a SQL
93file): >
94 ]] move forward to the next 'begin'
95 [[ move backwards to the previous 'begin'
96 ][ move forward to the next 'end'
97 [] move backwards to the previous 'end'
98<
99
1001.3 Predefined Object Motions *sql-predefined-objects*
101-----------------------------
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000102Most relational databases support various standard features, tables, indices,
Bram Moolenaar1056d982006-03-09 22:37:52 +0000103triggers and stored procedures. Each vendor also has a variety of proprietary
104objects. The next set of maps have been created to help move between these
105objects. Depends on which database vendor you are using, the list of objects
106must be configurable. The filetype plugin attempts to define many of the
107standard objects, plus many additional ones. In order to make this as
108flexible as possible, you can override the list of objects from within your
109|vimrc| with the following: >
110 let g:ftplugin_sql_objects = 'function,procedure,event,table,trigger' .
111 \ ',schema,service,publication,database,datatype,domain' .
112 \ ',index,subscription,synchronization,view,variable'
113<
114The following |Normal| mode and |Visual| mode maps have been created which use
115the above list: >
116 ]} move forward to the next 'create <object name>'
117 [{ move backward to the previous 'create <object name>'
118
119Repeatedly pressing ]} will cycle through each of these create statements: >
120 create table t1 (
121 ...
122 );
123
124 create procedure p1
125 begin
126 ...
127 end;
128
129 create index i1 on t1 (c1);
130<
131The default setting for g:ftplugin_sql_objects is: >
132 let g:ftplugin_sql_objects = 'function,procedure,event,' .
133 \ '\\(existing\\\\|global\\s\\+temporary\\s\\+\\)\\\{,1}' .
134 \ 'table,trigger' .
135 \ ',schema,service,publication,database,datatype,domain' .
136 \ ',index,subscription,synchronization,view,variable'
137<
138The above will also handle these cases: >
139 create table t1 (
140 ...
141 );
142 create existing table t2 (
143 ...
144 );
145 create global temporary table t3 (
146 ...
147 );
148<
149By default, the ftplugin only searches for CREATE statements. You can also
150override this via your |vimrc| with the following: >
151 let g:ftplugin_sql_statements = 'create,alter'
152
153The filetype plugin defines three types of comments: >
154 1. --
155 2. //
156 3. /*
157 *
158 */
159<
160The following |Normal| mode and |Visual| mode maps have been created to work
161with comments: >
162 ]" move forward to the beginning of a comment
163 [" move forward to the end of a comment
164
165
166
1671.4 Macros *sql-macros*
168----------
169Vim's feature to find macro definitions, |'define'|, is supported using this
170regular expression: >
171 \c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\>
172<
173This addresses the following code: >
174 CREATE VARIABLE myVar1 INTEGER;
175
176 CREATE PROCEDURE sp_test(
177 IN myVar2 INTEGER,
178 OUT myVar3 CHAR(30),
179 INOUT myVar4 NUMERIC(20,0)
180 )
181 BEGIN
182 DECLARE myVar5 INTEGER;
183
184 SELECT c1, c2, c3
185 INTO myVar2, myVar3, myVar4
186 FROM T1
187 WHERE c4 = myVar1;
188 END;
189<
190Place your cursor on "myVar1" on this line: >
191 WHERE c4 = myVar1;
192 ^
193<
194Press any of the following keys: >
195 [d
196 [D
197 [CTRL-D
198
199
200==============================================================================
2012. SQL Dialects *sql-dialects* *sql-types*
202 *sybase* *TSQL* *Transact-SQL*
203 *sqlanywhere*
204 *oracle* *plsql* *sqlj*
205 *sqlserver*
206 *mysql* *postgress* *psql*
207 *informix*
208
209All relational databases support SQL. There is a portion of SQL that is
210portable across vendors (ex. CREATE TABLE, CREATE INDEX), but there is a
211great deal of vendor specific extensions to SQL. Oracle supports the
212"CREATE OR REPLACE" syntax, column defaults specified in the CREATE TABLE
213statement and the procedural language (for stored procedures and triggers).
214
215The default Vim distribution ships with syntax highlighting based on Oracle's
216PL/SQL. The default SQL indent script works for Oracle and SQL Anywhere.
217The default filetype plugin works for all vendors and should remain vendor
218neutral, but extendable.
219
220Vim currently has support for a variety of different vendors, currently this
221is via syntax scripts. Unfortunately, to flip between different syntax rules
222you must either create:
223 1. New filetypes
224 2. Custom autocmds
225 3. Manual steps / commands
226
227The majority of people work with only one vendor's database product, it would
228be nice to specify a default in your |vimrc|.
229
230
2312.1 SQLSetType *sqlsettype* *SQLSetType*
232--------------
233For the people that work with many different databases, it would be nice to be
234able to flip between the various vendors rules (indent, syntax) on a per
235buffer basis, at any time. The ftplugin/sql.vim file defines this function: >
236 SQLSetType
237<
238Executing this function without any parameters will set the indent and syntax
239scripts back to their defaults, see |sql-type-default|. If you have turned
240off Vi's compatibility mode, |'compatible'|, you can use the <Tab> key to
241complete the optional parameter.
242
243After typing the function name and a space, you can use the completion to
244supply a parameter. The function takes the name of the Vim script you want to
245source. Using the |cmdline-completion| feature, the SQLSetType function will
246search the |'runtimepath'| for all Vim scripts with a name containing 'sql'.
247This takes the guess work out of the spelling of the names. The following are
248examples: >
249 :SQLSetType
250 :SQLSetType sqloracle
251 :SQLSetType sqlanywhere
252 :SQLSetType sqlinformix
253 :SQLSetType mysql
254<
255The easiest approach is to the use <Tab> character which will first complete
256the command name (SQLSetType), after a space and another <Tab>, display a list
257of available Vim script names: >
258 :SQL<Tab><space><Tab>
259<
260
2612.2 SQL Dialect Default *sql-type-default*
262-----------------------
263As mentioned earlier, the default syntax rules for Vim is based on Oracle
264(PL/SQL). You can override this default by placing one of the following in
265your |vimrc|: >
266 let g:sql_type_default = 'sqlanywhere'
267 let g:sql_type_default = 'sqlinformix'
268 let g:sql_type_default = 'mysql'
269<
270If you added the following to your |vimrc|: >
271 let g:sql_type_default = 'sqlinformix'
272<
273The next time edit a SQL file the following scripts will be automatically
274loaded by Vim: >
275 ftplugin/sql.vim
276 syntax/sqlinformix.vim
277 indent/sql.vim
278>
279Notice indent/sqlinformix.sql was not loaded. There is no indent file
280for Informix, Vim loads the default files if the specified files does not
281exist.
282
283
284==============================================================================
2853. Adding new SQL Dialects *sql-adding-dialects*
286
287If you begin working with a SQL dialect which does not have any customizations
288available with the default Vim distribution you can check http://www.vim.org
289to see if any customization currently exist. If not, you can begin by cloning
290an existing script. Read |filetype-plugins| for more details.
291
292To help identify these scripts, try to create the files with a "sql" prefix.
293If you decide you wish to create customizations for the SQLite database, you
294can create any of the following: >
295 Unix
296 ~/.vim/syntax/sqlite.vim
297 ~/.vim/indent/sqlite.vim
298 Windows
299 $VIM/vimfiles/syntax/sqlite.vim
300 $VIM/vimfiles/indent/sqlite.vim
301<
302No changes are necessary to the SQLSetType function. It will automatically
303pickup the new SQL files and load them when you issue the SQLSetType command.
304
305
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000306==============================================================================
3074. OMNI SQL Completion *sql-completion*
308 *omni-sql-completion*
Bram Moolenaar1056d982006-03-09 22:37:52 +0000309
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000310Vim 7 includes a code completion interface and functions which allows plugin
311developers to build in code completion for any language. Vim 7 includes
312code completion for the SQL language.
313
314There are two modes to the SQL completion plugin, static and dynamic. The
315static mode populates the popups with the data generated from current syntax
316highlight rules. The dynamic mode populates the popups with data retrieved
317directly from a database. This includes, table lists, column lists,
318procedures names and more.
319
3204.1 Static Mode *sql-completion-static*
321---------------
322The static popups created contain items defined by the active syntax rules
323while editing a file with a filetype of SQL. The plugin defines (by default)
324various maps to help the user refine which list of items they wish displayed.
325The defaults static maps are: >
326 imap <buffer> <C-C>a <C-\><C-O>:let b:sql_compl_type='syntax'<CR><C-X><C-O>
327 imap <buffer> <C-C>s <C-\><C-O>:let b:sql_compl_type='sqlStatement'<CR><C-X><C-O>
328 imap <buffer> <C-C>f <C-\><C-O>:let b:sql_compl_type='sqlFunction'<CR><C-X><C-O>
329 imap <buffer> <C-C>k <C-\><C-O>:let b:sql_compl_type='sqlKeyword'<CR><C-X><C-O>
330 imap <buffer> <C-C>o <C-\><C-O>:let b:sql_compl_type='sqlOption'<CR><C-X><C-O>
331 imap <buffer> <C-C>T <C-\><C-O>:let b:sql_compl_type='sqlType'<CR><C-X><C-O>
332<
333The static maps (which are based on the syntax highlight groups) follow this
334format: >
335 imap <buffer> <C-C>k <C-\><C-O>:let b:sql_compl_type='sqlKeyword'<CR><C-X><C-O>
336<
337This command breaks down as: >
338 imap - Create an insert map
339 <buffer> - Only for this buffer
340 <C-C>k - Your choice of key map
341 <C-\><C-O> - Execute one command, return to Insert mode
342 :let b:sql_compl_type= - Choose the highlight group's entries to display.
343 You can view a list of highlight group names to
344 choose from by executing the
345 :syntax list
346 command while editing a SQL file.
347 'sqlKeyword' - Display the items for the sqlKeyword highlight
348 group
349 <CR> - Execute the :let command
350 <C-X><C-O> - Trigger the standard omni completion key stroke.
351 By setting the b:sql_compl_type variable, this
352 instructs the SQL completion plugin to populate
353 the popup with items from the sqlKeyword highlight
354 group. The plugin will also cache this result
355 until Vim is restarted. The syntax list is
356 retrieved using the syntaxcomplete plugin.
357<
358Setting b:sql_compl_type = 'syntax' is a special case. This instructs the
359syntaxcomplete plugin to retrieve all syntax items. So this will effectively
360work for any of Vim's SQL syntax files. At the time of writing this includes
36110 different syntax files for the different dialects of SQL (see section 3
362above, |sql-dialects|).
363
364Here are some examples of the entries which are pulled from the syntax files: >
365 All
366 - Contains the contents of all syntax highlight groups
367 Statements
368 - Select, Insert, Update, Delete, Create, Alter, ...
369 Functions
370 - Min, Max, Trim, Round, Date, ...
371 Keywords
372 - Index, Database, Having, Group, With
373 Options
374 - Isolation_level, On_error, Qualify_owners, Fire_triggers, ...
375 Types
376 - Integer, Char, Varchar, Date, DateTime, Timestamp, ...
377<
378
3794.2 Dynamic Mode *sql-completion-dynamic*
380----------------
381Dynamic mode populates the popups with data directly from a database. In
382order for the dynamic feature to be enabled you must have the dbext.vim
383plugin installed, (http://vim.sourceforge.net/script.php?script_id=356).
384
385Dynamic mode is used by several features of the SQL completion plugin.
386After installing the dbext plugin see the |dbext-tutorial| for additional
387configuration and usage. The dbext plugin allows the SQL completion plugin
388to display a list of tables, procedures, views and columns. >
389 Table List
390 - All tables for all schema owners
391 Procedure List
392 - All stored procedures for all schema owners
393 View List
394 - All stored procedures for all schema owners
395 Column List
396 - For the selected table, the columns that are part of the table
397<
398To enable the popup, while in INSERT mode, use the following key combinations
399for each group (where <C-C> means hold the CTRL key down while pressing
400the space bar):
401 Table List - <C-C>t
402 - <C-X><C-O> (the default map assumes tables)
403 Stored Procedure List - <C-C>p
404 View List - <C-C>v
405 Column List - <C-C>c
406 - .<C-X><C-O>
407 - If <C-X><C-O> is pressed following a period
408 it is assumed you are asking for a column list.
409 - When viewing a popup window displaying the list
410 of tables, you can press <C-Right>, this will
411 replace the table currently highlighted with
412 the column list for that table.
413 - When viewing a popup window displaying the list
414 of columns, you can press <C-Left>, this will
415 replace the column list with the list of tables.
416
417The SQL completion plugin caches various lists that are displayed in
418the popup window. This makes the re-displaying of these lists very
419fast. If new tables or columns are added to the database it may become
420necessary to clear the plugins cache. The default map for this is: >
421 imap <buffer> <C-C>R <C-O>:let b:sql_compl_type='ResetCache'<CR><C-X><C-O>
422<
423
4244.3 SQL Tutorial *sql-completion-tutorial*
425----------------
426
427This tutorial is designed to take you through the common features of the SQL
428completion plugin so that: >
429 a) You gain familiarity with the plugin
430 b) You are introduced to some of the more common features
431 c) Show how to customize it to your preferences
432 d) Demonstrate "Best of Use" of the plugin (easiest way to configure).
433<
434First, create a new buffer: >
435 :e tutorial.sql
436<
437
438Static features
439---------------
440To take you through the various lists, simply enter insert mode, hit:
441 <C-C>s (show SQL statements)
442At this point, you can page down through the list until you find "select".
443If you are familiar with the item you are looking for, for example you know
444the statement begins with the letter "s". You can type ahead (without the
445quotes) "se" then press:
446 <C-Spact>t
447Assuming "select" is highlighted in the popup list press <Enter> to choose
448the entry. Now type:
449 * fr<C-C>a (show all syntax items)
450choose "from" from the popup list.
451
452When writing stored procedures using the "type" list is useful. It contains
453a list of all the database supported types. This may or may not be true
454depending on the syntax file you are using. The SQL Anywhere syntax file
455(sqlanywhere.vim) has support for this: >
456 BEGIN
457 DECLARE customer_id <C-C>T <-- Choose a type from the list
458<
459
460Dynamic features
461----------------
462To take advantage of the dynamic features you must first install the
463dbext.vim plugin (http://vim.sourceforge.net/script.php?script_id=356). It
464also comes with a tutorial. From the SQL completion plugin's perspective,
465the main feature dbext provides is a connection to a database. dbext
466connection profiles are the most efficient mechanism to define connection
467information. Once connections have been setup, the SQL completion plugin
468uses the features of dbext in the background to populate the popups.
469
470What follows assumes dbext.vim has been correctly configured, a simple test
471is to run the command, :DBListTable. If a list of tables is shown, you know
472dbext.vim is working as expected. If not, please consult the dbext.txt
473documentation.
474
475Assuming you have followed the |dbext-tutorial| you can press <C-C>t to
476display a list of tables. There is a delay while dbext is creating the table
477list. After the list is displayed press <C-W>. This will remove both the
478popup window and the table name already chosen when the list became active. >
479
480 4.3.1 Table Completion: *sql-completion-tables*
481<
482Press <C-C>t to display a list of tables from within the database you
483have connected via the dbext plugin.
484NOTE: All of the SQL completion popups support typing a prefix before pressing
485the key map. This will limit the contents of the popup window to just items
486beginning with those characters. >
487
488 4.3.2 Column Completion: *sql-completion-columns*
489<
490The SQL completion plugin can also display a list of columns for particular
491tables. The column completion is trigger via <C-C>c.
492
493NOTE: The following example uses <C-Right> to trigger a column list while
494the popup window is active. This map is only available on the Windows
495platforms since *nix does not recognize CTRL and the right arrow held down
496together. If you wish to enable this functionality on a *nix platform choose
497a key and create this mapping (see |sql-completion-maps| for further
498details on where to create this imap): >
499 imap <buffer> <your_keystroke> <CR><C-\><C-O>:let b:sql_compl_type='column'<CR><C-X><C-O>
500<
501Example of using column completion:
502 - Press <C-C>t again to display the list of tables.
503 - When the list is displayed in the completion window, press <C-Right>,
504 this will replace the list of tables, with a list of columns for the
505 table highlighted (after the same short delay).
506 - If you press <C-Left>, this will again replace the column list with the
507 list of tables. This allows you to drill into tables and column lists
508 very quickly.
509 - Press <C-Right> again while the same table is highlighted. You will
510 notice there is no delay since the column list has been cached. If you
511 change the schema of a cached table you can press <C-C>R, which
512 clears the SQL completion cache.
513 - NOTE: <C-Right> and <C-Left> have been designed to work while the
514 completion window is active. If you use these maps when the completion
515 window is not active a carriage return will be inadvertently entered in
516 your buffer.
517
518Lets look how we can build a SQL statement dynamically. A select statement
519requires a list of columns. There are two ways to build a column list using
520the SQL completion plugin. >
521 One column at a time:
522< 1. After typing SELECT press <C-C>t to display a list of tables.
523 2. Choose a table from the list.
524 3. Press <C-Right> to display a list of columns.
525 4. Choose the column from the list and press enter.
526 5. Enter a "," and press <C-C>c. Generating a column list
527 generally requires having the cursor on a table name. The plugin
528 uses this name to determine what table to retrieve the column list.
529 In this step, since we are pressing <C-C>c without the cursor
530 on a table name the column list displayed will be for the previous
531 table. Choose a different column and move on.
532 6. Repeat step 5 as often as necessary. >
533 All columns for a table:
534< 1. After typing SELECT press <C-C>t to display a list of tables.
535 2. Highlight the table you need the column list for.
536 3. Press <Enter> to choose the table from the list.
537 4. Press <C-C>l to request a comma separated list of all columns
538 for this table.
539 5. Based on the table name chosen in step 3, the plugin attempts to
540 decide on a reasonable table alias. You are then prompted to
541 either accept of change the alias. Press OK.
542 6. The table name is replaced with the column list of the table is
543 replaced with the comma separate list of columns with the alias
544 prepended to each of the columns.
545 7. Step 3 and 4 can be replaced by pressing <C-C>L, which has
546 a <CR> embedded in the map to choose the currently highlighted
547 table in the list.
548
549There is a special provision when writing select statements. Consider the
550following statement: >
551 select *
552 from customer c,
553 contact cn,
554 department as dp,
555 employee e,
556 site_options so
557 where c.
558<
559In INSERT mode after typing the final "c." which is an alias for the
560"customer" table, you can press either <C-C>c or <C-X><C-O>. This will
561popup a list of columns for the customer table. It does this by looking back
562to the beginning of the select statement and finding a list of the tables
563specified in the FROM clause. In this case it notes that in the string
564"customer c", "c" is an alias for the customer table. The optional "AS"
565keyword is also supported, "customer AS c". >
566
567
568 4.3.3 Procedure Completion: *sql-completion-procedures*
569<
570Similar to the table list, <C-C>p, will display a list of stored
571procedures stored within the database. >
572
573 4.3.4 View Completion: *sql-completion-views*
574<
575Similar to the table list, <C-C>v, will display a list of views in the
576database.
577
578
5794.4 Completion Customization *sql-completion-customization*
580----------------------------
581
582The SQL completion plugin can be customized through various options set in
583your |vimrc|: >
584 omni_sql_no_default_maps
585< - Default: This variable is not defined
586 - If this variable is defined, no maps are created for OMNI
587 completion. See |sql-completion-maps| for further discussion.
588>
589 omni_sql_use_tbl_alias
590< - Default: a
591 - This setting is only used when generating a comma separated
592 column list. By default the map is <C-C>l. When generating
593 a column list, an alias can be prepended to the beginning of each
594 column, for example: e.emp_id, e.emp_name. This option has three
595 settings: >
596 n - do not use an alias
597 d - use the default (calculated) alias
598 a - ask to confirm the alias name
599<
600 An alias is determined following a few rules:
601 1. If the table name has an '_', then use it as a separator: >
602 MY_TABLE_NAME --> MTN
603 my_table_name --> mtn
604 My_table_NAME --> MtN
605< 2. If the table name does NOT contain an '_', but DOES use
606 mixed case then the case is used as a separator: >
607 MyTableName --> MTN
608< 3. If the table name does NOT contain an '_', and does NOT
609 use mixed case then the first letter of the table is used: >
610 mytablename --> m
611 MYTABLENAME --> M
612<
613
6144.5 Customizing Maps *sql-completion-maps*
615--------------------
616
617You can create as many additional key maps as you like. Generally, the maps
618will be specifying different syntax highlight groups.
619
620If you do not wish the default maps created or the key choices do not work on
621your platform (often a case on *nix) you define the following variable in
622your |vimrc|: >
623 let g:omni_sql_no_default_maps = 1
624<
625Do no edit ftplugin/sql.vim directly! If you change this file your changes
626will be over written on future updates. Vim has a special directory structure
627that allows you to make customizations without changing the files that are
628included with the Vim distribution. If you wish to customize the maps
629create an after/ftplugin/sql.vim (see |after-directory|) and place the same
630maps from the ftplugin/sql.vim in it using your own key strokes. <C-C> was
631chosen since it will work on both Windows and *nix platforms. On the windows
632platform you can also use <C-Space> or ALT keys.
633
Bram Moolenaar1056d982006-03-09 22:37:52 +0000634
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000635vim:tw=78:ts=8:ft=help:norl: