summaryrefslogtreecommitdiff
path: root/runtime/doc/sql.txt
blob: 7a6c5b826cef3c76c9ba8fba46e6817db02810c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
*sql.txt*   	For Vim version 7.0c.  Last change: Fri Jan 06 2006 8:09:25 AM

by David Fishburn

This is a filetype plugin to work with SQL files.

The Structured Query Language (SQL) is a standard which specifies statements
that allow a user to interact with a relational database.  Vim includes
features for navigation, indentation and syntax highlighting.

1. Navigation                                   |sql-navigation|
    1.1 Matchit		        		|sql-matchit|
    1.2 Text Object Motions		        |sql-object-motions|
    1.3 Predefined Object Motions               |sql-predefined-objects|
    1.4 Macros                                  |sql-macros|
2. SQL Dialects		                        |sql-dialects|
    2.1 SQLSetType		        	|SQLSetType|
    2.2 SQL Dialect Default		        |sql-type-default|
3. Adding new SQL Dialects		        |sql-adding-dialects|

==============================================================================
1. Navigation	        			*sql-navigation*

The SQL ftplugin provides a number of options to assist with file
navigation.


1.1 Matchit		        		*sql-matchit*
-----------
The matchit plugin (http://www.vim.org/scripts/script.php?script_id=39)
provides many additional features and can be customized for different
languages.  The matchit plugin is configured by defining a local 
buffer variable, b:match_words.  Pressing the % key while on various 
keywords will move the cursor to its match.  For example, if the cursor
is on an "if", pressing % will cycle between the "else", "elseif" and
"end if" keywords.

The following keywords are supported: >
    if
    elseif | elsif
    else [if]
    end if
    
    [while condition] loop
        leave
        break
        continue
        exit
    end loop
    
    for
        leave
        break
        continue
        exit
    end loop
    
    do
        statements
    doend
    
    case
    when 
    when
    default
    end case
    
    merge
    when not matched
    when matched

    create[ or replace] procedure|function|event
    returns
<

1.2 Text Object Motions		        	*sql-object-motions*
-----------------------
Vim has a number of predefined keys for working with text |object-motions|.
This filetype plugin attempts to translate these keys to maps which make sense
for the SQL language.

The following |Normal| mode and |Visual| mode maps exist (when you edit a SQL
file): >
    ]]              move forward to the next 'begin'
    [[              move backwards to the previous 'begin'
    ][              move forward to the next 'end'
    []              move backwards to the previous 'end'
<

1.3 Predefined Object Motions                   *sql-predefined-objects*
-----------------------------
Most relational databases support various standard features, tables, indicies,
triggers and stored procedures.  Each vendor also has a variety of proprietary
objects.  The next set of maps have been created to help move between these
objects.  Depends on which database vendor you are using, the list of objects
must be configurable.  The filetype plugin attempts to define many of the
standard objects, plus many additional ones.  In order to make this as
flexible as possible, you can override the list of objects from within your
|vimrc| with the following: >
    let g:ftplugin_sql_objects = 'function,procedure,event,table,trigger' .
                \ ',schema,service,publication,database,datatype,domain' .
                \ ',index,subscription,synchronization,view,variable'
<                
The following |Normal| mode and |Visual| mode maps have been created which use
the above list: >
    ]}              move forward to the next 'create <object name>'
    [{              move backward to the previous 'create <object name>'

Repeatedly pressing ]} will cycle through each of these create statements: >
    create table t1 (
        ...
    );

    create procedure p1
    begin
        ...
    end;

    create index i1 on t1 (c1);
<
The default setting for g:ftplugin_sql_objects is: >
    let g:ftplugin_sql_objects = 'function,procedure,event,' .
                \ '\\(existing\\\\|global\\s\\+temporary\\s\\+\\)\\\{,1}' .
                \ 'table,trigger' .
                \ ',schema,service,publication,database,datatype,domain' .
                \ ',index,subscription,synchronization,view,variable'
<
The above will also handle these cases: >
    create table t1 (
        ...
    );
    create existing table t2 (
        ...
    );
    create global temporary table t3 (
        ...
    );
<
By default, the ftplugin only searches for CREATE statements.  You can also
override this via your |vimrc| with the following: >
    let g:ftplugin_sql_statements = 'create,alter'

The filetype plugin defines three types of comments: >
    1.  --
    2.  //
    3.  /*
         *
         */
