PageRenderTime 63ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/cfg/app-editors/vim/artifacts/vim/bundle/dbext.vim/autoload/dbext_dbi.vim

https://bitbucket.org/nextreamlabs/slice-official-repository
Vim Script | 1691 lines | 1237 code | 172 blank | 282 comment | 170 complexity | a1dbf0c79445ff84a9e8bfe913962176 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-2-Clause, AGPL-1.0, WTFPL

Large files files are truncated, but you can click here to view the full file

  1. " File: dbext_dbi.vim
  2. " Copyright (C) 2002-7, Peter Bagyinszki, David Fishburn
  3. " Purpose: A perl extension for use with dbext.vim.
  4. " It adds transaction support and the ability
  5. " to reach any database currently supported
  6. " by Perl and DBI.
  7. " Version: 12.00
  8. " Maintainer: David Fishburn <dfishburn dot vim at gmail dot com>
  9. " Authors: David Fishburn <dfishburn dot vim at gmail dot com>
  10. " Last Modified: 2010 Jul 15
  11. " Created: 2007-05-24
  12. " Homepage: http://vim.sourceforge.net/script.php?script_id=356
  13. "
  14. " Help: :h dbext.txt
  15. "
  16. " System Requirements:
  17. "
  18. " VIM with embedded perl support. You can check if your Vim has this
  19. " support using
  20. " :echo has('perl')
  21. "
  22. " This plugin supports these perl modules:
  23. " DBI
  24. " DBD::ODBC
  25. "
  26. " Install these perl modules, using ActiveState Perl on
  27. " Windows you can do it as follows:
  28. " cd Perl_Root_dir\bin
  29. " ppm.bat
  30. " install DBI
  31. " install DBD::ODBC
  32. " quit
  33. "
  34. " Installing the SQL Anywhere DBI module (ensure you are using 10.0.1.3525
  35. " and above)
  36. " cd %SQLANY10%\src\perl
  37. " copy "%SQLANYSAMP10%\demo.db"
  38. " dbeng10 demo
  39. "
  40. " Make sure SQLANY10 is in your path before any other versions of SQL
  41. " Anywhere.
  42. " "C:\Program Files\Microsoft Visual Studio .Net 2003\Common7\Tools\vsvars32.bat"
  43. " or
  44. " "C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat"
  45. " or
  46. " "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
  47. " perl Makefile.PL
  48. " nmake
  49. " nmake test
  50. " nmake install
  51. "
  52. " Installing the Oracle DBI module
  53. " cd Perl_Root_dir\bin
  54. " ppm-shell.bat
  55. " install DBD::Oracle
  56. " quit
  57. "
  58. " Installing the Sybase (ASE) DBI module
  59. " "C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat"
  60. " cd Perl_Root_dir\bin
  61. " perl -MCPAN -e shell
  62. " install DBD::Sybase
  63. " quit
  64. "
  65. " Installing the DB2 DBI module
  66. " Make sure your DB2_HOME directory has been set
  67. " cd Perl_Root_dir\bin
  68. " perl -MCPAN -e shell
  69. " install DBD::DB2
  70. " quit
  71. "
  72. " Installing the binary MySQL DBI module
  73. " cd Perl_Root_dir\bin
  74. " ppm-shell.bat
  75. " install DBD-mysql
  76. " quit
  77. "
  78. " Installing the Sybase ASE or SQL Server DBI module
  79. " http://lists.ibiblio.org/pipermail/freetds/2001q3/004748.html
  80. " cd Perl_Root_dir\bin
  81. " ppm-shell.bat
  82. " install Sybase-TdsServer
  83. " Testing:
  84. " http://www.easysoft.com/developer/languages/perl/sql_server_unix_tutorial.html
  85. " perl -MCPAN -e shell
  86. " perl -e "use DBD::ODBC;"
  87. " perl -MDBD::ODBC -e "print $DBD::ODBC::VERSION;"
  88. " perl -MDBI -e "DBI->installed_versions;"
  89. "
  90. " Usage:
  91. " dbext_dbi.vim is designed to be used by the dbext.vim plugin.
  92. " See :h dbext.txt
  93. "
  94. " Debugging Perl DBI:
  95. " http://www.easysoft.com/developer/languages/perl/dbi-debugging.html
  96. "
  97. " This program is free software; you can redistribute it and/or modify
  98. " it under the terms of the GNU General Public License as published by
  99. " the Free Software Foundation; either version 2 of the License, or
  100. " (at your option) any later version.
  101. "
  102. " This program is distributed in the hope that it will be useful,
  103. " but WITHOUT ANY WARRANTY; without even the implied warranty of
  104. " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  105. " GNU General Public License for more details.
  106. "
  107. " You should have received a copy of the GNU General Public License
  108. " along with this program; if not, write to the Free Software
  109. " Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  110. if exists("g:loaded_dbext_dbi")
  111. finish
  112. endif
  113. if !has('perl')
  114. let g:loaded_dbext_dbi = -1
  115. let g:loaded_dbext_dbi_msg = 'Vim does not have perl support enabled'
  116. finish
  117. endif
  118. let g:loaded_dbext_dbi = 1200
  119. if !exists("dbext_dbi_debug")
  120. let g:dbext_dbi_debug = 0
  121. endif
  122. if !exists("dbext_dbi_result")
  123. let g:dbext_dbi_result = -1
  124. endif
  125. if !exists("dbext_dbi_msg")
  126. let g:dbext_dbi_msg = ""
  127. endif
  128. if !exists("dbext_dbi_sql")
  129. let g:dbext_dbi_sql = ""
  130. endif
  131. if !exists("dbext_dbi_max_rows")
  132. let g:dbext_dbi_max_rows = 300
  133. endif
  134. if !exists("dbext_default_dbi_column_delimiter")
  135. let g:dbext_default_dbi_column_delimiter = " "
  136. endif
  137. if !exists("dbext_dbi_trace_level")
  138. let g:dbext_dbi_trace_level = 0
  139. endif
  140. " See help use-cpo-save for info on the variable save_cpo
  141. let s:save_cpo = &cpo
  142. set cpo&vim
  143. function! dbext_dbi#DBI_load_perl_subs()
  144. if exists("g:dbext_dbi_loaded_perl_subs")
  145. finish
  146. endif
  147. " echomsg "Loading Perl subroutines"
  148. let g:loaded_dbext_dbi_msg = 'pre test of perl version'
  149. perl << EOVersionTest
  150. require 5.8.0;
  151. VIM::DoCommand('let g:loaded_dbext_dbi_msg=\'passed test of perl version\'');
  152. EOVersionTest
  153. if (g:loaded_dbext_dbi_msg != 'passed test of perl version')
  154. let g:loaded_dbext_dbi_msg = 'failed test of perl version'
  155. return
  156. endif
  157. let g:loaded_dbext_dbi_msg = 'creating Perl subroutines'
  158. perl << EOCore
  159. BEGIN {(*STDERR = *STDOUT) || die;}
  160. use diagnostics;
  161. use warnings;
  162. use strict;
  163. use Data::Dumper qw( Dumper );
  164. use DBI;
  165. my %connections;
  166. my @result_headers;
  167. my @result_set;
  168. my @result_col_length;
  169. my $result_max_col_width;
  170. my $max_rows = 300;
  171. my $min_col_width = 4; # First NULL
  172. my $test_inc = 0;
  173. my $conn_inc = 0;
  174. my $dbext_dbi_sql = "";
  175. my $col_sep_vert = " ";
  176. my $debug = 0;
  177. my $inside_vim = 0;
  178. my $native_err = 0;
  179. sub db_set_vim_var
  180. {
  181. my $var_name = shift;
  182. my $string = shift;
  183. my $let = ('let '.$var_name.'="'.db_escape($string).'"');
  184. if( $inside_vim ) {
  185. # db_echo('db_set_vim_var:'.$let);
  186. VIM::DoCommand($let);
  187. # db_echo('db_set_vim_var finished');
  188. } else {
  189. print('let '.$var_name.'="'.$string."\"\n");
  190. }
  191. }
  192. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_trim_white_space');
  193. sub db_trim_white_space($)
  194. {
  195. my $string = shift;
  196. $string =~ s/^\s+//;
  197. $string =~ s/\s+$//;
  198. return $string;
  199. }
  200. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_echo');
  201. sub db_echo
  202. {
  203. my $msg = shift;
  204. # VIM::Msg('DBI:'.$msg, 'WarningMsg');
  205. db_vim_op( "Msg", 'DBI:'.$msg );
  206. return 0;
  207. }
  208. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_debug');
  209. sub db_debug
  210. {
  211. my $msg = shift;
  212. $debug and db_echo($msg);
  213. }
  214. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_is_debug');
  215. sub db_is_debug
  216. {
  217. return db_vim_eval('g:dbext_dbi_debug');
  218. }
  219. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_vim_check_inside');
  220. sub db_vim_check_inside
  221. {
  222. eval {
  223. VIM::Eval(1);
  224. };
  225. if ($@) {
  226. db_debug("Not inside Vim:".$@);
  227. $inside_vim = 0;
  228. } else {
  229. db_debug("Inside Vim:".$@);
  230. $inside_vim = 1;
  231. }
  232. return;
  233. }
  234. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_vim_eval');
  235. sub db_vim_eval
  236. {
  237. my $cmd = shift;
  238. my $rc;
  239. my $val;
  240. if( ! $inside_vim ) {
  241. if( $cmd eq "bufnr('%')" ) {
  242. return 1;
  243. }
  244. if( $cmd eq "g:dbext_dbi_max_rows" ) {
  245. return 10;
  246. }
  247. if( $cmd eq "g:dbext_default_dbi_column_delimiter" ) {
  248. return "\t";
  249. }
  250. }
  251. if( defined($cmd) ) {
  252. if( $inside_vim ) {
  253. # return VIM::Eval($cmd);
  254. ($rc, $val) = VIM::Eval($cmd);
  255. # db_echo("db_vim_eval:$cmd:$rc:$val");
  256. if( $rc == 1 ) {
  257. return $val;
  258. } else {
  259. return -1;
  260. }
  261. } else {
  262. return ($cmd);
  263. }
  264. }
  265. return "";
  266. }
  267. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_vim_op');
  268. sub db_vim_op
  269. {
  270. my $op = shift;
  271. if( ! defined($op) ) {
  272. return;
  273. }
  274. if( $op eq "Count" ) {
  275. if( $inside_vim ) {
  276. return $main::curbuf->Count();
  277. } else {
  278. return 1;
  279. }
  280. } elsif ( $op eq "Append" ) {
  281. my $line_nbr = shift;
  282. my $line_txt = shift;
  283. if( ! defined($line_nbr) ) {
  284. $line_nbr = "";
  285. }
  286. if( ! defined($line_txt) ) {
  287. $line_txt = "";
  288. }
  289. if( $inside_vim ) {
  290. $main::curbuf->Append($line_nbr, $line_txt);
  291. } else {
  292. print "$line_txt\n";
  293. }
  294. } elsif ( $op eq "call" ) {
  295. my $cmd = shift;
  296. if( ! defined($cmd) ) {
  297. $cmd = "";
  298. }
  299. if( $inside_vim ) {
  300. VIM::DoCommand($cmd);
  301. } else {
  302. print "Vim:DoCommand:$cmd\n";
  303. }
  304. } else {
  305. # Msg
  306. my $msg = shift;
  307. if( ! defined($msg) ) {
  308. $msg = "";
  309. }
  310. if( $inside_vim ) {
  311. VIM::Msg('DBI:'.$msg, 'WarningMsg');
  312. } else {
  313. print "$msg\n";
  314. }
  315. }
  316. return "";
  317. }
  318. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_vim_print');
  319. sub db_vim_print
  320. {
  321. my $line_nbr = shift;
  322. my $line_txt = shift;
  323. my $printed_lines = 0;
  324. my $max_col_width = $result_max_col_width + 2;
  325. if ( ! defined($line_nbr) ) {
  326. db_echo('db_vim_print invalid line number');
  327. return -1;
  328. }
  329. if ( ! defined($line_txt) ) {
  330. $line_txt = "";
  331. }
  332. my @lines = split("\n", $line_txt);
  333. foreach my $line (@lines) {
  334. if ( $printed_lines > 0 ) {
  335. # Multiple lines will only be within the string if the
  336. # user is printing in a vertical orientation.
  337. # Therefore if the printed_lines is > 1 we know
  338. # we have split the column data and we need to prepend
  339. # blanks to line up the text with the data above.
  340. $line = (' ' x $max_col_width).$line;
  341. }
  342. # $main::curbuf->Append($line_nbr, $line);
  343. db_vim_op("Append", $line_nbr, $line);
  344. $line_nbr++;
  345. $printed_lines++;
  346. }
  347. return $printed_lines;
  348. }
  349. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_get_defaults');
  350. sub db_get_defaults
  351. {
  352. $col_sep_vert = db_vim_eval('g:dbext_default_dbi_column_delimiter');
  353. }
  354. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_escape');
  355. sub db_escape
  356. {
  357. my $escaped = shift;
  358. if( defined($escaped) ) {
  359. $escaped =~ s/"/\\"/g;
  360. $escaped =~ s/\\/\\\\/g;
  361. $escaped =~ s/\n/\\n/g;
  362. }
  363. return $escaped;
  364. }
  365. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_remove_newlines');
  366. sub db_remove_newlines
  367. {
  368. my $escaped = shift;
  369. $escaped =~ s/\n/ /g;
  370. return $escaped;
  371. }
  372. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_get_available_drivers');
  373. sub db_get_available_drivers
  374. {
  375. my @ary = DBI->available_drivers;
  376. db_echo('db_available_drivers:'.Dumper(@ary));
  377. return 0;
  378. }
  379. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_list_connections');
  380. sub db_list_connections
  381. {
  382. db_debug('db_list_connections:'.Dumper(%connections));
  383. my @row;
  384. my @table;
  385. my @col_length;
  386. my $max_col_width = 0;
  387. my $i = 0;
  388. my @headers = [ ("Buffer", "Driver", "AutoCommit", "CommitOnDisconnect", "Connection Parameters", "LongReadLen") ];
  389. db_set_vim_var("g:dbext_dbi_msg", '');
  390. foreach my $row2 ( @headers ) {
  391. db_debug('db_list_connections:R'.Dumper($row2));
  392. foreach my $col2 ( @{$row2} ) {
  393. my $temp_length = length((defined($col2)?$col2:""));
  394. if ( !defined($col_length[$i]) ) {
  395. $col_length[$i] = 0;
  396. }
  397. $col_length[$i] = ( $temp_length > $col_length[$i] ? $temp_length : $col_length[$i] );
  398. db_debug("db_list_connections:i:$i:L:".$col_length[$i]);
  399. $max_col_width = ( $temp_length > $max_col_width ? $temp_length : $max_col_width );
  400. $i++;
  401. }
  402. }
  403. if ( keys(%connections) > 0 )
  404. {
  405. foreach my $bufnr ( keys %connections ) {
  406. @row = ($bufnr
  407. , $connections{$bufnr}->{'driver'}
  408. , $connections{$bufnr}->{'conn'}->{'AutoCommit'}
  409. , $connections{$bufnr}->{'CommitOnDisconnect'}
  410. , $connections{$bufnr}->{'params'}
  411. , $connections{$bufnr}->{'conn'}->{'LongReadLen'}
  412. );
  413. push @table, [ @row ];
  414. $i = 0;
  415. foreach my $col ( @row ) {
  416. my $temp_length = length((defined($col)?$col:""));
  417. $col_length[$i] = ( $temp_length > $col_length[$i] ? $temp_length : $col_length[$i] );
  418. $i++;
  419. }
  420. }
  421. }
  422. @result_headers = @headers;
  423. @result_set = @table;
  424. @result_col_length = @col_length;
  425. $result_max_col_width = $max_col_width;
  426. db_debug('db_list_connections:pre-formatting'.Dumper(@result_set));
  427. db_format_array();
  428. if ( keys(%connections) == 0 )
  429. {
  430. push @result_set, [ ("There are no active DBI connections", "", "", "", "", "") ];
  431. }
  432. db_debug('db_list_connections:final:'.Dumper(@result_set));
  433. # TODO
  434. # This should define an array so db_print_results can be used
  435. db_set_vim_var("g:dbext_dbi_result", 'DBI:');
  436. db_set_vim_var("g:dbext_dbi_msg", '');
  437. return 0;
  438. }
  439. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_get_info');
  440. sub db_get_info
  441. {
  442. my $option = shift;
  443. my $conn_local;
  444. my $driver;
  445. if ( ! db_is_connected() ) {
  446. db_echo("db_get_info:You must connect first");
  447. return -1;
  448. }
  449. ($conn_local, $driver) = db_get_connection();
  450. my $result = "";
  451. if ( defined($option) ) {
  452. $result = $conn_local->get_info($option);
  453. } else {
  454. $result = "DBMS Name[".$conn_local->get_info(17).
  455. "] Version[".$conn_local->get_info(18)."]";
  456. }
  457. db_debug('db_get_info:'.$result);
  458. db_set_vim_var("g:dbext_dbi_result", $result);
  459. return 0;
  460. }
  461. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_commit');
  462. sub db_commit
  463. {
  464. my $conn_local;
  465. my $driver;
  466. db_debug("Committing connection");
  467. if ( ! db_is_connected() ) {
  468. db_set_vim_var("g:dbext_dbi_result", -1);
  469. db_set_vim_var("g:dbext_dbi_msg", 'You are not connected to a database');
  470. return -1;
  471. }
  472. ($conn_local, $driver) = db_get_connection();
  473. my $rc = $conn_local->commit;
  474. db_set_vim_var("g:dbext_dbi_result", $rc);
  475. return $rc;
  476. }
  477. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_rollback');
  478. sub db_rollback
  479. {
  480. my $conn_local;
  481. my $driver;
  482. db_debug("Rolling back connection");
  483. if ( ! db_is_connected() ) {
  484. db_set_vim_var("g:dbext_dbi_result", -1);
  485. db_set_vim_var("g:dbext_dbi_msg", 'You are not connected to a database');
  486. return -1;
  487. }
  488. ($conn_local, $driver) = db_get_connection();
  489. my $rc = $conn_local->rollback;
  490. db_set_vim_var("g:dbext_dbi_result", $rc);
  491. return $rc;
  492. }
  493. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_is_connected');
  494. sub db_is_connected
  495. {
  496. my $bufnr = shift;
  497. my $is_connected = 0;
  498. my $conn_local;
  499. $test_inc++;
  500. db_debug('db_is_connected:test_inc:'.$test_inc);
  501. if( ! defined($bufnr) ) {
  502. db_debug('db_is_connected:$bufnr undefined');
  503. $bufnr = db_vim_eval("bufnr('%')");
  504. db_debug("db_is_connected:looking up $bufnr");
  505. }
  506. if( %connections ) {
  507. if( ! exists($connections{$bufnr}) ) {
  508. db_debug('db_is_connected:hash does not exist:'.$bufnr);
  509. } else {
  510. db_debug('db_is_connected:hash exists:'.$bufnr);
  511. $conn_local = $connections{$bufnr}->{'conn'};
  512. }
  513. }
  514. if( defined($conn_local) ) {
  515. db_debug('db_is_connected:conn exists');
  516. if( $conn_local->{Active} ) {
  517. db_debug('db_is_connected:seems active');
  518. $is_connected = 1;
  519. } else {
  520. db_debug('db_is_connected:disconnected');
  521. }
  522. } else {
  523. db_debug('db_is_connected:NO conn');
  524. }
  525. db_set_vim_var("g:dbext_dbi_result", $is_connected);
  526. return $is_connected;
  527. }
  528. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_get_connection');
  529. sub db_get_connection
  530. {
  531. my $bufnr = shift;
  532. my $driver = '';
  533. my $conn_local;
  534. if( ! defined($bufnr) ) {
  535. db_debug('db_get_connected:$bufnr undefined');
  536. $bufnr = db_vim_eval("bufnr('%')");
  537. db_debug("db_get_connection:looking up $bufnr");
  538. }
  539. if ( ! db_is_connected($bufnr) ) {
  540. db_debug('db_get_connection:connection not found:'.$bufnr);
  541. return undef;
  542. }
  543. db_debug('db_get_connection:returning:'.$bufnr);
  544. $conn_local = $connections{$bufnr}->{'conn'};
  545. $driver = $connections{$bufnr}->{'driver'};
  546. return ($conn_local, $driver);
  547. }
  548. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_check_error');
  549. sub db_check_error
  550. {
  551. my $err = 0;
  552. my $level = '';
  553. my $msg = '';
  554. my $state = '';
  555. my $bufnr = '';
  556. my $driver = shift;
  557. my $conn_local;
  558. if ( defined($DBI::err) && defined($DBI::errstr) && defined($DBI::state) ) {
  559. if( ! defined($driver) ) {
  560. ($conn_local, $driver) = db_get_connection();
  561. }
  562. db_debug("db_check_error:$bufnr:$driver");
  563. $err = $DBI::err;
  564. $msg = $DBI::errstr;
  565. $state = $DBI::state;
  566. # db_echo("db_check_error: initial values:$level:$err:$state:$msg");
  567. if( $driver eq "ODBC" ) {
  568. if ( $err eq "" && ! $msg eq "" ) {
  569. # Informational message
  570. $level = "I";
  571. } elsif ( $err eq "0" && ! $msg eq "" ) {
  572. # Warning message
  573. $level = "W";
  574. } elsif ( $err eq "1" && ! $msg eq "" ) {
  575. # Error message
  576. $level = "E";
  577. }
  578. if( defined($native_err) ) {
  579. $err = $native_err;
  580. }
  581. } else {
  582. if ( ($err eq "" || $err eq "0") && ! $msg eq "" ) {
  583. # Informational message
  584. $level = "I";
  585. } elsif ( $err gt "0" && ! $msg eq "" ) {
  586. # Warning message
  587. $level = "W";
  588. } elsif ( $err lt "0" && ! $msg eq "" ) {
  589. # Error message
  590. $level = "E";
  591. }
  592. }
  593. }
  594. db_debug("db_check_error: returning:$level:$err:$state:$msg");
  595. return ($level, $err, $msg, $state);
  596. }
  597. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_odbc_err_handler');
  598. sub db_odbc_err_handler
  599. {
  600. my ($state, $msg, $native) = @_;
  601. $native_err = $native;
  602. # db_echo("db_odbc_err_handler: native error:$native_err");
  603. # Do not ignore the error
  604. return 1;
  605. }
  606. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_connect');
  607. sub db_connect
  608. {
  609. my $driver = shift;
  610. my $conn_parms = shift;
  611. my $uid = shift;
  612. my $pwd = shift;
  613. my $bufnr = db_vim_eval("bufnr('%')");
  614. my $conn_local;
  615. # $debug = db_is_debug();
  616. # db_debug("Connect: driver:$driver parms:$conn_parms U:$uid P:$pwd");
  617. db_debug('db_connected:checking for existing connection');
  618. if ( db_is_connected() ) {
  619. return 0;
  620. }
  621. if ( ! defined($driver) ) {
  622. # db_echo("Invalid driver:$driver");
  623. db_set_vim_var("g:dbext_dbi_msg", 'E. Invalid driver:'.$driver);
  624. db_set_vim_var("g:dbext_dbi_result", -1);
  625. return -1;
  626. }
  627. if ( ! defined($uid) ) {
  628. # db_echo("Invalid userid:$uid");
  629. db_set_vim_var("g:dbext_dbi_msg", 'E. Invalid userid:'.$uid);
  630. db_set_vim_var("g:dbext_dbi_result", -1);
  631. return -1;
  632. }
  633. if ( ! defined($pwd) ) {
  634. # db_echo("Invalid password:$pwd");
  635. db_set_vim_var("g:dbext_dbi_msg", 'E. Invalid password:'.$pwd);
  636. db_set_vim_var("g:dbext_dbi_result", -1);
  637. return -1;
  638. }
  639. my $DATA_SOURCE = "DBI:$driver:$conn_parms";
  640. db_debug('db_connected:connecting to:'.$DATA_SOURCE);
  641. # Use global connection object
  642. eval {
  643. # LongReadLen sets the maximum size of a BLOB that
  644. # can be retrieved from the database.
  645. # This value can be overriden from your connection string
  646. # or by using:
  647. # DBSetOption LongReadLen=4096
  648. # DBSetOption driver_parms=LongReadLen=4096
  649. #
  650. # LongTruncOk indicates to allow data truncation,
  651. # and do not report an error.
  652. $conn_local = DBI->connect( $DATA_SOURCE, $uid, $pwd,
  653. { AutoCommit => 1,
  654. LongReadLen => 1000,
  655. LongTruncOk => 1,
  656. RaiseError => 0,
  657. PrintError => 0,
  658. PrintWarn => 0 }
  659. );
  660. # or die $DBI::errstr;
  661. };
  662. if ($@) {
  663. db_set_vim_var('g:dbext_dbi_msg', "Cannot connect to data source:".$DATA_SOURCE." using:".$uid." E:".$@);
  664. db_set_vim_var('g:dbext_dbi_result', -1);
  665. return -1;
  666. }
  667. my( $level, $err, $msg, $state ) = db_check_error($driver);
  668. if ( ! $msg eq "" ) {
  669. $msg = "$level. DBC:".(($level ne "I")?"SQLCode:$err:":"").$msg.(($state ne "")?":$state":"").":\nConnection details:$DATA_SOURCE";
  670. db_set_vim_var('g:dbext_dbi_msg', $msg);
  671. if ( $level eq "E" ) {
  672. db_set_vim_var('g:dbext_dbi_result', -1);
  673. db_debug("db_connect:$msg - exiting");
  674. return -1;
  675. }
  676. }
  677. if ( $driver eq "ODBC" ) {
  678. db_debug("db_connect: Enabling odbc_err_handler");
  679. $conn_local->{odbc_err_handler} = \&db_odbc_err_handler;
  680. }
  681. $connections{$bufnr} = {'conn' => $conn_local
  682. ,'driver' => $driver
  683. ,'uid' => $uid
  684. ,'params' => $conn_parms
  685. ,'AutoCommit' => 1
  686. ,'CommitOnDisconnect' => 1
  687. ,'LastRequest' => localtime
  688. };
  689. # db_debug('db_connected:checking if successful');
  690. # if ( ! db_is_connected() ) {
  691. # db_set_vim_var('g:dbext_dbi_msg', "Cannot connect to data source:$DATA_SOURCE using:$uid SQLCode:".$DBI::err.":".db_escape($DBI::errstr));
  692. # db_set_vim_var("g:dbext_dbi_result", -1);
  693. # return -1;
  694. # }
  695. my $trace_level = db_vim_eval("g:dbext_dbi_trace_level");
  696. if ( ! $trace_level eq "0" ) {
  697. my $vim_dir = db_vim_eval("expand('".'$VIM'."')");
  698. $conn_local->trace($trace_level, $vim_dir.'\dbi_trace.txt');
  699. }
  700. return 0;
  701. }
  702. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_disconnect');
  703. sub db_disconnect
  704. {
  705. my $bufnr = shift;
  706. my $conn_local;
  707. my $driver;
  708. db_set_vim_var('g:dbext_dbi_result', 0);
  709. if( ! defined($bufnr) ) {
  710. db_debug('db_disconnect:$bufnr is undefined');
  711. $bufnr = db_vim_eval("bufnr('%')");
  712. db_debug("db_disconnect:looking up $bufnr");
  713. }
  714. db_debug('db_disconnect:checking for existing connection:'.$bufnr);
  715. if ( ! db_is_connected($bufnr) ) {
  716. return 0;
  717. }
  718. ($conn_local, $driver) = db_get_connection();
  719. if( ! defined($conn_local) ) {
  720. db_debug('db_disconnect:This should not have happened since this buffer was connected:'.$bufnr);
  721. db_set_vim_var('g:dbext_dbi_result', -1);
  722. db_set_vim_var('g:loaded_dbext_dbi_msg', "db_disconnect:This should not have happened since this buffer was connected:$bufnr");
  723. return -1;
  724. }
  725. db_debug("db_disconnect:B:$bufnr A:".$conn_local->{AutoCommit}." C:".$connections{$bufnr}->{'CommitOnDisconnect'});
  726. if( $conn_local->{AutoCommit} == 0 && $connections{$bufnr}->{'CommitOnDisconnect'} == 1 ) {
  727. db_debug('db_disconnected: forcing COMMIT');
  728. $conn_local->commit;
  729. }
  730. db_debug('db_disconnect:disconnecting');
  731. my $rc = $conn_local->disconnect();
  732. # Remove the connection from the hash
  733. db_debug('db_disconnect:Removing connection for buffer from hash:'.$bufnr);
  734. delete $connections{$bufnr};
  735. db_debug('db_disconnect:'.Dumper(%connections));
  736. db_set_vim_var('g:dbext_dbi_result', 1);
  737. return 0;
  738. }
  739. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_disconnect_all');
  740. sub db_disconnect_all
  741. {
  742. my $conn_local;
  743. my $rc;
  744. db_debug('db_disconnect_all:Iterating through all open connections');
  745. if ( keys(%connections) > 0 )
  746. {
  747. foreach my $bufnr ( keys %connections ) {
  748. db_debug('db_disconnecting buffer:'.$bufnr);
  749. db_disconnect($bufnr);
  750. }
  751. }
  752. return 0;
  753. }
  754. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_get_connection_option');
  755. sub db_get_connection_option
  756. {
  757. my $option = shift;
  758. my $conn_local;
  759. my $driver;
  760. $debug = db_is_debug();
  761. if ( ! defined($option) ) {
  762. db_debug("An option and value must be specified");
  763. db_set_vim_var('g:dbext_dbi_msg', "An option must be specified");
  764. db_set_vim_var('g:dbext_dbi_result', -1);
  765. return -1;
  766. }
  767. if ( ! db_is_connected() ) {
  768. db_debug("You are not connected to a database");
  769. db_set_vim_var('g:dbext_dbi_msg', "You are not connected to a database");
  770. db_set_vim_var('g:dbext_dbi_result', -1);
  771. return -1;
  772. }
  773. ($conn_local, $driver) = db_get_connection();
  774. if ( ! defined($conn_local->{$option}) ) {
  775. db_debug("Option[$option] does not exist");
  776. db_set_vim_var('g:dbext_dbi_msg', "Option[".$option."] does not exist");
  777. db_set_vim_var('g:dbext_dbi_result', -1);
  778. return -1;
  779. }
  780. # Use global connection object
  781. # This expecting a boolean value (ie AutoCommit)
  782. db_set_vim_var('g:dbext_dbi_msg', "");
  783. db_set_vim_var('g:dbext_dbi_result', $conn_local->{$option});
  784. # or die $DBI::errstr;
  785. return 0;
  786. }
  787. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_set_connection_option');
  788. sub db_set_connection_option
  789. {
  790. my $option = shift;
  791. my $value = shift;
  792. my $bufnr = db_vim_eval("bufnr('%')");
  793. my $conn_local;
  794. my $driver = '';
  795. ($conn_local, $driver) = db_get_connection();
  796. $debug = db_is_debug();
  797. if ( ! defined($option) || ! defined($value) ) {
  798. db_debug("Option and value must be specified");
  799. db_set_vim_var('g:dbext_dbi_msg', "Option and value must be specified");
  800. db_set_vim_var('g:dbext_dbi_result', -1);
  801. return -1;
  802. }
  803. if ( ! db_is_connected() ) {
  804. db_debug("You are not connected to a database");
  805. db_set_vim_var('g:dbext_dbi_msg', "You are not connected to a database");
  806. db_set_vim_var('g:dbext_dbi_result', -1);
  807. return -1;
  808. }
  809. if ( $option eq 'DBI_commit_on_disconnect' ) {
  810. $connections{$bufnr}->{'CommitOnDisconnect'} = $value;
  811. db_debug("db_set_connection_option Conn[$bufnr]->Opt[$option] Val:[".$connections{$bufnr}->{'CommitOnDisconnect'}."]");
  812. } else {
  813. # Use global connection object
  814. # This expecting a boolean value (ie AutoCommit)
  815. $conn_local->{$option} = $value;
  816. # or die $DBI::errstr;
  817. db_debug("db_set_connection_option ConnLocal->Opt[$option] Val:[".$conn_local->{$option}."]");
  818. my( $level, $err, $msg, $state ) = db_check_error($driver);
  819. if ( ! $msg eq "" ) {
  820. $msg = "$level. DBSO:".(($level ne "I")?"SQLCode:$err:":"").$msg.(($state ne "")?":$state":"");
  821. db_set_vim_var('g:dbext_dbi_msg', $msg);
  822. if ( $level eq "E" ) {
  823. db_set_vim_var('g:dbext_dbi_result', -1);
  824. db_debug("db_query:$msg - exiting");
  825. return -1;
  826. }
  827. }
  828. }
  829. db_set_vim_var('g:dbext_dbi_msg', "");
  830. db_set_vim_var('g:dbext_dbi_result', "1");
  831. return 0;
  832. }
  833. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_query');
  834. sub db_query
  835. {
  836. my $sql = shift;
  837. my $conn_local;
  838. my $driver = '';
  839. if ( ! defined($sql) ) {
  840. $sql = '';
  841. }
  842. $debug = db_is_debug();
  843. # db_debug("db_query:SQL:".$sql);
  844. if ( length($sql) == 0 ) {
  845. $sql = db_vim_eval('g:dbext_dbi_sql');
  846. db_debug("db_query:SQL after eval:".$sql);
  847. }
  848. if ( length($sql) == 0 ) {
  849. db_set_vim_var("g:dbext_dbi_result", -1);
  850. db_set_vim_var("g:dbext_dbi_msg", 'No statement to exeucte');
  851. return -1;
  852. }
  853. if ( ! db_is_connected() ) {
  854. db_debug("db_query:You must connect first");
  855. db_set_vim_var("g:dbext_dbi_result", -1);
  856. db_set_vim_var("g:dbext_dbi_msg", 'You must connect first');
  857. return -1;
  858. }
  859. ($conn_local, $driver) = db_get_connection();
  860. my $sth = undef;
  861. $conn_local->{LastRequest} = localtime;
  862. $sth = $conn_local->prepare( $sql );
  863. # db_echo( "db_query:25".DBI::errstr );
  864. # db_debug("db_query:prepared:".$sql);
  865. # It is possible for an error to occur only when fetching data.
  866. # This will capture the error and report it.
  867. # if ( defined($DBI::err) && defined($DBI::errstr) ) {
  868. # if ( $DBI::err eq "" && ! $DBI::errstr eq "" ) {
  869. # # Informational message
  870. # db_set_vim_var('g:dbext_dbi_msg', 'I. DBQp:'.db_escape($DBI::errstr));
  871. # } elsif ( $DBI::err gt 0 && ! $DBI::errstr eq "" ) {
  872. # # Warning message
  873. # db_set_vim_var('g:dbext_dbi_msg', 'W. DBQp:SQLCode:'.$DBI::err.":".db_escape($DBI::errstr).":".db_escape($DBI::state));
  874. # db_debug("db_query:$result_msg");
  875. # } elsif ( ! $DBI::errstr eq "" ) {
  876. # # Error message
  877. # db_set_vim_var('g:dbext_dbi_msg', 'E. DBQp:SQLCode:'.$DBI::err.":".db_escape($DBI::errstr).":".db_escape($DBI::state));
  878. # db_set_vim_var('g:dbext_dbi_result', -1);
  879. # db_debug("db_query:$result_msg - exiting");
  880. # return -1;
  881. # }
  882. # }
  883. my( $level, $err, $msg, $state ) = db_check_error($driver);
  884. if ( ! $msg eq "" ) {
  885. $msg = "$level. DBQp:".(($level ne "I")?"SQLCode:$err:":"").$msg.(($state ne "")?":$state":"");
  886. db_set_vim_var('g:dbext_dbi_msg', $msg);
  887. if ( $level eq "E" ) {
  888. db_set_vim_var('g:dbext_dbi_result', -1);
  889. db_debug("db_query:$msg - exiting");
  890. return -1;
  891. }
  892. }
  893. my $row_count = $sth->execute;
  894. db_debug("db_query:rowcount[$row_count] executed[$sql]");
  895. if ( $row_count eq "0E0" || $row_count lt "0" ) {
  896. # 0E0 - Special case which means no rows were affected
  897. # -1 - Can be returned if executing DDL (as an example)
  898. $row_count = 0;
  899. }
  900. db_set_vim_var('g:dbext_rows_affected', $row_count);
  901. ( $level, $err, $msg, $state ) = db_check_error($driver);
  902. if ( ! $msg eq "" ) {
  903. $msg = "$level. DBQe:".(($level ne "I")?"SQLCode:$err:":"").$msg.(($state ne "")?":$state":"");
  904. db_set_vim_var('g:dbext_dbi_msg', $msg);
  905. if ( $level eq "E" ) {
  906. db_set_vim_var('g:dbext_dbi_result', -1);
  907. db_debug("db_query:$msg - exiting");
  908. return -1;
  909. }
  910. }
  911. db_format_results( $sth );
  912. $sth = undef;
  913. return 0;
  914. }
  915. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_format_results');
  916. sub db_format_results
  917. {
  918. my $sth = shift;
  919. my $i = 0;
  920. my $row_count = 0;
  921. my $max_col_width = 0;
  922. my $temp_length = 0;
  923. my $more_results;
  924. my $conn_local;
  925. my $driver;
  926. my @col_length;
  927. my @table;
  928. my @headers;
  929. ($conn_local, $driver) = db_get_connection();
  930. # Check if the NUM_OF_FIELDS is > 0.
  931. # In mysql a COMMIT does not provide a result set.
  932. if ( $sth->{NUM_OF_FIELDS} > 0 ) {
  933. # Add the column list to the array
  934. push @headers,[ @{$sth->{NAME}} ];
  935. # Set the initial length of the columns
  936. foreach my $col_name ( @{$sth->{NAME}} ) {
  937. $temp_length = length($col_name);
  938. $temp_length = ($temp_length > $min_col_width ? $temp_length : $min_col_width);
  939. $max_col_width = ( $temp_length > $max_col_width ? $temp_length : $max_col_width );
  940. $col_length[$i] = $temp_length;
  941. $i++;
  942. }
  943. while( my $row = $sth->fetchrow_arrayref() ) {
  944. $i = 0;
  945. push @table,[ @$row ];
  946. # For every column retrieved, check if it is longer than any of
  947. # the previous columns. If so, update the maximum length.
  948. # This must be done column by column since I am not aware of
  949. # a way to check the maximum length of an array without checking
  950. # every entry which we are already doing here.
  951. foreach my $col ( @{$row} ) {
  952. $temp_length = length((defined($col)?$col:""));
  953. $col_length[$i] = ( $temp_length > $col_length[$i] ? $temp_length : $col_length[$i] );
  954. $i++;
  955. }
  956. # Cap the number of rows displayed.
  957. $row_count++;
  958. $max_rows = db_vim_eval("g:dbext_dbi_max_rows");
  959. if ( $max_rows > 0 && $row_count >= $max_rows ) {
  960. db_debug('Bailing on row count:'.$max_rows);
  961. last;
  962. }
  963. }
  964. my( $level, $err, $msg, $state ) = db_check_error($driver);
  965. if ( ! $msg eq "" ) {
  966. $msg = "$level. DBfr:".(($level ne "I")?"SQLCode:$err:":"").$msg.(($state ne "")?":$state":"");
  967. db_set_vim_var('g:dbext_dbi_msg', $msg);
  968. if ( $level eq "E" ) {
  969. db_set_vim_var('g:dbext_dbi_result', -1);
  970. db_debug("db_format_results:$msg - exiting");
  971. return -1;
  972. }
  973. }
  974. db_set_vim_var('g:dbext_rows_affected', $row_count);
  975. if( $driver eq "ODBC" ) {
  976. $more_results = $sth->{odbc_more_results};
  977. } else {
  978. $more_results = $sth->{more_results};
  979. }
  980. db_debug("more_results:".(defined($more_results)?$more_results:''));
  981. if ( $more_results ) {
  982. db_debug("db_format_results: more_results:true");
  983. } else {
  984. db_debug("db_format_results: more_results:false");
  985. }
  986. }
  987. # db_echo(Dumper($sth));
  988. $sth->finish;
  989. @result_headers = @headers;
  990. @result_set = @table;
  991. @result_col_length = @col_length;
  992. $result_max_col_width = $max_col_width;
  993. db_debug("db_format_results H:".Dumper(@result_headers));
  994. db_debug('db_format_results:R count:'.length(@result_set));
  995. db_debug("db_format_results R:".Dumper(@result_set));
  996. db_format_array();
  997. # Setting the dbext_dbi_result variable to DBI: instructs
  998. # dbext.vim to call db_print_results() to add the results
  999. # to the results buffer.
  1000. my $result = "DBI:";
  1001. if ( defined($result) ) {
  1002. db_debug("db_format_results:Setting result to:$result");
  1003. db_set_vim_var('g:dbext_dbi_result', $result);
  1004. } else {
  1005. db_debug("db_format_results:returning -1");
  1006. db_set_vim_var('g:dbext_dbi_result', -1);
  1007. return -1;
  1008. }
  1009. db_debug("db_format_results:returning 0");
  1010. return 0;
  1011. }
  1012. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_format_array');
  1013. sub db_format_array()
  1014. {
  1015. # For each row returned concatenate the columns together
  1016. my $result = "DBI:";
  1017. my $fragment = "";
  1018. my $i;
  1019. my $val;
  1020. db_debug( "db_format_array:".$result );
  1021. $result = $result . db_vim_eval('g:dbext_dbi_msg');
  1022. db_debug( "db_format_array: Finished with dbext_dbi_msg:".length($result) );
  1023. # print Dumper( @result_set);
  1024. db_debug( "db_format_array: printing result" );
  1025. db_debug( "db_format_array:".$result );
  1026. db_debug( "db_format_array: finished result" );
  1027. foreach my $row2 ( @result_set ) {
  1028. $i = 0;
  1029. db_debug( "db_format_array: i:$i" );
  1030. $fragment = "";
  1031. # Ensure each column is the maximum width for the column by
  1032. # blank padding each string.
  1033. # Add an additional 3 spaces between columns.
  1034. foreach my $col2 ( @{$row2} ) {
  1035. $val = (defined($col2)?$col2:"NULL");
  1036. # Remove any unprintable characters
  1037. $val =~ tr/\x80-\xFF/ /d;
  1038. # Remove the NULL character since Vim will treat this as
  1039. # the end of the line
  1040. # For more of these see:
  1041. # http://www.asciitable.com/
  1042. $val =~ tr/\x00/ /d;
  1043. $fragment = substr ($val.(' ' x $result_col_length[$i]), 0, $result_col_length[$i]);
  1044. $col2 = $fragment;
  1045. $i++;
  1046. }
  1047. # Finally, escape any double quotes with a preceeding slash
  1048. # $result = db_escape($result) . "\n";
  1049. # RIGHT HERE, SOMETHING WITHT THE ESCAPE
  1050. $result = $result . "\n";
  1051. }
  1052. db_debug('db_format_array:result_set:'.Dumper(@result_set));
  1053. return 0;
  1054. }
  1055. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_print_results');
  1056. sub db_print_results
  1057. {
  1058. my $format = shift;
  1059. my $last_line = db_vim_op("Count");
  1060. my $row_count = 0;
  1061. my $i = 0;
  1062. my $line = "";
  1063. my $fragment = "";
  1064. my $col_name = "";
  1065. if ( ! defined($format) ) {
  1066. $format = "horizontal";
  1067. }
  1068. db_debug("db_print_results: $format");
  1069. my $msg = db_vim_eval('g:dbext_dbi_msg');
  1070. if ( ! $msg eq "" ) {
  1071. db_debug("db_print_results: Adding msg to output: $msg");
  1072. db_vim_print($last_line, $msg);
  1073. $msg = "";
  1074. $last_line = db_vim_op("Count");
  1075. }
  1076. db_set_vim_var('g:dbext_dbi_msg', '');
  1077. db_debug("db_print_results: Using format: $format");
  1078. if ( $format eq "horizontal" ) {
  1079. # Print column names
  1080. foreach my $row2 ( @result_headers ) {
  1081. $i = 0;
  1082. $fragment = "";
  1083. $line = "";
  1084. # Ensure each column is the maximum width for the column by
  1085. # blank padding each string.
  1086. # Add an additional 3 spaces between columns.
  1087. foreach my $col2 ( @{$row2} ) {
  1088. $fragment = substr ((defined($col2)?$col2:"").(' ' x $result_col_length[$i]), 0, $result_col_length[$i]);
  1089. $line .= db_remove_newlines($fragment).$col_sep_vert;
  1090. $i++;
  1091. }
  1092. # Finally, escape any double quotes with a preceeding slash
  1093. db_vim_print($last_line, $line);
  1094. $last_line++;
  1095. }
  1096. # Print underlines for each column the width of the
  1097. # largest column value
  1098. $i = 0;
  1099. $line = "";
  1100. db_debug("db_print_results: Horizontal, looping for col_length");
  1101. while ($i < scalar(@result_col_length) ) {
  1102. $line .= '-' x $result_col_length[$i].$col_sep_vert;
  1103. $i++;
  1104. }
  1105. db_vim_print($last_line, $line);
  1106. $last_line++;
  1107. # Print each row
  1108. foreach my $row3 ( @result_set ) {
  1109. $row_count++;
  1110. # db_echo("db_print_results: row count:$row_count");
  1111. $line = "";
  1112. foreach my $col3 ( @{$row3} ) {
  1113. $line .= $col3.$col_sep_vert;
  1114. }
  1115. db_vim_print($last_line, db_remove_newlines($line));
  1116. $last_line++;
  1117. }
  1118. } else {
  1119. my @formatted_headers;
  1120. my $col_nbr = 0;
  1121. my $max_col_width = $result_max_col_width + 1;
  1122. $i = 0;
  1123. db_debug("db_print_results: Vertical, looping for col_length");
  1124. while ($i < scalar(@result_col_length) ) {
  1125. $fragment = "";
  1126. $col_name = (defined($result_headers[0][$i])?$result_headers[0][$i]:"");
  1127. $col_name .= ':';
  1128. # Left justified
  1129. # $fragment = substr ($col_name.(' ' x $max_col_width), 0, $max_col_width);
  1130. # Right justified
  1131. $fragment = substr ((' ' x $max_col_width).$col_name, -$max_col_width, $max_col_width);
  1132. $formatted_headers[$i] = $fragment;
  1133. $i++;
  1134. }
  1135. my $lines_printed = 0;
  1136. foreach my $row4 ( @result_set ) {
  1137. $row_count++;
  1138. # db_echo("db_print_results: row count:$row_count");
  1139. $col_nbr = 0;
  1140. db_vim_print($last_line, "****** Row: $row_count ******");
  1141. $last_line++;
  1142. $lines_printed = 0;
  1143. foreach my $col4 ( @{$row4} ) {
  1144. $fragment = "";
  1145. $line = "";
  1146. $line .= $formatted_headers[$col_nbr].' '.$col4;
  1147. $lines_printed = db_vim_print($last_line, db_escape($line));
  1148. $last_line += $lines_printed;
  1149. $col_nbr++;
  1150. }
  1151. }
  1152. }
  1153. db_vim_print($last_line, "(".scalar(@result_set)." rows)");
  1154. db_debug("db_print_results: returning 0");
  1155. return 0;
  1156. }
  1157. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_results_variable');
  1158. sub db_results_variable
  1159. {
  1160. my $format = shift;
  1161. my $row_count = 0;
  1162. my $i = 0;
  1163. my $line = "";
  1164. my $fragment = "";
  1165. my $col_name = "";
  1166. my $result = "DBI:";
  1167. if ( ! defined($format) ) {
  1168. $format = "horizontal";
  1169. }
  1170. # db_echo("db_print_results: $format");
  1171. my $msg = db_vim_eval('g:dbext_dbi_msg');
  1172. if ( ! $msg eq "" ) {
  1173. $result .= db_escape($msg)."\n";
  1174. }
  1175. db_set_vim_var('g:dbext_dbi_msg', '');
  1176. if ( $format eq "horizontal" ) {
  1177. # Print column names
  1178. foreach my $row2 ( @result_headers ) {
  1179. $i = 0;
  1180. $fragment = "";
  1181. $line = "";
  1182. # Ensure each column is the maximum width for the column by
  1183. # blank padding each string.
  1184. # Add an additional 3 spaces between columns.
  1185. foreach my $col2 ( @{$row2} ) {
  1186. $fragment = substr ((defined($col2)?$col2:"").(' ' x $result_col_length[$i]), 0, $result_col_length[$i]);
  1187. $line .= db_remove_newlines($fragment).' ';
  1188. $i++;
  1189. }
  1190. # Finally, escape any double quotes with a preceeding slash
  1191. $result .= db_escape($line)."\n";
  1192. }
  1193. # Print underlines for each column the width of the
  1194. # largest column value
  1195. $i = 0;
  1196. $line = "";
  1197. while ($i < scalar(@result_col_length) ) {
  1198. $line .= '-' x $result_col_length[$i].' ';
  1199. $i++;
  1200. }
  1201. $result .= db_escape($line)."\n";
  1202. # Print each row
  1203. foreach my $row2 ( @result_set ) {
  1204. $row_count++;
  1205. # db_echo("db_print_results: row count:$row_count");
  1206. $line = "";
  1207. foreach my $col2 ( @{$row2} ) {
  1208. $line .= $col2.' ';
  1209. }
  1210. $result .= db_escape($line)."\n";
  1211. }
  1212. } else {
  1213. my @formatted_headers;
  1214. my $col_nbr = 0;
  1215. my $max_col_width = $result_max_col_width + 1;
  1216. $i = 0;
  1217. while ($i < scalar(@result_col_length) ) {
  1218. $fragment = "";
  1219. $col_name = (defined($result_headers[0][$i])?$result_headers[0][$i]:"");
  1220. $col_name .= ':';
  1221. # Left justified
  1222. # $fragment = substr ($col_name.(' ' x $max_col_width), 0, $max_col_width);
  1223. # Right justified
  1224. $fragment = substr ((' ' x $max_col_width).$col_name, -$max_col_width, $max_col_width);
  1225. $formatted_headers[$i] = $fragment;
  1226. $i++;
  1227. }
  1228. foreach my $row2 ( @result_set ) {
  1229. $row_count++;
  1230. # db_echo("db_print_results: row count:$row_count");
  1231. $col_nbr = 0;
  1232. $result .= "****** Row: $row_count ******\n";
  1233. foreach my $col2 ( @{$row2} ) {
  1234. $fragment = "";
  1235. $line = "";
  1236. $line .= $formatted_headers[$col_nbr].' '.$col2;
  1237. $result .= db_escape($line);
  1238. $col_nbr++;
  1239. }
  1240. }
  1241. }
  1242. $result .= "(".scalar(@result_set)." rows)\n";
  1243. if ( defined($result) ) {
  1244. db_debug("db_format_results:Setting result to:$result");
  1245. db_set_vim_var('g:dbext_dbi_result', $result);
  1246. } else {
  1247. db_set_vim_var('g:dbext_dbi_result', -1);
  1248. return -1;
  1249. }
  1250. return 0;
  1251. }
  1252. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_results_list');
  1253. sub db_results_list
  1254. {
  1255. my $show_headers = shift;
  1256. my $format = shift;
  1257. my $row_count = 0;
  1258. my $i = 0;
  1259. my $line = "";
  1260. my $fragment = "";
  1261. my $col_name = "";
  1262. my $result = "DBI:";
  1263. if ( ! defined($format) ) {
  1264. $format = "horizontal";
  1265. }
  1266. if ( ! defined($show_headers) ) {
  1267. $show_headers = 1;
  1268. }
  1269. # db_echo("db_print_results: $format");
  1270. db_set_vim_var('g:dbext_dbi_result', '[]');
  1271. if ( $format eq "horizontal" ) {
  1272. # Print column names
  1273. if ( $show_headers == 1 ) {
  1274. foreach my $row2 ( @result_headers ) {
  1275. $line = join("','", @{$row2});
  1276. if ( scalar(@{$row2}) > 0 ) {
  1277. $line = "'".$line."'";
  1278. }
  1279. $line = 'call add(g:dbext_dbi_result, ['.$line.']';
  1280. # Escape any double quotes with a preceeding slash
  1281. $line = db_escape($line)."\n";
  1282. # VIM::DoCommand($line);
  1283. db_vim_op("call", $line);
  1284. }
  1285. }
  1286. # Print each row
  1287. foreach my $row2 ( @result_set ) {
  1288. $line = join("','", @{$row2});
  1289. if ( scalar(@{$row2}) > 0 ) {
  1290. $line = "'".$line."'";
  1291. }
  1292. $line = 'call add(g:dbext_dbi_result, ['.$line.']';
  1293. # Escape any double quotes with a preceeding slash
  1294. $line = db_escape($line)."\n";
  1295. # VIM::DoCommand($line);
  1296. db_vim_op("call", $line);
  1297. }
  1298. } else {
  1299. my @formatted_headers;
  1300. my $col_nbr = 0;
  1301. my $max_col_width = $result_max_col_width + 1;
  1302. $i = 0;
  1303. while ($i < scalar(@result_col_length) ) {
  1304. $fragment = "";
  1305. $col_name = (defined($result_headers[0][$i])?$result_headers[0][$i]:"");
  1306. $col_name .= ':';
  1307. # Left justified
  1308. # $fragment = substr ($col_name.(' ' x $max_col_width), 0, $max_col_width);
  1309. # Right justified
  1310. $fragment = substr ((' ' x $max_col_width).$col_name, -$max_col_width, $max_col_width);
  1311. $formatted_headers[$i] = $fragment;
  1312. $i++;
  1313. }
  1314. foreach my $row2 ( @result_set ) {
  1315. $row_count++;
  1316. # db_echo("db_print_results: row count:$row_count");
  1317. $col_nbr = 0;
  1318. $result .= "****** Row: $row_count ******\n";
  1319. foreach my $col2 ( @{$row2} ) {
  1320. $fragment = "";
  1321. $line = "";
  1322. $line .= $formatted_headers[$col_nbr].' '.$col2;
  1323. $result .= db_escape($line);
  1324. $col_nbr++;
  1325. }
  1326. }
  1327. }
  1328. return 0;
  1329. }
  1330. db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_catalogue');
  1331. sub db_catalogue
  1332. {
  1333. my $request_type = shift;
  1334. my $result = undef;
  1335. my $object_type = undef;
  1336. my $catalogue = undef;
  1337. my $schema = undef;
  1338. my $table = undef;
  1339. my $column = '%';
  1340. my $level;
  1341. my $err;
  1342. my $msg;
  1343. my $state;
  1344. my $conn_local;
  1345. my $driver;
  1346. # $debug = db_is_debug();
  1347. if ( length($request_type) == 0 ) {
  1348. db_set_vim_var('g:dbext_dbi_msg', 'A request_type must be specified');
  1349. db_set_vim_var('g:dbext_dbi_result', -1);
  1350. return -1;
  1351. }
  1352. if ( ! db_is_connected() ) {
  1353. db_debug("You must connect first");
  1354. db_set_vim_var('g:dbext_dbi_msg', 'You are not connected to a database');
  1355. db_set_vim_var('g:dbext_dbi_result', -1);
  1356. return -1;
  1357. }
  1358. ($conn_local, $driver) = db_get_connection();
  1359. my $sth = undef;
  1360. if ( $request_type eq 'TABLE' || $request_type eq 'VIEW' ) {
  1361. $object_type = shift;
  1362. $schema = shift;
  1363. $table = shift;
  1364. db_debug("db_catalogue using the following:".(defined($catalogue)?$catalogue:"").":".(defined($schema)?$schema:"").":".(defined($table)?$table:"").":".(defined($object_type)?$object_type:""));
  1365. # Working call would be
  1366. # table_info(undef, undef, undef, '%TABLE%');
  1367. # table_info(undef, undef, 'c%', '%TABLE%');
  1368. # table_info(undef, 'DB%', 'c%', '%TABLE%');
  1369. eval {
  1370. $sth = $conn_local->table_info($catalogue, $schema, $table, $object_type);
  1371. };
  1372. }
  1373. if ( $request_type eq 'COLUMN' ) {
  1374. $schema = shift;
  1375. $table = shift;
  1376. db_debug("db_catalogue using the following:".(defined($catalogue)?$catalogue:"").":".(defined($schema)?$schema:"").":".(defined($table)?$table:"").":".(defined($object_type)?$object_type:""));
  1377. # Working calls would be:
  1378. # column_info(undef, undef, 'customers', '%');
  1379. # column_info(undef, 'DBA', 'customers', '%');
  1380. eval {
  1381. $sth = $conn_local->column_info($catalogue, $schema, $table, $column);
  1382. };
  1383. }
  1384. if ($@) {
  1385. db_debug("db_catalogue statement error for request type:$request_type\n".db_escape($@));
  1386. db_set_vim_var('g:dbext_dbi_msg', 'Invalid statement for request type:'.$request_type.":".db_escape($@));
  1387. db_set_vim_var('g:dbext_dbi_result', -1);
  1388. return -1;
  1389. }
  1390. if ( defined($sth) ) {
  1391. db_debug("db_catalogue statement is defined");
  1392. $sth->execute;
  1393. ( $level, $err, $msg, $state ) = db_check_error($driver);
  1394. if ( ! $msg eq "" ) {
  1395. $msg = "$level. DBcate:".(($level ne "I")?"SQLCode:$err:":"").$msg.(($state ne "")?":$state":"");
  1396. db_set_vim_var('g:dbext_dbi_msg', $msg);
  1397. if ( $level eq "E" ) {
  1398. db_set_vim_var('g:dbext_dbi_result', -1);
  1399. db_debug("db_catalogue:$msg - exiting");
  1400. return -1;
  1401. }
  1402. }
  1403. db_format_results( $sth );
  1404. } else {
  1405. db_set_vim_var('g:dbext_dbi_result', -1);
  1406. ( $level, $err, $msg, $state ) = db_check_error($driver);
  1407. if ( ! $msg eq "" ) {
  1408. $msg = "$level. DBcats:".(($leve

Large files files are truncated, but you can click here to view the full file