diff --git a/plugin/.mysql.vim.swp b/plugin/.mysql.vim.swp new file mode 100644 index 0000000..0323d83 Binary files /dev/null and b/plugin/.mysql.vim.swp differ diff --git a/plugin/.test.vim.swp b/plugin/.test.vim.swp new file mode 100644 index 0000000..bc75fd7 Binary files /dev/null and b/plugin/.test.vim.swp differ diff --git a/plugin/mysql.vim b/plugin/mysql.vim new file mode 100644 index 0000000..da22466 --- /dev/null +++ b/plugin/mysql.vim @@ -0,0 +1,103 @@ +python << EOF +import os +import json +import vim +import commands +def run(env,db_name, 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 {} "use {};\n{}"'.format(env, db_name, 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() + 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 . '")') +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 . '")') +endfunction + + +" command! -nargs=0 Rs call RunSql() + +" 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') +nnoremap rs :call RunSqlLine('local') +nnoremap rsp :call RunSqlLine('prod') + + diff --git a/plugin/test.vim b/plugin/test.vim new file mode 100644 index 0000000..82b055c --- /dev/null +++ b/plugin/test.vim @@ -0,0 +1,66 @@ +function! HelloWorld() + echo "hello world" +endfunction + +python << EOF +import os +def helloworld(sql): + print('hello') +EOF + +function! Test() +python << EOF +import vim +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( vim.current.buffer ) +print( vim.current.window ) +print( vim.current.tabpage ) +print( vim.current.range ) + +EOF +endfunction + +" 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 +endfunction + +" command! -nargs=0 HelloWorld call HelloWorld() +" command! -nargs=0 Hpy exec('py hello("ls")') +" command! -nargs=0 Test call Test() +" command! -nargs=0 Gv call Get_visual_selection() +" command! -nargs=0 GV call GetVisualSelection() +