ft_sql.txt 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. *ft_sql.txt* Nvim
  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 (https://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 |init.vim| 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 |init.vim|.
  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|. You can use the <Tab>
  196. key to complete the optional parameter.
  197. After typing the function name and a space, you can use the completion to
  198. supply a parameter. The function takes the name of the Vim script you want to
  199. source. Using the |cmdline-completion| feature, the SQLSetType function will
  200. search the |'runtimepath'| for all Vim scripts with a name containing "sql".
  201. This takes the guess work out of the spelling of the names. The following are
  202. examples: >
  203. :SQLSetType
  204. :SQLSetType sqloracle
  205. :SQLSetType sqlanywhere
  206. :SQLSetType sqlinformix
  207. :SQLSetType mysql
  208. The easiest approach is to the use <Tab> character which will first complete
  209. the command name (SQLSetType), after a space and another <Tab>, display a list
  210. of available Vim script names: >
  211. :SQL<Tab><space><Tab>
  212. 2.2 SQLGetType *sqlgettype* *SQLGetType*
  213. --------------
  214. At anytime you can determine which SQL dialect you are using by calling the
  215. SQLGetType command. The ftplugin/sql.vim file defines this function: >
  216. SQLGetType
  217. This will echo: >
  218. Current SQL dialect in use:sqlanywhere
  219. 2.3 SQL Dialect Default *sql-type-default*
  220. -----------------------
  221. As mentioned earlier, the default syntax rules for Vim is based on Oracle
  222. (PL/SQL). You can override this default by placing one of the following in
  223. your |init.vim|: >
  224. let g:sql_type_default = 'sqlanywhere'
  225. let g:sql_type_default = 'sqlinformix'
  226. let g:sql_type_default = 'mysql'
  227. If you added the following to your |init.vim|: >
  228. let g:sql_type_default = 'sqlinformix'
  229. The next time edit a SQL file the following scripts will be automatically
  230. loaded by Vim: >
  231. ftplugin/sql.vim
  232. syntax/sqlinformix.vim
  233. indent/sql.vim
  234. >
  235. Notice indent/sqlinformix.sql was not loaded. There is no indent file
  236. for Informix, Vim loads the default files if the specified files does not
  237. exist.
  238. ==============================================================================
  239. 3. Adding new SQL Dialects *sql-adding-dialects*
  240. If you begin working with a SQL dialect which does not have any customizations
  241. available with the default Vim distribution you can check https://www.vim.org
  242. to see if any customization currently exist. If not, you can begin by cloning
  243. an existing script. Read |filetype-plugins| for more details.
  244. To help identify these scripts, try to create the files with a "sql" prefix.
  245. If you decide you wish to create customizations for the SQLite database, you
  246. can create any of the following: >
  247. Unix
  248. ~/.config/nvim/syntax/sqlite.vim
  249. ~/.config/nvim/indent/sqlite.vim
  250. No changes are necessary to the SQLSetType function. It will automatically
  251. pick up the new SQL files and load them when you issue the SQLSetType command.
  252. ==============================================================================
  253. 4. OMNI SQL Completion *sql-completion*
  254. *omni-sql-completion*
  255. Vim 7 includes a code completion interface and functions which allows plugin
  256. developers to build in code completion for any language. Vim 7 includes
  257. code completion for the SQL language.
  258. There are two modes to the SQL completion plugin, static and dynamic. The
  259. static mode populates the popups with the data generated from current syntax
  260. highlight rules. The dynamic mode populates the popups with data retrieved
  261. directly from a database. This includes, table lists, column lists,
  262. procedures names and more.
  263. 4.1 Static Mode *sql-completion-static*
  264. ---------------
  265. The static popups created contain items defined by the active syntax rules
  266. while editing a file with a filetype of SQL. The plugin defines (by default)
  267. various maps to help the user refine the list of items to be displayed.
  268. The defaults static maps are: >
  269. imap <buffer> <C-C>a <C-\><C-O>:call sqlcomplete#Map('syntax')<CR><C-X><C-O>
  270. imap <buffer> <C-C>k <C-\><C-O>:call sqlcomplete#Map('sqlKeyword')<CR><C-X><C-O>
  271. imap <buffer> <C-C>f <C-\><C-O>:call sqlcomplete#Map('sqlFunction')<CR><C-X><C-O>
  272. imap <buffer> <C-C>o <C-\><C-O>:call sqlcomplete#Map('sqlOption')<CR><C-X><C-O>
  273. imap <buffer> <C-C>T <C-\><C-O>:call sqlcomplete#Map('sqlType')<CR><C-X><C-O>
  274. imap <buffer> <C-C>s <C-\><C-O>:call sqlcomplete#Map('sqlStatement')<CR><C-X><C-O>
  275. The use of "<C-C>" can be user chosen by using the following in your |init.vim|
  276. as it may not work properly on all platforms: >
  277. let g:ftplugin_sql_omni_key = '<C-C>'
  278. >
  279. The static maps (which are based on the syntax highlight groups) follow this
  280. format: >
  281. imap <buffer> <C-C>k <C-\><C-O>:call sqlcomplete#Map('sqlKeyword')<CR><C-X><C-O>
  282. imap <buffer> <C-C>k <C-\><C-O>:call sqlcomplete#Map('sqlKeyword\w*')<CR><C-X><C-O>
  283. This command breaks down as: >
  284. imap - Create an insert map
  285. <buffer> - Only for this buffer
  286. <C-C>k - Your choice of key map
  287. <C-\><C-O> - Execute one command, return to Insert mode
  288. :call sqlcomplete#Map( - Allows the SQL completion plugin to perform some
  289. housekeeping functions to allow it to be used in
  290. conjunction with other completion plugins.
  291. Indicate which item you want the SQL completion
  292. plugin to complete.
  293. In this case we are asking the plugin to display
  294. items from the syntax highlight group
  295. 'sqlKeyword'.
  296. You can view a list of highlight group names to
  297. choose from by executing the
  298. :syntax list
  299. command while editing a SQL file.
  300. 'sqlKeyword' - Display the items for the sqlKeyword highlight
  301. group
  302. 'sqlKeyword\w*' - A second option available with Vim 7.4 which
  303. uses a regular expression to determine which
  304. syntax groups to use
  305. )<CR> - Execute the :let command
  306. <C-X><C-O> - Trigger the standard omni completion key stroke.
  307. Passing in 'sqlKeyword' instructs the SQL
  308. completion plugin to populate the popup with
  309. items from the sqlKeyword highlight group. The
  310. plugin will also cache this result until Vim is
  311. restarted. The syntax list is retrieved using
  312. the syntaxcomplete plugin.
  313. Using the 'syntax' keyword is a special case. This instructs the
  314. syntaxcomplete plugin to retrieve all syntax items. So this will effectively
  315. work for any of Vim's SQL syntax files. At the time of writing this includes
  316. 10 different syntax files for the different dialects of SQL (see section 3
  317. above, |sql-dialects|).
  318. Here are some examples of the entries which are pulled from the syntax files: >
  319. All
  320. - Contains the contents of all syntax highlight groups
  321. Statements
  322. - Select, Insert, Update, Delete, Create, Alter, ...
  323. Functions
  324. - Min, Max, Trim, Round, Date, ...
  325. Keywords
  326. - Index, Database, Having, Group, With
  327. Options
  328. - Isolation_level, On_error, Qualify_owners, Fire_triggers, ...
  329. Types
  330. - Integer, Char, Varchar, Date, DateTime, Timestamp, ...
  331. 4.2 Dynamic Mode *sql-completion-dynamic*
  332. ----------------
  333. Dynamic mode populates the popups with data directly from a database. In
  334. order for the dynamic feature to be enabled you must have the dbext.vim
  335. plugin installed, (https://vim.sourceforge.net/script.php?script_id=356).
  336. Dynamic mode is used by several features of the SQL completion plugin.
  337. After installing the dbext plugin see the dbext-tutorial for additional
  338. configuration and usage. The dbext plugin allows the SQL completion plugin
  339. to display a list of tables, procedures, views and columns. >
  340. Table List
  341. - All tables for all schema owners
  342. Procedure List
  343. - All stored procedures for all schema owners
  344. View List
  345. - All stored procedures for all schema owners
  346. Column List
  347. - For the selected table, the columns that are part of the table
  348. To enable the popup, while in INSERT mode, use the following key combinations
  349. for each group (where <C-C> means hold the CTRL key down while pressing
  350. the space bar):
  351. Table List - <C-C>t
  352. - <C-X><C-O> (the default map assumes tables)
  353. Stored Procedure List - <C-C>p
  354. View List - <C-C>v
  355. Column List - <C-C>c
  356. Drilling In / Out - When viewing a popup window displaying the list
  357. of tables, you can press <Right>, this will
  358. replace the table currently highlighted with
  359. the column list for that table.
  360. - When viewing a popup window displaying the list
  361. of columns, you can press <Left>, this will
  362. replace the column list with the list of tables.
  363. - This allows you to quickly drill down into a
  364. table to view its columns and back again.
  365. - <Right> and <Left> can also be chosen via
  366. your |init.vim| >
  367. let g:ftplugin_sql_omni_key_right = '<Right>'
  368. let g:ftplugin_sql_omni_key_left = '<Left>'
  369. The SQL completion plugin caches various lists that are displayed in
  370. the popup window. This makes the re-displaying of these lists very
  371. fast. If new tables or columns are added to the database it may become
  372. necessary to clear the plugins cache. The default map for this is: >
  373. imap <buffer> <C-C>R <C-\><C-O>:call sqlcomplete#Map('ResetCache')<CR><C-X><C-O>
  374. 4.3 SQL Tutorial *sql-completion-tutorial*
  375. ----------------
  376. This tutorial is designed to take you through the common features of the SQL
  377. completion plugin so that: >
  378. a) You gain familiarity with the plugin
  379. b) You are introduced to some of the more common features
  380. c) Show how to customize it to your preferences
  381. d) Demonstrate "Best of Use" of the plugin (easiest way to configure).
  382. First, create a new buffer: >
  383. :e tutorial.sql
  384. Static features
  385. ---------------
  386. To take you through the various lists, simply enter insert mode, hit:
  387. <C-C>s (show SQL statements)
  388. At this point, you can page down through the list until you find "select".
  389. If you are familiar with the item you are looking for, for example you know
  390. the statement begins with the letter "s". You can type ahead (without the
  391. quotes) "se" then press:
  392. <C-Space>t
  393. Assuming "select" is highlighted in the popup list press <Enter> to choose
  394. the entry. Now type:
  395. * fr<C-C>a (show all syntax items)
  396. choose "from" from the popup list.
  397. When writing stored procedures using the "type" list is useful. It contains
  398. a list of all the database supported types. This may or may not be true
  399. depending on the syntax file you are using. The SQL Anywhere syntax file
  400. (sqlanywhere.vim) has support for this: >
  401. BEGIN
  402. DECLARE customer_id <C-C>T <-- Choose a type from the list
  403. Dynamic features
  404. ----------------
  405. To take advantage of the dynamic features you must first install the
  406. dbext.vim plugin (https://vim.sourceforge.net/script.php?script_id=356). It
  407. also comes with a tutorial. From the SQL completion plugin's perspective,
  408. the main feature dbext provides is a connection to a database. dbext
  409. connection profiles are the most efficient mechanism to define connection
  410. information. Once connections have been setup, the SQL completion plugin
  411. uses the features of dbext in the background to populate the popups.
  412. What follows assumes dbext.vim has been correctly configured, a simple test
  413. is to run the command, :DBListTable. If a list of tables is shown, you know
  414. dbext.vim is working as expected. If not, please consult the dbext.txt
  415. documentation.
  416. Assuming you have followed the dbext-tutorial you can press <C-C>t to
  417. display a list of tables. There is a delay while dbext is creating the table
  418. list. After the list is displayed press <C-W>. This will remove both the
  419. popup window and the table name already chosen when the list became active.
  420. 4.3.1 Table Completion: *sql-completion-tables*
  421. Press <C-C>t to display a list of tables from within the database you
  422. have connected via the dbext plugin.
  423. NOTE: All of the SQL completion popups support typing a prefix before pressing
  424. the key map. This will limit the contents of the popup window to just items
  425. beginning with those characters.
  426. 4.3.2 Column Completion: *sql-completion-columns*
  427. The SQL completion plugin can also display a list of columns for particular
  428. tables. The column completion is triggered via <C-C>c.
  429. NOTE: The following example uses <Right> to trigger a column list while
  430. the popup window is active.
  431. Example of using column completion:
  432. - Press <C-C>t again to display the list of tables.
  433. - When the list is displayed in the completion window, press <Right>,
  434. this will replace the list of tables, with a list of columns for the
  435. table highlighted (after the same short delay).
  436. - If you press <Left>, this will again replace the column list with the
  437. list of tables. This allows you to drill into tables and column lists
  438. very quickly.
  439. - Press <Right> again while the same table is highlighted. You will
  440. notice there is no delay since the column list has been cached. If you
  441. change the schema of a cached table you can press <C-C>R, which
  442. clears the SQL completion cache.
  443. - NOTE: <Right> and <Left> have been designed to work while the
  444. completion window is active. If the completion popup window is
  445. not active, a normal <Right> or <Left> will be executed.
  446. Let's look at how we can build a SQL statement dynamically. A select statement
  447. requires a list of columns. There are two ways to build a column list using
  448. the SQL completion plugin. >
  449. One column at a time:
  450. < 1. After typing SELECT press <C-C>t to display a list of tables.
  451. 2. Choose a table from the list.
  452. 3. Press <Right> to display a list of columns.
  453. 4. Choose the column from the list and press enter.
  454. 5. Enter a "," and press <C-C>c. Generating a column list
  455. generally requires having the cursor on a table name. The plugin
  456. uses this name to determine what table to retrieve the column list.
  457. In this step, since we are pressing <C-C>c without the cursor
  458. on a table name the column list displayed will be for the previous
  459. table. Choose a different column and move on.
  460. 6. Repeat step 5 as often as necessary. >
  461. All columns for a table:
  462. < 1. After typing SELECT press <C-C>t to display a list of tables.
  463. 2. Highlight the table you need the column list for.
  464. 3. Press <Enter> to choose the table from the list.
  465. 4. Press <C-C>l to request a comma-separated list of all columns
  466. for this table.
  467. 5. Based on the table name chosen in step 3, the plugin attempts to
  468. decide on a reasonable table alias. You are then prompted to
  469. either accept of change the alias. Press OK.
  470. 6. The table name is replaced with the column list of the table is
  471. replaced with the comma separate list of columns with the alias
  472. prepended to each of the columns.
  473. 7. Step 3 and 4 can be replaced by pressing <C-C>L, which has
  474. a <C-Y> embedded in the map to choose the currently highlighted
  475. table in the list.
  476. There is a special provision when writing select statements. Consider the
  477. following statement: >
  478. select *
  479. from customer c,
  480. contact cn,
  481. department as dp,
  482. employee e,
  483. site_options so
  484. where c.
  485. In INSERT mode after typing the final "c." which is an alias for the
  486. "customer" table, you can press either <C-C>c or <C-X><C-O>. This will
  487. popup a list of columns for the customer table. It does this by looking back
  488. to the beginning of the select statement and finding a list of the tables
  489. specified in the FROM clause. In this case it notes that in the string
  490. "customer c", "c" is an alias for the customer table. The optional "AS"
  491. keyword is also supported, "customer AS c".
  492. 4.3.3 Procedure Completion: *sql-completion-procedures*
  493. Similar to the table list, <C-C>p, will display a list of stored
  494. procedures stored within the database.
  495. 4.3.4 View Completion: *sql-completion-views*
  496. Similar to the table list, <C-C>v, will display a list of views in the
  497. database.
  498. 4.4 Completion Customization *sql-completion-customization*
  499. ----------------------------
  500. The SQL completion plugin can be customized through various options set in
  501. your |init.vim|: >
  502. omni_sql_no_default_maps
  503. < - Default: This variable is not defined
  504. - If this variable is defined, no maps are created for OMNI
  505. completion. See |sql-completion-maps| for further discussion.
  506. >
  507. omni_sql_use_tbl_alias
  508. < - Default: a
  509. - This setting is only used when generating a comma-separated
  510. column list. By default the map is <C-C>l. When generating
  511. a column list, an alias can be prepended to the beginning of each
  512. column, for example: e.emp_id, e.emp_name. This option has three
  513. settings: >
  514. n - do not use an alias
  515. d - use the default (calculated) alias
  516. a - ask to confirm the alias name
  517. <
  518. An alias is determined following a few rules:
  519. 1. If the table name has an '_', then use it as a separator: >
  520. MY_TABLE_NAME --> MTN
  521. my_table_name --> mtn
  522. My_table_NAME --> MtN
  523. < 2. If the table name does NOT contain an '_', but DOES use
  524. mixed case then the case is used as a separator: >
  525. MyTableName --> MTN
  526. < 3. If the table name does NOT contain an '_', and does NOT
  527. use mixed case then the first letter of the table is used: >
  528. mytablename --> m
  529. MYTABLENAME --> M
  530. omni_sql_ignorecase
  531. < - Default: Current setting for 'ignorecase'
  532. - Valid settings are 0 or 1.
  533. - When entering a few letters before initiating completion, the list
  534. will be filtered to display only the entries which begin with the
  535. list of characters. When this option is set to 0, the list will be
  536. filtered using case sensitivity. >
  537. omni_sql_include_owner
  538. < - Default: 0, unless dbext.vim 3.00 has been installed
  539. - Valid settings are 0 or 1.
  540. - When completing tables, procedure or views and using dbext.vim 3.00
  541. or higher the list of objects will also include the owner name.
  542. When completing these objects and omni_sql_include_owner is enabled
  543. the owner name will be replaced. >
  544. omni_sql_precache_syntax_groups
  545. < - Default:
  546. ['syntax','sqlKeyword','sqlFunction','sqlOption','sqlType','sqlStatement']
  547. - sqlcomplete can be used in conjunction with other completion
  548. plugins. This is outlined at |sql-completion-filetypes|. When the
  549. filetype is changed temporarily to SQL, the sqlcompletion plugin
  550. will cache the syntax groups listed in the List specified in this
  551. option.
  552. >
  553. 4.5 SQL Maps *sql-completion-maps*
  554. ------------
  555. The default SQL maps have been described in other sections of this document in
  556. greater detail. Here is a list of the maps with a brief description of each.
  557. Static Maps
  558. -----------
  559. These are maps which use populate the completion list using Vim's syntax
  560. highlighting rules. >
  561. <C-C>a
  562. < - Displays all SQL syntax items. >
  563. <C-C>k
  564. < - Displays all SQL syntax items defined as 'sqlKeyword'. >
  565. <C-C>f
  566. < - Displays all SQL syntax items defined as 'sqlFunction. >
  567. <C-C>o
  568. < - Displays all SQL syntax items defined as 'sqlOption'. >
  569. <C-C>T
  570. < - Displays all SQL syntax items defined as 'sqlType'. >
  571. <C-C>s
  572. < - Displays all SQL syntax items defined as 'sqlStatement'. >
  573. Dynamic Maps
  574. ------------
  575. These are maps which use populate the completion list using the dbext.vim
  576. plugin. >
  577. <C-C>t
  578. < - Displays a list of tables. >
  579. <C-C>p
  580. < - Displays a list of procedures. >
  581. <C-C>v
  582. < - Displays a list of views. >
  583. <C-C>c
  584. < - Displays a list of columns for a specific table. >
  585. <C-C>l
  586. < - Displays a comma-separated list of columns for a specific table. >
  587. <C-C>L
  588. < - Displays a comma-separated list of columns for a specific table.
  589. This should only be used when the completion window is active. >
  590. <Right>
  591. < - Displays a list of columns for the table currently highlighted in
  592. the completion window. <Right> is not recognized on most Unix
  593. systems, so this maps is only created on the Windows platform.
  594. If you would like the same feature on Unix, choose a different key
  595. and make the same map in your vimrc. >
  596. <Left>
  597. < - Displays the list of tables.
  598. <Left> is not recognized on most Unix systems, so this maps is
  599. only created on the Windows platform. If you would like the same
  600. feature on Unix, choose a different key and make the same map in
  601. your vimrc. >
  602. <C-C>R
  603. < - This maps removes all cached items and forces the SQL completion
  604. to regenerate the list of items.
  605. Customizing Maps
  606. ----------------
  607. You can create as many additional key maps as you like. Generally, the maps
  608. will be specifying different syntax highlight groups.
  609. If you do not wish the default maps created or the key choices do not work on
  610. your platform (often a case on *nix) you define the following variable in
  611. your |init.vim|: >
  612. let g:omni_sql_no_default_maps = 1
  613. Do not edit ftplugin/sql.vim directly! If you change this file your changes
  614. will be over written on future updates. Vim has a special directory structure
  615. which allows you to make customizations without changing the files that are
  616. included with the Vim distribution. If you wish to customize the maps
  617. create an after/ftplugin/sql.vim (see |after-directory|) and place the same
  618. maps from the ftplugin/sql.vim in it using your own key strokes. <C-C> was
  619. chosen since it will work on both Windows and *nix platforms. On the windows
  620. platform you can also use <C-Space> or ALT keys.
  621. 4.6 Using with other filetypes *sql-completion-filetypes*
  622. ------------------------------
  623. Many times SQL can be used with different filetypes. For example Perl, Java,
  624. PHP, Javascript can all interact with a database. Often you need both the SQL
  625. completion and the completion capabilities for the current language you are
  626. editing.
  627. This can be enabled easily with the following steps (assuming a Perl file): >
  628. 1. :e test.pl
  629. 2. :set filetype=sql
  630. 3. :set ft=perl
  631. Step 1
  632. ------
  633. Begins by editing a Perl file. Vim automatically sets the filetype to
  634. "perl". By default, Vim runs the appropriate filetype file
  635. ftplugin/perl.vim. If you are using the syntax completion plugin by following
  636. the directions at |ft-syntax-omni| then the |'omnifunc'| option has been set to
  637. "syntax#Complete". Pressing <C-X><C-O> will display the omni popup containing
  638. the syntax items for Perl.
  639. Step 2
  640. ------
  641. Manually setting the filetype to "sql" will also fire the appropriate filetype
  642. files ftplugin/sql.vim. This file will define a number of buffer specific
  643. maps for SQL completion, see |sql-completion-maps|. Now these maps have
  644. been created and the SQL completion plugin has been initialized. All SQL
  645. syntax items have been cached in preparation. The SQL filetype script detects
  646. we are attempting to use two different completion plugins. Since the SQL maps
  647. begin with <C-C>, the maps will toggle the |'omnifunc'| when in use. So you
  648. can use <C-X><C-O> to continue using the completion for Perl (using the syntax
  649. completion plugin) and <C-C> to use the SQL completion features.
  650. Step 3
  651. ------
  652. Setting the filetype back to Perl sets all the usual "perl" related items back
  653. as they were.
  654. vim:tw=78:ts=8:noet:ft=help:norl: