Skip to content

Commit

Permalink
remove-file-db
Browse files Browse the repository at this point in the history
  • Loading branch information
wenxiaoning committed Oct 26, 2017
1 parent ba75a77 commit 60bbe71
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 67 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@

VIM_MYSQL=`pwd`
echo $VIM_MYSQL
echo `pwd`

ln -sf ${VIM_MYSQL}/plugin/mysql_run /usr/local/bin/mysql_run
21 changes: 5 additions & 16 deletions plugin/mysql.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ 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'

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)

Expand Down Expand Up @@ -66,34 +67,22 @@ 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! `<v`>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


" command! -nargs=0 Rs call RunSql()

" vnoremap <buffer> <leader>r <delete><delete><delete><delete><delete>:call RunVisual()<cr>
" autocmd FileType sql nnoremap <buffer> <leader>r :call RunLine()<cr>
let g:mysql_local_db_name = 'tmddev'
let g:mysql_prod_db_name = 'tmdprd'

vnoremap <leader>rs :<c-u>call RunSqlVisual('local')<cr>
vnoremap <leader>rsp :<c-u>call RunSqlVisual('prod')<cr>
Expand Down
9 changes: 7 additions & 2 deletions plugin/mysql_run
Original file line number Diff line number Diff line change
Expand Up @@ -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
51 changes: 7 additions & 44 deletions plugin/test.vim
Original file line number Diff line number Diff line change
@@ -1,66 +1,29 @@
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()
" 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()

call s:Test()

0 comments on commit 60bbe71

Please sign in to comment.