Permalink
Browse files

Version 7.0

Version 6.0 added support for table names which had "spaces in them".  Unforunately, this broke a few things in the parsing of statements to determine which table to retrieve column names.  This version corrects those mistakes and makes the object names a bit more robust.
  • Loading branch information...
1 parent 70f2b19 commit 0ed37568677e8e49fb75bfc9f1361579dafcfa5b @dfishburn dfishburn committed with Jan 25, 2010
Showing with 46 additions and 20 deletions.
  1. +46 −20 plugin/sqlcomplete.vim
View
@@ -1,13 +1,19 @@
" Vim OMNI completion script for SQL
" Language: SQL
-" Maintainer: David Fishburn <fishburn@ianywhere.com>
-" Version: 6.0
-" Last Change: Thu 03 Apr 2008 10:37:54 PM Eastern Daylight Time
+" Maintainer: David Fishburn <dfishburn dot vim at gmail dot com>
+" Version: 7.0
+" Last Change: 2009 Nov 17
" Usage: For detailed help
" ":help sql.txt"
" or ":help ft-sql-omni"
-" or read $VIMRUNTIME/doc/sql.txt
-
+" or readVIMRUNTIME/doc/sql.txt
+
+" History
+" Version 7.0
+" Better handling of object names
+" Version 6.0
+" Supports object names with spaces "my table name"
+"
" Set completion with CTRL-X CTRL-O to autoloaded function.
" This check is in place in case this script is
" sourced directly instead of using the autoload feature.
@@ -22,7 +28,7 @@ endif
if exists('g:loaded_sql_completion')
finish
endif
-let g:loaded_sql_completion = 50
+let g:loaded_sql_completion = 70
" Maintains filename of dictionary
let s:sql_file_table = ""
@@ -106,10 +112,23 @@ function! sqlcomplete#Complete(findstart, base)
let begindot = 1
endif
while start > 0
- if line[start - 1] =~ '\(\w\|\s\+\)'
+ " Additional code was required to handle objects which
+ " can contain spaces like "my table name".
+ if line[start - 1] !~ '\(\w\|\.\)'
+ " If the previous character is not a period or word character
+ break
+ " elseif line[start - 1] =~ '\(\w\|\s\+\)'
+ " let start -= 1
+ elseif line[start - 1] =~ '\w'
+ " If the previous character is word character continue back
let start -= 1
elseif line[start - 1] =~ '\.' &&
\ compl_type =~ 'column\|table\|view\|procedure'
+ " If the previous character is a period and we are completing
+ " an object which can be specified with a period like this:
+ " table_name.column_name
+ " owner_name.table_name
+
" If lastword has already been set for column completion
" break from the loop, since we do not also want to pickup
" a table name if it was also supplied.
@@ -184,9 +203,10 @@ function! sqlcomplete#Complete(findstart, base)
endif
let compl_type_uc = substitute(compl_type, '\w\+', '\u&', '')
- if s:sql_file_{compl_type} == ""
- let s:sql_file_{compl_type} = DB_getDictionaryName(compl_type_uc)
- endif
+ " Same call below, no need to do it twice
+ " if s:sql_file_{compl_type} == ""
+ " let s:sql_file_{compl_type} = DB_getDictionaryName(compl_type_uc)
+ " endif
let s:sql_file_{compl_type} = DB_getDictionaryName(compl_type_uc)
if s:sql_file_{compl_type} != ""
if filereadable(s:sql_file_{compl_type})
@@ -312,9 +332,16 @@ function! sqlcomplete#Complete(findstart, base)
endif
if base != ''
- " Filter the list based on the first few characters the user
- " entered
- let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\|\\([^.]*\\)\\?'.base.'\\)"'
+ " Filter the list based on the first few characters the user entered.
+ " Check if the text matches at the beginning
+ " or
+ " Match to a owner.table or alias.column type match
+ " or
+ " Handle names with spaces "my table name"
+ let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\|^\\(\\w\\+\\.\\)\\?'.base.'\\)"'
+ " let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\)"'
+ " let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\|\\(\\.\\)\\?'.base.'\\)"'
+ " let expr = 'v:val '.(g:omni_sql_ignorecase==1?'=~?':'=~#').' "\\(^'.base.'\\|\\([^.]*\\)\\?'.base.'\\)"'
let compl_list = filter(deepcopy(compl_list), expr)
endif
@@ -582,7 +609,7 @@ function! s:SQLCGetColumns(table_name, list_type)
" Search backwards to the beginning of the statement
" and do NOT wrap
" exec 'silent! normal! v?\<\(select\|update\|delete\|;\)\>'."\n".'"yy'
- exec 'silent! normal! ?\<\(select\|update\|delete\|;\)\>'."\n"
+ exec 'silent! normal! ?\<\c\(select\|update\|delete\|;\)\>'."\n"
" Start characterwise visual mode
" Advance right one character
@@ -591,14 +618,14 @@ function! s:SQLCGetColumns(table_name, list_type)
" 2. A ; at the end of a line (the delimiter)
" 3. The end of the file (incase no delimiter)
" Yank the visually selected text into the "y register.
- exec 'silent! normal! vl/\(\<select\>\|\<update\>\|\<delete\>\|;\s*$\|\%$\)'."\n".'"yy'
+ exec 'silent! normal! vl/\c\(\<select\>\|\<update\>\|\<delete\>\|;\s*$\|\%$\)'."\n".'"yy'
let query = @y
let query = substitute(query, "\n", ' ', 'g')
let found = 0
- " if query =~? '^\(select\|update\|delete\)'
- if query =~? '^\(select\)'
+ " if query =~? '^\c\(select\)'
+ if query =~? '^\(select\|update\|delete\)'
let found = 1
" \(\(\<\w\+\>\)\.\)\? -
" 'from.\{-}' - Starting at the from clause
@@ -609,9 +636,9 @@ function! s:SQLCGetColumns(table_name, list_type)
" '\(\<where\>\|$\)' - Must be followed by a WHERE clause
" '.*' - Exclude the rest of the line in the match
let table_name_new = matchstr(@y,
- \ 'from.\{-}'.
+ \ '\cfrom.\{-}'.
\ '\zs\(\("\|\[\)\?.\{-}\("\|\]\)\.\)\?'.
- \ '\("\|\[\)\?.\{-}\("\|\]\)\ze'.
+ \ '\("\|\[\)\?.\{-}\("\|\]\)\?\ze'.
\ '\s\+\%(as\s\+\)\?\<'.
\ matchstr(table_name, '.\{-}\ze\.\?$').
\ '\>'.
@@ -690,4 +717,3 @@ function! s:SQLCGetColumns(table_name, list_type)
return table_cols
endfunction
-

0 comments on commit 0ed3756

Please sign in to comment.