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

Queries should be async #17

Open
joereynolds opened this issue Nov 27, 2017 · 2 comments
Open

Queries should be async #17

joereynolds opened this issue Nov 27, 2017 · 2 comments

Comments

@joereynolds
Copy link
Owner

joereynolds commented Nov 27, 2017

Currently, long queries block the editor
If we're on neovim or vim 8, use async, otherwise block as normal.

@joereynolds
Copy link
Owner Author

An attempt

function! mysql#GetResultsFromQuery(command)
    let user = g:sqh_connections[g:sqh_connection]['user']
    let password = g:sqh_connections[g:sqh_connection]['password']
    let host = g:sqh_connections[g:sqh_connection]['host']

    let connection_details = 'mysql --unbuffered -u' . user . ' -p' . password . ' -h' . host
    let system_command = connection_details . " --table -e '" . a:command . "'"

    let async_is_ok = has('job') && has ('channel') || has('nvim')
    if async_is_ok

        let query_results = jobstart(system_command, extend({'shell': 'shell 1'}, {'on_stdout': function('mysql#AsyncReturnQueryResultsCallback'), 'stdout_buffered' :v:true}))
    else
        let query_results = system(system_command)
        return query_results
    endif

endfunction

function! mysql#AsyncReturnQueryResultsCallback(id, data, event) dict
    call sqhell#InsertResultsToNewBufferAsync('SQHUnspecified', a:data)
endfunction
function! sqhell#InsertResultsToNewBufferAsync(local_filetype, query_results)

    call nvim_buf_set_lines(sqhell#GetResultsBuffer(), -2, -1, v:true, a:query_results)

    "Remove mysql junk"
    "this should probably go into the ftplugin on BufReadPre
    " normal gg2dd
    " setlocal buftype=nofile
    " setlocal bufhidden=hide
    " setlocal noswapfile
    setlocal nowrap
    " execute 'setlocal filetype=' . a:local_filetype
endfunction

function! sqhell#GetResultsBuffer()
    let buffers = filter(range(1, bufnr("$")), 'bufexists(v:val)')

    for buffer in buffers
        if bufname(buffer) ==? '_sq_'
            return buffer
        endif
    endfor


endfunction

@joereynolds
Copy link
Owner Author

Before this we should probably do the one buffer for all results idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant