ft_sql.txt 30 KB

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