├── ftplugin └── sql.vim ├── plugin └── simpledb.vim └── README.md /ftplugin/sql.vim: -------------------------------------------------------------------------------- 1 | if get(g:, 'simpledb_use_default_keybindings', 1) 2 | vnoremap :SimpleDBExecuteSql 3 | nnoremap m':SimpleDBExecuteSql g`' 4 | nnoremap m':'{,'}SimpleDBExecuteSqlg`' 5 | endif 6 | -------------------------------------------------------------------------------- /plugin/simpledb.vim: -------------------------------------------------------------------------------- 1 | if !exists('g:simpledb_show_timing') | let g:simpledb_show_timing = 1 | en 2 | 3 | function! s:GetQuery(first, last) 4 | let query = '' 5 | let lines = getline(a:first, a:last) 6 | for line in lines 7 | let fragment = matchstr(line, '\(.*\)\(--.*\)\?') 8 | if !empty(fragment) 9 | let query .= fragment . "\n" 10 | endif 11 | endfor 12 | return query 13 | endfunction 14 | 15 | function! s:ShowResults() 16 | let source_buf_nr = bufnr('%') 17 | 18 | if !exists('s:result_buf_nr') 19 | let s:result_buf_nr = -1 20 | endif 21 | 22 | if bufwinnr(s:result_buf_nr) > 0 23 | exec bufwinnr(s:result_buf_nr) . "wincmd w" 24 | else 25 | exec 'silent! botright sview +setlocal\ noswapfile\ autoread /tmp/vim-simpledb-result.txt' 26 | let s:result_buf_nr = bufnr('%') 27 | endif 28 | 29 | exec bufwinnr(source_buf_nr) . "wincmd w" 30 | endfunction 31 | 32 | function! simpledb#ExecuteSql() range 33 | let conprops = matchstr(getline(1), '--\s*\zs.*') 34 | let adapter = matchlist(conprops, 'db:\(\w\+\)') 35 | let conprops = substitute(conprops, "db:\\w\\+", "", "") 36 | let query = s:GetQuery(a:firstline, a:lastline) 37 | 38 | if len(adapter) > 1 && adapter[1] == 'mysql' 39 | let cmdline = s:MySQLCommand(conprops, query) 40 | else 41 | let cmdline = s:PostgresCommand(conprops, query) 42 | endif 43 | 44 | silent execute '!(' . cmdline . ' > /tmp/vim-simpledb-result.txt 2>&1)' 45 | call s:ShowResults() 46 | redraw! 47 | endfunction 48 | 49 | function! s:MySQLCommand(conprops, query) 50 | let sql_text = shellescape(a:query, 1) 51 | let cmdline = 'echo -e ' . sql_text . '| mysql -v -v -v -t ' . a:conprops 52 | return cmdline 53 | endfunction 54 | 55 | function! s:PostgresCommand(conprops, query) 56 | if g:simpledb_show_timing == 1 57 | let sql_text = shellescape('\\timing on \\\ ' . a:query, 1) 58 | else 59 | let sql_text = shellescape(a:query, 1) 60 | end 61 | 62 | let cmdline = 'echo -e ' . sql_text . '| psql ' . a:conprops 63 | return cmdline 64 | endfunction 65 | 66 | command! -range=% SimpleDBExecuteSql ,call simpledb#ExecuteSql() 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vim-simpledb 2 | 3 | Vim plugin to execute postgresql or mysql commands from VIM buffer 4 | 5 | ## Installation 6 | 7 | ### Pathogen 8 | 9 | If you use git submodules, run this command from your .vim folder: 10 | 11 | git submodule add https://github.com/ivalkeen/vim-simpledb bundle/simpledb 12 | 13 | Otherwise, , run this command from your .vim folder: 14 | 15 | git clone https://github.com/ivalkeen/vim-simpledb bundle/simpledb 16 | 17 | ### Vundle 18 | 19 | Add this line to your vimrc 20 | 21 | Bundle 'ivalkeen/vim-simpledb' 22 | 23 | ## Usage 24 | 25 | Default key mapping for execution: ``. 26 | 27 | 1. Create new file with .sql extension (without extensions, mapping would not work) 28 | 29 | 2. Create first line with commented parameters: 30 | 31 | for psql: 32 | 33 | `-- -h localhost -U postgres -d my_database` 34 | 35 | or for mysql: 36 | 37 | `-- db:mysql -D my_database -u root` 38 | 39 | Note: if you don't want to enter password each time, you should create .pgpass (.my.cnf for mysql) file 40 | 41 | There is also usefull key `-q` to avoid messages like 'Timing is on' etc. 42 | 43 | 3. Add sql statements to your file 44 | 45 | 4. Hit `` to execute all not commented queries 46 | 47 | 5. Hit `` to execute all queries in the current paragraph 48 | 49 | 6. Select multiple lines in visual mode and hit `` to execute just those queries 50 | 51 | ## Configuration 52 | 53 | If you do not want timings to be displayed, add this to your `.vimrc`: 54 | 55 | let g:simpledb_show_timing = 0 56 | 57 | If you want to customize the key mappings, 58 | set the variable `simpledb_use_default_keybindings` to 0 and add your own mappings in `~/.vimrc`. 59 | For example, to execute a sql statement by pressing "ctrl-q" instead of "enter", 60 | add the following: 61 | 62 | let g:simpledb_use_default_keybindings=0 63 | vnoremap :SimpleDBExecuteSql 64 | nnoremap m':SimpleDBExecuteSql g`' 65 | nnoremap m':'{,'}SimpleDBExecuteSqlg`' 66 | 67 | If you have any questions, [mail me](mailto:itkalin@gmail.com) 68 | 69 | ## TODO 70 | 71 | 1. Rewrite code to match vim script conventions 72 | 2. Intellisense 73 | 74 | ## Contributing 75 | 76 | 1. Fork it 77 | 2. Create your feature branch (`git checkout -b my-new-feature`) 78 | 3. Commit your changes (`git commit -am 'Added some feature'`) 79 | 4. Push to the branch (`git push origin my-new-feature`) 80 | 5. Create new Pull Request 81 | 82 | ## Self-Promotion 83 | 84 | If you like this project, please follow the repository on [GitHub](https://github.com/ivalkeen/vim-simpledb). Also, you might consider visiting my [blog](http://www.tkalin.com) and following me on [Twitter](https://twitter.com/ivalkeen) and [Github](https://github.com/ivalkeen). 85 | 86 | 87 | [1]: http://i.imgur.com/1UrMOpd.png 88 | [2]: https://github.com/kien/ctrlp.vim 89 | [3]: https://github.com/gmarik/vundle 90 | 91 | --------------------------------------------------------------------------------