From 60bbe71b75fb5d7802cb5e0adf59d7cd04540be7 Mon Sep 17 00:00:00 2001 From: wenxiaoning Date: Thu, 26 Oct 2017 10:53:10 +0800 Subject: [PATCH] remove-file-db --- README.md | 3 ++- install.sh | 4 ---- plugin/mysql.vim | 21 +++++--------------- plugin/mysql_run | 9 +++++++-- plugin/test.vim | 51 +++++++----------------------------------------- 5 files changed, 21 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 2583510..71d112c 100644 --- a/README.md +++ b/README.md @@ -36,10 +36,11 @@ $ ./install.sh echo 'export MYSQL_LOCAL_USER=your_local_user' >> ~/.bash_profile echo 'export MYSQL_LOCAL_PASSWORD=your_local_password' >> ~/.bash_profile echo 'export MYSQL_LOCAL_HOST=your_local_host' >> ~/.bash_profile +echo 'export MYSQL_LOCAL_DB=your_local_db' >> ~/.bash_profile source ~/.bash_profile ``` 这样可以配置 ***local*** 环境的 Mysql 信息,如果需要 ***prod*** 环境 -可以继续配置 `MYSQL_PROD_USER, MYSQL_PROD_PASSWORD, MYSQL_PROD_HOST` +可以继续配置 `MYSQL_PROD_USER, MYSQL_PROD_PASSWORD, MYSQL_PROD_HOST, MYSQL_LOCAL_DB` ## 使用 ```bash diff --git a/install.sh b/install.sh index 50765ee..381e40e 100755 --- a/install.sh +++ b/install.sh @@ -1,6 +1,2 @@ - VIM_MYSQL=`pwd` -echo $VIM_MYSQL -echo `pwd` - ln -sf ${VIM_MYSQL}/plugin/mysql_run /usr/local/bin/mysql_run diff --git a/plugin/mysql.vim b/plugin/mysql.vim index da22466..512e031 100644 --- a/plugin/mysql.vim +++ b/plugin/mysql.vim @@ -3,7 +3,7 @@ import os import json import vim import commands -def run(env,db_name, sql): +def run(env, sql): cur_buf = vim.current.buffer if 'prod' in str(cur_buf): env = 'prod' @@ -11,8 +11,9 @@ def run(env,db_name, sql): sql = sql.strip("'") sql = sql.replace('`', '\`') sql = sql.replace("'\''", "'") - cmd = 'mysql_run {} "use {};\n{}"'.format(env, db_name, sql) + cmd = 'mysql_run {} "{}"'.format(env, sql) s, msg = commands.getstatusoutput(cmd) + show_list = msg.split('\n')[15:-3] show_msg = '\n'.join(show_list) @@ -66,25 +67,15 @@ endfunction function! RunSqlVisual(env) " echo s:GetVisualSelection() let l:vm = visualmode() - let b:db_name = g:mysql_local_db_name - if a:env ==# 'prod' - let b:db_name = g:mysql_prod_db_name - endif normal! `y let b:sql = @@ let b:sql = shellescape(b:sql) - silent exec('py run("'. a:env . '","' . b:db_name . '","' . b:sql . '")') + silent exec('py run("'. a:env . '","' . b:sql . '")') endfunction function! RunSqlLine(env) - let b:db_name = g:mysql_local_db_name - if a:env ==# 'prod' - let b:db_name = g:mysql_prod_db_name - endif let b:sql = getline(line('.')) - " let b:sql = shellescape(b:sql) - exec('py run("'. a:env . '","' . b:db_name . '","' . b:sql . '")') - " exec('py run("'. a:env . '","' . b:db_name . '","' . b:sql . '")') + exec('py run("'. a:env . '","' . b:sql . '")') endfunction @@ -92,8 +83,6 @@ endfunction " vnoremap r :call RunVisual() " autocmd FileType sql nnoremap r :call RunLine() -let g:mysql_local_db_name = 'tmddev' -let g:mysql_prod_db_name = 'tmdprd' vnoremap rs :call RunSqlVisual('local') vnoremap rsp :call RunSqlVisual('prod') diff --git a/plugin/mysql_run b/plugin/mysql_run index 04063ea..53b6b10 100755 --- a/plugin/mysql_run +++ b/plugin/mysql_run @@ -13,17 +13,22 @@ if { $ENV == "local" } { set name $env(MYSQL_LOCAL_USER) set password $env(MYSQL_LOCAL_PASSWORD) set host $env(MYSQL_LOCAL_HOST) + set db $env(MYSQL_LOCAL_DB) } if { $ENV == "prod" } { set name $env(MYSQL_PROD_USER) set password $env(MYSQL_PROD_PASSWORD) set host $env(MYSQL_PROD_HOST) + set db $env(MYSQL_PROD_DB) } -spawn sh -c "mysql -u$name -p -h ${host} --tee=/data/log/mysql/`date +%Y-%m-%d`.log" +# spawn sh -c "mysql -u$name -p -h ${host} -A --tee=/data/log/mysql/`date +%Y-%m-%d`.log" +spawn sh -c "mysql -u$name -p -h ${host} -A --tee=~/.mysql_result" expect "*assword:*" send "$password\r" expect "*>" -send "$sql;\r" +send "use $db;\r" +expect "*>" +send "$sql\r" send "exit;\r" interact diff --git a/plugin/test.vim b/plugin/test.vim index 82b055c..143cecf 100644 --- a/plugin/test.vim +++ b/plugin/test.vim @@ -1,61 +1,24 @@ -function! HelloWorld() - echo "hello world" -endfunction - -python << EOF -import os -def helloworld(sql): - print('hello') -EOF - -function! Test() +function! s:Test() python << EOF import vim +import commands cur_buf = vim.current.buffer print("cur_buf {}".format(cur_buf)) print "Lines: {0}".format(len(cur_buf)) print "Contents: {0}".format(cur_buf[-1]) -print( vim.current.line ) +print("cur_line {}".format(vim.current.line )) print( vim.current.buffer ) print( vim.current.window ) print( vim.current.tabpage ) print( vim.current.range ) -EOF -endfunction +s, msg = commands.getstatusoutput('ls') +print(msg) -" function! GetVisualSelection() - " " Why is this not a built-in Vim script function?! - " let [line_start, column_start] = getpos("'<")[1:2] - " let [line_end, column_end] = getpos("'>")[1:2] - " echo line_start - " echo column_start - " echo line_end - " echo column_end - " let lines = getline(line_start, line_end) - " if len(lines) == 0 - " return '' - " endif - " let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)] - " let lines[0] = lines[0][column_start - 1:] - " echo join(lines, "\n") - " return join(lines, "\n") -" endfunction -function! Bar() abort - bar -endfunction -function! Foo() - try - call Bar() - catch /.*/ - let bt = lh#exception#callstack(v:throwpoint) - let data = map(copy(bt), '{"filename": v:val.script, "text": "called from here", "lnum": v:val.pos}') - let data[0].text = v:exception - call setqflist(data) - endtry +EOF endfunction " command! -nargs=0 HelloWorld call HelloWorld() @@ -63,4 +26,4 @@ endfunction " command! -nargs=0 Test call Test() " command! -nargs=0 Gv call Get_visual_selection() " command! -nargs=0 GV call GetVisualSelection() - +call s:Test()