<         
The following |Normal| mode and |Visual| mode maps have been created to work
with comments: >
    ]"              move forward to the beginning of a comment
    ["              move forward to the end of a comment



1.4 Macros                                         *sql-macros*
----------
Vim's feature to find macro definitions, |'define'|, is supported using this
regular expression: >
    \c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\>
<
This addresses the following code: >
    CREATE VARIABLE myVar1 INTEGER;

    CREATE PROCEDURE sp_test(
        IN myVar2 INTEGER,
        OUT myVar3 CHAR(30),
        INOUT myVar4 NUMERIC(20,0)
    )
    BEGIN
        DECLARE myVar5 INTEGER;

        SELECT c1, c2, c3
          INTO myVar2, myVar3, myVar4
          FROM T1
         WHERE c4 = myVar1;
    END;
<
Place your cursor on "myVar1" on this line: >
         WHERE c4 = myVar1;
                     ^
<
Press any of the following keys: >
    [d
    [D
    [CTRL-D


==============================================================================
2. SQL Dialects	        			*sql-dialects* *sql-types*
                                                *sybase* *TSQL* *Transact-SQL*
                                                *sqlanywhere* 
                                                *oracle* *plsql* *sqlj*
                                                *sqlserver*
                                                *mysql* *postgress* *psql*
                                                *informix*

All relational databases support SQL.  There is a portion of SQL that is
portable across vendors (ex. CREATE TABLE, CREATE INDEX), but there is a
great deal of vendor specific extensions to SQL.  Oracle supports the 
"CREATE OR REPLACE" syntax, column defaults specified in the CREATE TABLE
statement and the procedural language (for stored procedures and triggers).

The default Vim distribution ships with syntax highlighting based on Oracle's
PL/SQL.  The default SQL indent script works for Oracle and SQL Anywhere.
The default filetype plugin works for all vendors and should remain vendor
neutral, but extendable.

Vim currently has support for a variety of different vendors, currently this
is via syntax scripts. Unfortunately, to flip between different syntax rules
you must either create:
    1.  New filetypes
    2.  Custom autocmds
    3.  Manual steps / commands

The majority of people work with only one vendor's database product, it would
be nice to specify a default in your |vimrc|.


2.1 SQLSetType		        		*sqlsettype* *SQLSetType*
--------------
For the people that work with many different databases, it would be nice to be
able to flip between the various vendors rules (indent, syntax) on a per
buffer basis, at any time.  The ftplugin/sql.vim file defines this function: >
    SQLSetType
<
Executing this function without any parameters will set the indent and syntax
scripts back to their defaults, see |sql-type-default|.  If you have turned
off Vi's compatibility mode, |'compatible'|, you can use the <Tab> key to
complete the optional parameter.

After typing the function name and a space, you can use the completion to
supply a parameter.  The function takes the name of the Vim script you want to
source.  Using the |cmdline-completion| feature, the SQLSetType function will
search the |'runtimepath'| for all Vim scripts with a name containing 'sql'.
This takes the guess work out of the spelling of the names.  The following are
examples: >
    :SQLSetType 
    :SQLSetType sqloracle
    :SQLSetType sqlanywhere
    :SQLSetType sqlinformix
    :SQLSetType mysql
<
The easiest approach is to the use <Tab> character which will first complete
the command name (SQLSetType), after a space and another <Tab>, display a list
of available Vim script names: >
    :SQL<Tab><space><Tab>
<

2.2 SQL Dialect Default		        	*sql-type-default*
-----------------------
As mentioned earlier, the default syntax rules for Vim is based on Oracle
(PL/SQL).  You can override this default by placing one of the following in
your |vimrc|: >
    let g:sql_type_default = 'sqlanywhere'
    let g:sql_type_default = 'sqlinformix'
    let g:sql_type_default = 'mysql'
<
If you added the following to your |vimrc|: >
    let g:sql_type_default = 'sqlinformix'
<
The next time edit a SQL file the following scripts will be automatically 
loaded by Vim: >
    ftplugin/sql.vim
    syntax/sqlinformix.vim
    indent/sql.vim
>
Notice indent/sqlinformix.sql was not loaded.  There is no indent file
for Informix, Vim loads the default files if the specified files does not
exist.


==============================================================================
3. Adding new SQL Dialects		        *sql-adding-dialects*

If you begin working with a SQL dialect which does not have any customizations
available with the default Vim distribution you can check http://www.vim.org
to see if any customization currently exist.  If not, you can begin by cloning
an existing script.  Read |filetype-plugins| for more details.

To help identify these scripts, try to create the files with a "sql" prefix.
If you decide you wish to create customizations for the SQLite database, you
can create any of the following: >
    Unix
        ~/.vim/syntax/sqlite.vim
        ~/.vim/indent/sqlite.vim
    Windows
        $VIM/vimfiles/syntax/sqlite.vim
        $VIM/vimfiles/indent/sqlite.vim
<
No changes are necessary to the SQLSetType function.  It will automatically
pickup the new SQL files and load them when you issue the SQLSetType command. 




vim:tw=78:ts=8:ft=help:norl: