Code Monkey home page Code Monkey logo

Comments (10)

kristijanhusak avatar kristijanhusak commented on May 22, 2024

There's already a command that allows you to partially use this.
For example, if you have an sql file, you can use :DBUIFindBuffer. It will prompt you for db which you want to assign to it, or use the first one if there's only one in the list. From there you can use that buffer as it is a dbui buffer.

This is not working for non sql filetypes, because dbui converts it to sql filetype. I plan to improve that.

from vim-dadbod-ui.

kristijanhusak avatar kristijanhusak commented on May 22, 2024

I did the improvement. Now you can assign any filetype to dbui with :DBUIFindBuffer. It will not mess up the filetype or anything.
It should also allow using vim-dadbod-completion completion, but you need to make sure that it's included for your non-sql filetype. So if you use coc, you must include this in your coc config:

coc.source.db.filetypes: ['sql', 'yourfiletypehere']

If you can give it a test and let me know how it looks.

from vim-dadbod-ui.

idanarye avatar idanarye commented on May 22, 2024

Unless there is something I'm missing dbui can only execute the entire content of the dbui buffer. This doesn't work very well for inline SQL, because most of the buffer is not SQL.

Say I have this pseudo-C:

void main() {
    run_sql(con, "SELECT * FROM foo;");
}

If I turn this buffer to a dbui buffer with :DBUIFindBuffer I can only execute its entire content - which is obviously not valid SQL. It's also not useful to have dbui automatically execute it as query on save...

from vim-dadbod-ui.

kristijanhusak avatar kristijanhusak commented on May 22, 2024

You have a mapping for executing visual selection (https://github.com/kristijanhusak/vim-dadbod-ui/blob/master/doc/dadbod-ui.txt#L315), and by default it's mapped to <Leader>S in sql buffers. Problem is that it's not mapped for non-sql buffers, so you would have to do that manually. I think optimal setup for your case would be this:

augroup dbui_custom
 autocmd!
 autocmd FileType cpp vnoremap <buffer><silent><Leader>S <Plug>(DBUI_ExecuteQuery)
augroup END

" Do not execute whole buffer on save
let g:db_ui_execute_on_save = 0

With this, you do :DBUIFindBuffer in your c file, visual select SELECT * FORM foo;, and press <Leader>S. This should execute that selection only. If you use vim-dadbod-completion, you should also get completion for the selected database.

from vim-dadbod-ui.

idanarye avatar idanarye commented on May 22, 2024

Doesn't work...

Anyway, I managed to hack it it with this script:

function! s:getDbuiScriptNumber() abort
    " Make sure it's loaded:
    call db_ui#get_conn_info('')

    for l:line in split(execute('scriptnames'), '\n')
        if l:line =~ '\vautoload/db_ui.vim$'
            return str2nr(split(l:line, ':')[0])
        endif
    endfor
endfunction

function! s:getDbuiInstance() abort
    return function(printf('<SNR>%d_init', s:getDbuiScriptNumber()))()
endfunction

function! s:chooseDbuiConnection() abort
    let l:keys = keys(s:getDbuiInstance().dbs)
    call fzf#run({
                \ 'source': l:keys,
                \ 'sink': function('s:setDbConnection'),
                \ 'down': '~40%',
                \ })
endfunction

function! s:setDbConnection(key) abort
    let b:db = s:getDbuiInstance().dbs[a:key].url
endfunction

command! DBUISetDbConnection call s:chooseDbuiConnection()

from vim-dadbod-ui.

kristijanhusak avatar kristijanhusak commented on May 22, 2024

Which part doesn't work?

from vim-dadbod-ui.

kristijanhusak avatar kristijanhusak commented on May 22, 2024

I made a mistake in my comment, it shouldn't be vnoremap but vmap:

autocmd FileType cpp vmap <buffer><silent><Leader>S <Plug>(DBUI_ExecuteQuery)

I see that you are doing this nasty script id parsing to get all available connections. Do you want to get list of available connections in format [{ name: db_name, url: db_url }] through db_ui#get_conn_info so you can use your fzf?

from vim-dadbod-ui.

idanarye avatar idanarye commented on May 22, 2024

The mapping doesn't work. I see [DBUI] Executing query...Done after 0.042320 sec. but no result window is opened.

At any rate I'd still prefer to set b:db (or g:db) and keeping g:db_ui_execute_on_save on because it is useful in actual dbui buffers, so a db_ui#list_conns() or something would be nice...

from vim-dadbod-ui.

kristijanhusak avatar kristijanhusak commented on May 22, 2024

That's odd. Does it work if you try to execute visual selection in dbui sql buffer?

I added db_ui#connections_list() function, which returns name, url, source and is_connected properties.

from vim-dadbod-ui.

idanarye avatar idanarye commented on May 22, 2024

Weird - I tried the mapping again after updating the plugin, and it worked...

Anyway db_ui#connections_list() works like a charm. Thanks!

from vim-dadbod-ui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.