提交 06548141 编写于 作者: C Cosmin Popescu

Added variables

上级 d1299d0b
......@@ -30,10 +30,11 @@ CONTENTS:
6. Exporting
7. Sessions
8. Asynchronous
9. Commands
19. Settings
11. Screen shots
12. Missing features
9. Variables
10. Commands
11. Settings
12. Screen shots
13. Missing features
Requirements
========================================
......@@ -559,6 +560,65 @@ statement. The statement will get execute anyway.
If any of the requirements is not met, you will not receive any error message.
The plugin will silently switch to synchronous mode and work without issues.
Variables
========================================
`SQL Workbench/j` supports user defined variables (you can have your queries
sent to the database parameterized). See
[here](http://www.sql-workbench.net/manual/using-variables.html).
This plugin takes advantage of that and implements a few commands to help you
use variables.
By default, in `SQL Workbench`, the variables are enclosed between `$[` and
`]`. [These can be
changed](http://www.sql-workbench.net/manual/using-variables.html#access-variable).
By default, in `VIM SQL Workbench` the variable substitution is on. This
means, that when you send a query to the database, the plugin will search for
anything enclosed between the parameter prefix and suffix. Once a match is
found, if a value is defined with `SWVarSet` then the match is replaced with
this value. Please note that exactly the literal is replaced. No quotes are
added and no escaping is executed. If you want quotes, you need to add then in
the value.
If the variable is not defined using `SWVarSet` the plugin will ask for a
value. If you don't want this string to be replaced when the query is sent to
the database, then you can use an empty string as a value. If you want to send
to the database an empty string, then you have to set the value `''`.
If you set already a value for a variable, you can always change it by
executing again `SWVarSet`.
A variable can be unset using `SWVarUnset`.
If you don't want the plugin asking doing parameters substitution for a given
buffer, you can call `SWVarDisable`. You can always re-enable the parameter
switching by calling `SWVarEnable`.
Example:
In your `workbench.settings` file:
```
workbench.sql.parameter.prefix=:
workbench.sql.parameter.suffix=
```
The sql query: `select * from table where d = '2015-01-01 00:00:00'`.
When launching this query, you will be asked for the value of the `00`
variable. You can just press `enter` and the `:00` will not be replace.
The sql query: `select * from table where name = :name`.
When launching this query, you will be asked for the value of the `name`
variable. If you enter `'Cosmin Popescu'`, the query sent to the DBMS will be
`select * from table where name = 'Cosmin Popescu'`. Please note that if you
just enter `Cosmin Popescu` (notice the missing quotes), the query sent to the
DBMS will be `select * from table where name = Cosmin Popescu` which will
obviously return an error.
Commands
========================================
......@@ -894,6 +954,35 @@ this was active when `mksession` was executed.
This command will kill the current command being executed in asynchronous mode.
## SWVarSet
*Parameters*:
* the variable name: the name of the variable to be set
* the value: the value that you want to set for this variable
If you want to set a string enclosed between the `SQL Workbench/J` parameters
suffix and prefix without being substituted, then set it to an empty string.
If you want to replace a parameter with an empty string, set the value of the
variable to `''`.
## SWVarUnset
Unsets a variable
## SWVarDisable
Disables the replacement of the parameters in the queries sent to the DBMS.
## SWVarEnable
Enables the replacement of the parameters in the queries sent to the DBMS
(enabled by default).
## SWVarList
Lists the parameters values
Settings
========================================
......
......@@ -143,6 +143,7 @@ function! sw#check_async_result()
endfunction
function! sw#got_async_result(unique_id)
echomsg "GOT ASYNC RESULT" . a:unique_id
call add(g:sw_async_ended, a:unique_id)
if s:get_buff_unique_id() == a:unique_id
if mode() == 'i' || mode() == 'R'
......
......@@ -97,20 +97,24 @@ function! s:get_panels()
return ['__DBExplorer__-' . b:profile, '__SQL__-' . b:profile, '__Info__-' . b:profile]
endfunction
function! s:set_values_to_all_buffers(keys, values)
function! sw#dbexplorer#set_values_to_all_buffers(keys, values)
let name = bufname('%')
for w in s:get_panels()
call sw#goto_window(w)
let i = 0
while i < len(a:keys)
execute "let b:" . a:keys[i] . " = a:values[i]"
if (a:keys[i] != 'on_async_result')
execute "let b:" . a:keys[i] . " = a:values[i]"
else
call sw#set_on_async_result(a:values[i])
endif
let i = i + 1
endwhile
endfor
call sw#goto_window(name)
endfunction
function! s:unset_values_from_all_buffers(keys)
function! sw#dbexplorer#unset_values_from_all_buffers(keys)
let name = bufname('%')
for w in s:get_panels()
call sw#goto_window(w)
......@@ -128,7 +132,7 @@ function! s:set_async_variables()
for w in s:get_panels()
call sw#goto_window(w)
if exists('b:async_on_progress')
call s:set_values_to_all_buffers(['async_on_progress'], [b:async_on_progress])
call sw#dbexplorer#set_values_to_all_buffers(['async_on_progress'], [b:async_on_progress])
break
endif
endfor
......@@ -136,7 +140,7 @@ function! s:set_async_variables()
endfunction
function! s:unset_async_variables()
call s:unset_values_from_all_buffers(['async_on_progress'])
call sw#dbexplorer#unset_values_from_all_buffers(['async_on_progress'])
endfunction
function! s:process_result_1(result, shortcut, title)
......@@ -184,11 +188,11 @@ function! s:process_result_1(result, shortcut, title)
endfunction
function! s:set_tmp_variables_1(title, shortcut)
call s:set_values_to_all_buffers(['on_async_result', 'on_async_kill', '__title', '__shortcut'], ['sw#dbexplorer#on_async_result_1', 'sw#dbexplorer#on_async_result_1', a:title, a:shortcut])
call sw#dbexplorer#set_values_to_all_buffers(['on_async_result', 'on_async_kill', '__title', '__shortcut'], ['sw#dbexplorer#on_async_result_1', 'sw#dbexplorer#on_async_result_1', a:title, a:shortcut])
endfunction
function! s:unset_tmp_variables_1()
call s:unset_values_from_all_buffers(['on_async_result', 'on_async_kill', '__title', '__shortcut'])
call sw#dbexplorer#unset_values_from_all_buffers(['on_async_result', 'on_async_kill', '__title', '__shortcut'])
endfunction
function! sw#dbexplorer#on_async_result_1()
......@@ -283,11 +287,11 @@ function! s:process_result_2(result, tab_shortcut, shortcut, cmd)
endfunction
function! s:set_tmp_variables_2(tab_shortcut, shortcut, cmd)
call s:set_values_to_all_buffers(['__tab_shortcut', '__shortcut', '__cmd', 'on_async_result', 'on_async_kill'], [a:tab_shortcut, a:shortcut, a:cmd, 'sw#dbexplorer#on_async_result_2', 'sw#dbexplorer#on_async_kill_2'])
call sw#dbexplorer#set_values_to_all_buffers(['__tab_shortcut', '__shortcut', '__cmd', 'on_async_result', 'on_async_kill'], [a:tab_shortcut, a:shortcut, a:cmd, 'sw#dbexplorer#on_async_result_2', 'sw#dbexplorer#on_async_kill_2'])
endfunction
function! s:unset_tmp_variables_2()
call s:unset_values_from_all_buffers(['__tab_shortcut', '__shortcut', '__cmd', 'on_async_result', 'on_async_kill'])
call sw#dbexplorer#unset_values_from_all_buffers(['__tab_shortcut', '__shortcut', '__cmd', 'on_async_result', 'on_async_kill'])
endfunction
function! s:change_panel(command, shortcut, title, tab_shortcut)
......
......@@ -217,9 +217,17 @@ function! s:asynchronious(columns, set)
let a_name = s:get_resultset_name()
endif
if a:set
call s:set_async_variables(a:columns)
if sw#dbexplorer#is_db_explorer_tab()
call sw#dbexplorer#set_values_to_all_buffers(['on_async_result', 'on_async_kill', '__columns'], ['sw#search#on_async_result', 'sw#search#on_async_kill', 'a:columns'])
else
call s:set_async_variables(a:columns)
endif
else
call s:unset_async_variables()
if sw#dbexplorer#is_db_explorer_tab()
call sw#dbexplorer#unset_values_from_all_buffers(['on_async_result', 'on_async_kill', '__columns'])
else
call s:unset_async_variables()
endif
endif
call sw#goto_window(a_name)
if a:set
......@@ -243,6 +251,7 @@ function! s:unset_async_variables()
endfunction
function! sw#search#on_async_result()
echomsg "ASYNC SEARCH RESULT"
let result = sw#get_sql_result(0)
let columns = g:sw_search_default_result_columns
if exists('b:__columns')
......
......@@ -509,6 +509,17 @@ function! sw#sqlwindow#execute_sql(sql)
if (b:max_results != 0)
let _sql = w:auto_added1 . 'set maxrows = ' . b:max_results . "\n" . b:delimiter . "\n" . w:auto_added2 . _sql
endif
if !exists('b:no_variables')
let vars = sw#variables#extract(_sql)
if len(vars) > 0
for var in vars
let value = sw#variables#get(var)
if value != ''
let _sql = w:auto_added1 . 'wbvardef ' . var . ' = ' . value . "\n" . b:delimiter . "\n" . w:auto_added2 . _sql
endif
endfor
endif
endif
call sw#set_on_async_result('sw#sqlwindow#on_async_result')
let b:on_async_kill = 'sw#sqlwindow#on_async_kill'
let result = sw#execute_sql(b:profile, _sql, 0)
......
"============================================================================"
"
" Vim SQL Workbench/J Implementation
"
" Copyright (c) Cosmin Popescu
"
" Author: Cosmin Popescu <cosminadrianpopescu at gmail dot com>
" Version: 1.00 (2015-01-08)
" Requires: Vim 7
" License: GPL
"
" Description:
"
" Provides SQL database access to any DBMS supported by SQL Workbench/J. The
" only dependency is SQL Workbench/J. Also includes powefull intellisense
" autocomplete based on the current selected database
"
"============================================================================"
function! s:set_delimiters()
if !exists('g:sw_p_suffix') && !exists('g:sw_p_prefix')
let p1 = '\v\c^[\s \t]*workbench\.sql\.parameter\.(suffix|prefix)'
if exists('g:sw_config_dir')
let lines = readfile(g:sw_config_dir . 'workbench.settings')
for line in lines
if line =~ p1
let p2 = p1 . '[\s \t]*\=[\s\t ]*(.*)$'
let type = substitute(line, p2, '\1', 'g')
execute "let g:sw_p_" . type . " = substitute(line, p2, '\\2', 'g')"
endif
endfor
endif
if !exists('g:sw_p_prefix')
let g:sw_p_prefix = '$['
endif
if !exists('g:sw_p_suffix')
let g:sw_p_suffix = ']'
endif
endif
endfunction
function! s:init_vars()
if !exists('b:variables')
call sw#session#set_buffer_variable('variables', {})
endif
endfunction
function! sw#variables#set(key, value, ...)
call s:init_vars()
let val = a:value
if a:0
let i = 1
while i <= a:0
execute "let v = a:" . i
if i < a:0 || v != ''
execute "let val = val . ' ' . a:" . i
endif
let i = i + 1
endwhile
endif
call sw#session#set_buffer_variable("variables." . a:key, val)
endfunction
function! sw#variables#list()
call s:init_vars()
echo string(b:variables)
endfunction
function! sw#variables#autocomplete_names(ArgLead, CmdLine, CursorPos)
call s:init_vars()
let words = split('^' . a:CmdLine, '\v\s+')
let result = []
if len(words) == 1 || (len(words) == 2 && !(a:CmdLine =~ '\v\s+$'))
for key in keys(b:variables)
if key =~ a:ArgLead
call add(result, key)
endif
endfor
endif
return result
endfunction
function! sw#variables#unset(key)
call s:init_vars()
call sw#session#unset_buffer_variable('variables.' . a:key)
endfunction
function! sw#variables#get(key)
call s:init_vars()
if has_key(b:variables, a:key)
return b:variables[a:key]
endif
let value = input('Please input the value for ' . a:key . ': ')
call sw#variables#set(a:key, value)
return value
endfunction
function! sw#variables#enable()
call sw#session#unset_buffer_variable('no_variables')
endfunction
function! sw#variables#disable()
call sw#session#set_buffer_variable('no_variables', 1)
endfunction
function! sw#variables#extract(sql)
call s:set_delimiters()
let pattern = g:sw_p_prefix . '\(.\{-\}\)' . g:sw_p_suffix . '\>'
let result = []
let n = 0
let i = match(a:sql, pattern, n)
while i != -1
let l = matchlist(a:sql, pattern, n)
let s = substitute(l[0], '^' . g:sw_p_prefix, '', 'g')
let n = i + strlen(l[0]) + 1
if index(result, s) == -1
call add(result, s)
endif
let i = match(a:sql, pattern, n)
endwhile
return result
endfunction
......@@ -31,6 +31,7 @@ CONTENTS
6. Exporting ................................................ |sw-export|
7. Sessions ............................................... |sw-sessions|
8. Asynchronous ....................................... |sw-asynchronous|
9. Variables ............................................. |sw-variables|
9. Commands ............................................... |sw-commands|
10. Settings .............................................. |sw-settings|
11. Missing features .............................. |sw-missing-features|
......@@ -545,6 +546,63 @@ sent to the DBMS. This only means that once it's finished, vim will simply
ignore its output. This is not a way to stop a wrongly sent *update*
statement. The statement will get execute anyway.
==============================================================================
VARIABLES *sw-variables*
SQL Workbench/J supports user defined variables (you can have your queries
sent to the database parameterized).
This plugin takes advantage of that and implements a few commands to help you
use variables.
By default, in SQL Workbench, the variables are enclosed between $[ and
]. These can be changed.
By default, in VIM SQL Workbench the variable substitution is on. This
means, that when you send a query to the database, the plugin will search for
anything enclosed between the parameter prefix and suffix. Once a match is
found, if a value is defined with |SWVarSet| then the match is replaced with
this value. Please note that exactly the literal is replaced. No quotes are
added and no escaping is executed. If you want quotes, you need to add then in
the value.
If the variable is not defined using |SWVarSet| the plugin will ask for a
value. If you don't want this string to be replaced when the query is sent to
the database, then you can use an empty string as a value. If you want to send
to the database an empty string, then you have to set the value ''.
If you set already a value for a variable, you can always change it by
executing again |SWVarSet|.
A variable can be unset using |SWVarUnset|.
If you don't want the plugin asking doing parameters substitution for a given
buffer, you can call |SWVarDisable|. You can always re-enable the parameter
switching by calling |SWVarEnable|.
Example:
In your workbench.settings file:
***
workbench.sql.parameter.prefix=:
workbench.sql.parameter.suffix=
***
The sql query: select * from table where d = '2015-01-01 00:00:00'.
When launching this query, you will be asked for the value of the *00*
variable. You can just press `enter` and the *:00* will not be replace.
The sql query: select * from table where name = :name.
When launching this query, you will be asked for the value of the *name*
variable. If you enter *'Cosmin Popescu'*, the query sent to the DBMS will be
`select * from table where name = 'Cosmin Popescu'`. Please note that if you
just enter *Cosmin Popescu* (notice the missing quotes), the query sent to the
DBMS will be `select * from table where name = Cosmin Popescu` which will
obviously return an error.
==============================================================================
COMMANDS *sw-commands*
......@@ -861,6 +919,35 @@ this was active when `mksession` was executed.
This command will kill the current command being executed in asynchronous mode.
*SWVarSet*
Parameters:
* the variable name: the name of the variable to be set
* the value: the value that you want to set for this variable
If you want to set a string enclosed between the SQL Workbench/J parameters
suffix and prefix without being substituted, then set it to an empty string.
If you want to replace a parameter with an empty string, set the value of the
variable to ''.
*SWVarUnset*
Unsets a variable
*SWVarDisable*
Disables the replacement of the parameters in the queries sent to the DBMS.
*SWVarEnable*
Enables the replacement of the parameters in the queries sent to the DBMS
(enabled by default).
*SWVarList*
Lists the parameters values
==============================================================================
SETTINGS *sw-settings*
......
......@@ -212,6 +212,12 @@ command! SWSqlAutocompleteWithDefault setlocal omnifunc=sw#autocomplete#perform
command! SWSqlBufferRestore call sw#session#restore_sqlbuffer()
command! -nargs=0 SWKillCurrentCommand call sw#kill_current_command()
command! -nargs=+ -complete=customlist,sw#variables#autocomplete_names SWVarSet call sw#variables#set(<f-args>, '')
command! -nargs=1 -complete=customlist,sw#variables#autocomplete_names SWVarUnset call sw#variables#unset(<f-args>)
command! -nargs=0 SWVarDisable call sw#variables#disable()
command! -nargs=0 SWVarEnable call sw#variables#enable()
command! -nargs=0 SWVarList call sw#variables#list()
augroup sw
autocmd sw BufDelete,BufWipeout * call sw#session#sync()
autocmd sw SessionLoadPost * call sw#session#restore()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册