From 55136d460cd51a746016e8bbd3c468b2dee79548 Mon Sep 17 00:00:00 2001 From: wenxiaoning Date: Fri, 27 Oct 2017 19:04:01 +0800 Subject: [PATCH] add-autoload --- autoload/mysql.vim | 68 +++++++++++++++++++++++++++++++++ plugin/mysql.vim | 93 ++-------------------------------------------- plugin/test.vim | 2 +- 3 files changed, 73 insertions(+), 90 deletions(-) create mode 100644 autoload/mysql.vim diff --git a/autoload/mysql.vim b/autoload/mysql.vim new file mode 100644 index 0000000..e054640 --- /dev/null +++ b/autoload/mysql.vim @@ -0,0 +1,68 @@ +python << EOF +import os +import json +import vim +import commands +def run(env, sql): + cur_buf = vim.current.buffer + if 'prod' in str(cur_buf): + env = 'prod' + + sql = sql.strip("'") + sql = sql.replace('`', '\`') + sql = sql.replace("'\''", "'") + cmd = 'mysql_run {} "{}"'.format(env, sql) + s, msg = commands.getstatusoutput(cmd) + + show_list = msg.split('\n')[15:-3] + show_msg = '\n'.join(show_list) + + def _fmt(o): + o = o.strip() + item = dict(text=o) + return item + show_list = [_fmt(o) for o in show_list] + vim.command('call setqflist({}, "r")'.format(json.dumps(show_list))) + vim.command(':copen 35') +EOF + +function! s: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:] + return join(lines, "\n") +endfunction + + + +function! mysql#RunSqlVisual(env) + " echo s:GetVisualSelection() + let l:vm = visualmode() + normal! `y + let b:sql = @@ + let b:sql = shellescape(b:sql) + silent exec('py run("'. a:env . '","' . b:sql . '")') +endfunction + +function! mysql#RunSqlLine(env) + let b:sql = getline(line('.')) + exec('py run("'. a:env . '","' . b:sql . '")') +endfunction + + +" command! -nargs=0 Rs call RunSql() + +" vnoremap r :call RunVisual() +" autocmd FileType sql nnoremap r :call RunLine() + + diff --git a/plugin/mysql.vim b/plugin/mysql.vim index 512e031..2cb031a 100644 --- a/plugin/mysql.vim +++ b/plugin/mysql.vim @@ -1,92 +1,7 @@ -python << EOF -import os -import json -import vim -import commands -def run(env, sql): - cur_buf = vim.current.buffer - if 'prod' in str(cur_buf): - env = 'prod' - sql = sql.strip("'") - sql = sql.replace('`', '\`') - sql = sql.replace("'\''", "'") - cmd = 'mysql_run {} "{}"'.format(env, sql) - s, msg = commands.getstatusoutput(cmd) - - show_list = msg.split('\n')[15:-3] - show_msg = '\n'.join(show_list) - - def _fmt(o): - o = o.strip() - item = dict(text=o) - return item - show_list = [_fmt(o) for o in show_list] - vim.command('call setqflist({}, "r")'.format(json.dumps(show_list))) - vim.command(':copen 35') -EOF - -function! s: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:] - return join(lines, "\n") -endfunction - - -function! RunSql(env) - let l:vm = visualmode() - let b:db_name = g:mysql_local_db_name - if a:env ==# 'prod' - echo 'use prod' - let b:db_name = g:mysql_prod_db_name - echo 'use ' . b:db_name - endif - if l:vm ==# 'V' || l:vm ==# 'v' - normal! `y - let b:sql = shellescape(@") - exec('py run(' . b:sql . ')') - else - " let b:sql = shellescape(getline(line('.'))) - let b:sql = getline(line('.')) - exec('py run("'. a:env . '","' . b:db_name . - \'","' . getline(line('.')) . '")') - endif -endfunction - -function! RunSqlVisual(env) - " echo s:GetVisualSelection() - let l:vm = visualmode() - normal! `y - let b:sql = @@ - let b:sql = shellescape(b:sql) - silent exec('py run("'. a:env . '","' . b:sql . '")') -endfunction - -function! RunSqlLine(env) - let b:sql = getline(line('.')) - exec('py run("'. a:env . '","' . b:sql . '")') -endfunction - - -" command! -nargs=0 Rs call RunSql() - -" vnoremap r :call RunVisual() -" autocmd FileType sql nnoremap r :call RunLine() - -vnoremap rs :call RunSqlVisual('local') -vnoremap rsp :call RunSqlVisual('prod') -nnoremap rs :call RunSqlLine('local') -nnoremap rsp :call RunSqlLine('prod') +vnoremap rs :call mysql#RunSqlVisual('local') +vnoremap rsp :call mysql#RunSqlVisual('prod') +nnoremap rs :call mysql#RunSqlLine('local') +nnoremap rsp :call mysql#RunSqlLine('prod') diff --git a/plugin/test.vim b/plugin/test.vim index 143cecf..ce26501 100644 --- a/plugin/test.vim +++ b/plugin/test.vim @@ -26,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() +" call s:Test()