1
1
" dbext.vim - Commn Database Utility
2
- " Copyright (C) 2002-7 , Peter Bagyinszki, David Fishburn
2
+ " Copyright (C) 2002-10 , Peter Bagyinszki, David Fishburn
3
3
" ---------------------------------------------------------------
4
- " Version: 12 .00
4
+ " Version: 13 .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: 2010 Jul 15
8
+ " Last Modified: 2011 May 31
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,7 @@ 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 = 1200
41
+ let g: loaded_dbext_auto = 1300
42
42
43
43
" call confirm("Loaded dbext autoload", "&Ok")
44
44
" Script variable defaults, these are used internal and are never displayed
@@ -794,7 +794,7 @@ function! s:DB_getDefault(name)
794
794
" Q - CANNOT be surrounded in quotes
795
795
" , - delimiter between options
796
796
elseif a: name == # " variable_def" |return (exists (" g:dbext_default_variable_def" )?g: dbext_default_variable_def .' ' :' ?WQ,@wq,:wq,$wq' )
797
- elseif a: name == # " variable_def_regex" |return (exists (" g:dbext_default_variable_def_regex" )?g: dbext_default_variable_def_regex .' ' :' \(\w\|' ." '" .' \)\@<!?\(\w\|' ." '" .' \)\@<!,\zs\(@\|:\|$\)\w\+\>' )
797
+ elseif a: name == # " variable_def_regex" |return (exists (" g:dbext_default_variable_def_regex" )?g: dbext_default_variable_def_regex .' ' :' \(\w\|' ." '" .' \)\@<!?\(\w\|' ." '" .' \)\@<!,\zs\(@\|:\a\ |$\)\w\+\>' )
798
798
elseif a: name == # " buffer_lines" |return (exists (" g:dbext_default_buffer_lines" )?g: dbext_default_buffer_lines .' ' :10 )
799
799
elseif a: name == # " use_result_buffer" |return (exists (" g:dbext_default_use_result_buffer" )?g: dbext_default_use_result_buffer .' ' :1 )
800
800
elseif a: name == # " use_sep_result_buffer" |return (exists (" g:dbext_default_use_sep_result_buffer" )?g: dbext_default_use_sep_result_buffer .' ' :0 )
@@ -3665,13 +3665,17 @@ endfunction "}}}
3665
3665
" DBI (Perl) exec {{{
3666
3666
function ! s: DB_DBI_Autoload ()
3667
3667
if ! exists (" g:loaded_dbext_dbi" )
3668
- " Load the perl based module (if not already)
3669
- call dbext_dbi#DBI_load_perl_subs ()
3668
+ if has (' perl' )
3669
+ " Load the perl based module (if not already)
3670
+ call dbext_dbi#DBI_load_perl_subs ()
3670
3671
3671
- if ! exists (" g:loaded_dbext_dbi" )
3672
- call s: DB_warningMsg (
3673
- \ ' dbext:The DBI interface could not be loaded, ensure autoload/dbext_dbi.vim exists'
3674
- \ )
3672
+ if ! exists (" g:loaded_dbext_dbi" )
3673
+ call s: DB_warningMsg (
3674
+ \ ' dbext:The DBI interface could not be loaded, ensure autoload/dbext_dbi.vim exists'
3675
+ \ )
3676
+ endif
3677
+ else
3678
+ return -1
3675
3679
endif
3676
3680
endif
3677
3681
@@ -5793,6 +5797,7 @@ function! dbext#DB_auVimLeavePreOld() "{{{
5793
5797
" Switch back to the current buffer
5794
5798
silent ! exec cur_buf.' buffer'
5795
5799
endfunction " }}}
5800
+
5796
5801
function ! dbext#DB_auBufDelete (del_buf_nr) " {{{
5797
5802
" This function will delete any temporary dictionary files that were
5798
5803
" created and disconnect any DBI or ODBC connections
@@ -6523,7 +6528,7 @@ function! s:DB_addToResultBuffer(output, do_clear)
6523
6528
endif
6524
6529
" Delete all the lines prior to this run
6525
6530
if a: do_clear == " clear"
6526
- % d
6531
+ % d_
6527
6532
silent ! exec " normal! iConnection: " .conn_props.' at ' .strftime (" %H:%M" )." \<Esc> 0"
6528
6533
6529
6534
" We only clear the results buffer at the start of a command
@@ -6640,6 +6645,11 @@ function! s:DB_searchReplace(str, exp_find_str, exp_get_value, count_matches)
6640
6645
" Find the string index position of the first match
6641
6646
let index = match (str, a: exp_find_str )
6642
6647
while index > -1
6648
+ " DEBUGGING
6649
+ " This is a useful echo statemen to use inside the debug loop
6650
+ " when using breakadd
6651
+ " echo index matchstr(str, a:exp_find_str, index) var a:exp_find_str "\n" strpart(str, 0, (index-1))
6652
+
6643
6653
let count_nbr = count_nbr + 1
6644
6654
" Retrieve the name of what we found
6645
6655
" let var = matchstr(str, a:exp_get_value, index)
@@ -6650,107 +6660,118 @@ function! s:DB_searchReplace(str, exp_find_str, exp_get_value, count_matches)
6650
6660
" OUT @variable CHAR(1)
6651
6661
" INOUT @variable CHAR(1)
6652
6662
" DECLARE @variable CHAR(1)
6663
+ " VARIABLE @variable CHAR(1) -- CREATE VARIABLE @variable
6653
6664
" Or part of a string
6654
6665
" '@variable'
6655
6666
" Or part of path
6656
6667
" /@variable'
6657
- " Or part of path
6658
- " /@variable'
6659
6668
" Or a global variable
6660
6669
" SET @@variable = ...
6670
+ " Or the definition of a global variable
6671
+ " CREATE VARIABLE variable ...
6661
6672
" If so, ignore the match
6662
- let inout = matchstr (strpart (str, 1 , ( index - 1 ) ), ' \(\<\w\+\ze\s*$\|'' \ze$\|/\ze$\|@\ze$\)' )
6663
-
6664
- if inout !~? ' \(in\|out\|inout\|declare\|set\|'' \|/\|@\)'
6665
- " Check to see if the variable is part of the temporarily
6666
- " stored list of variables to ignore
6667
- if has_key ( b: dbext_sqlvar_temp_mv , var )
6668
- " Ingore match and move on
6669
- let index = match (str, a: exp_find_str , index + strlen ( var ))
6670
- else
6671
- " Prompt for value and continue
6672
- let index = index + 1
6673
-
6674
- let response = 2
6675
- " If enabled, default to using saved variables
6676
- let use_save_vars = ( s: DB_get ( " use_saved_variables " ) == 1 ? 1 : 2 )
6673
+ let inout = matchstr (strpart (str, 0 , index ), ' \(\<\w\+\ze\s*$\|'' \ze$\|/\ze$\|@\ze$\)' )
6674
+
6675
+ if inout !~? ' \(in\|out\|inout\|declare\|set\|variable\| '' \|/\|@\)'
6676
+ " Check if the variable name is preceeded by a comment character.
6677
+ " If so, ignore and continue.
6678
+ if strpart (str, 0 , ( index - 1 )) !~ ' \(--\|\/\/\)\s*$ '
6679
+ " Check to see if the variable is part of the temporarily
6680
+ " stored list of variables to ignore
6681
+ if has_key ( b: dbext_sqlvar_temp_mv , var )
6682
+ " Ingore match and move on
6683
+ " let index = match(str, a:exp_find_str, index+strlen(var))
6684
+ let index = index + strlen ( var ) + 1
6685
+ else
6686
+ " Prompt for value and continue
6687
+ " let index = index + 1
6677
6688
6678
- if has_key (b: dbext_sqlvar_mv , var )
6679
- let var_val = b: dbext_sqlvar_mv [var ]
6680
- let dialog_msg = " There are previously saved variables which can be used " .
6681
- \ " in your SQL. Should these saved variables be used? "
6682
- if s: DB_get (" use_saved_variables" ) == 0
6683
- let use_save_vars = confirm (dialog_msg,
6684
- \ " &Yes" .
6685
- \ " \n &No"
6686
- \ )
6687
- call s: DB_set (" use_saved_variables" , use_save_vars)
6689
+ let response = 2
6690
+ " If enabled, default to using saved variables
6691
+ let use_save_vars = (s: DB_get (" use_saved_variables" )== 1 ?1 :2 )
6692
+
6693
+ if has_key (b: dbext_sqlvar_mv , var )
6694
+ let var_val = b: dbext_sqlvar_mv [var ]
6695
+ let dialog_msg = " There are previously saved variables which can be used " .
6696
+ \ " in your SQL. Should these saved variables be used? "
6697
+ if s: DB_get (" use_saved_variables" ) == 0
6698
+ let use_save_vars = confirm (dialog_msg,
6699
+ \ " &Yes" .
6700
+ \ " \n &No"
6701
+ \ )
6702
+ call s: DB_set (" use_saved_variables" , use_save_vars)
6703
+ endif
6688
6704
endif
6689
- endif
6690
6705
6691
- if use_save_vars == 1 && has_key (b: dbext_sqlvar_mv , var )
6692
- let var_val = b: dbext_sqlvar_mv [var ]
6693
- else
6694
- " Prompt the user using the name of the variable
6695
- let dialog_msg = " Enter value for " . var
6696
- if a: count_matches == 1
6697
- " If there is no name (ie ?), then include the
6698
- " count of what was found so the user can
6699
- " distinguish between different ?s
6700
- let dialog_msg = dialog_msg . " number " . count_nbr
6706
+ if use_save_vars == 1 && has_key (b: dbext_sqlvar_mv , var )
6707
+ let var_val = b: dbext_sqlvar_mv [var ]
6708
+ else
6709
+ " Prompt the user using the name of the variable
6710
+ let dialog_msg = " Enter value for " . var
6711
+ if a: count_matches == 1
6712
+ " If there is no name (ie ?), then include the
6713
+ " count of what was found so the user can
6714
+ " distinguish between different ?s
6715
+ let dialog_msg = dialog_msg . " number " . count_nbr
6716
+ endif
6717
+ let dialog_msg = dialog_msg . " : "
6718
+ let var_val = s: DB_getInput (
6719
+ \ dialog_msg,
6720
+ \ ' ' ,
6721
+ \ " dbext_cancel"
6722
+ \ )
6723
+ let response = 2
6724
+ " Ok or Cancel result in an empty string
6725
+ if var_val == " dbext_cancel"
6726
+ let response = 5
6727
+ elseif var_val == " "
6728
+ " If empty, check if they want to leave it empty
6729
+ " of skip this variable
6730
+ let response = confirm (" Your value is empty!" ,
6731
+ \ " &Skip" .
6732
+ \ " \n &Use blank" .
6733
+ \ " \n S&top Prompting" .
6734
+ \ " \n &Never Prompt" .
6735
+ \ " \n &Abort"
6736
+ \ )
6737
+ endif
6701
6738
endif
6702
- let dialog_msg = dialog_msg . " : "
6703
- let var_val = s: DB_getInput (
6704
- \ dialog_msg,
6705
- \ ' ' ,
6706
- \ " dbext_cancel"
6707
- \ )
6708
- let response = 2
6709
- " Ok or Cancel result in an empty string
6710
- if var_val == " dbext_cancel"
6711
- let response = 5
6712
- elseif var_val == " "
6713
- " If empty, check if they want to leave it empty
6714
- " of skip this variable
6715
- let response = confirm (" Your value is empty!" ,
6716
- \ " &Skip" .
6717
- \ " \n &Use blank" .
6718
- \ " \n S&top Prompting" .
6719
- \ " \n &Never Prompt" .
6720
- \ " \n &Abort"
6721
- \ )
6722
- endif
6723
- endif
6724
- if response == 1
6725
- " Skip this match and move on to the next
6726
- let index = match (str, a: exp_find_str , index + strlen (var ))
6727
- elseif response == 2
6728
- " Use blank
6729
- " Replace the variable with what was entered
6730
- let replace_sub = ' \%' .index .' c' .' .\{' .strlen (var ).' }'
6731
- let str = substitute (str, replace_sub, var_val, ' ' )
6732
- let index = match (str, a: exp_find_str , index + strlen (var_val))
6733
- if a: count_matches != 1 && s: DB_get (' variable_remember' ) == ' 1'
6734
- " Add this assignment to the list of remembered
6735
- " assignments unless it is question marks as host
6736
- " variables.
6737
- call dbext#DB_sqlVarAssignment (0 , ' set ' .var .' = ' .var_val)
6739
+ if response == 1
6740
+ " Skip this match and move on to the next
6741
+ " let index = match(str, a:exp_find_str, index+strlen(var))
6742
+ let index = index + strlen (var ) + 1
6743
+ elseif response == 2
6744
+ " Use blank
6745
+ " Replace the variable with what was entered
6746
+ let replace_sub = ' \%' .(index + 1 ).' c' .' .\{' .strlen (var ).' }'
6747
+ let str = substitute (str, replace_sub, var_val, ' ' )
6748
+ " let index = match(str, a:exp_find_str, index+strlen(var_val))
6749
+ let index = index + strlen (var_val) + 1
6750
+ if a: count_matches != 1 && s: DB_get (' variable_remember' ) == ' 1'
6751
+ " Add this assignment to the list of remembered
6752
+ " assignments unless it is question marks as host
6753
+ " variables.
6754
+ call dbext#DB_sqlVarAssignment (0 , ' set ' .var .' = ' .var_val)
6755
+ endif
6756
+ elseif response == 4
6757
+ " Never Prompt
6758
+ call s: DB_set (" always_prompt_for_variables" , ' -1' )
6759
+ break
6760
+ elseif response == 5
6761
+ " Abort
6762
+ " If we are aborting, do not execute the SQL statement
6763
+ let str = " "
6764
+ break
6765
+ else
6766
+ " Stop Prompting
6767
+ " Skip all remaining matches
6768
+ call s: DB_set (" stop_prompt_for_variables" , 1 )
6769
+ break
6738
6770
endif
6739
- elseif response == 4
6740
- " Never Prompt
6741
- call s: DB_set (" always_prompt_for_variables" , ' -1' )
6742
- break
6743
- elseif response == 5
6744
- " Abort
6745
- " If we are aborting, do not execute the SQL statement
6746
- let str = " "
6747
- break
6748
- else
6749
- " Stop Prompting
6750
- " Skip all remaining matches
6751
- call s: DB_set (" stop_prompt_for_variables" ,1 )
6752
- break
6753
6771
endif
6772
+ else
6773
+ " Move on to next match
6774
+ let index = index + strlen (var ) + 1
6754
6775
endif
6755
6776
else
6756
6777
" if inout !~? "'" && s:DB_get('variable_remember') == '1'
@@ -6760,8 +6781,12 @@ function! s:DB_searchReplace(str, exp_find_str, exp_get_value, count_matches)
6760
6781
call dbext#DB_sqlVarAssignment (2 , ' set ' .var .' = ' .var )
6761
6782
endif
6762
6783
" Skip this match and move on to the next
6763
- let index = match (str, a: exp_find_str , index + strlen (var ))
6784
+ " let index = match(str, a:exp_find_str, index+strlen(var)) + 1
6785
+ let index = index + strlen (var ) + 1
6764
6786
endif
6787
+
6788
+ " Find next match
6789
+ let index = match (str, a: exp_find_str , index )
6765
6790
endwhile
6766
6791
return str
6767
6792
endfunction
@@ -8189,7 +8214,20 @@ function! dbext#DB_connect()
8189
8214
return 0
8190
8215
endfunction
8191
8216
8192
- function ! dbext#DB_disconnect ()
8217
+ function ! dbext#DB_disconnect (... )
8218
+ let bufnr = bufnr (" %" )
8219
+
8220
+ if a: 0 > 0 && a: 1 != ' '
8221
+ let bufnr = matchstr (a: 1 , ' \d\+' )
8222
+
8223
+ if bufnr == ' '
8224
+ call s: DB_warningMsg (
8225
+ \ " dbext: Input must be a buffer number "
8226
+ \ )
8227
+ return -1
8228
+ endif
8229
+ endif
8230
+
8193
8231
" Only valid for DBI and ODBC (perl)
8194
8232
let driver = s: DB_get (' type' )
8195
8233
if (driver !~ ' \<DBI\>\|\<ODBC\>' )
@@ -8205,23 +8243,36 @@ function! dbext#DB_disconnect()
8205
8243
return -1
8206
8244
endif
8207
8245
8208
- " If AutoCommit is on, there is no need to issue commits
8209
- " If AutoCommit is on disconnect, otherwise let the
8210
- " user make the choice since it could intefere
8211
- " with an already running transaction
8212
- perl db_get_connection_option (' AutoCommit' )
8213
-
8214
- let is_AutoCommit = g: dbext_dbi_result
8246
+ if bufnr == bufnr (' %' )
8247
+ " If AutoCommit is on, there is no need to issue commits
8248
+ " If AutoCommit is on disconnect, otherwise let the
8249
+ " user make the choice since it could intefere
8250
+ " with an already running transaction
8251
+ perl db_get_connection_option (' AutoCommit' )
8252
+
8253
+ let is_AutoCommit = g: dbext_dbi_result
8215
8254
8216
- if is_AutoCommit == 0
8217
- if s: DB_get (' DBI_commit_on_disconnect' ) == 1
8218
- call dbext#DB_commit ()
8219
- else
8220
- call dbext#DB_rollback ()
8255
+ if is_AutoCommit == 0
8256
+ if s: DB_get (' DBI_commit_on_disconnect' ) == 1
8257
+ call dbext#DB_commit ()
8258
+ else
8259
+ call dbext#DB_rollback ()
8260
+ endif
8221
8261
endif
8222
8262
endif
8223
8263
8224
- perl db_disconnect ()
8264
+ exec " perl db_disconnect( '" .bufnr ." ' )"
8265
+
8266
+ return 0
8267
+ endfunction
8268
+
8269
+ function ! dbext#DB_disconnectAll ()
8270
+ " Ensure the dbext_dbi plugin is loaded
8271
+ if s: DB_DBI_Autoload () == -1
8272
+ return -1
8273
+ endif
8274
+
8275
+ perl db_disconnect_all ()
8225
8276
8226
8277
return 0
8227
8278
endfunction
0 commit comments