blob: f830965c41591f5768712958a4ec4c5e6c54e10a [file] [log] [blame]
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001" Vim OMNI completion script for SQL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00002" Language: SQL
Bram Moolenaar5c736222010-01-06 20:54:52 +01003" Maintainer: David Fishburn <dfishburn dot vim at gmail dot com>
Bram Moolenaar34feacb2012-12-05 19:01:43 +01004" Version: 14.0
5" Last Change: 2012 Dec 04
6" Homepage: http://www.vim.org/scripts/script.php?script_id=1572
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007" Usage: For detailed help
Bram Moolenaardb7207e2012-02-22 17:30:19 +01008" ":help sql.txt"
9" or ":help ft-sql-omni"
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010" or read $VIMRUNTIME/doc/sql.txt
Bram Moolenaare2f98b92006-03-29 21:18:24 +000011
Bram Moolenaar5c736222010-01-06 20:54:52 +010012" History
Bram Moolenaar34feacb2012-12-05 19:01:43 +010013"
14" Version 14.0 (Dec 2012)
15" - BF: Added check for cpo
16"
17" Version 13.0 (Dec 2012)
18" - NF: When completing column lists or drilling into a table
19" and g:omni_sql_include_owner is enabled, the
20" only the table name would be replaced with the column
21" list instead of the table name and owner (if specified).
22" - NF: When completing column lists using table aliases
23" and g:omni_sql_include_owner is enabled, account
24" for the owner name when looking up the table
25" list instead of the table name and owner (if specified).
26" - BF: When completing column lists or drilling into a table
27" and g:omni_sql_include_owner is enabled, the
28" column list could often not be found for the table.
29" - BF: When OMNI popped up, possibly the wrong word
30" would be replaced for column and column list options.
31"
32" Version 12.0 (Feb 2012)
Bram Moolenaardb7207e2012-02-22 17:30:19 +010033" - Partial column name completion did not work when a table
34" name or table alias was provided (Jonas Enberg).
35" - Improved the handling of column completion. First we match any
36" columns from a previous completion. If not matches are found, we
Bram Moolenaar34feacb2012-12-05 19:01:43 +010037" consider the partial name to be a table or table alias for the
Bram Moolenaardb7207e2012-02-22 17:30:19 +010038" query and attempt to match on it.
39"
Bram Moolenaar34feacb2012-12-05 19:01:43 +010040" Version 11.0 (Jan 2012)
Bram Moolenaardb7207e2012-02-22 17:30:19 +010041" Added g:omni_sql_default_compl_type variable
42" - You can specify which type of completion to default to
43" when pressing <C-X><C-O>. The entire list of available
44" choices can be found in the calls to sqlcomplete#Map in:
45" ftplugin/sql.vim
46"
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +020047" Version 10.0
Bram Moolenaardb7207e2012-02-22 17:30:19 +010048" Updated PreCacheSyntax()
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +020049" - Now returns a List of the syntax items it finds.
50" This allows other plugins / scripts to use this list for their own
51" purposes. In this case XPTemplate can use them for a Choose list.
52" - Verifies the parameters are the correct type and displays a
53" warning if not.
54" - Verifies the parameters are the correct type and displays a
55" warning if not.
Bram Moolenaardb7207e2012-02-22 17:30:19 +010056" Updated SQLCWarningMsg()
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +020057" - Prepends warning message with SQLComplete so you know who issued
58" the warning.
Bram Moolenaardb7207e2012-02-22 17:30:19 +010059" Updated SQLCErrorMsg()
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +020060" - Prepends error message with SQLComplete so you know who issued
61" the error.
Bram Moolenaardb7207e2012-02-22 17:30:19 +010062"
Bram Moolenaar34feacb2012-12-05 19:01:43 +010063" Version 9.0 (May 2010)
Bram Moolenaar00a927d2010-05-14 23:24:24 +020064" This change removes some of the support for tables with spaces in their
Bram Moolenaardb7207e2012-02-22 17:30:19 +010065" names in order to simplify the regexes used to pull out query table
Bram Moolenaar00a927d2010-05-14 23:24:24 +020066" aliases for more robust table name and column name code completion.
67" Full support for "table names with spaces" can be added in again
68" after 7.3.
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +020069"
Bram Moolenaar00a927d2010-05-14 23:24:24 +020070" Version 8.0
Bram Moolenaardb7207e2012-02-22 17:30:19 +010071" Incorrectly re-executed the g:ftplugin_sql_omni_key_right and g:ftplugin_sql_omni_key_left
Bram Moolenaar00a927d2010-05-14 23:24:24 +020072" when drilling in and out of a column list for a table.
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +020073"
Bram Moolenaar34feacb2012-12-05 19:01:43 +010074" Version 7.0 (Jan 2010)
Bram Moolenaar5c736222010-01-06 20:54:52 +010075" Better handling of object names
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +020076"
Bram Moolenaar34feacb2012-12-05 19:01:43 +010077" Version 6.0 (Apr 2008)
Bram Moolenaar5c736222010-01-06 20:54:52 +010078" Supports object names with spaces "my table name"
79"
Bram Moolenaare2f98b92006-03-29 21:18:24 +000080" Set completion with CTRL-X CTRL-O to autoloaded function.
81" This check is in place in case this script is
Bram Moolenaardb7207e2012-02-22 17:30:19 +010082" sourced directly instead of using the autoload feature.
Bram Moolenaare2f98b92006-03-29 21:18:24 +000083if exists('&omnifunc')
84 " Do not set the option if already set since this
85 " results in an E117 warning.
86 if &omnifunc == ""
87 setlocal omnifunc=sqlcomplete#Complete
88 endif
89endif
90
91if exists('g:loaded_sql_completion')
Bram Moolenaardb7207e2012-02-22 17:30:19 +010092 finish
Bram Moolenaare2f98b92006-03-29 21:18:24 +000093endif
Bram Moolenaar34feacb2012-12-05 19:01:43 +010094let g:loaded_sql_completion = 130
95let s:keepcpo= &cpo
96set cpo&vim
Bram Moolenaare2f98b92006-03-29 21:18:24 +000097
98" Maintains filename of dictionary
Bram Moolenaar910f66f2006-04-05 20:41:53 +000099let s:sql_file_table = ""
100let s:sql_file_procedure = ""
101let s:sql_file_view = ""
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000102
103" Define various arrays to be used for caching
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000104let s:tbl_name = []
105let s:tbl_alias = []
106let s:tbl_cols = []
107let s:syn_list = []
108let s:syn_value = []
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100109
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000110" Used in conjunction with the syntaxcomplete plugin
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000111let s:save_inc = ""
112let s:save_exc = ""
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000113if exists('g:omni_syntax_group_include_sql')
114 let s:save_inc = g:omni_syntax_group_include_sql
115endif
116if exists('g:omni_syntax_group_exclude_sql')
117 let s:save_exc = g:omni_syntax_group_exclude_sql
118endif
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100119
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000120" Used with the column list
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000121let s:save_prev_table = ""
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000122
123" Default the option to verify table alias
124if !exists('g:omni_sql_use_tbl_alias')
125 let g:omni_sql_use_tbl_alias = 'a'
126endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000127" Default syntax items to precache
128if !exists('g:omni_sql_precache_syntax_groups')
129 let g:omni_sql_precache_syntax_groups = [
130 \ 'syntax',
131 \ 'sqlKeyword',
132 \ 'sqlFunction',
133 \ 'sqlOption',
134 \ 'sqlType',
135 \ 'sqlStatement'
136 \ ]
137endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000138" Set ignorecase to the ftplugin standard
139if !exists('g:omni_sql_ignorecase')
140 let g:omni_sql_ignorecase = &ignorecase
141endif
142" During table completion, should the table list also
143" include the owner name
144if !exists('g:omni_sql_include_owner')
145 let g:omni_sql_include_owner = 0
146 if exists('g:loaded_dbext')
147 if g:loaded_dbext >= 300
148 " New to dbext 3.00, by default the table lists include the owner
149 " name of the table. This is used when determining how much of
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100150 " whatever has been typed should be replaced as part of the
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000151 " code replacement.
152 let g:omni_sql_include_owner = 1
153 endif
154 endif
155endif
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100156" Default type of completion used when <C-X><C-O> is pressed
157if !exists('g:omni_sql_default_compl_type')
158 let g:omni_sql_default_compl_type = 'table'
159endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000160
161" This function is used for the 'omnifunc' option.
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100162" It is called twice by omni and it is responsible
163" for returning the completion list of items.
164" But it must also determine context of what to complete
165" and what to "replace" with the completion.
166" The a:base, is replaced directly with what the user
167" chooses from the choices.
168" The s:prepend provides context for the completion.
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000169function! sqlcomplete#Complete(findstart, base)
170
171 " Default to table name completion
172 let compl_type = 'table'
173 " Allow maps to specify what type of object completion they want
174 if exists('b:sql_compl_type')
175 let compl_type = b:sql_compl_type
176 endif
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100177 let begindot = 0
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000178
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000179 " First pass through this function determines how much of the line should
180 " be replaced by whatever is chosen from the completion list
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000181 if a:findstart
182 " Locate the start of the item, including "."
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000183 let line = getline('.')
184 let start = col('.') - 1
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000185 let lastword = -1
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000186 " Check if the first character is a ".", for column completion
187 if line[start - 1] == '.'
188 let begindot = 1
189 endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000190 while start > 0
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100191 " Additional code was required to handle objects which
Bram Moolenaar5c736222010-01-06 20:54:52 +0100192 " can contain spaces like "my table name".
193 if line[start - 1] !~ '\(\w\|\.\)'
194 " If the previous character is not a period or word character
195 break
196 " elseif line[start - 1] =~ '\(\w\|\s\+\)'
197 " let start -= 1
198 elseif line[start - 1] =~ '\w'
199 " If the previous character is word character continue back
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000200 let start -= 1
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100201 elseif line[start - 1] =~ '\.' &&
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000202 \ compl_type =~ 'column\|table\|view\|procedure'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100203 " If the previous character is a period and we are completing
204 " an object which can be specified with a period like this:
205 " table_name.column_name
206 " owner_name.table_name
207
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000208 " If lastword has already been set for column completion
209 " break from the loop, since we do not also want to pickup
210 " a table name if it was also supplied.
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100211 " Unless g:omni_sql_include_owner == 1, then we can
212 " include the ownername.
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100213 if lastword != -1 && compl_type == 'column'
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100214 \ && g:omni_sql_include_owner == 0
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000215 break
216 endif
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000217 " If column completion was specified stop at the "." if
218 " a . was specified, otherwise, replace all the way up
219 " to the owner name (if included).
220 if lastword == -1 && compl_type == 'column' && begindot == 1
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000221 let lastword = start
222 endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000223 " If omni_sql_include_owner = 0, do not include the table
224 " name as part of the substitution, so break here
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100225 if lastword == -1 &&
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100226 \ compl_type =~ '\<\(table\|view\|procedure\|column\|column_csv\)\>' &&
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000227 \ g:omni_sql_include_owner == 0
228 let lastword = start
229 break
230 endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000231 let start -= 1
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000232 else
233 break
234 endif
235 endwhile
236
237 " Return the column of the last word, which is going to be changed.
238 " Remember the text that comes before it in s:prepended.
239 if lastword == -1
240 let s:prepended = ''
241 return start
242 endif
243 let s:prepended = strpart(line, start, lastword - start)
244 return lastword
245 endif
246
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000247 " Second pass through this function will determine what data to put inside
248 " of the completion list
249 " s:prepended is set by the first pass
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000250 let base = s:prepended . a:base
251
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000252 " Default the completion list to an empty list
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000253 let compl_list = []
254
255 " Default to table name completion
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100256 let compl_type = g:omni_sql_default_compl_type
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000257 " Allow maps to specify what type of object completion they want
258 if exists('b:sql_compl_type')
259 let compl_type = b:sql_compl_type
260 unlet b:sql_compl_type
261 endif
262
263 if compl_type == 'tableReset'
264 let compl_type = 'table'
265 let base = ''
266 endif
267
268 if compl_type == 'table' ||
269 \ compl_type == 'procedure' ||
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100270 \ compl_type == 'view'
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000271
272 " This type of completion relies upon the dbext.vim plugin
273 if s:SQLCCheck4dbext() == -1
274 return []
275 endif
276
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000277 " Allow the user to override the dbext plugin to specify whether
278 " the owner/creator should be included in the list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000279 if g:loaded_dbext >= 300
280 let saveSetting = DB_listOption('dict_show_owner')
281 exec 'DBSetOption dict_show_owner='.(g:omni_sql_include_owner==1?'1':'0')
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000282 endif
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000283
284 let compl_type_uc = substitute(compl_type, '\w\+', '\u&', '')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100285 " Same call below, no need to do it twice
286 " if s:sql_file_{compl_type} == ""
287 " let s:sql_file_{compl_type} = DB_getDictionaryName(compl_type_uc)
288 " endif
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000289 let s:sql_file_{compl_type} = DB_getDictionaryName(compl_type_uc)
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000290 if s:sql_file_{compl_type} != ""
291 if filereadable(s:sql_file_{compl_type})
292 let compl_list = readfile(s:sql_file_{compl_type})
293 endif
294 endif
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000295
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000296 if g:loaded_dbext > 300
297 exec 'DBSetOption dict_show_owner='.saveSetting
298 endif
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000299 elseif compl_type =~? 'column'
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000300
301 " This type of completion relies upon the dbext.vim plugin
302 if s:SQLCCheck4dbext() == -1
303 return []
304 endif
305
306 if base == ""
307 " The last time we displayed a column list we stored
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100308 " the table name. If the user selects a column list
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000309 " without a table name of alias present, assume they want
310 " the previous column list displayed.
311 let base = s:save_prev_table
312 endif
313
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000314 let owner = ''
315 let column = ''
316
317 if base =~ '\.'
318 " Check if the owner/creator has been specified
319 let owner = matchstr( base, '^\zs.*\ze\..*\..*' )
320 let table = matchstr( base, '^\(.*\.\)\?\zs.*\ze\..*' )
321 let column = matchstr( base, '.*\.\zs.*' )
322
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100323 if g:omni_sql_include_owner == 1 && owner == '' && table != '' && column != ''
324 let owner = table
325 let table = column
326 let column = ''
327 endif
328
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000329 " It is pretty well impossible to determine if the user
330 " has entered:
331 " owner.table
332 " table.column_prefix
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100333 " So there are a couple of things we can do to mitigate
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000334 " this issue.
335 " 1. Check if the dbext plugin has the option turned
336 " on to even allow owners
337 " 2. Based on 1, if the user is showing a table list
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100338 " and the DrillIntoTable (using <Right>) then
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000339 " this will be owner.table. In this case, we can
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100340 " check to see the table.column exists in the
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000341 " cached table list. If it does, then we have
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100342 " determined the user has actually chosen
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000343 " owner.table, not table.column_prefix.
344 let found = -1
345 if g:omni_sql_include_owner == 1 && owner == ''
346 if filereadable(s:sql_file_table)
347 let tbl_list = readfile(s:sql_file_table)
348 let found = index( tbl_list, ((table != '')?(table.'.'):'').column)
349 endif
350 endif
351 " If the table.column was found in the table list, we can safely assume
352 " the owner was not provided and shift the items appropriately.
353 " OR
354 " If the user has indicated not to use table owners at all and
355 " the base ends in a '.' we know they are not providing a column
356 " name, so we can shift the items appropriately.
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100357 " if found != -1 || (g:omni_sql_include_owner == 0 && base !~ '\.$')
358 " let owner = table
359 " let table = column
360 " let column = ''
361 " endif
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000362 else
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100363 " If no "." was provided and the user asked for
364 " column level completion, first attempt the match
365 " on any previous column lists. If the user asked
366 " for a list of columns comma separated, continue as usual.
367 if compl_type == 'column' && s:save_prev_table != ''
368 " The last time we displayed a column list we stored
369 " the table name. If the user selects a column list
370 " without a table name of alias present, assume they want
371 " the previous column list displayed.
372 let table = s:save_prev_table
373 let list_type = ''
374
375 let compl_list = s:SQLCGetColumns(table, list_type)
376 if ! empty(compl_list)
377 " If no column prefix has been provided and the table
378 " name was provided, append it to each of the items
379 " returned.
380 let compl_list = filter(deepcopy(compl_list), 'v:val=~"^'.base.'"' )
381
382 " If not empty, we have a match on columns
383 " return the list
384 if ! empty(compl_list)
385 return compl_list
386 endif
387 endif
388 endif
389 " Since no columns were found to match the base supplied
390 " assume the user is trying to complete the column list
391 " for a table (and or an alias to a table).
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000392 let table = base
393 endif
394
395 " Get anything after the . and consider this the table name
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100396 " If an owner has been specified, then we must consider the
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000397 " base to be a partial column name
398 " let base = matchstr( base, '^\(.*\.\)\?\zs.*' )
399
400 if table != ""
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000401 let s:save_prev_table = base
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000402 let list_type = ''
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000403
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000404 if compl_type == 'column_csv'
405 " Return one array element, with a comma separated
406 " list of values instead of multiple array entries
407 " for each column in the table.
408 let list_type = 'csv'
409 endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000410
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100411 " If we are including the OWNER for the objects, then for
412 " table completion, if we have it, it should be included
413 " as there can be the same table names in a database yet
414 " with different owner names.
415 if g:omni_sql_include_owner == 1 && owner != '' && table != ''
416 let compl_list = s:SQLCGetColumns(owner.'.'.table, list_type)
417 else
418 let compl_list = s:SQLCGetColumns(table, list_type)
419 endif
420
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000421 if column != ''
422 " If no column prefix has been provided and the table
423 " name was provided, append it to each of the items
424 " returned.
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100425 let compl_list = map(compl_list, 'table.".".v:val')
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000426 if owner != ''
427 " If an owner has been provided append it to each of the
428 " items returned.
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100429 let compl_list = map(compl_list, 'owner.".".v:val')
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000430 endif
431 else
432 let base = ''
433 endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000434
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000435 if compl_type == 'column_csv'
436 " Join the column array into 1 single element array
437 " but make the columns column separated
438 let compl_list = [join(compl_list, ', ')]
439 endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000440 endif
441 elseif compl_type == 'resetCache'
442 " Reset all cached items
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100443 let s:tbl_name = []
444 let s:tbl_alias = []
445 let s:tbl_cols = []
446 let s:syn_list = []
447 let s:syn_value = []
448 let s:sql_file_table = ""
449 let s:sql_file_procedure = ""
450 let s:sql_file_view = ""
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000451
452 let msg = "All SQL cached items have been removed."
453 call s:SQLCWarningMsg(msg)
454 " Leave time for the user to read the error message
455 :sleep 2
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000456 else
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000457 let compl_list = s:SQLCGetSyntaxList(compl_type)
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000458 endif
459
460 if base != ''
Bram Moolenaar5c736222010-01-06 20:54:52 +0100461 " Filter the list based on the first few characters the user entered.
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100462 " Check if the text matches at the beginning
463 " \\(^.base.'\\)
464 " or
Bram Moolenaar5c736222010-01-06 20:54:52 +0100465 " Match to a owner.table or alias.column type match
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100466 " ^\\(\\w\\+\\.\\)\\?'.base.'\\)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100467 " or
468 " Handle names with spaces "my table name"
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100469 " "\\(^'.base.'\\|^\\(\\w\\+\\.\\)\\?'.base.'\\)"'
470 "
Bram Moolenaar5c736222010-01-06 20:54:52 +0100471 let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\|^\\(\\w\\+\\.\\)\\?'.base.'\\)"'
472 " let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\)"'
473 " let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\|\\(\\.\\)\\?'.base.'\\)"'
474 " let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\|\\([^.]*\\)\\?'.base.'\\)"'
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000475 let compl_list = filter(deepcopy(compl_list), expr)
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100476
477 if empty(compl_list) && compl_type == 'table' && base =~ '\.$'
478 " It is possible we could be looking for column name completion
479 " and the user simply hit C-X C-O to lets try it as well
480 " since we had no hits with the tables.
481 " If the base ends with a . it is hard to know if we are
482 " completing table names or column names.
483 let list_type = ''
484
485 let compl_list = s:SQLCGetColumns(base, list_type)
486 endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000487 endif
488
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000489 if exists('b:sql_compl_savefunc') && b:sql_compl_savefunc != ""
490 let &omnifunc = b:sql_compl_savefunc
491 endif
492
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100493 if empty(compl_list)
494 call s:SQLCWarningMsg( 'Could not find type['.compl_type.'] using prepend[.'.s:prepended.'] base['.a:base.']' )
495 endif
496
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000497 return compl_list
498endfunc
499
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000500function! sqlcomplete#PreCacheSyntax(...)
501 let syn_group_arr = []
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200502 let syn_items = []
503
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100504 if a:0 > 0
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200505 if type(a:1) != 3
506 call s:SQLCWarningMsg("Parameter is not a list. Example:['syntaxGroup1', 'syntaxGroup2']")
507 return ''
508 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000509 let syn_group_arr = a:1
510 else
511 let syn_group_arr = g:omni_sql_precache_syntax_groups
512 endif
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000513 " For each group specified in the list, precache all
514 " the sytnax items.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000515 if !empty(syn_group_arr)
516 for group_name in syn_group_arr
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200517 let syn_items = extend( syn_items, s:SQLCGetSyntaxList(group_name) )
518 endfor
519 endif
520
521 return syn_items
522endfunction
523
524function! sqlcomplete#ResetCacheSyntax(...)
525 let syn_group_arr = []
526
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100527 if a:0 > 0
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200528 if type(a:1) != 3
529 call s:SQLCWarningMsg("Parameter is not a list. Example:['syntaxGroup1', 'syntaxGroup2']")
530 return ''
531 endif
532 let syn_group_arr = a:1
533 else
534 let syn_group_arr = g:omni_sql_precache_syntax_groups
535 endif
536 " For each group specified in the list, precache all
537 " the sytnax items.
538 if !empty(syn_group_arr)
539 for group_name in syn_group_arr
540 let list_idx = index(s:syn_list, group_name, 0, &ignorecase)
541 if list_idx > -1
542 " Remove from list of groups
543 call remove( s:syn_list, list_idx )
544 " Remove from list of keywords
545 call remove( s:syn_value, list_idx )
546 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000547 endfor
548 endif
549endfunction
550
551function! sqlcomplete#Map(type)
552 " Tell the SQL plugin what you want to complete
553 let b:sql_compl_type=a:type
554 " Record previous omnifunc, if the SQL completion
555 " is being used in conjunction with other filetype
556 " completion plugins
557 if &omnifunc != "" && &omnifunc != 'sqlcomplete#Complete'
558 " Record the previous omnifunc, the plugin
559 " will automatically set this back so that it
560 " does not interfere with other ftplugins settings
561 let b:sql_compl_savefunc=&omnifunc
562 endif
563 " Set the OMNI func for the SQL completion plugin
564 let &omnifunc='sqlcomplete#Complete'
565endfunction
566
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000567function! sqlcomplete#DrillIntoTable()
568 " If the omni popup window is visible
569 if pumvisible()
570 call sqlcomplete#Map('column')
571 " C-Y, makes the currently highlighted entry active
572 " and trigger the omni popup to be redisplayed
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200573 call feedkeys("\<C-Y>\<C-X>\<C-O>", 'n')
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000574 else
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200575 " If the popup is not visible, simple perform the normal
576 " key behaviour.
577 " Must use exec since they key must be preceeded by "\"
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100578 " or feedkeys will simply push each character of the string
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200579 " rather than the "key press".
580 exec 'call feedkeys("\'.g:ftplugin_sql_omni_key_right.'", "n")'
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000581 endif
582 return ""
583endfunction
584
585function! sqlcomplete#DrillOutOfColumns()
586 " If the omni popup window is visible
587 if pumvisible()
588 call sqlcomplete#Map('tableReset')
589 " Trigger the omni popup to be redisplayed
590 call feedkeys("\<C-X>\<C-O>")
591 else
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200592 " If the popup is not visible, simple perform the normal
593 " key behaviour.
594 " Must use exec since they key must be preceeded by "\"
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100595 " or feedkeys will simply push each character of the string
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200596 " rather than the "key press".
597 exec 'call feedkeys("\'.g:ftplugin_sql_omni_key_left.'", "n")'
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000598 endif
599 return ""
600endfunction
601
602function! s:SQLCWarningMsg(msg)
603 echohl WarningMsg
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100604 echomsg 'SQLComplete:'.a:msg
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000605 echohl None
606endfunction
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100607
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000608function! s:SQLCErrorMsg(msg)
609 echohl ErrorMsg
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100610 echomsg 'SQLComplete:'.a:msg
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000611 echohl None
612endfunction
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100613
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000614function! s:SQLCGetSyntaxList(syn_group)
615 let syn_group = a:syn_group
616 let compl_list = []
617
618 " Check if we have already cached the syntax list
619 let list_idx = index(s:syn_list, syn_group, 0, &ignorecase)
620 if list_idx > -1
621 " Return previously cached value
622 let compl_list = s:syn_value[list_idx]
623 else
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100624 " Request the syntax list items from the
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000625 " syntax completion plugin
626 if syn_group == 'syntax'
627 " Handle this special case. This allows the user
628 " to indicate they want all the syntax items available,
629 " so do not specify a specific include list.
630 let g:omni_syntax_group_include_sql = ''
631 else
632 " The user has specified a specific syntax group
633 let g:omni_syntax_group_include_sql = syn_group
634 endif
635 let g:omni_syntax_group_exclude_sql = ''
Bram Moolenaarf9d5ca12010-08-01 16:13:51 +0200636 let syn_value = syntaxcomplete#OmniSyntaxList()
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637 let g:omni_syntax_group_include_sql = s:save_inc
638 let g:omni_syntax_group_exclude_sql = s:save_exc
639 " Cache these values for later use
640 let s:syn_list = add( s:syn_list, syn_group )
641 let s:syn_value = add( s:syn_value, syn_value )
642 let compl_list = syn_value
643 endif
644
645 return compl_list
646endfunction
647
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000648function! s:SQLCCheck4dbext()
649 if !exists('g:loaded_dbext')
650 let msg = "The dbext plugin must be loaded for dynamic SQL completion"
651 call s:SQLCErrorMsg(msg)
652 " Leave time for the user to read the error message
653 :sleep 2
654 return -1
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000655 elseif g:loaded_dbext < 600
656 let msg = "The dbext plugin must be at least version 5.30 " .
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000657 \ " for dynamic SQL completion"
658 call s:SQLCErrorMsg(msg)
659 " Leave time for the user to read the error message
660 :sleep 2
661 return -1
662 endif
663 return 1
664endfunction
665
666function! s:SQLCAddAlias(table_name, table_alias, cols)
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000667 " Strip off the owner if included
668 let table_name = matchstr(a:table_name, '\%(.\{-}\.\)\?\zs\(.*\)' )
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000669 let table_alias = a:table_alias
670 let cols = a:cols
671
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100672 if g:omni_sql_use_tbl_alias != 'n'
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000673 if table_alias == ''
674 if 'da' =~? g:omni_sql_use_tbl_alias
675 if table_name =~ '_'
676 " Treat _ as separators since people often use these
677 " for word separators
678 let save_keyword = &iskeyword
679 setlocal iskeyword-=_
680
681 " Get the first letter of each word
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100682 " [[:alpha:]] is used instead of \w
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000683 " to catch extended accented characters
684 "
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100685 let table_alias = substitute(
686 \ table_name,
687 \ '\<[[:alpha:]]\+\>_\?',
688 \ '\=strpart(submatch(0), 0, 1)',
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000689 \ 'g'
690 \ )
691 " Restore original value
692 let &iskeyword = save_keyword
693 elseif table_name =~ '\u\U'
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000694 let table_alias = substitute(
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000695 \ table_name, '\(\u\)\U*', '\1', 'g')
696 else
697 let table_alias = strpart(table_name, 0, 1)
698 endif
699 endif
700 endif
701 if table_alias != ''
702 " Following a word character, make sure there is a . and no spaces
703 let table_alias = substitute(table_alias, '\w\zs\.\?\s*$', '.', '')
704 if 'a' =~? g:omni_sql_use_tbl_alias && a:table_alias == ''
705 let table_alias = inputdialog("Enter table alias:", table_alias)
706 endif
707 endif
708 if table_alias != ''
709 let cols = substitute(cols, '\<\w', table_alias.'&', 'g')
710 endif
711 endif
712
713 return cols
714endfunction
715
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100716function! s:SQLCGetObjectOwner(object)
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000717 " The owner regex matches a word at the start of the string which is
718 " followed by a dot, but doesn't include the dot in the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000719 " ^ - from beginning of line
720 " \("\|\[\)\? - ignore any quotes
721 " \zs - start the match now
722 " .\{-} - get owner name
723 " \ze - end the match
724 " \("\|\[\)\? - ignore any quotes
725 " \. - must by followed by a .
726 " let owner = matchstr( a:object, '^\s*\zs.*\ze\.' )
727 let owner = matchstr( a:object, '^\("\|\[\)\?\zs\.\{-}\ze\("\|\]\)\?\.' )
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000728 return owner
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100729endfunction
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000730
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000731function! s:SQLCGetColumns(table_name, list_type)
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100732 if a:table_name =~ '\.'
733 " Check if the owner/creator has been specified
734 let owner = matchstr( a:table_name, '^\zs.*\ze\..*\..*' )
735 let table = matchstr( a:table_name, '^\(.*\.\)\?\zs.*\ze\..*' )
736 let column = matchstr( a:table_name, '.*\.\zs.*' )
737
738 if g:omni_sql_include_owner == 1 && owner == '' && table != '' && column != ''
739 let owner = table
740 let table = column
741 let column = ''
742 endif
743 else
744 let owner = ''
745 let table = matchstr(a:table_name, '^["\[\]a-zA-Z0-9_ ]\+\ze\.\?')
746 let column = ''
747 endif
748
Bram Moolenaar83e138c2007-05-05 18:27:07 +0000749 " Check if the table name was provided as part of the column name
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100750 " let table_name = matchstr(a:table_name, '^["\[\]a-zA-Z0-9_ ]\+\ze\.\?')
751 let table_name = table
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000752 let table_cols = []
753 let table_alias = ''
754 let move_to_top = 1
755
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000756 let table_name = substitute(table_name, '\s*\(.\{-}\)\s*$', '\1', 'g')
757
758 " If the table name was given as:
759 " where c.
760 let table_name = substitute(table_name, '^\c\(WHERE\|AND\|OR\)\s\+', '', '')
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000761 if g:loaded_dbext >= 300
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000762 let saveSettingAlias = DB_listOption('use_tbl_alias')
763 exec 'DBSetOption use_tbl_alias=n'
764 endif
765
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000766 let table_name_stripped = substitute(table_name, '["\[\]]*', '', 'g')
767
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000768 " Check if we have already cached the column list for this table
769 " by its name
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000770 let list_idx = index(s:tbl_name, table_name_stripped, 0, &ignorecase)
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000771 if list_idx > -1
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000772 let table_cols = split(s:tbl_cols[list_idx], '\n')
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000773 else
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100774 " Check if we have already cached the column list for this table
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000775 " by its alias, assuming the table_name provided was actually
776 " the alias for the table instead
777 " select *
778 " from area a
779 " where a.
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000780 let list_idx = index(s:tbl_alias, table_name_stripped, 0, &ignorecase)
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000781 if list_idx > -1
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000782 let table_alias = table_name_stripped
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000783 let table_name = s:tbl_name[list_idx]
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000784 let table_cols = split(s:tbl_cols[list_idx], '\n')
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000785 endif
786 endif
787
788 " If we have not found a cached copy of the table
789 " And the table ends in a "." or we are looking for a column list
790 " if list_idx == -1 && (a:table_name =~ '\.' || b:sql_compl_type =~ 'column')
791 " if list_idx == -1 && (a:table_name =~ '\.' || a:list_type =~ 'csv')
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100792 if list_idx == -1
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000793 let saveY = @y
794 let saveSearch = @/
795 let saveWScan = &wrapscan
796 let curline = line(".")
797 let curcol = col(".")
798
799 " Do not let searchs wrap
800 setlocal nowrapscan
801 " If . was entered, look at the word just before the .
802 " We are looking for something like this:
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100803 " select *
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000804 " from customer c
805 " where c.
806 " So when . is pressed, we need to find 'c'
807 "
808
809 " Search backwards to the beginning of the statement
810 " and do NOT wrap
811 " exec 'silent! normal! v?\<\(select\|update\|delete\|;\)\>'."\n".'"yy'
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200812 exec 'silent! normal! ?\<\c\(select\|update\|delete\|;\)\>'."\n"
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000813
814 " Start characterwise visual mode
815 " Advance right one character
816 " Search foward until one of the following:
817 " 1. Another select/update/delete statement
818 " 2. A ; at the end of a line (the delimiter)
819 " 3. The end of the file (incase no delimiter)
820 " Yank the visually selected text into the "y register.
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200821 exec 'silent! normal! vl/\c\(\<select\>\|\<update\>\|\<delete\>\|;\s*$\|\%$\)'."\n".'"yy'
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000822
823 let query = @y
824 let query = substitute(query, "\n", ' ', 'g')
825 let found = 0
826
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200827 " if query =~? '^\c\(select\)'
828 if query =~? '^\(select\|update\|delete\)'
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000829 let found = 1
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100830 " \(\(\<\w\+\>\)\.\)\? -
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200831 " '\c\(from\|join\|,\).\{-}' - Starting at the from clause (case insensitive)
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000832 " '\zs\(\(\<\w\+\>\)\.\)\?' - Get the owner name (optional)
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100833 " '\<\w\+\>\ze' - Get the table name
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000834 " '\s\+\<'.table_name.'\>' - Followed by the alias
835 " '\s*\.\@!.*' - Cannot be followed by a .
836 " '\(\<where\>\|$\)' - Must be followed by a WHERE clause
837 " '.*' - Exclude the rest of the line in the match
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100838 " let table_name_new = matchstr(@y,
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200839 " \ '\c\(from\|join\|,\).\{-}'.
840 " \ '\zs\(\("\|\[\)\?.\{-}\("\|\]\)\.\)\?'.
841 " \ '\("\|\[\)\?.\{-}\("\|\]\)\?\ze'.
842 " \ '\s\+\%(as\s\+\)\?\<'.
843 " \ matchstr(table_name, '.\{-}\ze\.\?$').
844 " \ '\>'.
845 " \ '\s*\.\@!.*'.
846 " \ '\(\<where\>\|$\)'.
847 " \ '.*'
848 " \ )
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100849 "
850 "
851 " ''\c\(\<from\>\|\<join\>\|,\)\s*' - Starting at the from clause (case insensitive)
852 " '\zs\(\("\|\[\)\?\w\+\("\|\]\)\?\.\)\?' - Get the owner name (optional)
853 " '\("\|\[\)\?\w\+\("\|\]\)\?\ze' - Get the table name
854 " '\s\+\%(as\s\+\)\?\<'.matchstr(table_name, '.\{-}\ze\.\?$').'\>' - Followed by the alias
855 " '\s*\.\@!.*' - Cannot be followed by a .
856 " '\(\<where\>\|$\)' - Must be followed by a WHERE clause
857 " '.*' - Exclude the rest of the line in the match
858 let table_name_new = matchstr(@y,
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200859 \ '\c\(\<from\>\|\<join\>\|,\)\s*'.
860 \ '\zs\(\("\|\[\)\?\w\+\("\|\]\)\?\.\)\?'.
861 \ '\("\|\[\)\?\w\+\("\|\]\)\?\ze'.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000862 \ '\s\+\%(as\s\+\)\?\<'.
863 \ matchstr(table_name, '.\{-}\ze\.\?$').
864 \ '\>'.
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000865 \ '\s*\.\@!.*'.
866 \ '\(\<where\>\|$\)'.
867 \ '.*'
868 \ )
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000869
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000870 if table_name_new != ''
871 let table_alias = table_name
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100872 if g:omni_sql_include_owner == 1
873 let table_name = matchstr( table_name_new, '^\zs\(.\{-}\.\)\?\(.\{-}\.\)\?.*\ze' )
874 else
875 " let table_name = matchstr( table_name_new, '^\(.*\.\)\?\zs.*\ze' )
876 let table_name = matchstr( table_name_new, '^\(.\{-}\.\)\?\zs\(.\{-}\.\)\?.*\ze' )
877 endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000878
879 let list_idx = index(s:tbl_name, table_name, 0, &ignorecase)
880 if list_idx > -1
881 let table_cols = split(s:tbl_cols[list_idx])
882 let s:tbl_name[list_idx] = table_name
883 let s:tbl_alias[list_idx] = table_alias
884 else
885 let list_idx = index(s:tbl_alias, table_name, 0, &ignorecase)
886 if list_idx > -1
887 let table_cols = split(s:tbl_cols[list_idx])
888 let s:tbl_name[list_idx] = table_name
889 let s:tbl_alias[list_idx] = table_alias
890 endif
891 endif
892
893 endif
894 else
895 " Simply assume it is a table name provided with a . on the end
896 let found = 1
897 endif
898
899 let @y = saveY
900 let @/ = saveSearch
901 let &wrapscan = saveWScan
902
903 " Return to previous location
904 call cursor(curline, curcol)
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100905
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000906 if found == 0
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000907 if g:loaded_dbext > 300
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000908 exec 'DBSetOption use_tbl_alias='.saveSettingAlias
909 endif
910
911 " Not a SQL statement, do not display a list
912 return []
913 endif
Bram Moolenaardb7207e2012-02-22 17:30:19 +0100914 endif
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000915
916 if empty(table_cols)
917 " Specify silent mode, no messages to the user (tbl, 1)
918 " Specify do not comma separate (tbl, 1, 1)
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100919 " let table_cols_str = DB_getListColumn(table_name, 1, 1)
920 let table_cols_str = DB_getListColumn((owner!=''?owner.'.':'').table_name, 1, 1)
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000921
922 if table_cols_str != ""
923 let s:tbl_name = add( s:tbl_name, table_name )
924 let s:tbl_alias = add( s:tbl_alias, table_alias )
925 let s:tbl_cols = add( s:tbl_cols, table_cols_str )
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000926 let table_cols = split(table_cols_str, '\n')
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000927 endif
928
929 endif
930
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000931 if g:loaded_dbext > 300
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000932 exec 'DBSetOption use_tbl_alias='.saveSettingAlias
933 endif
934
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000935 " If the user has asked for a comma separate list of column
936 " values, ask the user if they want to prepend each column
937 " with a tablename alias.
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000938 if a:list_type == 'csv' && !empty(table_cols)
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000939 let cols = join(table_cols, ', ')
940 let cols = s:SQLCAddAlias(table_name, table_alias, cols)
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000941 let table_cols = [cols]
942 endif
943
944 return table_cols
945endfunction
Bram Moolenaar34feacb2012-12-05 19:01:43 +0100946" Restore:
947let &cpo= s:keepcpo
948unlet s:keepcpo
949" vim: ts=4 fdm=marker