tpope / vim-dadbod

dadbod.vim: Modern database interface for Vim
https://www.vim.org/scripts/script.php?script_id=5665
3.75k stars 132 forks source link

sqlcmd (Sql Server adapter) output - add option to use additional filter - feature request #97

Closed bybor closed 3 years ago

bybor commented 3 years ago

The issue is probably specific to sqlcmd - if the column is varchar(256) then it will take 256 characters no matter if content is shorter.

There is -W command line parameter (and it's used in function! s:complete(url, query) abort, file sqlserver.vim) which removes trailing spaces. This doesn't work great since the visual columns are lost.

I found xsv tool that does "elastic tabstops" (https://github.com/BurntSushi/xsv), which could be used as additional filter on sqlcmd's output. The point is to make every column long enough to fit every cell's content, but not longer. sqlcmd allows to use a custom column delimiter with -s parameter,

Here is a pseudo code:

sqlcmd -W Q "set nocount on; ..." -s"^" > sql_output.txt
xsv.exe table -d ^ -o gg.txt sql_output.txt

set nocount on; is also important (and it is also used in s:complete function, this removes a message X rows affected from the output.

Could you extend vim-dadbod please

What I really want is a better way for displaying query results, the above is just one approach that I tried to implement wrapping sqlcmd in extra cmd (windows) file and failed.

Thanks!

tpope commented 3 years ago
  • add option to use additional "filter" (as xsv) against the output before showing it. It may be better to use a file and not a pipe

The other stuff is maybe but this would require significant retooling and is almost certainly a no.

What I really want is a better way for displaying query results, the above is just one approach that I tried to implement wrapping sqlcmd in extra cmd (windows) file and failed.

With just the set nocount on option, I would expect making a wrapper script to be a lot more tenable. I would encourage you to try adding a hard-coded version and see if you can crack this nut.

function! db#adapter#sqlserver#massage(input) abort
  return "SET NOCOUNT ON;" . a:input
endfunction
bybor commented 3 years ago

Thanks, makes sense.

The above function! db#adapter#sqlserver#massage(input) abort doesn't seem to work for me. I assumed that it gets called for every query executed with :DB, is it right?

If I add function! db#adapter#sqlserver#massage(input) abort into vim-dadbod\autoload\db\adapter\sqlserver.vim is it enough?

I don't understand how it's called. I can see massage functions in other adapters, but cannot figure it out.

Thank you for help!

tpope commented 3 years ago

Looks like it doesn't get called for operations on a range like :%DB. Try passing a string argument and see if that works.

bybor commented 3 years ago

Yes, it does. Before that I ran DB against visually selected lines.

Could you tell if there is a way to apply set nocount on; everywhere "sql server"?

Thanks

tpope commented 3 years ago

There's not, and there's not really a good way to add that, because sometimes we directly pass a file name (it can give better error messages). I'm afraid you may be stuck with figuring out how to write a wrapper script. I can't speak for cmd, but if you escalated to something like python, it's probably pretty doable. Check for -i as the next to last argument, and if it's found, replace the last argument with a temp file that adds the set nocount on;.

bybor commented 3 years ago

Thanks!