blob: a0e6f024011fa5d54255381a757a72e57be00ef2 [file] [log] [blame]
Bram Moolenaar1056d982006-03-09 22:37:52 +00001*sql.txt* For Vim version 7.0aa. Last change: Fri Jan 06 2006 8:09:25 AM
2
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|
20
21==============================================================================
221. Navigation *sql-navigation*
23
24The SQL ftplugin provides a number of options to assist with file
25navigation.
26
27
281.1 Matchit *sql-matchit*
29-----------
30The matchit plugin (http://www.vim.org/scripts/script.php?script_id=39)
31provides many additional features and can be customized for different
32languages. The matchit plugin is configured by defining a local
33buffer variable, b:match_words. Pressing the % key while on various
34keywords will move the cursor to its match. For example, if the cursor
35is on an "if", pressing % will cycle between the "else", "elseif" and
36"end if" keywords.
37
38The following keywords are supported: >
39 if
40 elseif | elsif
41 else [if]
42 end if
43
44 [while condition] loop
45 leave
46 break
47 continue
48 exit
49 end loop
50
51 for
52 leave
53 break
54 continue
55 exit
56 end loop
57
58 do
59 statements
60 doend
61
62 case
63 when
64 when
65 default
66 end case
67
68 merge
69 when not matched
70 when matched
71
72 create[ or replace] procedure|function|event
73 returns
74<
75
761.2 Text Object Motions *sql-object-motions*
77-----------------------
78Vim has a number of predefined keys for working with text |object-motions|.
79This filetype plugin attempts to translate these keys to maps which make sense
80for the SQL language.
81
82The following |Normal| mode and |Visual| mode maps exist (when you edit a SQL
83file): >
84 ]] move forward to the next 'begin'
85 [[ move backwards to the previous 'begin'
86 ][ move forward to the next 'end'
87 [] move backwards to the previous 'end'
88<
89
901.3 Predefined Object Motions *sql-predefined-objects*
91-----------------------------
92Most relational databases support various standard features, tables, indicies,
93triggers and stored procedures. Each vendor also has a variety of proprietary
94objects. The next set of maps have been created to help move between these
95objects. Depends on which database vendor you are using, the list of objects
96must be configurable. The filetype plugin attempts to define many of the
97standard objects, plus many additional ones. In order to make this as
98flexible as possible, you can override the list of objects from within your
99|vimrc| with the following: >
100 let g:ftplugin_sql_objects = 'function,procedure,event,table,trigger' .
101 \ ',schema,service,publication,database,datatype,domain' .
102 \ ',index,subscription,synchronization,view,variable'
103<
104The following |Normal| mode and |Visual| mode maps have been created which use
105the above list: >
106 ]} move forward to the next 'create <object name>'
107 [{ move backward to the previous 'create <object name>'
108
109Repeatedly pressing ]} will cycle through each of these create statements: >
110 create table t1 (
111 ...
112 );
113
114 create procedure p1
115 begin
116 ...
117 end;
118
119 create index i1 on t1 (c1);
120<
121The default setting for g:ftplugin_sql_objects is: >
122 let g:ftplugin_sql_objects = 'function,procedure,event,' .
123 \ '\\(existing\\\\|global\\s\\+temporary\\s\\+\\)\\\{,1}' .
124 \ 'table,trigger' .
125 \ ',schema,service,publication,database,datatype,domain' .
126 \ ',index,subscription,synchronization,view,variable'
127<
128The above will also handle these cases: >
129 create table t1 (
130 ...
131 );
132 create existing table t2 (
133 ...
134 );
135 create global temporary table t3 (
136 ...
137 );
138<
139By default, the ftplugin only searches for CREATE statements. You can also
140override this via your |vimrc| with the following: >
141 let g:ftplugin_sql_statements = 'create,alter'
142
143The filetype plugin defines three types of comments: >
144 1. --
145 2. //
146 3. /*
147 *
148 */
149<
150The following |Normal| mode and |Visual| mode maps have been created to work
151with comments: >
152 ]" move forward to the beginning of a comment
153 [" move forward to the end of a comment
154
155
156
1571.4 Macros *sql-macros*
158----------
159Vim's feature to find macro definitions, |'define'|, is supported using this
160regular expression: >
161 \c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\>
162<
163This addresses the following code: >
164 CREATE VARIABLE myVar1 INTEGER;
165
166 CREATE PROCEDURE sp_test(
167 IN myVar2 INTEGER,
168 OUT myVar3 CHAR(30),
169 INOUT myVar4 NUMERIC(20,0)
170 )
171 BEGIN
172 DECLARE myVar5 INTEGER;
173
174 SELECT c1, c2, c3
175 INTO myVar2, myVar3, myVar4
176 FROM T1
177 WHERE c4 = myVar1;
178 END;
179<
180Place your cursor on "myVar1" on this line: >
181 WHERE c4 = myVar1;
182 ^
183<
184Press any of the following keys: >
185 [d
186 [D
187 [CTRL-D
188
189
190==============================================================================
1912. SQL Dialects *sql-dialects* *sql-types*
192 *sybase* *TSQL* *Transact-SQL*
193 *sqlanywhere*
194 *oracle* *plsql* *sqlj*
195 *sqlserver*
196 *mysql* *postgress* *psql*
197 *informix*
198
199All relational databases support SQL. There is a portion of SQL that is
200portable across vendors (ex. CREATE TABLE, CREATE INDEX), but there is a
201great deal of vendor specific extensions to SQL. Oracle supports the
202"CREATE OR REPLACE" syntax, column defaults specified in the CREATE TABLE
203statement and the procedural language (for stored procedures and triggers).
204
205The default Vim distribution ships with syntax highlighting based on Oracle's
206PL/SQL. The default SQL indent script works for Oracle and SQL Anywhere.
207The default filetype plugin works for all vendors and should remain vendor
208neutral, but extendable.
209
210Vim currently has support for a variety of different vendors, currently this
211is via syntax scripts. Unfortunately, to flip between different syntax rules
212you must either create:
213 1. New filetypes
214 2. Custom autocmds
215 3. Manual steps / commands
216
217The majority of people work with only one vendor's database product, it would
218be nice to specify a default in your |vimrc|.
219
220
2212.1 SQLSetType *sqlsettype* *SQLSetType*
222--------------
223For the people that work with many different databases, it would be nice to be
224able to flip between the various vendors rules (indent, syntax) on a per
225buffer basis, at any time. The ftplugin/sql.vim file defines this function: >
226 SQLSetType
227<
228Executing this function without any parameters will set the indent and syntax
229scripts back to their defaults, see |sql-type-default|. If you have turned
230off Vi's compatibility mode, |'compatible'|, you can use the <Tab> key to
231complete the optional parameter.
232
233After typing the function name and a space, you can use the completion to
234supply a parameter. The function takes the name of the Vim script you want to
235source. Using the |cmdline-completion| feature, the SQLSetType function will
236search the |'runtimepath'| for all Vim scripts with a name containing 'sql'.
237This takes the guess work out of the spelling of the names. The following are
238examples: >
239 :SQLSetType
240 :SQLSetType sqloracle
241 :SQLSetType sqlanywhere
242 :SQLSetType sqlinformix
243 :SQLSetType mysql
244<
245The easiest approach is to the use <Tab> character which will first complete
246the command name (SQLSetType), after a space and another <Tab>, display a list
247of available Vim script names: >
248 :SQL<Tab><space><Tab>
249<
250
2512.2 SQL Dialect Default *sql-type-default*
252-----------------------
253As mentioned earlier, the default syntax rules for Vim is based on Oracle
254(PL/SQL). You can override this default by placing one of the following in
255your |vimrc|: >
256 let g:sql_type_default = 'sqlanywhere'
257 let g:sql_type_default = 'sqlinformix'
258 let g:sql_type_default = 'mysql'
259<
260If you added the following to your |vimrc|: >
261 let g:sql_type_default = 'sqlinformix'
262<
263The next time edit a SQL file the following scripts will be automatically
264loaded by Vim: >
265 ftplugin/sql.vim
266 syntax/sqlinformix.vim
267 indent/sql.vim
268>
269Notice indent/sqlinformix.sql was not loaded. There is no indent file
270for Informix, Vim loads the default files if the specified files does not
271exist.
272
273
274==============================================================================
2753. Adding new SQL Dialects *sql-adding-dialects*
276
277If you begin working with a SQL dialect which does not have any customizations
278available with the default Vim distribution you can check http://www.vim.org
279to see if any customization currently exist. If not, you can begin by cloning
280an existing script. Read |filetype-plugins| for more details.
281
282To help identify these scripts, try to create the files with a "sql" prefix.
283If you decide you wish to create customizations for the SQLite database, you
284can create any of the following: >
285 Unix
286 ~/.vim/syntax/sqlite.vim
287 ~/.vim/indent/sqlite.vim
288 Windows
289 $VIM/vimfiles/syntax/sqlite.vim
290 $VIM/vimfiles/indent/sqlite.vim
291<
292No changes are necessary to the SQLSetType function. It will automatically
293pickup the new SQL files and load them when you issue the SQLSetType command.
294
295
296
297
298vim:tw=78:ts=8:ft=help:norl:ff=unix: