|
1 | 1 | " Plugin: vim-mysql-plugin
|
2 | 2 | " Author: Ke Zhenxu <[email protected]>
|
3 |
| -" License: GPL |
| 3 | +" License: Apache License, Version 2.0 |
4 | 4 | " Origin: https://github.com/kezhenxu94/vim-mysql-plugin
|
5 | 5 |
|
6 | 6 | if exists("g:vim_mysql_plugin_loaded") || &cp
|
@@ -40,50 +40,52 @@ fun! g:GetSelection()
|
40 | 40 | return lines
|
41 | 41 | endfun
|
42 | 42 |
|
43 |
| -fun! g:RunSelection() |
44 |
| - let l:Selection = g:GetSelection() |
45 |
| - if len(l:Selection) == 0 |
| 43 | +fun! g:RunArray(sqlarray, timing) |
| 44 | + if len(a:sqlarray) == 0 |
46 | 45 | echohl Error | echon 'Nothing Selected' | echohl None
|
47 | 46 | return
|
48 | 47 | endif
|
49 |
| - call writefile(l:Selection, '/tmp/vim-mysql-plugin.sql') |
50 | 48 |
|
51 |
| - let l:Command = s:GetCommand() . ' < ' . '/tmp/vim-mysql-plugin.sql' |
52 |
| - let l:Command = escape(l:Command, '%#\`') |
| 49 | + if a:timing |
| 50 | + let l:thesql = ['SELECT NOW(3)+0 INTO @startTime;'] + a:sqlarray + ['SELECT CONCAT(ROUND(NOW(3) - @startTime, 3), "s") Took'] |
| 51 | + else |
| 52 | + let l:thesql = a:sqlarray |
| 53 | + endif |
| 54 | + call writefile(l:thesql, '/tmp/vim-mysql-plugin.sql') |
| 55 | + let l:Command = s:GetCommand() . ' </tmp/vim-mysql-plugin.sql' |
53 | 56 | call g:RunShellCommand(l:Command)
|
54 | 57 | endf
|
55 | 58 |
|
| 59 | +fun! g:RunSelection() |
| 60 | + let l:Selection = g:GetSelection() |
| 61 | + call g:RunArray(l:Selection, 1) |
| 62 | +endfun |
| 63 | + |
| 64 | + |
56 | 65 | func! g:SelectCursorTable()
|
57 |
| - let l:Table = '`' . expand('<cword>') . '`' |
58 |
| - let l:Command = s:GetCommand() . ' -e ' . '"select * from ' . l:Table . '"' |
59 |
| - let l:Command = escape(l:Command, '$!%#\`') |
60 |
| - call g:RunShellCommand(l:Command) |
| 66 | + let l:Table = expand('<cword>') |
| 67 | + call RunArray(['SELECT * FROM `' . l:Table . '` LIMIT 100;'], 0) |
61 | 68 | endfun
|
62 | 69 |
|
63 | 70 | func! g:DescriptCursorTable()
|
64 |
| - let l:Table = '`' . expand('<cword>') . '`' |
65 |
| - let l:Command = s:GetCommand() . ' -e ' . '"show full columns from ' . l:Table . '"' |
66 |
| - let l:Command = escape(l:Command, '$!%#\`') |
67 |
| - call g:RunShellCommand(l:Command) |
| 71 | + let l:Table = expand('<cword>') |
| 72 | + call RunArray(['SHOW FULL COLUMNS FROM `' . l:Table . '`;'], 0) |
68 | 73 | endfun
|
69 | 74 |
|
70 | 75 | fun! g:RunInstruction()
|
71 | 76 | let l:PrevSemicolon = search(';', 'bnW')
|
72 | 77 | let l:NextSemicolon = search(';', 'nW')
|
73 | 78 | let l:Lines = getline(l:PrevSemicolon, l:NextSemicolon)[1:]
|
74 |
| - let l:Lines = map(l:Lines, "substitute(v:val, '--.*$', '', 'g')") |
75 |
| - let l:CurrentInstruction = join(l:Lines, ' ') |
76 |
| - let l:Command = s:GetCommand() . ' -e "' . l:CurrentInstruction . '"' |
77 |
| - let l:Command = escape(l:Command, '$!%#\`') |
78 |
| - call g:RunShellCommand(l:Command) |
| 79 | + call g:RunArray(l:Lines, 1) |
79 | 80 | endfun
|
80 | 81 |
|
81 | 82 | fun! s:GetCommand()
|
82 | 83 | let l:Command = 'mysql '
|
83 | 84 | let l:LineNum = 1
|
84 | 85 | let l:Line = getline(l:LineNum)
|
85 | 86 | while l:Line != '--'
|
86 |
| - let l:Command .= substitute(l:Line, '^--\s*\(.*\)$', '\1', 'g') |
| 87 | + let l:arg = shellescape(substitute(l:Line, '^--\s*\(.*\)$', '\1', 'g')) |
| 88 | + let l:Command .= l:arg . ' ' |
87 | 89 | let l:LineNum = l:LineNum + 1
|
88 | 90 | let l:Line = getline(l:LineNum)
|
89 | 91 | endwhile
|
|
0 commit comments