kristijanhusak / vim-dadbod-ui

Simple UI for https://github.com/tpope/vim-dadbod
MIT License
1.54k stars 96 forks source link

Running query as multiple-line selection gives out error #56

Closed ichirou2910 closed 4 years ago

ichirou2910 commented 4 years ago

image

I found issue #30 about executing only selection as query, and while single line query works just fine, multiple-line doesn't seems to work and always throws out error as in the image. ERROR: Can't initialize batch_readline - may be the input source is a directory or a block device. It would be quite uncomfortable to move those lines to another query then execute them as a whole. Do you have any idea?

kristijanhusak commented 4 years ago

@ichirou2910 does executing the whole buffer work?

ichirou2910 commented 4 years ago

Yes it works fine

kristijanhusak commented 4 years ago

Can you just give me the format of your connection url? For example:

mysql://user:pass@host:port/dbname
ichirou2910 commented 4 years ago

mysql://user@host/dbname Edit: sry got the wrong one

kristijanhusak commented 4 years ago

OK thanks, I'll look into it.

kristijanhusak commented 4 years ago

I'm not able to reproduce this. I'll need more info.

I found this regarding the error https://stackoverflow.com/questions/28452021/error-cant-initialize-batch-readline-may-be-the-input-source-is-a-directory

But i can't figure out what's wrong.

If you can, test this scenario and let me know if it works:

Does it work?

ichirou2910 commented 4 years ago
kristijanhusak commented 4 years ago

@ichirou2910 I'll need you to test 2 more things:

  1. Select multiple lines and execute :DB command on that visual selection. Basically just use vim-dadbod's functionality.
  2. If that works, Try setting let g:dbui_tmp_query_location to some location, for example:
    let g:dbui_tmp_query_location = '~/Documents/queries`

    Restart neovim and give it a test from start. This will create queries in that folder instead of using temp folder.

It's really confusing that thing from previous comment (DB < filename) works fine, and this isn't, because i'm doing the same thing internally.

Just try these 2 things and let me know how it goes.

kristijanhusak commented 4 years ago

@ichirou2910 I added a debug mode in order to get some information around things that are happening in queries.

Add let g:db_ui_debug = 1 to your init.vim, and reproduce the bug. Then give me the output of :messages.

ichirou2910 commented 4 years ago

@kristijanhusak Hi, took some time looking into your code and found that you parsed selected lines to another file. You can use '<,'> prefix to do that instead. I created a pull request for that, can you check it out? https://github.com/kristijanhusak/vim-dadbod-ui/pull/57

kristijanhusak commented 4 years ago

@ichirou2910 I know, but I'm doing that for a reason, and vim-dadbod does the same thing internally. Can you debug it with current functionality and see what's happening? Thanks.

ichirou2910 commented 4 years ago

That I tried too. Here are the what I found out

kristijanhusak commented 4 years ago

What happens when you try to execute the command part from the debug message? In this case :DB < /tmp/nvim32zTHl/3, but path will different on next run.

ichirou2910 commented 4 years ago

It's the same image Somehow when I created a file containing the same queries in home folder it works image

kristijanhusak commented 4 years ago

can you cat that file?

I just pushed a small change to add an extension to the temp name. Can you give it another try?

ichirou2910 commented 4 years ago

Here you go image I pretty much believe this is because you create temp file in /tmp. I changed this line of code:

function! s:query.execute_lines(db, lines, is_visual_mode) abort
  let filename = tempname().'.'.db#adapter#call(a:db.conn, 'input_extension', [], 'sql')

to:

function! s:query.execute_lines(db, lines, is_visual_mode) abort
  let filename = 'temp.sql'

And it worked just fine

kristijanhusak commented 4 years ago

That shouldn't be the issue, because dadbod does exactly same thing when executing queries https://github.com/tpope/vim-dadbod/blob/master/autoload/db.vim#L213

If you execute :DB on visual selection, and check the content of the tmp folder (/tmp/nvim****/), you will see an .sql file that dadbod generated to execute. That's what confuses me here.

Can you try that and then manually execute that sql file generated by dadbod with same command (:DB < /tmp/nvim****/dadbodsql.sql) ?

ichirou2910 commented 4 years ago

'kay I'm so confused rn :confused: image As expected, :'<,'>DB worked while :DB < /tmp/nvim*****/sqlfile didn't work

