server.vim 5.0 KB
Newer Older
C
Cosmin Popescu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
"============================================================================"
"
"  Vim SQL Workbench/J Implementation
"
"  Copyright (c) Cosmin Popescu
"
"  Author:      Cosmin Popescu <cosminadrianpopescu at gmail dot com>
"  Version:     1.00 (2015-01-08)
"  Requires:    Vim 7
"  License:     GPL
"
"  Description:
"
"  Provides SQL database access to any DBMS supported by SQL Workbench/J. The
"  only dependency is SQL Workbench/J. Also includes powefull intellisense
"  autocomplete based on the current selected database
"
"============================================================================"

let s:current_file = expand('<sfile>:p:h')
let s:active_servers = []

function! s:get_pipe_name(id)
    return g:sw_tmp . '/sw-pipe-' . a:id
endfunction

function! sw#server#run(port, ...)
    if !exists('g:loaded_dispatch')
        throw 'You cannot start a server without vim dispatch plugin. Please install it first. If you don''t want or you don''t have the possibility to install it, you can always start the server manually. '
    endif
    let cmd = 'Start! ' . s:current_file . '/../../resources/sqlwbconsole' . ' -t ' . g:sw_tmp . ' -s ' . v:servername . ' -c ' . g:sw_exe . ' -v ' . g:sw_vim_exe . ' -o ' . a:port

    if a:0
        let cmd = cmd + ' -p ' . a:1
    endif

    execute cmd
    redraw!
endfunction

C
Cosmin Popescu 已提交
41 42 43 44 45 46 47 48 49 50
function! sw#server#connect_buffer(port, ...)
    let file = bufname('%')
    let command = 'e'
    if (a:0 >= 2)
        let file = a:1
        let command = a:2
    elseif a:0 >= 1
        let command = a:1
    endif
    call sw#sqlwindow#open_buffer(a:port, file, command)
C
Cosmin Popescu 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
endfunction

function! sw#server#new(port)
    call add(s:active_servers, a:port)
    echomsg "Added new server on port: " . a:port
    call sw#interrupt()
    redraw!
    return ''
endfunction

function! sw#server#remove(port)
    let i = 0
    for port in s:active_servers
        if port == a:port
            unlet s:active_servers[i]
        endif
        let i = i + 1
    endfor
    echomsg "Removed server from port: " . a:port
    call sw#interrupt()
    redraw!
    return ''
endfunction

C
Cosmin Popescu 已提交
75
function! s:pipe_execute(type, cmd, wait_result, ...)
C
Cosmin Popescu 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    let port = 0
    if a:0
        let port = a:1
    else
        if exists('b:port')
            let port = b:port
        endif
    endif
    if port == 0
        throw "There is no port set for this buffer. "
    endif
    let uid = -1
    if exists('b:unique_id')
        let uid = b:unique_id
    endif

C
Cosmin Popescu 已提交
92
    try
C
Cosmin Popescu 已提交
93 94
    python << SCRIPT
import vim
C
Cosmin Popescu 已提交
95
import socket
C
Cosmin Popescu 已提交
96 97
import re
identifier = vim.eval('v:servername') + "#" + vim.eval('uid')
C
Cosmin Popescu 已提交
98
cmd = vim.eval('a:cmd')
C
Cosmin Popescu 已提交
99
port = int(vim.eval('port'))
C
Cosmin Popescu 已提交
100
type = vim.eval('a:type')
C
Cosmin Popescu 已提交
101 102
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', port))
C
Cosmin Popescu 已提交
103 104
packet = ''
packet += type
C
Cosmin Popescu 已提交
105
if vim.eval('a:wait_result') == '0':
C
Cosmin Popescu 已提交
106
    packet += "!#identifier = " + identifier + "\n"
C
Cosmin Popescu 已提交
107
#end if
C
Cosmin Popescu 已提交
108 109 110
packet += cmd
packet = str(len(packet)) + "#" + packet
s.sendall(packet)
C
Cosmin Popescu 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123
result = ''
if vim.eval('a:wait_result') == '1':
    while 1:
        data = s.recv(4096)
        if (re.search('^DISCONNECT', data)):
            break
        #end if
        if not data:
            break
        #end if
        result += data
    #end while
#end if
C
Cosmin Popescu 已提交
124
s.close()
C
Cosmin Popescu 已提交
125 126 127 128 129
vim.command("let result = ''")
lines = result.split("\n")
for line in lines:
    vim.command("let result = result . '%s\n'" % line.replace("'", "''"))
#end for
C
Cosmin Popescu 已提交
130
SCRIPT
C
Cosmin Popescu 已提交
131 132 133 134
    catch
        call sw#display_error("There is a problem communicating with the server on port " . port . ". Maybe the server is down?")
        return ''
    endtry
C
Cosmin Popescu 已提交
135 136 137 138
    if len(result) <= 3
        let result = ''
    endif
    return substitute(result, '\r', '', 'g')
C
Cosmin Popescu 已提交
139 140 141
endfunction

function! sw#server#stop(port)
C
Cosmin Popescu 已提交
142 143 144 145 146 147 148 149 150
    call s:pipe_execute('COM', "exit", 0, a:port)
endfunction

function! sw#server#fetch_result()
    let result = s:pipe_execute('RES', v:servername . "#" . b:unique_id, 1, b:port)
    return result
endfunction

function! sw#server#open_dbexplorer(profile, port)
C
Cosmin Popescu 已提交
151
    return s:pipe_execute('DBE', substitute(a:profile, '___', "\\\\", 'g') . "\n", 1, a:port)
C
Cosmin Popescu 已提交
152 153 154 155 156 157
endfunction

function! sw#server#dbexplorer(sql)
    if !exists('b:profile')
        return
    endif
C
Cosmin Popescu 已提交
158 159 160 161 162 163
    if a:sql =~ "^:"
        let func = substitute(a:sql, '^:', '', 'g')
        execute "let s = " . func . "(getline('.'))"
    else
        let s = s:pipe_execute('DBE', substitute(b:profile, '___', "\\\\", 'g') . "\n" . a:sql . ';', 1)
    endif
C
Cosmin Popescu 已提交
164 165
    let lines = split(s, "\n")
    let result = []
C
Cosmin Popescu 已提交
166
    let rec = 0
C
Cosmin Popescu 已提交
167
    for line in lines
C
Cosmin Popescu 已提交
168
        if rec && !(line =~ '\v^[\=]+$')
C
Cosmin Popescu 已提交
169 170 171 172 173 174
            call add(result, line)
        endif
        if line =~ '\v\c^[\=]+$'
            let rec = 1
        endif
    endfor
C
Cosmin Popescu 已提交
175 176 177
    if len(result) == 0
        let result = split(s, "\n")
    endif
C
Cosmin Popescu 已提交
178
    return result
C
Cosmin Popescu 已提交
179 180
endfunction

C
Cosmin Popescu 已提交
181
function! sw#server#execute_sql(sql, wait_result, port)
C
Cosmin Popescu 已提交
182
    let sql = a:sql
C
Cosmin Popescu 已提交
183
    if !(substitute(sql, "^\\v\\c\\n", ' ', 'g') =~ b:delimiter . '[ \s\t\r\n]*$')
C
Cosmin Popescu 已提交
184
        let sql = sql . b:delimiter . "\n"
C
Cosmin Popescu 已提交
185
    endif
C
Cosmin Popescu 已提交
186
    return s:pipe_execute('COM', sql, a:wait_result, a:port)
C
Cosmin Popescu 已提交
187
endfunction