Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame^] | 1 | *sql.txt* For Vim version 7.0c. Last change: Tue Mar 28 2006 9:33:14 PM |
Bram Moolenaar | 1056d98 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 2 | |
| 3 | by David Fishburn |
| 4 | |
| 5 | This is a filetype plugin to work with SQL files. |
| 6 | |
| 7 | The Structured Query Language (SQL) is a standard which specifies statements |
| 8 | that allow a user to interact with a relational database. Vim includes |
| 9 | features for navigation, indentation and syntax highlighting. |
| 10 | |
| 11 | 1. 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| |
| 16 | 2. SQL Dialects |sql-dialects| |
| 17 | 2.1 SQLSetType |SQLSetType| |
| 18 | 2.2 SQL Dialect Default |sql-type-default| |
| 19 | 3. Adding new SQL Dialects |sql-adding-dialects| |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame^] | 20 | 4. 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 Moolenaar | 1056d98 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 30 | |
| 31 | ============================================================================== |
| 32 | 1. Navigation *sql-navigation* |
| 33 | |
| 34 | The SQL ftplugin provides a number of options to assist with file |
| 35 | navigation. |
| 36 | |
| 37 | |
| 38 | 1.1 Matchit *sql-matchit* |
| 39 | ----------- |
| 40 | The matchit plugin (http://www.vim.org/scripts/script.php?script_id=39) |
| 41 | provides many additional features and can be customized for different |
| 42 | languages. The matchit plugin is configured by defining a local |
| 43 | buffer variable, b:match_words. Pressing the % key while on various |
| 44 | keywords will move the cursor to its match. For example, if the cursor |
| 45 | is on an "if", pressing % will cycle between the "else", "elseif" and |
| 46 | "end if" keywords. |
| 47 | |
| 48 | The 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 | |
| 86 | 1.2 Text Object Motions *sql-object-motions* |
| 87 | ----------------------- |
| 88 | Vim has a number of predefined keys for working with text |object-motions|. |
| 89 | This filetype plugin attempts to translate these keys to maps which make sense |
| 90 | for the SQL language. |
| 91 | |
| 92 | The following |Normal| mode and |Visual| mode maps exist (when you edit a SQL |
| 93 | file): > |
| 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 | |
| 100 | 1.3 Predefined Object Motions *sql-predefined-objects* |
| 101 | ----------------------------- |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame^] | 102 | Most relational databases support various standard features, tables, indices, |
Bram Moolenaar | 1056d98 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 103 | triggers and stored procedures. Each vendor also has a variety of proprietary |
| 104 | objects. The next set of maps have been created to help move between these |
| 105 | objects. Depends on which database vendor you are using, the list of objects |
| 106 | must be configurable. The filetype plugin attempts to define many of the |
| 107 | standard objects, plus many additional ones. In order to make this as |
| 108 | flexible 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 | < |
| 114 | The following |Normal| mode and |Visual| mode maps have been created which use |
| 115 | the above list: > |
| 116 | ]} move forward to the next 'create <object name>' |
| 117 | [{ move backward to the previous 'create <object name>' |
| 118 | |
| 119 | Repeatedly 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 | < |
| 131 | The 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 | < |
| 138 | The 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 | < |
| 149 | By default, the ftplugin only searches for CREATE statements. You can also |
| 150 | override this via your |vimrc| with the following: > |
| 151 | let g:ftplugin_sql_statements = 'create,alter' |
| 152 | |
| 153 | The filetype plugin defines three types of comments: > |
| 154 | 1. -- |
| 155 | 2. // |
| 156 | 3. /* |
| 157 | * |
| 158 | */ |
| 159 | < |
| 160 | The following |Normal| mode and |Visual| mode maps have been created to work |
| 161 | with comments: > |
| 162 | ]" move forward to the beginning of a comment |
| 163 | [" move forward to the end of a comment |
| 164 | |
| 165 | |
| 166 | |
| 167 | 1.4 Macros *sql-macros* |
| 168 | ---------- |
| 169 | Vim's feature to find macro definitions, |'define'|, is supported using this |
| 170 | regular expression: > |
| 171 | \c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\> |
| 172 | < |
| 173 | This 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 | < |
| 190 | Place your cursor on "myVar1" on this line: > |
| 191 | WHERE c4 = myVar1; |
| 192 | ^ |
| 193 | < |
| 194 | Press any of the following keys: > |
| 195 | [d |
| 196 | [D |
| 197 | [CTRL-D |
| 198 | |
| 199 | |
| 200 | ============================================================================== |
| 201 | 2. 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 | |
| 209 | All relational databases support SQL. There is a portion of SQL that is |
| 210 | portable across vendors (ex. CREATE TABLE, CREATE INDEX), but there is a |
| 211 | great deal of vendor specific extensions to SQL. Oracle supports the |
| 212 | "CREATE OR REPLACE" syntax, column defaults specified in the CREATE TABLE |
| 213 | statement and the procedural language (for stored procedures and triggers). |
| 214 | |
| 215 | The default Vim distribution ships with syntax highlighting based on Oracle's |
| 216 | PL/SQL. The default SQL indent script works for Oracle and SQL Anywhere. |
| 217 | The default filetype plugin works for all vendors and should remain vendor |
| 218 | neutral, but extendable. |
| 219 | |
| 220 | Vim currently has support for a variety of different vendors, currently this |
| 221 | is via syntax scripts. Unfortunately, to flip between different syntax rules |
| 222 | you must either create: |
| 223 | 1. New filetypes |
| 224 | 2. Custom autocmds |
| 225 | 3. Manual steps / commands |
| 226 | |
| 227 | The majority of people work with only one vendor's database product, it would |
| 228 | be nice to specify a default in your |vimrc|. |
| 229 | |
| 230 | |
| 231 | 2.1 SQLSetType *sqlsettype* *SQLSetType* |
| 232 | -------------- |
| 233 | For the people that work with many different databases, it would be nice to be |
| 234 | able to flip between the various vendors rules (indent, syntax) on a per |
| 235 | buffer basis, at any time. The ftplugin/sql.vim file defines this function: > |
| 236 | SQLSetType |
| 237 | < |
| 238 | Executing this function without any parameters will set the indent and syntax |
| 239 | scripts back to their defaults, see |sql-type-default|. If you have turned |
| 240 | off Vi's compatibility mode, |'compatible'|, you can use the <Tab> key to |
| 241 | complete the optional parameter. |
| 242 | |
| 243 | After typing the function name and a space, you can use the completion to |
| 244 | supply a parameter. The function takes the name of the Vim script you want to |
| 245 | source. Using the |cmdline-completion| feature, the SQLSetType function will |
| 246 | search the |'runtimepath'| for all Vim scripts with a name containing 'sql'. |
| 247 | This takes the guess work out of the spelling of the names. The following are |
| 248 | examples: > |
| 249 | :SQLSetType |
| 250 | :SQLSetType sqloracle |
| 251 | :SQLSetType sqlanywhere |
| 252 | :SQLSetType sqlinformix |
| 253 | :SQLSetType mysql |
| 254 | < |
| 255 | The easiest approach is to the use <Tab> character which will first complete |
| 256 | the command name (SQLSetType), after a space and another <Tab>, display a list |
| 257 | of available Vim script names: > |
| 258 | :SQL<Tab><space><Tab> |
| 259 | < |
| 260 | |
| 261 | 2.2 SQL Dialect Default *sql-type-default* |
| 262 | ----------------------- |
| 263 | As 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 |
| 265 | your |vimrc|: > |
| 266 | let g:sql_type_default = 'sqlanywhere' |
| 267 | let g:sql_type_default = 'sqlinformix' |
| 268 | let g:sql_type_default = 'mysql' |
| 269 | < |
| 270 | If you added the following to your |vimrc|: > |
| 271 | let g:sql_type_default = 'sqlinformix' |
| 272 | < |
| 273 | The next time edit a SQL file the following scripts will be automatically |
| 274 | loaded by Vim: > |
| 275 | ftplugin/sql.vim |
| 276 | syntax/sqlinformix.vim |
| 277 | indent/sql.vim |
| 278 | > |
| 279 | Notice indent/sqlinformix.sql was not loaded. There is no indent file |
| 280 | for Informix, Vim loads the default files if the specified files does not |
| 281 | exist. |
| 282 | |
| 283 | |
| 284 | ============================================================================== |
| 285 | 3. Adding new SQL Dialects *sql-adding-dialects* |
| 286 | |
| 287 | If you begin working with a SQL dialect which does not have any customizations |
| 288 | available with the default Vim distribution you can check http://www.vim.org |
| 289 | to see if any customization currently exist. If not, you can begin by cloning |
| 290 | an existing script. Read |filetype-plugins| for more details. |
| 291 | |
| 292 | To help identify these scripts, try to create the files with a "sql" prefix. |
| 293 | If you decide you wish to create customizations for the SQLite database, you |
| 294 | can 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 | < |
| 302 | No changes are necessary to the SQLSetType function. It will automatically |
| 303 | pickup the new SQL files and load them when you issue the SQLSetType command. |
| 304 | |
| 305 | |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame^] | 306 | ============================================================================== |
| 307 | 4. OMNI SQL Completion *sql-completion* |
| 308 | *omni-sql-completion* |
Bram Moolenaar | 1056d98 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 309 | |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame^] | 310 | Vim 7 includes a code completion interface and functions which allows plugin |
| 311 | developers to build in code completion for any language. Vim 7 includes |
| 312 | code completion for the SQL language. |
| 313 | |
| 314 | There are two modes to the SQL completion plugin, static and dynamic. The |
| 315 | static mode populates the popups with the data generated from current syntax |
| 316 | highlight rules. The dynamic mode populates the popups with data retrieved |
| 317 | directly from a database. This includes, table lists, column lists, |
| 318 | procedures names and more. |
| 319 | |
| 320 | 4.1 Static Mode *sql-completion-static* |
| 321 | --------------- |
| 322 | The static popups created contain items defined by the active syntax rules |
| 323 | while editing a file with a filetype of SQL. The plugin defines (by default) |
| 324 | various maps to help the user refine which list of items they wish displayed. |
| 325 | The 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 | < |
| 333 | The static maps (which are based on the syntax highlight groups) follow this |
| 334 | format: > |
| 335 | imap <buffer> <C-C>k <C-\><C-O>:let b:sql_compl_type='sqlKeyword'<CR><C-X><C-O> |
| 336 | < |
| 337 | This 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 | < |
| 358 | Setting b:sql_compl_type = 'syntax' is a special case. This instructs the |
| 359 | syntaxcomplete plugin to retrieve all syntax items. So this will effectively |
| 360 | work for any of Vim's SQL syntax files. At the time of writing this includes |
| 361 | 10 different syntax files for the different dialects of SQL (see section 3 |
| 362 | above, |sql-dialects|). |
| 363 | |
| 364 | Here 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 | |
| 379 | 4.2 Dynamic Mode *sql-completion-dynamic* |
| 380 | ---------------- |
| 381 | Dynamic mode populates the popups with data directly from a database. In |
| 382 | order for the dynamic feature to be enabled you must have the dbext.vim |
| 383 | plugin installed, (http://vim.sourceforge.net/script.php?script_id=356). |
| 384 | |
| 385 | Dynamic mode is used by several features of the SQL completion plugin. |
| 386 | After installing the dbext plugin see the |dbext-tutorial| for additional |
| 387 | configuration and usage. The dbext plugin allows the SQL completion plugin |
| 388 | to 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 | < |
| 398 | To enable the popup, while in INSERT mode, use the following key combinations |
| 399 | for each group (where <C-C> means hold the CTRL key down while pressing |
| 400 | the 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 | |
| 417 | The SQL completion plugin caches various lists that are displayed in |
| 418 | the popup window. This makes the re-displaying of these lists very |
| 419 | fast. If new tables or columns are added to the database it may become |
| 420 | necessary 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 | |
| 424 | 4.3 SQL Tutorial *sql-completion-tutorial* |
| 425 | ---------------- |
| 426 | |
| 427 | This tutorial is designed to take you through the common features of the SQL |
| 428 | completion 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 | < |
| 434 | First, create a new buffer: > |
| 435 | :e tutorial.sql |
| 436 | < |
| 437 | |
| 438 | Static features |
| 439 | --------------- |
| 440 | To take you through the various lists, simply enter insert mode, hit: |
| 441 | <C-C>s (show SQL statements) |
| 442 | At this point, you can page down through the list until you find "select". |
| 443 | If you are familiar with the item you are looking for, for example you know |
| 444 | the statement begins with the letter "s". You can type ahead (without the |
| 445 | quotes) "se" then press: |
| 446 | <C-Spact>t |
| 447 | Assuming "select" is highlighted in the popup list press <Enter> to choose |
| 448 | the entry. Now type: |
| 449 | * fr<C-C>a (show all syntax items) |
| 450 | choose "from" from the popup list. |
| 451 | |
| 452 | When writing stored procedures using the "type" list is useful. It contains |
| 453 | a list of all the database supported types. This may or may not be true |
| 454 | depending 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 | |
| 460 | Dynamic features |
| 461 | ---------------- |
| 462 | To take advantage of the dynamic features you must first install the |
| 463 | dbext.vim plugin (http://vim.sourceforge.net/script.php?script_id=356). It |
| 464 | also comes with a tutorial. From the SQL completion plugin's perspective, |
| 465 | the main feature dbext provides is a connection to a database. dbext |
| 466 | connection profiles are the most efficient mechanism to define connection |
| 467 | information. Once connections have been setup, the SQL completion plugin |
| 468 | uses the features of dbext in the background to populate the popups. |
| 469 | |
| 470 | What follows assumes dbext.vim has been correctly configured, a simple test |
| 471 | is to run the command, :DBListTable. If a list of tables is shown, you know |
| 472 | dbext.vim is working as expected. If not, please consult the dbext.txt |
| 473 | documentation. |
| 474 | |
| 475 | Assuming you have followed the |dbext-tutorial| you can press <C-C>t to |
| 476 | display a list of tables. There is a delay while dbext is creating the table |
| 477 | list. After the list is displayed press <C-W>. This will remove both the |
| 478 | popup window and the table name already chosen when the list became active. > |
| 479 | |
| 480 | 4.3.1 Table Completion: *sql-completion-tables* |
| 481 | < |
| 482 | Press <C-C>t to display a list of tables from within the database you |
| 483 | have connected via the dbext plugin. |
| 484 | NOTE: All of the SQL completion popups support typing a prefix before pressing |
| 485 | the key map. This will limit the contents of the popup window to just items |
| 486 | beginning with those characters. > |
| 487 | |
| 488 | 4.3.2 Column Completion: *sql-completion-columns* |
| 489 | < |
| 490 | The SQL completion plugin can also display a list of columns for particular |
| 491 | tables. The column completion is trigger via <C-C>c. |
| 492 | |
| 493 | NOTE: The following example uses <C-Right> to trigger a column list while |
| 494 | the popup window is active. This map is only available on the Windows |
| 495 | platforms since *nix does not recognize CTRL and the right arrow held down |
| 496 | together. If you wish to enable this functionality on a *nix platform choose |
| 497 | a key and create this mapping (see |sql-completion-maps| for further |
| 498 | details 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 | < |
| 501 | Example 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 | |
| 518 | Lets look how we can build a SQL statement dynamically. A select statement |
| 519 | requires a list of columns. There are two ways to build a column list using |
| 520 | the 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 | |
| 549 | There is a special provision when writing select statements. Consider the |
| 550 | following 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 | < |
| 559 | In 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 |
| 561 | popup a list of columns for the customer table. It does this by looking back |
| 562 | to the beginning of the select statement and finding a list of the tables |
| 563 | specified 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" |
| 565 | keyword is also supported, "customer AS c". > |
| 566 | |
| 567 | |
| 568 | 4.3.3 Procedure Completion: *sql-completion-procedures* |
| 569 | < |
| 570 | Similar to the table list, <C-C>p, will display a list of stored |
| 571 | procedures stored within the database. > |
| 572 | |
| 573 | 4.3.4 View Completion: *sql-completion-views* |
| 574 | < |
| 575 | Similar to the table list, <C-C>v, will display a list of views in the |
| 576 | database. |
| 577 | |
| 578 | |
| 579 | 4.4 Completion Customization *sql-completion-customization* |
| 580 | ---------------------------- |
| 581 | |
| 582 | The SQL completion plugin can be customized through various options set in |
| 583 | your |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 | |
| 614 | 4.5 Customizing Maps *sql-completion-maps* |
| 615 | -------------------- |
| 616 | |
| 617 | You can create as many additional key maps as you like. Generally, the maps |
| 618 | will be specifying different syntax highlight groups. |
| 619 | |
| 620 | If you do not wish the default maps created or the key choices do not work on |
| 621 | your platform (often a case on *nix) you define the following variable in |
| 622 | your |vimrc|: > |
| 623 | let g:omni_sql_no_default_maps = 1 |
| 624 | < |
| 625 | Do no edit ftplugin/sql.vim directly! If you change this file your changes |
| 626 | will be over written on future updates. Vim has a special directory structure |
| 627 | that allows you to make customizations without changing the files that are |
| 628 | included with the Vim distribution. If you wish to customize the maps |
| 629 | create an after/ftplugin/sql.vim (see |after-directory|) and place the same |
| 630 | maps from the ftplugin/sql.vim in it using your own key strokes. <C-C> was |
| 631 | chosen since it will work on both Windows and *nix platforms. On the windows |
| 632 | platform you can also use <C-Space> or ALT keys. |
| 633 | |
Bram Moolenaar | 1056d98 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 634 | |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 635 | vim:tw=78:ts=8:ft=help:norl: |