kristijanhusak commented 4 years ago

Yeah, same :/

I'll need your help to debug it, since i can't reproduce it.

First, pull latest changes. Then, go to vim-dadbod folder, and add echom string(cmd) after this line https://github.com/tpope/vim-dadbod/blob/master/autoload/db.vim#L114

Then, open up the query buffer, and do first visual selection command ':<,'>DB, and then try doing it with dadbod-ui mapping. Compare the two, and if possible, send me both here.

Also, try executing that command directly from terminal to see what will happen.

ichirou2910 commented 4 years ago

Mesages when use <leader>S (didn;t work)

[DBUI Debug] {'input_filename': '/tmp/nvimcLLTiS/4.sql', 'message': 'Executing multiple lines', 'lines': ['des
cribe student;', 'describe teacher;'], 'command': 'DB < /tmp/nvimcLLTiS/4.sql'}
'mysql -h ''localhost'' -u ''<username>'' ''-p<password>'' ''test'' -t < ''/home/ichirou2910/'''

Messages when use :'<,'>DB (worked)

'mysql -h ''localhost'' -u ''<username>'' ''-p<password>'' ''test'' -t < ''/tmp/nvimcLLTiS/8.sql'''

Mesages when use :DB < /tmp/nvim.... (didn't work)

'mysql -h ''localhost'' -u ''<username>'' ''-p<password>'' ''test'' -t < ''/home/ichirou2910/'''

Mesages when use :DB < test.sql (worked)

'mysql -h ''localhost'' -u ''<username>'' ''-p<password>'' ''test'' -t < ''/home/ichirou2910/test.sql'''

And mysql -u <username> -p <password> test < /tmp/nvim... worked

kristijanhusak commented 4 years ago

For some reason it is not properly figuring out the input file in some cases, and it defaults to home folder.

Lets try this. Add this line:

echom 'MAYBE INFILE '.string(maybe_infile)

After this line https://github.com/tpope/vim-dadbod/blob/master/autoload/db.vim#L215

And give it another test. Let me know what it prints out for this line.

ichirou2910 commented 4 years ago

Mesages when use <leader>S (didn;t work)

[DBUI Debug] {'input_filename': '/tmp/nvimU2VYBz/4.sql', 'message': 'Executing multiple lines', 'lines': ['describe employee;', 'des
cribe student;'], 'command': 'DB < /tmp/nvimU2VYBz/4.sql'}
MAYBE INFILE '/tmp/nvimU2VYBz/4.sql'
'mysql -h ''localhost'' -u ''username '' ''-ppassword'' ''test'' -t < ''/home/ichirou2910/'''

Messages when use :'<,'>DB (worked)

MAYBE INFILE ''
'mysql -h ''localhost'' -u ''username'' ''-ppassword'' ''test'' -t < ''/tmp/nvimU2VYBz/8.sql'''

Mesages when use :DB < /tmp/nvim.... (didn't work)

MAYBE INFILE '/tmp/nvimU2VYBz/4.sql'
'mysql -h ''localhost'' -u ''username'' ''-ppassword'' ''test'' -t < ''/home/ichirou2910/'''

Mesages when use :DB < test.sql (worked)

MAYBE INFILE 'test.sql'
'mysql -h ''localhost'' -u ''username'' ''-ppassword'' ''test'' -t < ''/home/ichirou2910/test.sql'''
kristijanhusak commented 4 years ago

So confusing...

Sorry for bothering you so much, but this is really strange. I'll need few more things:

  1. What's the output for you when you do echo expand('/tmp/nvimU2VYBz/4.sql')? Put valid tmp path in there just to be sure.
  2. After this line https://github.com/tpope/vim-dadbod/blob/master/autoload/db.vim#L259, add this:
    echom 'INFILE BEFORE '.string(infile)

    And after line https://github.com/tpope/vim-dadbod/blob/master/autoload/db.vim#L260, add this:

    echom 'INFILE AFTER '.string(infile)

    and re-test it.

ichirou2910 commented 4 years ago
  1. Nothing, srsly, nothing. Tried with my test.sql file and it gave /home/ichirou2910/test.sql tho.
  2. <leader>S vs :'<,'>DB
    [DBUI Debug] {'input_filename': '/tmp/nvimkvRSfR/4.sql', 'message': 'Executing multiple lines', 'lines': ['describe student;', 'desc
    ribe student;'], 'command': 'DB < /tmp/nvimkvRSfR/4.sql'}
    MAYBE INFILE '/tmp/nvimkvRSfR/4.sql'
    INFILE BEFORE ''
    INFILE AFTER '/home/ichirou2910/'
    'mysql -h ''localhost'' -u ''ichirou2910'' ''-pmegamanzero2910'' ''test'' -t < ''/home/ichirou2910/'''
    MAYBE INFILE ''
    INFILE BEFORE '/tmp/nvimkvRSfR/8.sql'
    INFILE AFTER '/tmp/nvimkvRSfR/8.sql'
    'mysql -h ''localhost'' -u ''ichirou2910'' ''-pmegamanzero2910'' ''test'' -t < ''/tmp/nvimkvRSfR/8.sql'''

    The other cases I guess it's not important anymore

kristijanhusak commented 4 years ago

Ok that's definitely the issue. It properly expands for me. What does it print when you do echo expand('/tmp/nvimkvRSfR/8.sql', ':p') ? Which neovim version are you using?

ichirou2910 commented 4 years ago
  1. Again, nothing :disappointed:
  2. 
    :version
    NVIM v0.4.4
    Build type: Release
    LuaJIT 2.0.5
    Compilation: /usr/bin/cc -D_FORTIFY_SOURCE=2 -march=x86-64 -mtune=generic -O2 -pipe -fno-plt -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -
    O2 -DNDEBUG -DMIN_LOG_LEVEL=3 -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wm
    issing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=always -DINCLUDE_GENERATED_D
    ECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -I/build/neovim/src/build/config -I/build/neovim/src/
    neovim-0.4.4/src -I/usr/include -I/build/neovim/src/build/src/nvim/auto -I/build/neovim/src/build/include
    Compiled by builduser

Features: +acl +iconv +tui See ":help feature-compile"

system vimrc file: "$VIM/sysinit.vim" fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info

kristijanhusak commented 4 years ago

@ichirou2910 and what about this:

echo fnamemodify('/tmp/nvimkvRSfR/8.sql', ':p')

I'm using latest neovim from master. Will try the stable version.

ichirou2910 commented 4 years ago

/tmp/nvimkvRSfR/8.sql

kristijanhusak commented 4 years ago

I just tried same version, everything works fine.

Do you have your tmp folder as a separate partition which you mount? Is it maybe a symlink?

I found this regarding failed expand() calls https://github.com/chrisbra/SudoEdit.vim/issues/17#issue-9338315

What does ls -la /tmp/nvim*** give after you try to execute these queries?

It would be good if you could try this on a different version of neovim, or in vim.

kristijanhusak commented 4 years ago

I pushed a fix. It uses dadbod's visual selection when available. So if you don't use bind parameters feature, which you don't (judging by the examples in this issue), it should work out for you.

We are still left with this issue, which should be fixed in vim-dadbod, so I would still appreciate if you could help me out on this. I just want to figure out why expand returns empty for you, so we can have an explanation why it needs fixing in vim-dadbod.

ichirou2910 commented 4 years ago
kristijanhusak commented 4 years ago

Does vim work without the fix, where it also do "Execute multiple lines" ?

ichirou2910 commented 4 years ago

Sadly no, I rolled back 1 commit and tried, still got that error

kristijanhusak commented 4 years ago

Did you add custom mapping for executing visual selection?

ichirou2910 commented 4 years ago

No, I don't map any extra key for dadbod-ui

kristijanhusak commented 4 years ago

Ok I'll check.

kristijanhusak commented 4 years ago

I can't reproduce it :/ I did push small change, but it shouldn't affect this. Give it a try anyway. It is detecting visual mode, but looks like it thinks there are some variables to inject.

If the latest change doesn't work for you, join all lines that you are trying to execute into one line, separated by a space, and try executing this search:

/[^:]:\w\+

Is it finding something?

ichirou2910 commented 4 years ago

image Just wanna say that, IT WORKED :tada: Imma close this right now! Still I don't quite understand how that commit made it work :laughing: but nah it just works, that's enuff

kristijanhusak commented 4 years ago

Great! :) Thanks for help in debugging.