11" dbext.vim - Commn Database Utility
2- " Copyright (C) 2002-7 , Peter Bagyinszki, David Fishburn
2+ " Copyright (C) 2002-10 , Peter Bagyinszki, David Fishburn
33" ---------------------------------------------------------------
4- " Version: 12 .00
4+ " Version: 13 .00
55" Maintainer: David Fishburn <dfishburn dot vim at gmail dot com>
66" Authors: Peter Bagyinszki <petike1 at dpg dot hu>
77" David Fishburn <dfishburn dot vim at gmail dot com>
8- " Last Modified: 2010 Jul 15
8+ " Last Modified: 2011 May 31
99" Based On: sqlplus.vim (author: Jamis Buck)
1010" Created: 2002-05-24
1111" Homepage: http://vim.sourceforge.net/script.php?script_id=356
@@ -38,7 +38,7 @@ if v:version < 700
3838 echomsg " dbext: Version 4.00 or higher requires Vim7. Version 3.50 can stil be used with Vim6."
3939 finish
4040endif
41- let g: loaded_dbext_auto = 1200
41+ let g: loaded_dbext_auto = 1300
4242
4343" call confirm("Loaded dbext autoload", "&Ok")
4444" Script variable defaults, these are used internal and are never displayed
@@ -794,7 +794,7 @@ function! s:DB_getDefault(name)
794794 " Q - CANNOT be surrounded in quotes
795795 " , - delimiter between options
796796 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\+\>' )
798798 elseif a: name == # " buffer_lines" |return (exists (" g:dbext_default_buffer_lines" )?g: dbext_default_buffer_lines .' ' :10 )
799799 elseif a: name == # " use_result_buffer" |return (exists (" g:dbext_default_use_result_buffer" )?g: dbext_default_use_result_buffer .' ' :1 )
800800 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 "}}}
36653665" DBI (Perl) exec {{{
36663666function ! s: DB_DBI_Autoload ()
36673667 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 ()
36703671
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
36753679 endif
36763680 endif
36773681
@@ -5793,6 +5797,7 @@ function! dbext#DB_auVimLeavePreOld() "{{{
57935797 " Switch back to the current buffer
57945798 silent ! exec cur_buf.' buffer'
57955799endfunction " }}}
5800+
57965801function ! dbext#DB_auBufDelete (del_buf_nr) " {{{
57975802 " This function will delete any temporary dictionary files that were
57985803 " created and disconnect any DBI or ODBC connections
@@ -6523,7 +6528,7 @@ function! s:DB_addToResultBuffer(output, do_clear)
65236528 endif
65246529 " Delete all the lines prior to this run
65256530 if a: do_clear == " clear"
6526- % d
6531+ % d_
65276532 silent ! exec " normal! iConnection: " .conn_props.' at ' .strftime (" %H:%M" )." \<Esc> 0"
65286533
65296534 " 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)
66406645 " Find the string index position of the first match
66416646 let index = match (str, a: exp_find_str )
66426647 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+
66436653 let count_nbr = count_nbr + 1
66446654 " Retrieve the name of what we found
66456655 " 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)
66506660 " OUT @variable CHAR(1)
66516661 " INOUT @variable CHAR(1)
66526662 " DECLARE @variable CHAR(1)
6663+ " VARIABLE @variable CHAR(1) -- CREATE VARIABLE @variable
66536664 " Or part of a string
66546665 " '@variable'
66556666 " Or part of path
66566667 " /@variable'
6657- " Or part of path
6658- " /@variable'
66596668 " Or a global variable
66606669 " SET @@variable = ...
6670+ " Or the definition of a global variable
6671+ " CREATE VARIABLE variable ...
66616672 " 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
66776688
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
66886704 endif
6689- endif
66906705
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
67016738 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
67386770 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
67536771 endif
6772+ else
6773+ " Move on to next match
6774+ let index = index + strlen (var ) + 1
67546775 endif
67556776 else
67566777 " 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)
67606781 call dbext#DB_sqlVarAssignment (2 , ' set ' .var .' = ' .var )
67616782 endif
67626783 " 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
67646786 endif
6787+
6788+ " Find next match
6789+ let index = match (str, a: exp_find_str , index )
67656790 endwhile
67666791 return str
67676792endfunction
@@ -8189,7 +8214,20 @@ function! dbext#DB_connect()
81898214 return 0
81908215endfunction
81918216
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+
81938231 " Only valid for DBI and ODBC (perl)
81948232 let driver = s: DB_get (' type' )
81958233 if (driver !~ ' \<DBI\>\|\<ODBC\>' )
@@ -8205,23 +8243,36 @@ function! dbext#DB_disconnect()
82058243 return -1
82068244 endif
82078245
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
82158254
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
82218261 endif
82228262 endif
82238263
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 ()
82258276
82268277 return 0
82278278endfunction
0 commit comments