Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ichirou2910 opened this issue Aug 22, 2020 · 39 comments
Closed

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

ichirou2910 opened this issue Aug 22, 2020 · 39 comments
Labels
bug Something isn't working partially-fixed Bug is partially fixed but there's still bug in certain situation

Comments

@ichirou2910
Copy link

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
Copy link
Owner

@ichirou2910 does executing the whole buffer work?

@ichirou2910
Copy link
Author

Yes it works fine

@kristijanhusak
Copy link
Owner

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

mysql://user:pass@host:port/dbname

@ichirou2910
Copy link
Author

ichirou2910 commented Aug 22, 2020

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

@kristijanhusak
Copy link
Owner

OK thanks, I'll look into it.

@kristijanhusak kristijanhusak added the bug Something isn't working label Aug 22, 2020
@kristijanhusak
Copy link
Owner

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

  • Which OS?
  • Vim/Neovim?
  • Did you try with some other queries, for example having 3 select queries and trying to execute 2 of them?

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:

  • Create a noname file somewhere, anywhere (For example, /home/yourname/Documents/myquery)
  • add the lines you are trying to execute visually from dbui to this file, save
  • Open vim, connect to your mysql db, set up a new query, and do :DB < /home/yourname/Documents/myquery

Does it work?

@ichirou2910
Copy link
Author

  • OS: Arch Linux (updated and upgraded before testing)
  • Editor: Neovim
  • Any multi-line queries doesn't work and throws the error I mentioned above
    A gif to better demonstrate the issue
    dbui
    As you can see, a multi-line create table query doesn't work, while a one-line create table query works just fine.
    And for your scenario, it worked normally

@kristijanhusak
Copy link
Owner

@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
Copy link
Owner

@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
Copy link
Author

@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?
#57

@kristijanhusak
Copy link
Owner

@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
Copy link
Author

ichirou2910 commented Aug 23, 2020

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

  • The correct config line is g:db_ui_tmp_query_location
  • Screenshot of what happened
    image
  • Somehow, only when I set tmp query location to home folder, query buffer is set to 'readonly' after executing, as shown also in the image above

@kristijanhusak
Copy link
Owner

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
Copy link
Author

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

@kristijanhusak
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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

@kristijanhusak
Copy link
Owner

kristijanhusak commented Aug 23, 2020

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

ichirou2910 commented Aug 23, 2020

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
Copy link
Owner

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
Copy link
Author

ichirou2910 commented Aug 23, 2020

  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
Copy link
Owner

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
Copy link
Author

  1. Again, nothing 😞
: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
Copy link
Owner

@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
Copy link
Author

/tmp/nvimkvRSfR/8.sql

@kristijanhusak
Copy link
Owner

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 chrisbra/SudoEdit.vim#17 (comment)

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
Copy link
Owner

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.

@kristijanhusak kristijanhusak added the partially-fixed Bug is partially fixed but there's still bug in certain situation label Aug 23, 2020
@ichirou2910
Copy link
Author

  • No, my /tmp is in the same partition as /
$ ls -la /tmp/nvimbT0WNc
total 8
drwx------  3 ichirou2910 wheel 120 Aug 23 18:14 .
drwxrwxrwt 17 root        root  380 Aug 23 18:14 ..
srwxr-xr-x  1 ichirou2910 wheel   0 Aug 23 18:14 0
-rw-r--r--  1 ichirou2910 wheel  36 Aug 23 18:14 4.sql
-rw-r--r--  1 ichirou2910 wheel  98 Aug 23 18:14 6.dbout
drwxr-xr-x  2 ichirou2910 wheel  60 Aug 23 18:14 coc.nvim-3304

On neovim it seems like the dadbod-ui cannot detect using visual selection, cuz the messages still said Executing multiple lines while vim worked and the messages said Executing visual selection
image
Don't know why 😞

$ ls -la /tmp/vQ1hfIz
total 8
drwx------  3 ichirou2910 wheel 100 Aug 23 18:29 .
drwxrwxrwt 19 root        root  420 Aug 23 18:29 ..
-rw-r--r--  1 ichirou2910 wheel 769 Aug 23 18:29 18.dbout
-rw-r--r--  1 ichirou2910 wheel  36 Aug 23 18:29 18.sql
drwxr-xr-x  2 ichirou2910 wheel  60 Aug 23 18:28 coc.nvim-23518

@kristijanhusak
Copy link
Owner

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

@ichirou2910
Copy link
Author

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

@kristijanhusak
Copy link
Owner

Did you add custom mapping for executing visual selection?

@ichirou2910
Copy link
Author

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

@kristijanhusak
Copy link
Owner

Ok I'll check.

@kristijanhusak
Copy link
Owner

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
Copy link
Author

ichirou2910 commented Aug 23, 2020

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

@kristijanhusak
Copy link
Owner

Great! :) Thanks for help in debugging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working partially-fixed Bug is partially fixed but there's still bug in certain situation
Projects
None yet
Development

No branches or pull requests

2 participants