Skip to content

Instantly share code, notes, and snippets.

@bootleq
Created June 12, 2015 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bootleq/deb69768b798953faeb2 to your computer and use it in GitHub Desktop.
Save bootleq/deb69768b798953faeb2 to your computer and use it in GitHub Desktop.
Use vim-quickrun as SQL client
" First, install thinca/vim-quickrun
"
" https://github.com/thinca/vim-quickrun
"
" Then write some vimrc:
"
function! s:init_quickrun()
if executable("psql")
function! s:quickrun_sql_config() "{{{
let g:quickrun_config['sql'] = {
\ 'outputter/buffer/into': 0,
\ 'outputter/buffer/name': '[QR] %{expand("%:t")} \@%{strftime("%T")}'
\ }
" Main mappings go here:
nmap <buffer> <Leader>r [quickrun]
nnoremap <silent> <buffer> [quickrun]j :call <SID>quickrun_sql_run('j')<CR>
nnoremap <silent> <buffer> [quickrun]l :call <SID>quickrun_sql_run('l')<CR>
nnoremap <silent> <buffer> [quickrun]r :call <SID>quickrun_sql_run('last')<CR>
endfunction "}}}
let s:hook = {
\ 'name': 'psql_extra',
\ 'kind': 'hook',
\ 'config': {
\ 'enabled': 1
\ }
\ }
function! s:hook.on_output(session, context) "{{{
if self.config.enabled
let text = a:context.data
let cmdopt = get(a:session.config, 'cmdopt', '')
let buffer_nr = winnr('$')
if !empty(cmdopt)
let columns = matchstr(cmdopt, 'columns=\zs\d\+')
let expanded = matchstr(cmdopt, 'expanded=\zs\w\+')
if columns > 0 && !empty(expanded)
" Add blank line between records
let title_pattern = '-\[ RECORD \d\+ \]-[^\n]\+'
let text = substitute(text, '\ze\n' . title_pattern, '\n', 'g')
let a:context.data = text
let last_col = len(matchstr(text, title_pattern))
if last_col && last_col < winwidth(buffer_nr)
execute buffer_nr . 'wincmd w'
" narrow down to release unused space
execute 'vertical resize ' . last_col
wincmd p
endif
endif
endif
endif
endfunction "}}}
function! s:hook.on_outputter_buffer_opened(session, context) "{{{
if self.config.enabled
setlocal nonumber nowrap sidescrolloff=0
nnoremap <buffer> <Leader>r [noop]
command! -buffer PGExplanTimeFormat call <SID>postgres_explan_time_format()
endif
endfunction "}}}
call quickrun#module#register(s:hook, 1)
unlet s:hook
function! s:quickrun_sql_run(method) "{{{
let method = a:method == 'last' ? get(s:, 'quickrun_sql_run_last_method', 'j') : a:method
let b:quickrun_db_name = get(b:, 'quickrun_db_name', '')
let split = ''
let cmdopt = ''
if empty(b:quickrun_db_name)
if !exists('b:rails_root')
call RailsDetect()
endif
if exists('b:rails_root')
let b:quickrun_db_name = rails#app().db_config('development').database
endif
endif
if empty(b:quickrun_db_name)
echohl WarningMsg | echomsg "Missing database config." | echohl None
return
endif
let cmdopt = '-d ' . b:quickrun_db_name . ' -P pager=off -P format=wrapped -P expanded=auto'
call <SID>quit_winodws_by_filetype('^quickrun') " close previous output buffer
if method == 'j'
let s:quickrun_sql_run_last_method = method
let split = 'silent botright 16split'
elseif method == 'l'
let s:quickrun_sql_run_last_method = method
let split = 'silent botright 78vsplit'
let cmdopt .= ' -P columns=78'
else
echohl WarningMsg | echomsg "Unknown quickrun helper run method." | echohl None
endif
execute printf(
\ "QuickRun -cmdopt '%s' -outputter/buffer/split '%s'",
\ cmdopt,
\ split
\ )
endfunction "}}}
autocmd FileType sql call s:quickrun_sql_config()
endif
endfunction
call s:init_quickrun()
" Convert PostgreSQL EXPLAN time range to time diff {{{2
function! s:postgres_explan_time_format()
" Show SQL EXPLAN ANALYZE time as 'time difference'
execute '%substitute/' .
\ '\v(cost|actual time)' . '\=([0-9.]+)' . '\.{2}' . '([0-9.]+)' . '[^\)]+' .
\ '/\=toupper(matchstr(submatch(1), ''\w\+'')) . ": " . (str2nr(submatch(3)) - submatch(2))/g'
endfunction
" quit quick window (just a helper fucntion)
function! s:quit_winodws_by_filetype(...) "{{{
let win_count = winnr('$')
for filetype_pattern in a:000
silent windo if &filetype =~ filetype_pattern | execute "quit" | endif
endfor
execute 'wincmd t'
return win_count > winnr('$')
endfunction "}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment