-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathdbext_dbi.vim
More file actions
1970 lines (1778 loc) · 64.4 KB
/
dbext_dbi.vim
File metadata and controls
1970 lines (1778 loc) · 64.4 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
" dbext.vim - Commn Database Utility
" Copyright (C) 2002-16, Peter Bagyinszki, David Fishburn
" ---------------------------------------------------------------
" File: dbext_dbi.vim
" Copyright (C) 2002-10, Peter Bagyinszki, David Fishburn
" Purpose: A perl extension for use with dbext.vim.
" It adds transaction support and the ability
" to reach any database currently supported
" by Perl and DBI.
" Version: 23.00
" Maintainer: David Fishburn <dfishburn dot vim at gmail dot com>
" Authors: David Fishburn <dfishburn dot vim at gmail dot com>
" Last Modified: 2015 Jan 06
" Created: 2007-05-24
" Homepage: http://vim.sourceforge.net/script.php?script_id=356
"
" Help: :h dbext.txt
"
" System Requirements:
"
" VIM with embedded perl support. You can check if your Vim has this
" support using
" :echo has('perl')
"
" This plugin supports these perl modules:
" DBI
" DBD::ODBC
"
" Install these perl modules, using ActiveState Perl on
" Windows you can do it as follows:
" cd Perl_Root_dir\bin
" ppm.bat
" install DBI
" install DBD::ODBC
" quit
"
" Installing the SQL Anywhere DBI module (ensure you are using 10.0.1.3525
" and above)
" cd %SQLANY10%\src\perl
" copy "%SQLANYSAMP10%\demo.db"
" dbeng10 demo
"
" cd %SQLANY11%\SDK\perl
" copy "%SQLANYSAMP11%\demo.db"
" dbeng11 demo
"
" cd %SQLANY12%\SDK\perl
" copy "%SQLANYSAMP12%\demo.db"
" dbeng12 demo
"
" cd %SQLANY16%\SDK\perl
" copy "%SQLANYSAMP16%\demo.db"
" dbeng16 demo
"
" Make sure SQLANY(10|11|12|16) is in your path before any other versions of SQL
" Anywhere.
" "C:\Program Files\Microsoft Visual Studio .Net 2003\Common7\Tools\vsvars32.bat"
" or
" "C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat"
" or
" "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
" or
" "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"
" or
" "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\vsvars32.bat"
" perl Makefile.PL
" nmake
" nmake test
" nmake install
"
" Installing the Oracle DBI module
" cd Perl_Root_dir\bin
" ppm-shell.bat
" install DBD::Oracle
" quit
"
" Using the Oracle Instant Client with DBI
" http://www.oracle.com/technetwork/topics/winsoft-085727.html
"
" Installing the Sybase (ASE) DBI module
" "C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat"
" cd Perl_Root_dir\bin
" perl -MCPAN -e shell
" install DBD::Sybase
" quit
"
" Installing the DB2 DBI module
" Make sure your DB2_HOME directory has been set
" cd Perl_Root_dir\bin
" perl -MCPAN -e shell
" install DBD::DB2
" quit
"
" Installing the CrateIO DBI module
" Make sure your DB2_HOME directory has been set
" cd Perl_Root_dir\bin
" perl -MCPAN -e shell
" install DBD::Crate
" quit
"
" Installing the binary MySQL DBI module
" cd Perl_Root_dir\bin
" ppm-shell.bat
" install DBD-mysql
" quit
"
" Installing the Sybase ASE or SQL Server DBI module
" http://lists.ibiblio.org/pipermail/freetds/2001q3/004748.html
" cd Perl_Root_dir\bin
" ppm-shell.bat
" install Sybase-TdsServer
" Testing:
" http://www.easysoft.com/developer/languages/perl/sql_server_unix_tutorial.html
" perl -MCPAN -e shell
" perl -e "use DBD::ODBC;"
" perl -MDBD::ODBC -e "print $DBD::ODBC::VERSION;"
" perl -MDBI -e "DBI->installed_versions;"
"
" Usage:
" dbext_dbi.vim is designed to be used by the dbext.vim plugin.
" See :h dbext.txt
"
" Debugging Perl DBI:
" http://www.easysoft.com/developer/languages/perl/dbi-debugging.html
"
" This program is free software; you can redistribute it and/or modify
" it under the terms of the GNU General Public License as published by
" the Free Software Foundation; either version 2 of the License, or
" (at your option) any later version.
"
" This program is distributed in the hope that it will be useful,
" but WITHOUT ANY WARRANTY; without even the implied warranty of
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
" GNU General Public License for more details.
"
" You should have received a copy of the GNU General Public License
" along with this program; if not, write to the Free Software
" Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
if exists("g:loaded_dbext_dbi")
finish
endif
let g:loaded_dbext_dbi = 2300
" Turn on support for line continuations when creating the script
let s:cpo_save = &cpo
set cpo&vim
function! dbext_dbi#DBI_initialize()
if !exists("dbext_dbi_debug")
let g:dbext_dbi_debug = 0
endif
if !exists("dbext_dbi_result")
let g:dbext_dbi_result = -1
endif
if !exists("dbext_dbi_msg")
let g:dbext_dbi_msg = ""
endif
if !exists("dbext_dbi_sql")
let g:dbext_dbi_sql = ""
endif
if !exists("dbext_default_DBI_max_rows")
let g:dbext_default_DBI_max_rows = 300
endif
if !exists("dbext_default_DBI_max_column_width")
let g:dbext_default_DBI_max_column_width = 0
endif
if !exists("dbext_default_DBI_column_delimiter")
let g:dbext_default_DBI_column_delimiter = " "
endif
if !exists("dbext_dbi_trace_level")
let g:dbext_dbi_trace_level = 0
endif
endfunction
call dbext_dbi#DBI_initialize()
if ! has('perl')
let g:loaded_dbext_dbi = -1
let g:loaded_dbext_dbi_msg = 'Vim does not have perl support enabled'
finish
endif
" dbext_dbi sub routines:
" sub db_trim_white_space
" sub db_echo
" sub db_debug
" sub db_is_debug
" - Debugging subroutines
" sub db_vim_check_inside
" - For debugging purposes this code can run outside of
" Vim. This routine will bypass certain code if running
" outside of Vim.
" sub db_vim_eval
" - Uses Vim to evaluate expressions
" sub db_vim_op
" sub db_vim_print
" - Write to a Vim buffer
" sub db_get_defaults
" sub db_escape
" - Escape strings for expressions
" sub db_remove_newlines
" sub db_strdisplaywidth
" sub db_get_available_drivers
" - Returns a list of installed DBI drivers
" sub db_list_connections
" - Lists all open database connections
" sub db_get_info
" - Returns information about the DBI driver
" sub db_commit
" sub db_rollback
" sub db_is_connected
" - Used to determine if a new connection is required
" sub db_get_connection
" - Returns this buffers connection handle
" sub db_check_error
" sub db_odbc_err_handler
" - Error handling and reporting
" sub db_connect
" sub db_disconnect
" sub db_disconnect_all
" - Connecting and disconnecting
" sub db_get_connection_option
" sub db_set_connection_option
" - Allows DBI options to be set like BLOB size
" sub db_query
" - Executes a statement against the database
" sub db_format_results
" - Loops through the results of a query and determines
" maximum column size for formatting
" sub db_format_array
" - Loops through the results and formats the array
" for display in a Vim buffer
" sub db_print_results
" - Makes the appropriate calls to add the data
" to the Vim dbext buffer
" sub db_results_variable
" - Instead of adding the results to the Vim dbext buffer
" directly, the results can be returned to Vim as a string
" and this can be displayed via a echo command.
" sub db_results_list
" - Loops through the result set from the query and creates
" and array of values.
" sub db_catalogue
" sub db_odbc_catalogue
" - Used to query the DBI catalogue and return metadata
" like lists of tables, columns, stored procedures
" and so on.
"
" When a query is executed (db_query) dbext will check for any errors
" (db_check_error).
"
" If no errors, loop through the results and determine the maximum length of
" each column (db_format_results) and store the results of the query
" (db_format_array) then place in the global variable @result_set.
"
" Then I ask dbext for the column separator
" (dbext_default_DBI_column_delimiter, which defaults to 3 blank spaces).
"
" Then db_format_array() loops through the array, @result_set and using the
" array of column maximums builds an appropriate length string so all columns
" line up. Then eventually we write to a Vim buffer using dbext_dbi.vim/sub
" db_vim_print.
"
function! dbext_dbi#DBI_load_perl_subs()
if exists("g:dbext_dbi_loaded_perl_subs")
finish
endif
" echomsg "Loading Perl subroutines"
let g:loaded_dbext_dbi_msg = 'pre test of perl version'
perl << EOVersionTest
require 5.8.0;
VIM::DoCommand('let g:loaded_dbext_dbi_msg=\'passed test of perl version\'');
EOVersionTest
if (g:loaded_dbext_dbi_msg != 'passed test of perl version')
let g:loaded_dbext_dbi_msg = 'failed test of perl version'
return
endif
let g:loaded_dbext_dbi_msg = 'creating Perl subroutines'
perl << EOCore
BEGIN {(*STDERR = *STDOUT) || die;}
use diagnostics;
use warnings;
use strict;
use Data::Dumper qw( Dumper );
use DBI;
my %connections;
my @result_headers;
my @result_set;
my @result_col_length;
my $result_max_col_width = 0;
my $max_rows = 300;
my $min_col_width = 4; # First NULL
my $test_inc = 0;
my $conn_inc = 0;
my $dbext_dbi_sql = "";
my $col_sep_vert = " ";
my $col_max_width = 0;
my $debug = 0;
my $native_err = 0;
my $inside_vim = 0;
eval {
VIM::Eval(1);
};
if ($@) {
$inside_vim = 0;
} else {
$inside_vim = 1;
}
# Sets a Vim variable to a value. Will worry about
# escaping strings when necessary.
sub db_set_vim_var
{
my $var_name = shift;
my $string = shift;
my $let = ('let '.$var_name.'="'.db_escape($string).'"');
if( $inside_vim ) {
# db_echo('db_set_vim_var:'.$let);
VIM::DoCommand($let);
# db_echo('db_set_vim_var finished');
} else {
print('let '.$var_name.'="'.$string."\"\n");
}
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_trim_white_space');
sub db_trim_white_space($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_echo');
sub db_echo
{
my $msg = shift;
# VIM::Msg('DBI:'.$msg, 'WarningMsg');
db_vim_op( "Msg", 'DBI:'.$msg );
return 0;
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_debug');
sub db_debug
{
my $msg = shift;
$debug and db_echo($msg);
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_is_debug');
# Checks if the dbext plugin enabled a debug mode.
# If so, will echo out various messages to track it's
# behaviour. This is less necessary if you read the
# comments in the db_vim_check_inside routine.
sub db_is_debug
{
return db_vim_eval('g:dbext_dbi_debug');
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_vim_check_inside');
# Allows dbext_dbi code to be run inside a straight Perl
# file. This allows you to write and debug Perl code using
# your usual tools. Once you have it correct and working outside
# of Vim, simply paste the Perl code back into dbext_dbi.vim.
# If you follow the use of the helper functions, like db_vim_eval,
# db_vim_op and db_vim_print, and so on, the code is portable.
# This is really the only way to get code working effectively
# and quickly.
sub db_vim_check_inside
{
eval {
VIM::Eval(1);
};
if ($@) {
db_debug("Not inside Vim:".$@);
$inside_vim = 0;
} else {
db_debug("Inside Vim:".$@);
$inside_vim = 1;
}
return;
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_vim_eval');
# Use Vim to evaluate a string
sub db_vim_eval
{
my $cmd = shift;
my $rc;
my $val;
if( ! $inside_vim ) {
# This sub asks Vim for certain values.
# If we are running this as a Perl program outside of Vim
# then we cannot get Vim to evaluate what we need.
# So, for some of the values that we need to tweak to test
# we can return some default values for them.
if( $cmd eq "bufnr('%')" ) {
return 1;
}
if( $cmd eq "g:dbext_default_DBI_max_rows" || $cmd eq "b:dbext_DBI_max_rows" ) {
return 10;
}
if( $cmd eq "g:dbext_default_DBI_max_column_width" || $cmd eq "b:dbext_DBI_max_column_width" ) {
return 0;
}
if( $cmd eq "g:dbext_default_DBI_column_delimiter" || $cmd eq "b:dbext_DBI_column_delimiter" ) {
return "\t";
}
}
if( defined($cmd) ) {
if( $inside_vim ) {
# return VIM::Eval($cmd);
($rc, $val) = VIM::Eval($cmd);
# db_echo("db_vim_eval:$cmd:$rc:$val");
if( $rc == 1 ) {
return $val;
} else {
return -1;
}
} else {
return ($cmd);
}
}
return "";
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_vim_op');
# Perform a Vim operation (Vim Perl supported API) against
# the instance of Vim already running.
# Available operators are:
# Count - Returns the count of lines in the buffer
# Append - Adds a new line to a Vim buffer
# call - Calls an object in Vim
# Default - Echo the string
sub db_vim_op
{
my $op = shift;
if( ! defined($op) ) {
return;
}
if( $op eq "Count" ) {
if( $inside_vim ) {
return $main::curbuf->Count();
} else {
return 1;
}
} elsif ( $op eq "Append" ) {
my $line_nbr = shift;
my $line_txt = shift;
if( ! defined($line_nbr) ) {
$line_nbr = "";
}
if( ! defined($line_txt) ) {
$line_txt = "";
}
if( $inside_vim ) {
$main::curbuf->Append($line_nbr, $line_txt);
} else {
print "$line_txt\n";
}
} elsif ( $op eq "call" ) {
my $cmd = shift;
if( ! defined($cmd) ) {
$cmd = "";
}
if( $inside_vim ) {
VIM::DoCommand($cmd);
} else {
print "Vim:DoCommand:$cmd\n";
}
} else {
# Msg
my $msg = shift;
if( ! defined($msg) ) {
$msg = "";
}
if( $inside_vim ) {
VIM::Msg('DBI:'.$msg, 'WarningMsg');
} else {
print "$msg\n";
}
}
return "";
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_vim_print');
# Prints a line into the Vim buffer
sub db_vim_print
{
my $line_nbr = shift;
my $line_txt = shift;
my $printed_lines = 0;
my $max_col_width = 2;
if( defined $result_max_col_width ) {
$max_col_width += $result_max_col_width;
}
if ( ! defined($line_nbr) ) {
db_echo('db_vim_print invalid line number');
return -1;
}
if ( ! defined($line_txt) ) {
$line_txt = "";
}
my @lines = split("\n", $line_txt);
foreach my $line (@lines) {
if ( $printed_lines > 0 ) {
# Multiple lines will only be within the string if the
# user is printing in a vertical orientation.
# Therefore if the printed_lines is > 1 we know
# we have split the column data and we need to prepend
# blanks to line up the text with the data above.
$line = (' ' x $max_col_width).$line;
}
# $main::curbuf->Append($line_nbr, $line);
db_vim_op("Append", $line_nbr, $line);
$line_nbr++;
$printed_lines++;
}
return $printed_lines;
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_get_defaults');
sub db_get_defaults
{
$max_rows = db_vim_eval('g:dbext_default_DBI_max_rows');
$col_sep_vert = db_vim_eval('g:dbext_default_DBI_column_delimiter');
$col_max_width = db_vim_eval('g:dbext_default_DBI_max_column_width');
db_debug("db_get_defaults:max rows[$max_rows] col separator[$col_sep_vert] max col width[$col_max_width]");
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_escape');
sub db_escape
{
my $escaped = shift;
if( defined($escaped) ) {
$escaped =~ s/\\/\\\\/g;
$escaped =~ s/"/\\"/g;
$escaped =~ s/\n/\\n/g;
}
return $escaped;
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_remove_newlines');
sub db_remove_newlines
{
my $escaped = shift;
$escaped =~ s/\n/ /g;
return $escaped;
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_strdisplaywidth');
sub db_strdisplaywidth
{
my $str = shift;
return db_vim_eval('strdisplaywidth("' . db_escape($str) . '")');
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_get_available_drivers');
sub db_get_available_drivers
{
my @ary = DBI->available_drivers;
db_echo('db_available_drivers:'.Dumper(@ary));
return 0;
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_list_connections');
# Called by the dbext, DBListConnections command.
# In the dbext results buffer, it will list all the buffers
# with existing database connections.
sub db_list_connections
{
db_debug('db_list_connections:'.Dumper(%connections));
my @row;
my @table;
my @col_length;
my $max_col_width = 0;
my $i = 0;
my @headers = [ ("Buffer", "Driver", "AutoCommit", "CommitOnDisconnect", "Connection Parameters", "LongReadLen", "FileName") ];
db_set_vim_var("g:dbext_dbi_msg", '');
foreach my $row2 ( @headers ) {
db_debug('db_list_connections:R'.Dumper($row2));
foreach my $col2 ( @{$row2} ) {
my $temp_length = length((defined($col2)?$col2:""));
if ( !defined($col_length[$i]) ) {
$col_length[$i] = 0;
}
$col_length[$i] = ( $temp_length > $col_length[$i] ? $temp_length : $col_length[$i] );
db_debug("db_list_connections:i:$i:L:".$col_length[$i]);
$max_col_width = ( $temp_length > $max_col_width ? $temp_length : $max_col_width );
$i++;
}
}
if ( keys(%connections) > 0 )
{
foreach my $bufnr ( keys %connections ) {
@row = ($bufnr
, $connections{$bufnr}->{'driver'}
, $connections{$bufnr}->{'conn'}->{'AutoCommit'}
, $connections{$bufnr}->{'CommitOnDisconnect'}
, $connections{$bufnr}->{'params'}
, $connections{$bufnr}->{'conn'}->{'LongReadLen'}
, db_vim_eval('fnamemodify( bufname( bufnr('.$bufnr.')), ":p:t")' )
);
push @table, [ @row ];
$i = 0;
foreach my $col ( @row ) {
my $temp_length = length((defined($col)?$col:""));
$col_length[$i] = ( $temp_length > $col_length[$i] ? $temp_length : $col_length[$i] );
$i++;
}
}
}
@result_headers = @headers;
@result_set = @table;
@result_col_length = @col_length;
$result_max_col_width = $max_col_width;
db_debug('db_list_connections:pre-formatting'.Dumper(@result_set));
db_format_array();
if ( keys(%connections) == 0 )
{
push @result_set, [ ("There are no active DBI connections", "", "", "", "", "") ];
}
db_debug('db_list_connections:final:'.Dumper(@result_set));
# TODO
# This should define an array so db_print_results can be used
db_set_vim_var("g:dbext_dbi_result", 'DBI:');
db_set_vim_var("g:dbext_dbi_msg", '');
return 0;
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_get_info');
sub db_get_info
{
my $option = shift;
my $conn_local;
my $driver;
if ( ! db_is_connected() ) {
db_echo("db_get_info:You must connect first");
return -1;
}
($conn_local, $driver) = db_get_connection();
my $result = "";
if ( defined($option) ) {
$result = $conn_local->get_info($option);
} else {
$result = "DBMS Name[".$conn_local->get_info(17).
"] Version[".$conn_local->get_info(18)."]";
}
db_debug('db_get_info:'.$result);
db_set_vim_var("g:dbext_dbi_result", $result);
return 0;
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_commit');
sub db_commit
{
my $conn_local;
my $driver;
my $bufnr = shift;
if( ! defined($bufnr) ) {
db_debug('db_commit:$bufnr undefined');
$bufnr = db_vim_eval("bufnr('%')");
db_debug("db_commit:looking up $bufnr");
}
db_debug("Committing connection");
if ( ! db_is_connected($bufnr) ) {
db_set_vim_var("g:dbext_dbi_result", -1);
db_set_vim_var("g:dbext_dbi_msg", 'You are not connected to a database');
return -1;
}
($conn_local, $driver) = db_get_connection($bufnr);
my $rc = $conn_local->commit;
db_set_vim_var("g:dbext_dbi_result", $rc);
return $rc;
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_rollback');
sub db_rollback
{
my $conn_local;
my $driver;
my $bufnr = shift;
if( ! defined($bufnr) ) {
db_debug('db_rollback:$bufnr undefined');
$bufnr = db_vim_eval("bufnr('%')");
db_debug("db_rollback:looking up $bufnr");
}
db_debug("Rolling back connection");
if ( ! db_is_connected($bufnr) ) {
db_set_vim_var("g:dbext_dbi_result", -1);
db_set_vim_var("g:dbext_dbi_msg", 'You are not connected to a database');
return -1;
}
($conn_local, $driver) = db_get_connection($bufnr);
my $rc = $conn_local->rollback;
db_set_vim_var("g:dbext_dbi_result", $rc);
return $rc;
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_is_connected');
# Returns a 1 if this buffer already has an existing connection
sub db_is_connected
{
my $bufnr = shift;
my $is_connected = 0;
my $conn_local;
$test_inc++;
db_debug('db_is_connected:test_inc:'.$test_inc);
if( ! defined($bufnr) ) {
db_debug('db_is_connected:$bufnr undefined');
$bufnr = db_vim_eval("bufnr('%')");
db_debug("db_is_connected:looking up $bufnr");
}
if( %connections ) {
if( ! exists($connections{$bufnr}) ) {
db_debug('db_is_connected:hash does not exist:'.$bufnr);
} else {
db_debug('db_is_connected:hash exists:'.$bufnr);
$conn_local = $connections{$bufnr}->{'conn'};
}
}
if( defined($conn_local) ) {
db_debug('db_is_connected:conn exists checking validity');
$is_connected = 1;
#if( $conn_local->{Active} ) {
# db_debug('db_is_connected:existing conn seems active');
# $is_connected = 1;
#} else {
# db_debug('db_is_connected:existing conn not valid, disconnected');
#}
} else {
db_debug('db_is_connected:NO existing conn');
}
db_debug("db_is_connected:returning:$is_connected");
db_set_vim_var("g:dbext_dbi_result", $is_connected);
return $is_connected;
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_get_connection');
# Returns the connection handle for this buffer number
sub db_get_connection
{
my $bufnr = shift;
my $driver = '';
my $conn_local;
if( ! defined($bufnr) ) {
db_debug('db_get_connected:$bufnr undefined');
$bufnr = db_vim_eval("bufnr('%')");
db_debug("db_get_connection:looking up $bufnr");
}
if ( ! db_is_connected($bufnr) ) {
db_debug('db_get_connection:connection not found:'.$bufnr);
return undef;
}
# Each time a buffer is requested, look up any specific
# settings for this buffer. Since this is single threaded
# this approach is fine and allows for the settings to be
# changed at anytime.
# $max_rows = db_vim_eval('b:dbext_DBI_max_rows');
# $col_sep_vert = db_vim_eval('b:dbext_DBI_column_delimiter');
# $col_max_width = db_vim_eval('b:dbext_DBI_max_column_width');
$max_rows = db_vim_eval("getbufvar(".$bufnr.", 'dbext_DBI_max_rows', g:dbext_default_DBI_max_rows)");
$col_sep_vert = db_vim_eval("getbufvar(".$bufnr.", 'dbext_DBI_column_delimiter', g:dbext_default_DBI_column_delimiter)");
$col_max_width = db_vim_eval("getbufvar(".$bufnr.", 'dbext_DBI_max_column_width', g:dbext_default_DBI_max_column_width)");
db_debug("db_get_connection:max rows[$max_rows] col separator[$col_sep_vert] max col width[$col_max_width]");
db_debug('db_get_connection:returning:'.$bufnr);
db_debug("db_get_connection:".Dumper($connections{$bufnr}));
$conn_local = $connections{$bufnr}->{'conn'};
$driver = $connections{$bufnr}->{'driver'};
$connections{$bufnr}->{LastRequest} = localtime;
return ($conn_local, $driver);
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_check_error');
# Reports an error if one detected
sub db_check_error
{
my $err = 0;
my $level = '';
my $msg = '';
my $state = '';
my $bufnr = '';
my $driver = shift;
my $conn_local;
if ( defined($DBI::err) && defined($DBI::errstr) && defined($DBI::state) ) {
if( ! defined($driver) ) {
($conn_local, $driver) = db_get_connection();
}
db_debug("db_check_error:$bufnr:$driver");
$err = $DBI::err;
$msg = $DBI::errstr;
$state = $DBI::state;
# db_echo("db_check_error: initial values:$level:$err:$state:$msg");
if( $driver eq "ODBC" ) {
if ( $err eq "" && ! $msg eq "" ) {
# Informational message
$level = "I";
} elsif ( $err eq "0" && ! $msg eq "" ) {
# Warning message
$level = "W";
} elsif ( $err eq "1" && ! $msg eq "" ) {
# Error message
$level = "E";
}
if( defined($native_err) ) {
$err = $native_err;
}
} else {
if ( ($err eq "" || $err eq "0") && ! $msg eq "" ) {
# Informational message
$level = "I";
} elsif ( $err gt "0" && ! $msg eq "" ) {
# Warning message
$level = "W";
} elsif ( $err lt "0" && ! $msg eq "" ) {
# Error message
$level = "E";
}
}
}
db_debug("db_check_error: returning:$level:$err:$state:$msg");
return ($level, $err, $msg, $state);
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_odbc_err_handler');
sub db_odbc_err_handler
{
my ($state, $msg, $native) = @_;
$native_err = $native;
# db_echo("db_odbc_err_handler: native error:$native_err");
# Do not ignore the error
return 1;
}
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_connect');
# Creates a new connection to the database and sets any
# appropriate options. Tracks the connection handles made
# in here: $connections
sub db_connect
{
my $driver = shift;
my $conn_parms = shift;
my $uid = shift;
my $pwd = shift;
my $bufnr = db_vim_eval("bufnr('%')");
my $conn_local;
# $debug = db_is_debug();
# db_debug("Connect: driver:$driver parms:$conn_parms U:$uid P:$pwd");
db_debug('db_connect:checking for existing connection');
if ( db_is_connected() ) {
db_debug('db_connect:Already connected');
return 0;
}
if ( ! defined($driver) ) {
db_debug("db_connect:Invalid driver:$driver");
db_set_vim_var("g:dbext_dbi_msg", 'E. Invalid driver:'.$driver);
db_set_vim_var("g:dbext_dbi_result", -1);
return -1;
}
if ( ! defined($uid) ) {
db_debug("db_connect:Invalid userid:$uid");
db_set_vim_var("g:dbext_dbi_msg", 'E. Invalid userid:'.$uid);
db_set_vim_var("g:dbext_dbi_result", -1);
return -1;
}
if ( ! defined($pwd) ) {
db_debug("db_connect:Invalid password:$pwd");
db_set_vim_var("g:dbext_dbi_msg", 'E. Invalid password:'.$pwd);
db_set_vim_var("g:dbext_dbi_result", -1);
return -1;
}
my $DATA_SOURCE = "DBI:$driver:$conn_parms";
db_debug('db_connect:connecting to:'.$DATA_SOURCE);
# Use global connection object
eval {
# LongReadLen sets the maximum size of a BLOB that
# can be retrieved from the database.
# This value can be overriden from your connection string
# or by using:
# DBSetOption LongReadLen=4096
# DBSetOption driver_parms=LongReadLen=4096
#
# LongTruncOk indicates to allow data truncation,
# and do not report an error.
$conn_local = DBI->connect( $DATA_SOURCE, $uid, $pwd,
{ AutoCommit => 1,
LongReadLen => 1000,
LongTruncOk => 1,
RaiseError => 0,
PrintError => 0,
PrintWarn => 0 }
);
# or die $DBI::errstr;
};
if ($@) {
db_debug("db_connect:Cannot connect to data source:".$DATA_SOURCE." using:".$uid." E:".$@);
db_set_vim_var('g:dbext_dbi_msg', "Cannot connect to data source:".$DATA_SOURCE." using:".$uid." E:".$@);
db_set_vim_var('g:dbext_dbi_result', -1);
return -1;
}
my( $level, $err, $msg, $state ) = db_check_error($driver);
if ( ! $msg eq "" ) {
$msg = "$level. DBC:".(($level ne "I")?"SQLCode:$err:":"").$msg.(($state ne "")?":$state":"").":\nConnection details:$DATA_SOURCE";
db_set_vim_var('g:dbext_dbi_msg', $msg);
if ( $level eq "E" ) {
db_set_vim_var('g:dbext_dbi_result', -1);
db_debug("db_connect:Connect failed:[$msg] exiting");
return -1;
}
}
if ( $driver eq "ODBC" ) {
db_debug("db_connect: Enabling odbc_err_handler");
$conn_local->{odbc_err_handler} = \&db_odbc_err_handler;
}
$connections{$bufnr} = {'conn' => $conn_local
,'driver' => $driver
,'uid' => $uid
,'params' => $conn_parms
,'AutoCommit' => 1
,'CommitOnDisconnect' => 1
,'LastRequest' => localtime
};
# db_debug('db_connected:checking if successful');
# if ( ! db_is_connected() ) {
# db_set_vim_var('g:dbext_dbi_msg', "Cannot connect to data source:$DATA_SOURCE using:$uid SQLCode:".$DBI::err.":".db_escape($DBI::errstr));
# db_set_vim_var("g:dbext_dbi_result", -1);
# return -1;
# }
my $trace_level = db_vim_eval("g:dbext_dbi_trace_level");
if ( ! $trace_level eq "0" ) {
my $vim_dir = db_vim_eval("expand('".'$VIM'."')");
$conn_local->trace($trace_level, $vim_dir.'\dbi_trace.txt');
}