1
1
" dbext.vim - Commn Database Utility
2
2
" Copyright (C) 2002-10, Peter Bagyinszki, David Fishburn
3
3
" ---------------------------------------------------------------
4
- " Version: 13 .00
4
+ " Version: 14 .00
5
5
" Maintainer: David Fishburn <dfishburn dot vim at gmail dot com>
6
6
" Authors: Peter Bagyinszki <petike1 at dpg dot hu>
7
7
" David Fishburn <dfishburn dot vim at gmail dot com>
8
- " Last Modified: 2011 May 31
8
+ " Last Modified: 2012 Mar 23
9
9
" Based On: sqlplus.vim (author: Jamis Buck)
10
10
" Created: 2002-05-24
11
11
" Homepage: http://vim.sourceforge.net/script.php?script_id=356
@@ -38,7 +38,11 @@ if v:version < 700
38
38
echomsg " dbext: Version 4.00 or higher requires Vim7. Version 3.50 can stil be used with Vim6."
39
39
finish
40
40
endif
41
- let g: loaded_dbext_auto = 1300
41
+ let g: loaded_dbext_auto = 1400
42
+
43
+ " Turn on support for line continuations when creating the script
44
+ let s: cpo_save = &cpo
45
+ set cpo &vim
42
46
43
47
" call confirm("Loaded dbext autoload", "&Ok")
44
48
" Script variable defaults, these are used internal and are never displayed
@@ -169,6 +173,7 @@ function! s:DB_buildLists()
169
173
call add (s: config_params_mv , ' autoclose' )
170
174
call add (s: config_params_mv , ' autoclose_min_lines' )
171
175
call add (s: config_params_mv , ' variable_remember' )
176
+ call add (s: config_params_mv , ' filetype' )
172
177
173
178
" Script parameters
174
179
let s: script_params_mv = []
@@ -884,6 +889,7 @@ function! s:DB_getDefault(name)
884
889
elseif a: name == # " PGSQL_cmd_terminator" |return (exists (" g:dbext_default_PGSQL_cmd_terminator" )?g: dbext_default_PGSQL_cmd_terminator .' ' :' ;' )
885
890
elseif a: name == # " PGSQL_SQL_Top_pat" |return (exists (" g:dbext_default_PGSQL_SQL_Top_pat" )?g: dbext_default_PGSQL_SQL_Top_pat .' ' :' \(.*\)' )
886
891
elseif a: name == # " PGSQL_SQL_Top_sub" |return (exists (" g:dbext_default_PGSQL_SQL_Top_sub" )?g: dbext_default_PGSQL_SQL_Top_sub .' ' :' \1 LIMIT @dbext_topX ' )
892
+ elseif a: name == # " PGSQL_pgpass" |return (exists (" g:dbext_default_PGSQL_pgpass" )?g: dbext_default_PGSQL_pgpass .' ' :' $HOME/.pgpass' )
887
893
elseif a: name == # " RDB_bin" |return (exists (" g:dbext_default_RDB_bin" )?g: dbext_default_RDB_bin .' ' :' mc sql$' )
888
894
elseif a: name == # " RDB_cmd_header" |return (exists (" g:dbext_default_RDB_cmd_header" )?g: dbext_default_RDB_cmd_header .' ' :" " .
889
895
\ " set line length 10000\n " .
@@ -1125,6 +1131,10 @@ function! s:DB_resetBufferParameters(use_defaults)
1125
1131
call s: DB_promptForParameters ()
1126
1132
endif
1127
1133
1134
+ " if s:DB_get('filetype') == ''
1135
+ " let s:DB_set('filetype') = &filetype
1136
+ " endif
1137
+
1128
1138
" call s:DB_validateBufferParameters()
1129
1139
let retval = s: DB_validateBufferParameters ()
1130
1140
@@ -1795,7 +1805,9 @@ function! s:DB_ASA_stripHeaderFooter(result)
1795
1805
let stripped = substitute ( stripped, ' \((\)\?\(First\s\+\)\?\d\+ row\_.*' , ' ' , ' ' )
1796
1806
" Strip off trailing spaces
1797
1807
" let stripped = substitute( stripped, '\(\<\w\+\>\)\s*', '\1', 'g' )
1798
- let stripped = substitute ( stripped, ' \(\<\w\+\>\)\s*\(\n\)' , ' \1\2' , ' ' )
1808
+ let stripped = substitute ( stripped, ' \(\<\w\+\>\)\s*\(\n\)' , ' \1\2' , ' g' )
1809
+ " Strip blank lines
1810
+ let stripped = substitute ( stripped, ' \(\n\)\(\n\)' , ' ' , ' g' )
1799
1811
return stripped
1800
1812
endfunction
1801
1813
@@ -2870,11 +2882,11 @@ function! s:DB_ORA_execSql(str)
2870
2882
2871
2883
let cmd = dbext_bin .
2872
2884
\ ' ' . dbext#DB_getWType (" cmd_options" ) .
2873
- \ s: DB_option (' ' , s: DB_get (" user" ), ' ' ) .
2885
+ \ s: DB_option (" ' " , s: DB_get (" user" ), ' ' ) .
2874
2886
\ s: DB_option (' /' , s: DB_get (" passwd" ), ' ' ) .
2875
2887
\ s: DB_option (' @' , s: DB_get (" srvname" ), ' ' ) .
2876
2888
\ s: DB_option (' ' , s: DB_get (" extra" ), ' ' ) .
2877
- \ ' @' . s: dbext_tempfile
2889
+ \ ' " @' . s: dbext_tempfile
2878
2890
let result = s: DB_runCmd (cmd, output, " " )
2879
2891
2880
2892
return result
@@ -3026,7 +3038,32 @@ function! s:DB_ORA_getDictionaryView() "{{{
3026
3038
endfunction " }}}
3027
3039
" }}}
3028
3040
" PGSQL exec {{{
3041
+ function ! s: DB_PGSQL_check_pgpass ()
3042
+ " All defaults are specified in the DB_getDefault function.
3043
+ " This contains the defaults settings for all database types
3044
+ let filename = dbext#DB_getWType (" pgpass" )
3045
+
3046
+ if ! filereadable (expand (filename))
3047
+ call s: DB_warningMsg (
3048
+ \ " dbext:PostgreSQL requires a '" .
3049
+ \ dbext#DB_getWType (" pgpass" ).
3050
+ \ " ' file in order to authenticate. " .
3051
+ \ ' This file is missing. ' .
3052
+ \ " The binary '" .
3053
+ \ dbext#DB_getWType (" bin" ).
3054
+ \ " ' does not accept commandline passwords."
3055
+ \ )
3056
+ return -1
3057
+ endif
3058
+
3059
+ return
3060
+ endfunction
3061
+
3029
3062
function ! s: DB_PGSQL_execSql (str)
3063
+ if s: DB_PGSQL_check_pgpass () == -1
3064
+ return -1
3065
+ endif
3066
+
3030
3067
" All defaults are specified in the DB_getDefault function.
3031
3068
" This contains the defaults settings for all database types
3032
3069
let terminator = dbext#DB_getWType (" cmd_terminator" )
@@ -4973,6 +5010,10 @@ function! dbext#DB_execSql(query)
4973
5010
return -1
4974
5011
endif
4975
5012
5013
+ " Mark the current line to return to
5014
+ let curline = line (" ." )
5015
+ let curcol = virtcol (" ." )
5016
+
4976
5017
" Add query to internal history
4977
5018
call s: DB_historyAdd (query)
4978
5019
@@ -4994,14 +5035,26 @@ function! dbext#DB_execSql(query)
4994
5035
endif
4995
5036
4996
5037
if query != " "
4997
- return dbext#DB_execFuncTypeWCheck (' execSql' , query)
5038
+ let rc = dbext#DB_execFuncTypeWCheck (' execSql' , query)
5039
+
5040
+ " Return to previous location
5041
+ " Accounting for beginning of the line
5042
+ " silent! exe 'norm! '.curline."G\<bar>".(curcol-1).(((curcol-1)> 0)?'l':'')
5043
+ call cursor (curline, curcol)
5044
+
5045
+ return rc
4998
5046
else
4999
5047
" If the query was cancelled, close the history
5000
5048
" window which was opened when we added the
5001
5049
" query above.
5002
5050
call dbext#DB_windowClose (s: DB_resBufName ())
5003
5051
endif
5004
5052
5053
+ " Return to previous location
5054
+ " Accounting for beginning of the line
5055
+ " silent! exe 'norm! '.curline."G\<bar>".(curcol-1).(((curcol-1)> 0)?'l':'')
5056
+ call cursor (curline, curcol)
5057
+
5005
5058
return -1
5006
5059
endfunction
5007
5060
@@ -5086,6 +5139,10 @@ function! dbext#DB_execSqlTopX(...)
5086
5139
endfunction
5087
5140
5088
5141
function ! dbext#DB_execRangeSql () range
5142
+ " Mark the current line to return to
5143
+ let curline = a: lastline
5144
+ let curcol = 0
5145
+
5089
5146
if a: firstline != a: lastline
5090
5147
let saveR = @"
5091
5148
silent ! exec a: firstline .' ,' .a: lastline .' y'
@@ -5095,7 +5152,13 @@ function! dbext#DB_execRangeSql() range
5095
5152
let query = getline (a: firstline )
5096
5153
endif
5097
5154
5098
- return dbext#DB_execSql (query)
5155
+ let rc = dbext#DB_execSql (query)
5156
+
5157
+ " Return to previous location
5158
+ " Accounting for beginning of the line
5159
+ call cursor (curline, curcol)
5160
+
5161
+ return rc
5099
5162
endfunction
5100
5163
5101
5164
function ! s: DB_getLoginScript (filename)
@@ -5814,6 +5877,13 @@ function! dbext#DB_auBufDelete(del_buf_nr) "{{{
5814
5877
return
5815
5878
endif
5816
5879
5880
+ " Do not let current buffer and syntax highlighting go which may
5881
+ " happen when current value of 'bufhidden' is 'delete', 'wipe' etc.
5882
+ let cur_bufhidden = &bufhidden
5883
+ let cur_syntax = &syntax
5884
+ let cur_filetype = &filetype
5885
+ setlocal bufhidden =
5886
+
5817
5887
let idx = index (s: dbext_buffers_with_dict_files , del_buf)
5818
5888
5819
5889
if idx > -1 || exists (' g:loaded_dbext_auto' )
@@ -5839,6 +5909,13 @@ function! dbext#DB_auBufDelete(del_buf_nr) "{{{
5839
5909
5840
5910
" Switch back to the current buffer
5841
5911
silent ! exec cur_buf.' buffer'
5912
+
5913
+ " Switch back value of 'bufhidden' and syntax
5914
+ if ! empty (cur_bufhidden)
5915
+ exec " setlocal bufhidden=" .cur_bufhidden
5916
+ exec " setlocal syntax=" .cur_syntax
5917
+ exec " setlocal filetype=" .cur_filetype
5918
+ endif
5842
5919
endif
5843
5920
endfunction " }}}
5844
5921
" }}}
@@ -6597,33 +6674,41 @@ function! dbext#DB_parseQuery(query)
6597
6674
return a: query
6598
6675
endif
6599
6676
6600
- if &filetype == " sql"
6677
+ " If the user has not overriden the filetype using DB_setOption
6678
+ " then use the filetype Vim set
6679
+ let l: filetype = s: DB_get (' filetype' )
6680
+ if l: filetype == ' '
6681
+ call s: DB_set (' filetype' , &filetype )
6682
+ let l: filetype = &filetype
6683
+ endif
6684
+
6685
+ if matchstr ( l: filetype , " sql" ) == " sql"
6601
6686
" Dont parse the SQL query, since DB_parseHostVariables
6602
6687
" will pickup the standard host variables for prompting.
6603
6688
" let query = s:DB_parseSQL(a:query)
6604
6689
return s: DB_parseHostVariables (a: query )
6605
- elseif & filetype == " java" ||
6606
- \ & filetype == " jsp" ||
6607
- \ & filetype == " html" ||
6608
- \ & filetype == " javascript"
6690
+ elseif matchstr ( l: filetype, " java " ) == " java" ||
6691
+ \ matchstr ( l: filetype, " jsp " ) == " jsp" ||
6692
+ \ matchstr ( l: filetype, " html " ) == " html" ||
6693
+ \ matchstr ( l: filetype, " javascript " ) == " javascript"
6609
6694
let query = s: DB_parseJava (a: query )
6610
6695
return s: DB_parseHostVariables (query)
6611
- elseif & filetype == " jproperties"
6696
+ elseif matchstr ( l: filetype, " jproperties " ) == " jproperties"
6612
6697
let query = s: DB_parseJProperties (a: query )
6613
6698
return s: DB_parseHostVariables (query)
6614
- elseif & filetype == " perl"
6699
+ elseif matchstr ( l: filetype, " perl " ) == " perl"
6615
6700
" The Perl parser will deal with string concatenation
6616
6701
let query = s: DB_parsePerl (a: query )
6617
6702
" Let the SQL parser handle embedded host variables
6618
6703
return s: DB_parseHostVariables (query)
6619
- elseif & filetype == " php"
6704
+ elseif matchstr ( l: filetype, " php " ) == " php"
6620
6705
let query = s: DB_parsePHP (a: query )
6621
6706
return s: DB_parseHostVariables (query)
6622
- elseif & filetype == " vim"
6707
+ elseif matchstr ( l: filetype, " vim " ) == " vim"
6623
6708
let query = s: DB_parseVim (a: query )
6624
6709
return s: DB_parseHostVariables (query)
6625
- elseif & filetype == " vb" ||
6626
- \ & filetype == " basic"
6710
+ elseif matchstr ( l: filetype, " vb " ) == " vb" ||
6711
+ \ matchstr ( l: filetype, " basic " ) == " basic"
6627
6712
let query = s: DB_parseVB (a: query )
6628
6713
return s: DB_parseHostVariables (query)
6629
6714
else
@@ -7553,7 +7638,7 @@ endfunction
7553
7638
function ! s: DB_parseVB (query)
7554
7639
7555
7640
" Join all line continuations by removing the ending "_"
7556
- let a_query = substitute (a: query , " _[\r \n ]\\ +\\ s*" , " " ," " )
7641
+ let a_query = substitute (a: query , " _[\r \n ]\\ +\\ s*" , " " ," g " )
7557
7642
7558
7643
" Get the string part of the vb query and remove the beginning
7559
7644
" and closing quotes
@@ -7603,20 +7688,7 @@ function! s:DB_parseVB(query)
7603
7688
" \s*[+&] - Any space and a plus sign
7604
7689
" \s*" - Any space followed by a double quote
7605
7690
7606
- " Replace all sql vars using existing buffer variables
7607
- let index = match (query, var_expr_q)
7608
- while index > -1
7609
- let var = matchstr (query, var_expr, index )
7610
- let val = s: DB_sqlvar_get (var )
7611
- " echo b:dbext_sqlvar_mv
7612
- if index == 0
7613
- let query = val.query[index + strlen (var )+ 2 :]
7614
- else
7615
- let query = query[0 :index - 1 ].val.query[index + strlen (var )+ 2 :]
7616
- endif
7617
- let index = match (query, var_expr, index + strlen (var ))
7618
- endwhile
7619
- " let query = s:DB_searchReplace(query, var_expr, var_expr, 0)
7691
+ let query = s: DB_searchReplace (query, var_expr, var_expr, 0 )
7620
7692
7621
7693
" call inputdialog(query)
7622
7694
return query
@@ -8281,4 +8353,8 @@ endfunction
8281
8353
call s: DB_buildLists ()
8282
8354
8283
8355
call s: DB_resetGlobalParameters ()
8356
+
8357
+ let &cpo = s: cpo_save
8358
+ unlet s: cpo_save
8359
+
8284
8360
" vim:fdm = marker:nowrap :ts = 4 :expandtab :ff = unix:
0 commit comments