| version 1.5, 2004/02/29 20:01:11 |
version 1.6, 2004/03/08 10:55:18 |
|
|
| " |
" |
| " File: database-client.vim |
" File: database-client.vim |
| " Maintainer: Lubomir Host 'rajo' <rajo AT platon.sk> |
" Maintainer: Lubomir Host 'rajo' <rajo AT platon.sk> |
| " Version: $Platon: vimconfig/vim/modules/database-client.vim,v 1.3 2004/02/23 17:52:54 rajo Exp $ |
" Version: $Platon: vimconfig/vim/modules/database-client.vim,v 1.5 2004/02/29 20:01:11 rajo Exp $ |
| " |
" |
| " Copyright (c) 2003 Platon SDG, http://platon.sk/ |
" Copyright (c) 2003 Platon SDG, http://platon.sk/ |
| " Licensed under terms of GNU General Public License. |
" Licensed under terms of GNU General Public License. |
| " All rights reserved. |
" All rights reserved. |
| " |
" |
| " $Platon: vimconfig/vim/modules/database-client.vim,v 1.3 2004/02/23 17:52:54 rajo Exp $ |
" $Platon: vimconfig/vim/modules/database-client.vim,v 1.5 2004/02/29 20:01:11 rajo Exp $ |
| " |
" |
| |
|
| " This plugin needs Perl interpreter to be enabled (+perl feature) |
" This plugin needs Perl interpreter to be enabled (+perl feature) |
| Line 30 if !exists('g:SQL_data_window_title') |
|
| Line 30 if !exists('g:SQL_data_window_title') |
|
| let g:SQL_data_window_title = "SQL_data" |
let g:SQL_data_window_title = "SQL_data" |
| endif |
endif |
| if !exists('g:SQL_cmd_window_title') |
if !exists('g:SQL_cmd_window_title') |
| let g:SQL_cmd_window_title = "SQL_command" |
let g:SQL_cmd_window_title = "SQL_log" |
| endif |
endif |
| |
|
| " width of main window |
" width of main window |
|
|
| |
|
| " initialize DBI engine |
" initialize DBI engine |
| function! SQL_Init() " {{{ |
function! SQL_Init() " {{{ |
| perl << EOF |
perl << EOF |
| |
|
| |
use Time::HiRes qw(gettimeofday tv_interval); |
| |
use POSIX qw(strftime); |
| |
use Env; |
| |
use DBI; |
| |
|
| package DBI::st; |
package DBI::st; |
| |
|
| # fetch and print data from _executed_ DBI statement handler |
# fetch and print data from _executed_ DBI statement handler |
| sub dump_data ($) |
sub dump_data ($$) |
| { # {{{ |
{ # {{{ |
| my $sth = shift; |
my ($sth, $curbuf) = @_; |
| |
|
| |
# if buffer is not defined, dump data to current window |
| |
unless (defined $curbuf) { |
| |
$curbuf = $main::curbuf; |
| |
} |
| |
|
| my $numFields = $sth->{'NUM_OF_FIELDS'}; |
my $numFields = $sth->{'NUM_OF_FIELDS'}; |
| my $column_names = $sth->{'NAME'}; |
my $column_names = $sth->{'NAME'}; |
|
|
| : ($$column_is_num[$i] ? -1 : 1) * length($$column_names[$i]); # WARN: negative length! - used for right aligment of numbers |
: ($$column_is_num[$i] ? -1 : 1) * length($$column_names[$i]); # WARN: negative length! - used for right aligment of numbers |
| $header_names .= sprintf("%s%s", $i ? " | " : "| ", $$column_names[$i] . " " x ($$column_sizes[$i] - length($$column_names[$i]))); |
$header_names .= sprintf("%s%s", $i ? " | " : "| ", $$column_names[$i] . " " x ($$column_sizes[$i] - length($$column_names[$i]))); |
| } |
} |
| $header_names .= " |\n"; |
$header_names .= " |"; |
| |
|
| # build header separator |
# build header separator |
| my $separator = ""; |
my $separator = ""; |
| foreach (my $i = 0; $i < $numFields; $i++) { |
foreach (my $i = 0; $i < $numFields; $i++) { |
| $separator .= "+" . ("-" x (abs($$column_sizes[$i]) + 2)); |
$separator .= "+" . ("-" x (abs($$column_sizes[$i]) + 2)); |
| } |
} |
| $separator .= "+\n"; # the end |
$separator .= "+"; # the end |
| |
|
| # print header |
# print header |
| VIM::Msg($separator); |
$curbuf->Append($curbuf->Count(), $separator, $header_names, $separator); |
| VIM::Msg($header_names); |
|
| VIM::Msg($separator); |
|
| |
|
| # print data |
# print data |
| while (my @row = $sth->fetchrow_array()) { |
while (my @row = $sth->fetchrow_array()) { |
|
|
| : " " x (- $$column_sizes[$i] - length($row[$i])) . $row[$i] |
: " " x (- $$column_sizes[$i] - length($row[$i])) . $row[$i] |
| ); |
); |
| } |
} |
| VIM::Msg("$line |\n"); |
$curbuf->Append($curbuf->Count(), "$line |"); |
| } |
} |
| |
|
| # print footer |
# print footer |
| VIM::Msg($separator); |
$curbuf->Append($curbuf->Count(), $separator); |
| |
|
| } # }}} |
} # }}} |
| |
|
| package main; |
package main; |
| |
|
| |
# return reference to array with database names |
| |
sub SQL_database_list($;) |
| |
{ # {{{ |
| |
my ($dbh) = @_; |
| |
|
| |
my $sth = $dbh->prepare("SHOW DATABASES"); |
| |
$sth->execute(); |
| |
my @arr; |
| |
|
| |
while (my @row = $sth->fetchrow()) { |
| |
push @arr, $row[0]; |
| |
} |
| |
|
| |
return \@arr; |
| |
|
| |
} # }}} |
| |
|
| |
sub SQL_add_database_list($$;) |
| |
{ # {{{ |
| |
my ($key, $line) = @_; |
| |
|
| |
my $added_lines = 0; |
| |
foreach my $db (@{ $main::connections->{$key}->{databases} }) { |
| |
$main::curbuf->Append($line + $added_lines, " " . $db); |
| |
$added_lines++; |
| |
} |
| |
|
| |
} # }}} |
| |
|
| # number of executed SQL commands |
# number of executed SQL commands |
| my $sql_cmd_num = 0; |
my $sql_cmd_num = 0; |
| |
|
|
|
| " connect to database |
" connect to database |
| function! SQL_Connect() " {{{ |
function! SQL_Connect() " {{{ |
| perl << EOF |
perl << EOF |
| use DBI; |
|
| |
|
| package main; |
package main; |
| |
|
| # get hostname from user |
# get hostname from user |
| $db_host = "localhost" unless (defined $db_host); # remember server name |
$db_host = "localhost" unless (defined $db_host); # remember server name |
| my ($success, $value) = VIM::Eval("inputdialog('Hostname of your database server', '$db_host')"); |
my ($success, $value) = VIM::Eval("inputdialog('Hostname of your database server', '$db_host')"); |
| if ($success) { |
#VIM::Msg("success = $success value = $value"); |
| |
if ($success and $value ne '') { |
| $db_host = $value; |
$db_host = $value; |
| } |
} |
| else { |
else { |
| Line 163 function! SQL_Connect() " {{{ |
|
| Line 200 function! SQL_Connect() " {{{ |
|
| } |
} |
| |
|
| # get username from user |
# get username from user |
| $db_user = "user" unless (defined $db_user); # remember username |
$db_user = $ENV{"USER"} unless (defined $db_user); # remember username |
| my ($success, $value) = VIM::Eval("inputdialog('Login', '$db_user')"); |
my ($success, $value) = VIM::Eval("inputdialog('Login', '$db_user')"); |
| if ($success) { |
if ($success and $value ne '') { |
| $db_user = $value; |
$db_user = $value; |
| } |
} |
| else { |
else { |
| return; |
return; |
| } |
} |
| |
|
| if (exists $main::connections->{"$db_host; $db_user;"}) { |
|
| VIM::Msg("This connection already exists!"); |
|
| return; |
|
| } |
|
| |
|
| # get password from user |
# get password from user |
| my ($success, $value) = VIM::Eval('inputsecret("Password: ")'); |
my ($success, $value) = VIM::Eval('inputsecret("Password: ")'); |
| if ($success) { |
if ($success) { |
| Line 186 function! SQL_Connect() " {{{ |
|
| Line 218 function! SQL_Connect() " {{{ |
|
| return; |
return; |
| } |
} |
| |
|
| $dbh = DBI->connect("DBI:mysql:host=$db_host", $db_user, $db_pass) |
$dbh = DBI->connect("DBI:mysql:host=$db_host", $db_user, $db_pass, |
| |
{ RaiseError => 0, AutoCommit => 1}) |
| or die $DBI::errstr; |
or die $DBI::errstr; |
| |
# reconnect to database if the connection is lost |
| |
$dbh->{mysql_auto_reconnect} = 1; |
| |
|
| |
my $key = "$db_host; $db_user;"; |
| |
my $ext = ""; |
| |
my $index = 2; |
| |
while (exists %main::connections->{"$key$ext"}) { |
| |
$ext = " ($index)"; |
| |
$index++; |
| |
} |
| |
$key .= $ext; # add number of the same connection |
| |
|
| if (defined $dbh) { |
if (defined $dbh) { |
| $main::connections->{"$db_host; $db_user;"} = { |
$main::connections->{$key} = { |
| 'dbh' => $dbh, |
'dbh' => $dbh, |
| 'desc' => "$db_user\@$db_host", |
'desc' => "$db_user\@$db_host$ext", |
| 'list_tables' => 1, |
'list_databases' => 1, |
| |
'databases' => SQL_database_list($dbh), |
| }; |
}; |
| $main::current_conn = "$db_host; $db_user;"; |
$main::current_conn = $key; |
| } |
} |
| else { |
else { |
| undef $main::connections->{"$db_host; $db_user;"}->{dbh}; |
undef $main::connections->{$key}->{dbh}; |
| undef $main::connections->{"$db_host; $db_user;"}->{desc}; |
undef $main::connections->{$key}->{desc}; |
| undef $main::connections->{"$db_host; $db_user;"}; |
undef $main::connections->{$key}; |
| } |
} |
| |
undef $key, $ext, $index; # cleanup |
| |
|
| my $count = 0; |
my $count = 0; |
| foreach my $key (sort keys %{$main::connections}) { |
foreach my $key (sort keys %{$main::connections}) { |
| Line 209 function! SQL_Connect() " {{{ |
|
| Line 255 function! SQL_Connect() " {{{ |
|
| # reorder connections |
# reorder connections |
| $main::connections->{$key}->{order} = $count; |
$main::connections->{$key}->{order} = $count; |
| } |
} |
| |
undef $count; # cleanup |
| |
|
| VIM::Msg("Connect to database succsesfull"); |
VIM::Msg("Connect to database succsesfull"); |
| |
|
|
|
| " show used connections |
" show used connections |
| function! SQL_ShowConnections() " {{{ |
function! SQL_ShowConnections() " {{{ |
| perl << EOF |
perl << EOF |
| use DBI; |
|
| |
|
| package main; |
package main; |
| |
|
| Line 304 function! SQL_Execute() " {{{ |
|
| Line 350 function! SQL_Execute() " {{{ |
|
| endfunction |
endfunction |
| " }}} |
" }}} |
| |
|
| |
|
| " execute SQL command |
" execute SQL command |
| function! SQL_Do(sql_cmd) " {{{ |
function! SQL_Do(sql_cmd) " {{{ |
| |
|
| " get number of SQL cmd window |
|
| let cmd_winnum = bufwinnr(g:SQL_cmd_window_title) |
|
| |
|
| perl << EOF |
perl << EOF |
| use DBI; |
|
| |
|
| package main; |
package main; |
| |
|
| Line 322 function! SQL_Do(sql_cmd) " {{{ |
|
| Line 363 function! SQL_Do(sql_cmd) " {{{ |
|
| # remove empty chars from beginning and end of string |
# remove empty chars from beginning and end of string |
| # add semilon at the end |
# add semilon at the end |
| $sql_cmd =~ s/^\s+//g; |
$sql_cmd =~ s/^\s+//g; |
| $sql_cmd =~ s/;?\s*$/;/g; |
$sql_cmd =~ s/[;\s]{1,}$//g; |
| |
$sql_cmd = $sql_cmd . ";"; |
| |
|
| # remember last SQL command |
# remember last SQL command |
| VIM::DoCommand('let g:SQL_last_command="' . $sql_cmd . '"'); |
VIM::DoCommand('let g:SQL_last_command="' . $sql_cmd . '"'); |
| |
|
| $main::sql_cmd_num++; |
$main::sql_cmd_num++; |
| my $winnr = VIM::Eval("cmd_winnum"); |
|
| my ($window) = VIM::Windows($winnr); |
my ($success, $value) = VIM::Eval("bufwinnr(g:SQL_cmd_window_title)"); |
| my $cmd_buf = $window->Buffer(); |
my ($cmd_window) = $success ? VIM::Windows($value) : undef; |
| my $last_line = $cmd_buf->Count(); # number of lines |
my $cmd_buf = $cmd_window ? $cmd_window->Buffer() : $main::curbuf; |
| |
|
| |
my ($success, $value) = VIM::Eval("bufwinnr(g:SQL_data_window_title)"); |
| |
my ($data_window) = $success ? VIM::Windows($value) : undef; |
| |
my $data_buf = $data_window ? $data_window->Buffer() : $main::curbuf; |
| |
|
| # add SQL cmd separator |
# add SQL cmd separator |
| $cmd_buf->Append($last_line, "-- Commnad #$main::sql_cmd_num: " . scalar localtime, $sql_cmd, ""); |
$data_buf->Append($data_buf->Count(), |
| |
"", "-- Commnad #$main::sql_cmd_num: " . scalar localtime, $sql_cmd); |
| |
$cmd_buf->Append($cmd_buf->Count(), |
| |
"", "-- Commnad #$main::sql_cmd_num: " . scalar localtime, $sql_cmd); |
| |
|
| |
my $start_time = [ Time::HiRes::gettimeofday ]; |
| |
my $sth = $dbh->prepare($sql_cmd); |
| |
if ($dbh->errstr) { |
| |
$cmd_buf->Append($cmd_buf->Count(), "-- ERROR: " . $dbh->errstr); |
| |
} |
| |
else { |
| |
$sth->execute(); |
| |
if ($dbh->errstr) { |
| |
$cmd_buf->Append($cmd_buf->Count(), "-- ERROR: " . $dbh->errstr); |
| |
} |
| |
else { |
| |
$sth->dump_data($data_buf); |
| |
$data_buf->Append($curbuf->Count(), |
| |
"-- " . $sth->rows() . " rows in set (" . Time::HiRes::tv_interval($start_time) . " sec)"); |
| |
$sth->finish(); |
| |
} |
| |
} |
| |
#$cmd_window->Cursor($cmd_buf->Count(), 1); |
| |
|
| my $sth = $dbh->prepare($sql_cmd) or warn $DBI::errstr; |
undef $cmd_window, $data_window; |
| $sth->execute(); |
undef $cmd_buf, $data_buf; |
| $sth->dump_data(); |
|
| |
|
| undef $window; |
|
| undef $cmd_buf; |
|
| undef $last_line; |
|
| undef $winnr; |
|
| |
|
| EOF |
EOF |
| |
|
| unlet cmd_winnum |
" get number of current window |
| |
let curwinnum = bufwinnr('%') |
| |
|
| |
let winnum = bufwinnr(g:SQL_cmd_window_title) |
| |
if winnum < 0 " window doesn't exists |
| |
return |
| |
endif |
| |
|
| |
" toggle to SQL window, scroll to end, and then switch back |
| |
execute "normal \<c-w>" . winnum . "w" |
| |
execute "normal G" |
| |
execute "normal \<c-w>" . curwinnum . "w" |
| |
|
| endfunction |
endfunction |
| " }}} |
" }}} |
| |
|
| |
|
| "create menu |
"create menu |
| function! SQL_CreateMenu() " {{{ |
function! SQL_CreateMenu() " {{{ |
| " remove whole menu |
" remove whole menu |
| Line 365 function! SQL_CreateMenu() " {{{ |
|
| Line 437 function! SQL_CreateMenu() " {{{ |
|
| amenu 200.40.30 S&QL.&Show.&tables :call SQL_Do('SHOW TABLES')<Return> |
amenu 200.40.30 S&QL.&Show.&tables :call SQL_Do('SHOW TABLES')<Return> |
| amenu 200.40.40 S&QL.&Show.&variables :call SQL_Do('SHOW VARIABLES')<Return> |
amenu 200.40.40 S&QL.&Show.&variables :call SQL_Do('SHOW VARIABLES')<Return> |
| amenu 200.40.50 S&QL.&Show.&server\ status :call SQL_Do('SHOW STATUS')<Return> |
amenu 200.40.50 S&QL.&Show.&server\ status :call SQL_Do('SHOW STATUS')<Return> |
| amenu 200.50.10 S&QL.&Windows.&create :call <SID>SQL_CreateWindows()<Return> |
amenu 200.50.10 S&QL.&Windows.&create :call SQL_CreateWindows()<Return> |
| endfunction |
endfunction |
| " }}} |
" }}} |
| call SQL_CreateMenu() |
call SQL_CreateMenu() |
|
|
| " }}} |
" }}} |
| |
|
| " create windows |
" create windows |
| function! s:SQL_CreateWindows() " {{{ |
function! SQL_CreateWindows() " {{{ |
| let s:main_winnum = bufwinnr(g:SQL_main_window_title) |
let s:main_winnum = bufwinnr(g:SQL_main_window_title) |
| call s:CloseWindow(s:main_winnum) |
call s:CloseWindow(s:main_winnum) |
| |
|
| Line 395 function! s:SQL_CreateWindows() " {{{ |
|
| Line 467 function! s:SQL_CreateWindows() " {{{ |
|
| setlocal buftype=nowrite |
setlocal buftype=nowrite |
| setlocal bufhidden=delete |
setlocal bufhidden=delete |
| setlocal nonumber |
setlocal nonumber |
| setlocal nowrap |
setlocal wrap |
| setlocal norightleft |
setlocal norightleft |
| setlocal foldcolumn=0 |
setlocal foldcolumn=0 |
| setlocal modifiable |
setlocal modifiable |
| setlocal filetype=sql |
setlocal filetype=sql_log |
| |
|
| |
|
| execute g:SQL_main_window_width " vsplit " . g:SQL_main_window_title |
execute g:SQL_main_window_width " vsplit " . g:SQL_main_window_title |
| Line 450 function! s:SQL_Display_Help() |
|
| Line 522 function! s:SQL_Display_Help() |
|
| |
|
| if s:sql_brief_help |
if s:sql_brief_help |
| " Add the brief help |
" Add the brief help |
| call append(0, '" Press ? to display help text') |
call append(0, "\" Press 'h' to display help text") |
| call append(1, '') |
call append(1, '') |
| else |
else |
| " Add the extensive help |
" Add the extensive help |
| call append(0, '" u : Update table list') |
call append(0, '" u : Update table list') |
| call append(1, '" U : Update database list') |
call append(1, '" U : Update database list') |
| call append(2, '" ? : Remove help text') |
call append(2, '" h : Remove help text') |
| call append(3, '') |
call append(3, '') |
| endif |
endif |
| endfunction |
endfunction |
| Line 522 function! s:Map_SQL_mappings() |
|
| Line 594 function! s:Map_SQL_mappings() |
|
| " toggle to SQL window and then back |
" toggle to SQL window and then back |
| execute "normal \<c-w>" . winnum . "w" |
execute "normal \<c-w>" . winnum . "w" |
| |
|
| inoremap <buffer> <silent> ? <C-o>:call <SID>SQL_Toggle_Help_Text()<CR> |
" toggle between long and short help format |
| nnoremap <buffer> <silent> ? :call <SID>SQL_Toggle_Help_Text()<CR> |
inoremap <buffer> <silent> h <C-o>:call <SID>SQL_Toggle_Help_Text()<CR> |
| |
nnoremap <buffer> <silent> h :call <SID>SQL_Toggle_Help_Text()<CR> |
| |
|
| inoremap <buffer> <silent> U <C-o>:call <SID>SQL_UpdateDatabaseList()<CR> |
inoremap <buffer> <silent> U <C-o>:call <SID>SQL_UpdateDatabaseList()<CR> |
| nnoremap <buffer> <silent> U :call <SID>SQL_UpdateDatabaseList()<CR> |
nnoremap <buffer> <silent> U :call <SID>SQL_UpdateDatabaseList()<CR> |
| |
|
| |
inoremap <buffer> <silent> <CR> <C-o>:call <SID>SQL_SwitchCurrentDB()<CR> |
| |
nnoremap <buffer> <silent> <CR> :call <SID>SQL_SwitchCurrentDB()<CR> |
| |
|
| |
execute "normal \<c-w>" . curwinnum . "w" |
| |
endif |
| |
|
| |
let winnum = bufwinnr(g:SQL_cmd_window_title) |
| |
if winnum >= 0 " if window exists |
| |
" toggle to SQL window and then back |
| |
execute "normal \<c-w>" . winnum . "w" |
| |
|
| |
" execute current SQL command |
| |
inoremap <buffer> <silent> <F9> <C-o>:call SQL_Do(getline("."))<CR> |
| |
nnoremap <buffer> <silent> <F9> :call SQL_Do(getline("."))<CR> |
| |
|
| |
execute "normal \<c-w>" . curwinnum . "w" |
| |
endif |
| |
|
| |
let winnum = bufwinnr(g:SQL_data_window_title) |
| |
if winnum >= 0 " if window exists |
| |
" toggle to SQL window and then back |
| |
execute "normal \<c-w>" . winnum . "w" |
| |
|
| |
" execute current SQL command |
| |
inoremap <buffer> <silent> <F9> <C-o>:call SQL_Do(getline("."))<CR> |
| |
nnoremap <buffer> <silent> <F9> :call SQL_Do(getline("."))<CR> |
| |
|
| execute "normal \<c-w>" . curwinnum . "w" |
execute "normal \<c-w>" . curwinnum . "w" |
| endif |
endif |
| |
|
| Line 541 function! s:SQL_UpdateDatabaseList() |
|
| Line 641 function! s:SQL_UpdateDatabaseList() |
|
| let curwinnum = bufwinnr('%') |
let curwinnum = bufwinnr('%') |
| |
|
| let winnum = bufwinnr(g:SQL_main_window_title) |
let winnum = bufwinnr(g:SQL_main_window_title) |
| if winnum >= 0 " if window exists |
if winnum < 0 " window doesn't exists |
| " toggle to SQL window and then back |
return |
| execute "normal \<c-w>" . winnum . "w" |
endif |
| |
|
| |
" toggle to SQL window and then back |
| |
execute "normal \<c-w>" . winnum . "w" |
| |
|
| setlocal modifiable |
setlocal modifiable |
| |
|
| " remove all lines after help lines |
" remove all lines after help lines |
| if s:sql_brief_help |
if s:sql_brief_help |
| exe (s:brief_help_size + 1) . ',$ delete _' |
silent! execute (s:brief_help_size + 1) . ',$ delete _' |
| else |
else |
| exe (s:full_help_size + 1) . ',$ delete _' |
silent! execute (s:full_help_size + 1) . ',$ delete _' |
| endif |
endif |
| |
|
| perl << EOF |
perl << EOF |
| use DBI; |
|
| |
|
| package main; |
|
| |
|
| my $count = 0; |
|
| foreach my $key (sort keys %{$main::connections}) { |
|
| $count++; |
|
| # reorder connections |
|
| my $conn = $main::connections->{$key}; |
|
| $conn->{order} = $count; |
|
| $main::curbuf->Append($main::curbuf->Count(), |
|
| ($conn->{list_tables} ? |
|
| ($main::current_conn eq $key ? '* ' : '+ ' ) : |
|
| '- ') |
|
| . "$main::connections->{$key}->{desc}" |
|
| ); |
|
| } |
|
| |
|
| if ($count == 0) { # No connections |
package main; |
| $main::curbuf->Append($main::curbuf->Count(), '" -- No connections'); |
|
| |
my $count = 0; |
| |
foreach my $key (sort keys %{$main::connections}) { |
| |
$count++; |
| |
# reorder connections |
| |
my $conn = $main::connections->{$key}; |
| |
$conn->{order} = $count; |
| |
$main::curbuf->Append($main::curbuf->Count(), |
| |
($conn->{list_databases} ? |
| |
($main::current_conn eq $key ? '* ' : '+ ' ) : |
| |
'- ') |
| |
. $conn->{desc} |
| |
); |
| |
if ($conn->{list_databases}) { |
| |
SQL_add_database_list($key, $main::curbuf->Count()); |
| } |
} |
| |
} |
| |
|
| |
if ($count == 0) { # No connections |
| |
$main::curbuf->Append($main::curbuf->Count(), '" -- No connections'); |
| |
} |
| |
|
| EOF |
EOF |
| |
|
| setlocal nomodifiable |
setlocal nomodifiable |
| execute "normal \<c-w>" . curwinnum . "w" |
execute "normal \<c-w>" . curwinnum . "w" |
| |
|
| |
endfunction |
| |
" }}} |
| |
|
| |
" s:SQL_SwitchCurrentDB() {{{ |
| |
function! s:SQL_SwitchCurrentDB() |
| |
|
| |
" get number of current window |
| |
let curwinnum = bufwinnr('%') |
| |
|
| |
let winnum = bufwinnr(g:SQL_main_window_title) |
| |
if winnum < 0 " window doesn't exists |
| |
return |
| |
endif |
| |
|
| |
" toggle to SQL window and then back |
| |
execute "normal \<c-w>" . winnum . "w" |
| |
|
| |
setlocal modifiable |
| |
let curr_line = line(".") |
| |
silent! execute "%s/^\*/+/g" |
| |
silent! execute "normal " . curr_line . "G" |
| |
|
| |
" search for new active connection |
| |
if match(getline("."), "^[+-]") == -1 |
| |
let linenum = search("^[+-]", "bW") " first search backward |
| |
else " current line matches connection |
| |
let linenum = curr_line |
| endif |
endif |
| |
if linenum == 0 " search also forward (if we are on top) |
| |
let linenum = search("^[+-]", "W") |
| |
endif |
| |
if linenum == 0 " no db found, return |
| |
return |
| |
endif |
| |
let curr_db = getline(linenum) |
| |
call setline(linenum, substitute(curr_db, "^.", "*", "")) |
| |
|
| |
perl << EOF |
| |
|
| |
package main; |
| |
|
| |
my $new_conn = VIM::Eval("curr_db"); |
| |
my $new_db = VIM::Eval("getline(curr_line)"); |
| |
$new_conn =~ s/^..//g; |
| |
$new_db =~ s/^....//g; |
| |
|
| |
foreach my $key (keys %{$main::connections}) { |
| |
if ($new_conn eq $main::connections->{$key}->{desc}) { |
| |
$main::current_conn = $key; |
| |
my $retval = $main::connections->{$key}->{dbh}->do("USE $new_db"); |
| |
last; |
| |
} |
| |
} |
| |
EOF |
| |
|
| |
silent! execute "normal " . curr_line . "G" |
| |
|
| |
setlocal nomodifiable |
| |
execute "normal \<c-w>" . curwinnum . "w" |
| |
|
| endfunction |
endfunction |
| " }}} |
" }}} |