blob: 303ebd5bdc49145a2a985870a5ca74fa72869c32 [file] [log] [blame]
Bram Moolenaar1887deb2007-05-12 15:04:19 +00001*sql.txt* For Vim version 7.1. Last change: Wed Apr 26 2006 3:05:33 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
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000111. 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|
204. OMNI SQL Completion |sql-completion|
21 4.1 Static mode |sql-completion-static|
22 4.2 Dynamic mode |sql-completion-dynamic|
Bram Moolenaare2f98b92006-03-29 21:18:24 +000023 4.3 Tutorial |sql-completion-tutorial|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000024 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|
Bram Moolenaare2f98b92006-03-29 21:18:24 +000028 4.4 Completion Customization |sql-completion-customization|
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000029 4.5 SQL Maps |sql-completion-maps|
Bram Moolenaar910f66f2006-04-05 20:41:53 +000030 4.6 Using with other filetypes |sql-completion-filetypes|
Bram Moolenaar1056d982006-03-09 22:37:52 +000031
32==============================================================================
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000331. Navigation *sql-navigation*
Bram Moolenaar1056d982006-03-09 22:37:52 +000034
35The SQL ftplugin provides a number of options to assist with file
36navigation.
37
38
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000391.1 Matchit *sql-matchit*
Bram Moolenaar1056d982006-03-09 22:37:52 +000040-----------
41The matchit plugin (http://www.vim.org/scripts/script.php?script_id=39)
42provides many additional features and can be customized for different
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000043languages. The matchit plugin is configured by defining a local
44buffer variable, b:match_words. Pressing the % key while on various
Bram Moolenaar1056d982006-03-09 22:37:52 +000045keywords will move the cursor to its match. For example, if the cursor
46is on an "if", pressing % will cycle between the "else", "elseif" and
47"end if" keywords.
48
49The following keywords are supported: >
50 if
51 elseif | elsif
52 else [if]
53 end if
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000054
Bram Moolenaar1056d982006-03-09 22:37:52 +000055 [while condition] loop
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000056 leave
57 break
58 continue
59 exit
Bram Moolenaar1056d982006-03-09 22:37:52 +000060 end loop
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000061
Bram Moolenaar1056d982006-03-09 22:37:52 +000062 for
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000063 leave
64 break
65 continue
66 exit
Bram Moolenaar1056d982006-03-09 22:37:52 +000067 end loop
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000068
Bram Moolenaar1056d982006-03-09 22:37:52 +000069 do
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000070 statements
Bram Moolenaar1056d982006-03-09 22:37:52 +000071 doend
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000072
Bram Moolenaar1056d982006-03-09 22:37:52 +000073 case
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000074 when
Bram Moolenaar1056d982006-03-09 22:37:52 +000075 when
76 default
77 end case
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000078
Bram Moolenaar1056d982006-03-09 22:37:52 +000079 merge
80 when not matched
81 when matched
82
83 create[ or replace] procedure|function|event
84 returns
Bram Moolenaar1056d982006-03-09 22:37:52 +000085
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000086
871.2 Text Object Motions *sql-object-motions*
Bram Moolenaar1056d982006-03-09 22:37:52 +000088-----------------------
89Vim has a number of predefined keys for working with text |object-motions|.
90This filetype plugin attempts to translate these keys to maps which make sense
91for the SQL language.
92
93The following |Normal| mode and |Visual| mode maps exist (when you edit a SQL
94file): >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000095 ]] move forward to the next 'begin'
96 [[ move backwards to the previous 'begin'
97 ][ move forward to the next 'end'
98 [] move backwards to the previous 'end'
Bram Moolenaar1056d982006-03-09 22:37:52 +000099
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000100
1011.3 Predefined Object Motions *sql-predefined-objects*
Bram Moolenaar1056d982006-03-09 22:37:52 +0000102-----------------------------
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000103Most relational databases support various standard features, tables, indices,
Bram Moolenaar1056d982006-03-09 22:37:52 +0000104triggers and stored procedures. Each vendor also has a variety of proprietary
105objects. The next set of maps have been created to help move between these
106objects. Depends on which database vendor you are using, the list of objects
107must be configurable. The filetype plugin attempts to define many of the
108standard objects, plus many additional ones. In order to make this as
109flexible as possible, you can override the list of objects from within your
110|vimrc| with the following: >
111 let g:ftplugin_sql_objects = 'function,procedure,event,table,trigger' .
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000112 \ ',schema,service,publication,database,datatype,domain' .
113 \ ',index,subscription,synchronization,view,variable'
114
Bram Moolenaar1056d982006-03-09 22:37:52 +0000115The following |Normal| mode and |Visual| mode maps have been created which use
116the above list: >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000117 ]} move forward to the next 'create <object name>'
118 [{ move backward to the previous 'create <object name>'
Bram Moolenaar1056d982006-03-09 22:37:52 +0000119
120Repeatedly pressing ]} will cycle through each of these create statements: >
121 create table t1 (
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000122 ...
Bram Moolenaar1056d982006-03-09 22:37:52 +0000123 );
124
125 create procedure p1
126 begin
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000127 ...
Bram Moolenaar1056d982006-03-09 22:37:52 +0000128 end;
129
130 create index i1 on t1 (c1);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000131
Bram Moolenaar1056d982006-03-09 22:37:52 +0000132The default setting for g:ftplugin_sql_objects is: >
133 let g:ftplugin_sql_objects = 'function,procedure,event,' .
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000134 \ '\\(existing\\\\|global\\s\\+temporary\\s\\+\\)\\\{,1}' .
135 \ 'table,trigger' .
136 \ ',schema,service,publication,database,datatype,domain' .
137 \ ',index,subscription,synchronization,view,variable'
138
Bram Moolenaar1056d982006-03-09 22:37:52 +0000139The above will also handle these cases: >
140 create table t1 (
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000141 ...
Bram Moolenaar1056d982006-03-09 22:37:52 +0000142 );
143 create existing table t2 (
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000144 ...
Bram Moolenaar1056d982006-03-09 22:37:52 +0000145 );
146 create global temporary table t3 (
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000147 ...
Bram Moolenaar1056d982006-03-09 22:37:52 +0000148 );
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000149
Bram Moolenaar1056d982006-03-09 22:37:52 +0000150By default, the ftplugin only searches for CREATE statements. You can also
151override this via your |vimrc| with the following: >
152 let g:ftplugin_sql_statements = 'create,alter'
153
154The filetype plugin defines three types of comments: >
155 1. --
156 2. //
157 3. /*
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000158 *
159 */
160
Bram Moolenaar1056d982006-03-09 22:37:52 +0000161The following |Normal| mode and |Visual| mode maps have been created to work
162with comments: >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000163 ]" move forward to the beginning of a comment
164 [" move forward to the end of a comment
Bram Moolenaar1056d982006-03-09 22:37:52 +0000165
166
167
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001681.4 Macros *sql-macros*
Bram Moolenaar1056d982006-03-09 22:37:52 +0000169----------
170Vim's feature to find macro definitions, |'define'|, is supported using this
171regular expression: >
172 \c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\>
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000173
Bram Moolenaar1056d982006-03-09 22:37:52 +0000174This addresses the following code: >
175 CREATE VARIABLE myVar1 INTEGER;
176
177 CREATE PROCEDURE sp_test(
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000178 IN myVar2 INTEGER,
179 OUT myVar3 CHAR(30),
180 INOUT myVar4 NUMERIC(20,0)
Bram Moolenaar1056d982006-03-09 22:37:52 +0000181 )
182 BEGIN
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000183 DECLARE myVar5 INTEGER;
Bram Moolenaar1056d982006-03-09 22:37:52 +0000184
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000185 SELECT c1, c2, c3
186 INTO myVar2, myVar3, myVar4
187 FROM T1
188 WHERE c4 = myVar1;
Bram Moolenaar1056d982006-03-09 22:37:52 +0000189 END;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000190
Bram Moolenaar1056d982006-03-09 22:37:52 +0000191Place your cursor on "myVar1" on this line: >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000192 WHERE c4 = myVar1;
193 ^
194
Bram Moolenaar1056d982006-03-09 22:37:52 +0000195Press any of the following keys: >
196 [d
197 [D
198 [CTRL-D
199
200
201==============================================================================
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002022. SQL Dialects *sql-dialects* *sql-types*
203 *sybase* *TSQL* *Transact-SQL*
204 *sqlanywhere*
205 *oracle* *plsql* *sqlj*
206 *sqlserver*
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000207 *mysql* *postgres* *psql*
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000208 *informix*
Bram Moolenaar1056d982006-03-09 22:37:52 +0000209
210All relational databases support SQL. There is a portion of SQL that is
211portable across vendors (ex. CREATE TABLE, CREATE INDEX), but there is a
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212great deal of vendor specific extensions to SQL. Oracle supports the
Bram Moolenaar1056d982006-03-09 22:37:52 +0000213"CREATE OR REPLACE" syntax, column defaults specified in the CREATE TABLE
214statement and the procedural language (for stored procedures and triggers).
215
216The default Vim distribution ships with syntax highlighting based on Oracle's
217PL/SQL. The default SQL indent script works for Oracle and SQL Anywhere.
218The default filetype plugin works for all vendors and should remain vendor
219neutral, but extendable.
220
221Vim currently has support for a variety of different vendors, currently this
222is via syntax scripts. Unfortunately, to flip between different syntax rules
223you must either create:
224 1. New filetypes
225 2. Custom autocmds
226 3. Manual steps / commands
227
228The majority of people work with only one vendor's database product, it would
229be nice to specify a default in your |vimrc|.
230
231
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002322.1 SQLSetType *sqlsettype* *SQLSetType*
Bram Moolenaar1056d982006-03-09 22:37:52 +0000233--------------
234For the people that work with many different databases, it would be nice to be
235able to flip between the various vendors rules (indent, syntax) on a per
236buffer basis, at any time. The ftplugin/sql.vim file defines this function: >
237 SQLSetType
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000238
Bram Moolenaar1056d982006-03-09 22:37:52 +0000239Executing this function without any parameters will set the indent and syntax
240scripts back to their defaults, see |sql-type-default|. If you have turned
241off Vi's compatibility mode, |'compatible'|, you can use the <Tab> key to
242complete the optional parameter.
243
244After typing the function name and a space, you can use the completion to
245supply a parameter. The function takes the name of the Vim script you want to
246source. Using the |cmdline-completion| feature, the SQLSetType function will
247search the |'runtimepath'| for all Vim scripts with a name containing 'sql'.
248This takes the guess work out of the spelling of the names. The following are
249examples: >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000250 :SQLSetType
Bram Moolenaar1056d982006-03-09 22:37:52 +0000251 :SQLSetType sqloracle
252 :SQLSetType sqlanywhere
253 :SQLSetType sqlinformix
254 :SQLSetType mysql
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000255
Bram Moolenaar1056d982006-03-09 22:37:52 +0000256The easiest approach is to the use <Tab> character which will first complete
257the command name (SQLSetType), after a space and another <Tab>, display a list
258of available Vim script names: >
259 :SQL<Tab><space><Tab>
Bram Moolenaar1056d982006-03-09 22:37:52 +0000260
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000261
2622.2 SQL Dialect Default *sql-type-default*
Bram Moolenaar1056d982006-03-09 22:37:52 +0000263-----------------------
264As mentioned earlier, the default syntax rules for Vim is based on Oracle
265(PL/SQL). You can override this default by placing one of the following in
266your |vimrc|: >
267 let g:sql_type_default = 'sqlanywhere'
268 let g:sql_type_default = 'sqlinformix'
269 let g:sql_type_default = 'mysql'
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000270
Bram Moolenaar1056d982006-03-09 22:37:52 +0000271If you added the following to your |vimrc|: >
272 let g:sql_type_default = 'sqlinformix'
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000273
274The next time edit a SQL file the following scripts will be automatically
Bram Moolenaar1056d982006-03-09 22:37:52 +0000275loaded by Vim: >
276 ftplugin/sql.vim
277 syntax/sqlinformix.vim
278 indent/sql.vim
279>
280Notice indent/sqlinformix.sql was not loaded. There is no indent file
281for Informix, Vim loads the default files if the specified files does not
282exist.
283
284
285==============================================================================
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002863. Adding new SQL Dialects *sql-adding-dialects*
Bram Moolenaar1056d982006-03-09 22:37:52 +0000287
288If you begin working with a SQL dialect which does not have any customizations
289available with the default Vim distribution you can check http://www.vim.org
290to see if any customization currently exist. If not, you can begin by cloning
291an existing script. Read |filetype-plugins| for more details.
292
293To help identify these scripts, try to create the files with a "sql" prefix.
294If you decide you wish to create customizations for the SQLite database, you
295can create any of the following: >
296 Unix
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000297 ~/.vim/syntax/sqlite.vim
298 ~/.vim/indent/sqlite.vim
Bram Moolenaar1056d982006-03-09 22:37:52 +0000299 Windows
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000300 $VIM/vimfiles/syntax/sqlite.vim
301 $VIM/vimfiles/indent/sqlite.vim
302
Bram Moolenaar1056d982006-03-09 22:37:52 +0000303No changes are necessary to the SQLSetType function. It will automatically
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000304pickup the new SQL files and load them when you issue the SQLSetType command.
Bram Moolenaar1056d982006-03-09 22:37:52 +0000305
306
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000307==============================================================================
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003084. OMNI SQL Completion *sql-completion*
309 *omni-sql-completion*
Bram Moolenaar1056d982006-03-09 22:37:52 +0000310
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000311Vim 7 includes a code completion interface and functions which allows plugin
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000312developers to build in code completion for any language. Vim 7 includes
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000313code completion for the SQL language.
314
315There are two modes to the SQL completion plugin, static and dynamic. The
316static mode populates the popups with the data generated from current syntax
317highlight rules. The dynamic mode populates the popups with data retrieved
318directly from a database. This includes, table lists, column lists,
319procedures names and more.
320
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003214.1 Static Mode *sql-completion-static*
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000322---------------
323The static popups created contain items defined by the active syntax rules
324while editing a file with a filetype of SQL. The plugin defines (by default)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000325various maps to help the user refine the list of items to be displayed.
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000326The defaults static maps are: >
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000327 imap <buffer> <C-C>a <C-\><C-O>:call sqlcomplete#Map('syntax')<CR><C-X><C-O>
328 imap <buffer> <C-C>k <C-\><C-O>:call sqlcomplete#Map('sqlKeyword')<CR><C-X><C-O>
329 imap <buffer> <C-C>f <C-\><C-O>:call sqlcomplete#Map('sqlFunction')<CR><C-X><C-O>
330 imap <buffer> <C-C>o <C-\><C-O>:call sqlcomplete#Map('sqlOption')<CR><C-X><C-O>
331 imap <buffer> <C-C>T <C-\><C-O>:call sqlcomplete#Map('sqlType')<CR><C-X><C-O>
332 imap <buffer> <C-C>s <C-\><C-O>:call sqlcomplete#Map('sqlStatement')<CR><C-X><C-O>
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000333
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000334The static maps (which are based on the syntax highlight groups) follow this
335format: >
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000336 imap <buffer> <C-C>k <C-\><C-O>:call sqlcomplete#Map('sqlKeyword')<CR><C-X><C-O>
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000337
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000338This command breaks down as: >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000339 imap - Create an insert map
340 <buffer> - Only for this buffer
341 <C-C>k - Your choice of key map
342 <C-\><C-O> - Execute one command, return to Insert mode
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000343 :call sqlcomplete#Map( - Allows the SQL completion plugin to perform some
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000344 housekeeping functions to allow it to be used in
345 conjunction with other completion plugins.
346 Indicate which item you want the SQL completion
347 plugin to complete.
348 In this case we are asking the plugin to display
349 items from the syntax highlight group
350 'sqlKeyword'.
351 You can view a list of highlight group names to
352 choose from by executing the
353 :syntax list
354 command while editing a SQL file.
355 'sqlKeyword' - Display the items for the sqlKeyword highlight
356 group
357 )<CR> - Execute the :let command
358 <C-X><C-O> - Trigger the standard omni completion key stroke.
359 Passing in 'sqlKeyword' instructs the SQL
360 completion plugin to populate the popup with
361 items from the sqlKeyword highlight group. The
362 plugin will also cache this result until Vim is
363 restarted. The syntax list is retrieved using
364 the syntaxcomplete plugin.
365
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000366Using the 'syntax' keyword is a special case. This instructs the
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000367syntaxcomplete plugin to retrieve all syntax items. So this will effectively
368work for any of Vim's SQL syntax files. At the time of writing this includes
36910 different syntax files for the different dialects of SQL (see section 3
370above, |sql-dialects|).
371
372Here are some examples of the entries which are pulled from the syntax files: >
373 All
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000374 - Contains the contents of all syntax highlight groups
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000375 Statements
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000376 - Select, Insert, Update, Delete, Create, Alter, ...
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000377 Functions
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000378 - Min, Max, Trim, Round, Date, ...
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000379 Keywords
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000380 - Index, Database, Having, Group, With
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000381 Options
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000382 - Isolation_level, On_error, Qualify_owners, Fire_triggers, ...
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000383 Types
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000384 - Integer, Char, Varchar, Date, DateTime, Timestamp, ...
385
386
3874.2 Dynamic Mode *sql-completion-dynamic*
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000388----------------
389Dynamic mode populates the popups with data directly from a database. In
390order for the dynamic feature to be enabled you must have the dbext.vim
391plugin installed, (http://vim.sourceforge.net/script.php?script_id=356).
392
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000393Dynamic mode is used by several features of the SQL completion plugin.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000394After installing the dbext plugin see the dbext-tutorial for additional
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000395configuration and usage. The dbext plugin allows the SQL completion plugin
396to display a list of tables, procedures, views and columns. >
397 Table List
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000398 - All tables for all schema owners
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000399 Procedure List
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000400 - All stored procedures for all schema owners
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000401 View List
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000402 - All stored procedures for all schema owners
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000403 Column List
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000404 - For the selected table, the columns that are part of the table
405
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000406To enable the popup, while in INSERT mode, use the following key combinations
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000407for each group (where <C-C> means hold the CTRL key down while pressing
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000408the space bar):
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000409 Table List - <C-C>t
410 - <C-X><C-O> (the default map assumes tables)
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000411 Stored Procedure List - <C-C>p
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000412 View List - <C-C>v
413 Column List - <C-C>c
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000414
415 Windows platform only - When viewing a popup window displaying the list
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000416 of tables, you can press <C-Right>, this will
417 replace the table currently highlighted with
418 the column list for that table.
419 - When viewing a popup window displaying the list
420 of columns, you can press <C-Left>, this will
421 replace the column list with the list of tables.
422 - This allows you to quickly drill down into a
423 table to view it's columns and back again.
424
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000425The SQL completion plugin caches various lists that are displayed in
426the popup window. This makes the re-displaying of these lists very
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000427fast. If new tables or columns are added to the database it may become
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000428necessary to clear the plugins cache. The default map for this is: >
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000429 imap <buffer> <C-C>R <C-\><C-O>:call sqlcomplete#Map('ResetCache')<CR><C-X><C-O>
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000430
431
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004324.3 SQL Tutorial *sql-completion-tutorial*
433----------------
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000434
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000435This tutorial is designed to take you through the common features of the SQL
436completion plugin so that: >
437 a) You gain familiarity with the plugin
438 b) You are introduced to some of the more common features
439 c) Show how to customize it to your preferences
440 d) Demonstrate "Best of Use" of the plugin (easiest way to configure).
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000441
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000442First, create a new buffer: >
443 :e tutorial.sql
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000444
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000445
446Static features
447---------------
448To take you through the various lists, simply enter insert mode, hit:
449 <C-C>s (show SQL statements)
450At this point, you can page down through the list until you find "select".
451If you are familiar with the item you are looking for, for example you know
452the statement begins with the letter "s". You can type ahead (without the
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000453quotes) "se" then press:
454 <C-Space>t
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000455Assuming "select" is highlighted in the popup list press <Enter> to choose
456the entry. Now type:
457 * fr<C-C>a (show all syntax items)
458choose "from" from the popup list.
459
460When writing stored procedures using the "type" list is useful. It contains
461a list of all the database supported types. This may or may not be true
462depending on the syntax file you are using. The SQL Anywhere syntax file
463(sqlanywhere.vim) has support for this: >
464 BEGIN
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000465 DECLARE customer_id <C-C>T <-- Choose a type from the list
466
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000467
468Dynamic features
469----------------
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000470To take advantage of the dynamic features you must first install the
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000471dbext.vim plugin (http://vim.sourceforge.net/script.php?script_id=356). It
472also comes with a tutorial. From the SQL completion plugin's perspective,
473the main feature dbext provides is a connection to a database. dbext
474connection profiles are the most efficient mechanism to define connection
475information. Once connections have been setup, the SQL completion plugin
476uses the features of dbext in the background to populate the popups.
477
478What follows assumes dbext.vim has been correctly configured, a simple test
479is to run the command, :DBListTable. If a list of tables is shown, you know
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000480dbext.vim is working as expected. If not, please consult the dbext.txt
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000481documentation.
482
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000483Assuming you have followed the dbext-tutorial you can press <C-C>t to
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000484display a list of tables. There is a delay while dbext is creating the table
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000485list. After the list is displayed press <C-W>. This will remove both the
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000486popup window and the table name already chosen when the list became active. >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000487
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000488 4.3.1 Table Completion: *sql-completion-tables*
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000489
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000490Press <C-C>t to display a list of tables from within the database you
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000491have connected via the dbext plugin.
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000492NOTE: All of the SQL completion popups support typing a prefix before pressing
493the key map. This will limit the contents of the popup window to just items
494beginning with those characters. >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000495
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000496 4.3.2 Column Completion: *sql-completion-columns*
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000497
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000498The SQL completion plugin can also display a list of columns for particular
499tables. The column completion is trigger via <C-C>c.
500
501NOTE: The following example uses <C-Right> to trigger a column list while
502the popup window is active. This map is only available on the Windows
503platforms since *nix does not recognize CTRL and the right arrow held down
504together. If you wish to enable this functionality on a *nix platform choose
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000505a key and create one of these mappings (see |sql-completion-maps| for further
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000506details on where to create this imap): >
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000507 imap <buffer> <your_keystroke> <C-R>=sqlcomplete#DrillIntoTable()<CR>
508 imap <buffer> <your_keystroke> <C-Y><C-\><C-O>:call sqlcomplete#Map('column')<CR><C-X><C-O>
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000509
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000510Example of using column completion:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000511 - Press <C-C>t again to display the list of tables.
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000512 - When the list is displayed in the completion window, press <C-Right>,
513 this will replace the list of tables, with a list of columns for the
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000514 table highlighted (after the same short delay).
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000515 - If you press <C-Left>, this will again replace the column list with the
516 list of tables. This allows you to drill into tables and column lists
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000517 very quickly.
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000518 - Press <C-Right> again while the same table is highlighted. You will
519 notice there is no delay since the column list has been cached. If you
520 change the schema of a cached table you can press <C-C>R, which
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000521 clears the SQL completion cache.
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000522 - NOTE: <C-Right> and <C-Left> have been designed to work while the
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000523 completion window is active. If the completion popup window is
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000524 not active, a normal <C-Right> or <C-Left> will be executed.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000525
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000526Lets look how we can build a SQL statement dynamically. A select statement
527requires a list of columns. There are two ways to build a column list using
528the SQL completion plugin. >
529 One column at a time:
530< 1. After typing SELECT press <C-C>t to display a list of tables.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000531 2. Choose a table from the list.
532 3. Press <C-Right> to display a list of columns.
533 4. Choose the column from the list and press enter.
534 5. Enter a "," and press <C-C>c. Generating a column list
535 generally requires having the cursor on a table name. The plugin
536 uses this name to determine what table to retrieve the column list.
537 In this step, since we are pressing <C-C>c without the cursor
538 on a table name the column list displayed will be for the previous
539 table. Choose a different column and move on.
540 6. Repeat step 5 as often as necessary. >
541 All columns for a table:
542< 1. After typing SELECT press <C-C>t to display a list of tables.
543 2. Highlight the table you need the column list for.
544 3. Press <Enter> to choose the table from the list.
545 4. Press <C-C>l to request a comma separated list of all columns
546 for this table.
547 5. Based on the table name chosen in step 3, the plugin attempts to
548 decide on a reasonable table alias. You are then prompted to
549 either accept of change the alias. Press OK.
550 6. The table name is replaced with the column list of the table is
551 replaced with the comma separate list of columns with the alias
552 prepended to each of the columns.
553 7. Step 3 and 4 can be replaced by pressing <C-C>L, which has
554 a <C-Y> embedded in the map to choose the currently highlighted
555 table in the list.
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000556
557There is a special provision when writing select statements. Consider the
558following statement: >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000559 select *
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000560 from customer c,
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000561 contact cn,
562 department as dp,
563 employee e,
564 site_options so
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000565 where c.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000566
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000567In INSERT mode after typing the final "c." which is an alias for the
568"customer" table, you can press either <C-C>c or <C-X><C-O>. This will
569popup a list of columns for the customer table. It does this by looking back
570to the beginning of the select statement and finding a list of the tables
571specified in the FROM clause. In this case it notes that in the string
572"customer c", "c" is an alias for the customer table. The optional "AS"
573keyword is also supported, "customer AS c". >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000574
575
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000576 4.3.3 Procedure Completion: *sql-completion-procedures*
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000577
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000578Similar to the table list, <C-C>p, will display a list of stored
579procedures stored within the database. >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000580
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000581 4.3.4 View Completion: *sql-completion-views*
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000582
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000583Similar to the table list, <C-C>v, will display a list of views in the
584database.
585
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000586
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005874.4 Completion Customization *sql-completion-customization*
588----------------------------
589
590The SQL completion plugin can be customized through various options set in
591your |vimrc|: >
592 omni_sql_no_default_maps
593< - Default: This variable is not defined
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000594 - If this variable is defined, no maps are created for OMNI
595 completion. See |sql-completion-maps| for further discussion.
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000596>
597 omni_sql_use_tbl_alias
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000598< - Default: a
599 - This setting is only used when generating a comma separated
600 column list. By default the map is <C-C>l. When generating
601 a column list, an alias can be prepended to the beginning of each
602 column, for example: e.emp_id, e.emp_name. This option has three
603 settings: >
604 n - do not use an alias
605 d - use the default (calculated) alias
606 a - ask to confirm the alias name
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000607<
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000608 An alias is determined following a few rules:
609 1. If the table name has an '_', then use it as a separator: >
610 MY_TABLE_NAME --> MTN
611 my_table_name --> mtn
612 My_table_NAME --> MtN
613< 2. If the table name does NOT contain an '_', but DOES use
614 mixed case then the case is used as a separator: >
615 MyTableName --> MTN
616< 3. If the table name does NOT contain an '_', and does NOT
617 use mixed case then the first letter of the table is used: >
618 mytablename --> m
619 MYTABLENAME --> M
620
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000621 omni_sql_ignorecase
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000622< - Default: Current setting for|ignorecase|
623 - Valid settings are 0 or 1.
624 - When entering a few letters before initiating completion, the list
625 will be filtered to display only the entries which begin with the
626 list of characters. When this option is set to 0, the list will be
627 filtered using case sensitivity. >
628
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000629 omni_sql_include_owner
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000630< - Default: 0, unless dbext.vim 3.00 has been installed
631 - Valid settings are 0 or 1.
632 - When completing tables, procedure or views and using dbext.vim 3.00
633 or higher the list of objects will also include the owner name.
634 When completing these objects and omni_sql_include_owner is enabled
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000635 the owner name will be replaced. >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000636
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000637 omni_sql_precache_syntax_groups
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000638< - Default:
639 ['syntax','sqlKeyword','sqlFunction','sqlOption','sqlType','sqlStatement']
640 - sqlcomplete can be used in conjunction with other completion
641 plugins. This is outlined at |sql-completion-filetypes|. When the
642 filetype is changed temporarily to SQL, the sqlcompletion plugin
643 will cache the syntax groups listed in the List specified in this
644 option.
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000645>
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000646
6474.5 SQL Maps *sql-completion-maps*
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000648------------
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000649
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000650The default SQL maps have been described in other sections of this document in
651greater detail. Here is a list of the maps with a brief description of each.
652
653Static Maps
654-----------
655These are maps which use populate the completion list using Vim's syntax
656highlighting rules. >
657 <C-C>a
658< - Displays all SQL syntax items. >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000659 <C-C>k
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000660< - Displays all SQL syntax items defined as 'sqlKeyword'. >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000661 <C-C>f
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000662< - Displays all SQL syntax items defined as 'sqlFunction. >
663 <C-C>o
664< - Displays all SQL syntax items defined as 'sqlOption'. >
665 <C-C>T
666< - Displays all SQL syntax items defined as 'sqlType'. >
667 <C-C>s
668< - Displays all SQL syntax items defined as 'sqlStatement'. >
669
670Dynamic Maps
671------------
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000672These are maps which use populate the completion list using the dbext.vim
673plugin. >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000674 <C-C>t
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000675< - Displays a list of tables. >
676 <C-C>p
677< - Displays a list of procedures. >
678 <C-C>v
679< - Displays a list of views. >
680 <C-C>c
681< - Displays a list of columns for a specific table. >
682 <C-C>l
683< - Displays a comma separated list of columns for a specific table. >
684 <C-C>L
685< - Displays a comma separated list of columns for a specific table.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000686 This should only be used when the completion window is active. >
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000687 <C-Right>
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000688< - Displays a list of columns for the table currently highlighted in
689 the completion window. <C-Right> is not recognized on most Unix
690 systems, so this maps is only created on the Windows platform.
691 If you would like the same feature on Unix, choose a different key
692 and make the same map in your vimrc. >
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000693 <C-Left>
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000694< - Displays the list of tables.
695 <C-Left> is not recognized on most Unix systems, so this maps is
696 only created on the Windows platform. If you would like the same
697 feature on Unix, choose a different key and make the same map in
698 your vimrc. >
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000699 <C-C>R
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000700< - This maps removes all cached items and forces the SQL completion
701 to regenerate the list of items.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000702
703Customizing Maps
704----------------
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000705You can create as many additional key maps as you like. Generally, the maps
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000706will be specifying different syntax highlight groups.
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000707
708If you do not wish the default maps created or the key choices do not work on
709your platform (often a case on *nix) you define the following variable in
710your |vimrc|: >
711 let g:omni_sql_no_default_maps = 1
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000712
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000713Do no edit ftplugin/sql.vim directly! If you change this file your changes
714will be over written on future updates. Vim has a special directory structure
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000715which allows you to make customizations without changing the files that are
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000716included with the Vim distribution. If you wish to customize the maps
717create an after/ftplugin/sql.vim (see |after-directory|) and place the same
718maps from the ftplugin/sql.vim in it using your own key strokes. <C-C> was
719chosen since it will work on both Windows and *nix platforms. On the windows
720platform you can also use <C-Space> or ALT keys.
Bram Moolenaar1056d982006-03-09 22:37:52 +0000721
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000722
7234.6 Using with other filetypes *sql-completion-filetypes*
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000724------------------------------
725
726Many times SQL can be used with different filetypes. For example Perl, Java,
727PHP, Javascript can all interact with a database. Often you need both the SQL
728completion as well as the completion capabilities for the current language you
729are editing.
730
731This can be enabled easily with the following steps (assuming a Perl file): >
732 1. :e test.pl
733 2. :set filetype=sql
734 3. :set ft=perl
735
736Step 1
737------
738Begins by editing a Perl file. Vim automatically sets the filetype to
739"perl". By default, Vim runs the appropriate filetype file
740ftplugin/perl.vim. If you are using the syntax completion plugin by following
741the directions at |ft-syntax-omni| then the |'omnifunc'| option has been set to
742"syntax#Complete". Pressing <C-X><C-O> will display the omni popup containing
743the syntax items for Perl.
744
745Step 2
746------
747Manually setting the filetype to 'sql' will also fire the appropriate filetype
748files ftplugin/sql.vim. This file will define a number of buffer specific
749maps for SQL completion, see |sql-completion-maps|. Now these maps have
750been created and the SQL completion plugin has been initialized. All SQL
751syntax items have been cached in preparation. The SQL filetype script detects
752we are attempting to use two different completion plugins. Since the SQL maps
753begin with <C-C>, the maps will toggle the |'omnifunc'| when in use. So you
754can use <C-X><C-O> to continue using the completion for Perl (using the syntax
755completion plugin) and <C-C> to use the SQL completion features.
756
757Step 3
758------
759Setting the filetype back to Perl sets all the usual "perl" related items back
760as they were.
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000761
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000762
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000763vim:tw=78:ts=8:ft=help:norl: