Skip to content

Commit 8458ff0

Browse files
committed
Adressed @dfishburn code review comments
1 parent d55a540 commit 8458ff0

File tree

2 files changed

+140
-78
lines changed

2 files changed

+140
-78
lines changed

autoload/dbext_dbi.vim

Lines changed: 63 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -543,12 +543,12 @@ sub db_vim_print
543543
$line_txt = "";
544544
}
545545

546-
# ans, 2022-04-08 - allow to print from dbms_output empty lines
547546
my @lines = ();
548547
if (!defined ($line_txt) || length($line_txt) == 0) {
549-
@lines = ("");
548+
# from dbms_output it is possible to get empty lines to print
549+
@lines = ("");
550550
} else {
551-
@lines = split("\n", $line_txt);
551+
@lines = split("\n", $line_txt);
552552
}
553553

554554
foreach my $line (@lines) {
@@ -973,9 +973,8 @@ sub db_connect
973973
LongTruncOk => 1,
974974
RaiseError => 0,
975975
PrintError => 0,
976-
## hello from ans
977-
ora_charset => 'AL32UTF8',
978-
ora_ncharset => 'AL32UTF8',
976+
ora_charset => 'AL32UTF8',
977+
ora_ncharset => 'AL32UTF8',
979978
PrintWarn => 0 }
980979
);
981980
# or die $DBI::errstr;
@@ -1288,10 +1287,9 @@ sub db_query
12881287
return 0;
12891288
}
12901289

1291-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1292-
# 2021-09-19 ans: fetch all rows of dbms_output
1293-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1294-
sub fetch_dbms_output {
1290+
# Fetches all available rows of dbms_output from server
1291+
sub fetch_dbms_output
1292+
{
12951293
my ($conn_local) = (@_);
12961294

12971295
my $max_line_size = 32768; # anything we put here can be too short...
@@ -1306,7 +1304,7 @@ sub fetch_dbms_output {
13061304
$get_lines_st->bind_param_inout(':n', \$num_lines, 50, {ora_type => 1});
13071305

13081306
$get_lines_st->execute();
1309-
db_debug("executed get_lines() ok. num_lines = $num_lines");
1307+
db_debug("executed get_lines() ok. num_lines fetched = $num_lines");
13101308

13111309
my @text_2 = ();
13121310
if ($num_lines) {
@@ -1323,7 +1321,6 @@ sub fetch_dbms_output {
13231321
}
13241322
}
13251323

1326-
#push @text_2, "WARNING: please not foget to close the statement, debug version only!!!";
13271324
$get_lines_st = undef;
13281325

13291326
## max_line_size exceeded {
@@ -1698,48 +1695,34 @@ sub db_print_results
16981695
db_debug("db_print_results: before last_line(...rows...), last_line=$last_line");
16991696
db_vim_print($last_line, "(".scalar(@result_set)." rows)");
17001697
if (@dbms_output) {
1701-
$last_line++;
1702-
# ans: we need this "join and afterwards split" trick because otherwise
1703-
# individual rows from @dbms_output are coming encoded differently, see as well
1704-
# exploit Perl_Oracle_DBD_dbms_output/perl_dbd_oracle_dbms_output__inside_vim__simple.vim
1705-
# call :so % | call Check_my_perl__load()
1706-
##my @dbms_output_remade = split ("\n", join("\n", @dbms_output));
1707-
# ans: 2021-09-19 now we use decode("utf8"...) and other method of
1708-
# fetching of array, there are chances we dont need this ugly workaround
1709-
# any more
1710-
db_debug("db_print_results: \@dbms_output size: ".scalar(@dbms_output));
1711-
my @dbms_output_remade = @dbms_output;
1712-
for my $dol0 (@dbms_output_remade) {
1713-
#### THIS IS MAGIC !!!!!!!
1714-
#### THIS_IS_MAGIC !!!!!!!
1715-
#### THIS IS MAGIC !!!!!!!
1716-
my $dol = decode('utf8', ($dol0 // ''));
1717-
db_debug("db_print_results: printed dbms_output[i] (raw non-splitted) : ".$dol);
1718-
#chomp($dol);
1719-
1720-
# we try to overtake the line number from the "printer" method
1721-
$last_line += db_vim_print($last_line, $dol);
1722-
#$last_line++;
1723-
1724-
#my @lines = split("\n", $dol);
1725-
#for my $diag_ln (@lines) {
1726-
# db_debug("db_print_results: printed dbms_output[i] (splitted redundantly): ".$diag_ln);
1727-
#}
1728-
}
1729-
db_debug("db_print_results: printed dbms_output: " . join ("\n", map { $_ // '' } @dbms_output))
1730-
if ($debug);
1731-
@dbms_output = ();
1732-
$last_line += db_vim_print($last_line, "printed at: " . scalar localtime);
1698+
$last_line++;
1699+
db_debug("db_print_results: \@dbms_output size: ".scalar(@dbms_output));
1700+
for my $dol0 (@dbms_output) {
1701+
# needed due to (DBI?) encoding problems when receiving from oracle with plsql
1702+
# in/out table, see https://github.com/ant0sha/dbext.vim/commit/b83befafd0fdc45f683aca1fa89b52ab959e8277
1703+
my $dol = decode('utf8', ($dol0 // ''));
1704+
db_debug("db_print_results: printed dbms_output[i] (raw non-splitted) : ".$dol);
1705+
#chomp($dol);
1706+
1707+
# we try to overtake the line number from the "printer" method
1708+
$last_line += db_vim_print($last_line, $dol);
1709+
#$last_line++;
1710+
}
1711+
if ($debug) {
1712+
db_debug("db_print_results: printed dbms_output: " . join ("\n", map { $_ // '' } @dbms_output))
1713+
}
1714+
@dbms_output = ();
1715+
$last_line += db_vim_print($last_line, "printed at: " . scalar localtime);
17331716
} else {
1734-
$last_line++;
1735-
my ($connection_ignored, $driver_ignored, $z_user_data) = db_get_connection();
1736-
if ($z_user_data->{z_srv_out_enabled}) {
1737-
db_vim_print($last_line, "dbms_output is empty.");
1738-
db_debug("db_print_results: dbms_output is empty.");
1739-
} else {
1740-
db_vim_print($last_line, "ACHTUNG: dbms_output is OFF.");
1741-
db_debug("db_print_results: dbms_output is OFF.");
1742-
}
1717+
$last_line++;
1718+
my ($connection_ignored, $driver_ignored, $z_user_data) = db_get_connection();
1719+
if ($z_user_data->{z_srv_out_enabled}) {
1720+
db_vim_print($last_line, "dbms_output is empty.");
1721+
db_debug("db_print_results: dbms_output is empty.");
1722+
} else {
1723+
db_vim_print($last_line, "ACHTUNG: dbms_output is OFF.");
1724+
db_debug("db_print_results: dbms_output is OFF.");
1725+
}
17431726
}
17441727
db_debug("db_print_results: returning 0");
17451728
return 0;
@@ -1947,37 +1930,39 @@ sub db_results_list
19471930
sub db_enable_srv_out
19481931
{
19491932

1950-
# see ~/Documents/0.mop/Perl_Oracle_DBD_dbms_output/dbms_output_test_case_1.vim
1951-
# for automated test case:
1952-
# vim -c 'source ~/Documents/0.mop/Perl_Oracle_DBD_dbms_output/dbms_output_test_case_1.vim|call Test_Case_1()'
1933+
# TODO: refactor/publish related automated test case for dbms_output, which
1934+
# can be called like this:
1935+
#
1936+
# vim -c 'source \
1937+
# Perl_Oracle_DBD_dbms_output/dbms_output_test_case_1.vim|call \
1938+
# Test_Case_1()'
19531939

1954-
my ($conn_local, $driver, $z_user_data) = db_get_connection();
1955-
if ($z_user_data->{z_srv_out_enabled}) {
1956-
db_debug("skip <enable> of already enabled server output");
1957-
}
1958-
else {
1959-
$conn_local->func( 1000000, 'dbms_output_enable' );
1960-
$z_user_data->{z_srv_out_enabled} = 1;
1961-
}
1940+
my ($conn_local, $driver, $z_user_data) = db_get_connection();
1941+
if ($z_user_data->{z_srv_out_enabled}) {
1942+
db_debug("skip <enable> of already enabled server output");
1943+
}
1944+
else {
1945+
$conn_local->func( 1000000, 'dbms_output_enable' );
1946+
$z_user_data->{z_srv_out_enabled} = 1;
1947+
}
19621948

1963-
#db_set_vim_var('g:dbext_dbi_msg', 'A request_type must be specified');
1964-
#db_set_vim_var('g:dbext_dbi_result', -1);
1949+
#db_set_vim_var('g:dbext_dbi_msg', 'A request_type must be specified');
1950+
#db_set_vim_var('g:dbext_dbi_result', -1);
19651951
}
19661952

19671953
sub db_disable_srv_out
19681954
{
1969-
my ($conn_local, $driver, $z_user_data) = db_get_connection();
1970-
if ($z_user_data->{z_srv_out_enabled}) {
1971-
my $stmt = $conn_local->prepare_cached( 'begin dbms_output.disable; end;' );
1972-
$stmt->execute();
1973-
$z_user_data->{z_srv_out_enabled} = 0;
1974-
}
1975-
else {
1976-
db_debug("skip <disable> of already disabled server output");
1977-
}
1955+
my ($conn_local, $driver, $z_user_data) = db_get_connection();
1956+
if ($z_user_data->{z_srv_out_enabled}) {
1957+
my $stmt = $conn_local->prepare_cached( 'begin dbms_output.disable; end;' );
1958+
$stmt->execute();
1959+
$z_user_data->{z_srv_out_enabled} = 0;
1960+
} else {
1961+
db_debug("skip <disable> of already disabled server output");
1962+
}
19781963

1979-
#db_set_vim_var('g:dbext_dbi_msg', 'A request_type must be specified');
1980-
#db_set_vim_var('g:dbext_dbi_result', -1);
1964+
#db_set_vim_var('g:dbext_dbi_msg', 'A request_type must be specified');
1965+
#db_set_vim_var('g:dbext_dbi_result', -1);
19811966
}
19821967

19831968
db_set_vim_var('g:loaded_dbext_dbi_msg', 'db_ora_extract_ddl');
@@ -2108,7 +2093,7 @@ sub db_ora_extract_ddl
21082093
end loop;
21092094
end;
21102095

2111-
dbms_output.put_line('gesmtlaenge='||length(ddl)||', iNL='||iNL);
2096+
--dbms_output.put_line('gesmtlaenge='||length(ddl)||', iNL='||iNL);
21122097
exception when others then
21132098
l('exception happened');
21142099
declare

doc/dbext.txt

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,37 @@ David Fishburn
118118
==============================================================================
119119
2. What's New *dbext-new*
120120

121+
Version ??.?? (<Mon> ??, YYYY)
122+
123+
New Features
124+
------------
125+
- :DBXtractDdl - new command (oracle+DBI specific), which uses dbms_output to
126+
extract (and display) DDL of requested DB object.
127+
- :DBEnableSrvOut - new command (oracle+DBI specific), which enables
128+
dbms_output to be collected by oracle for our session and as well enables
129+
fetching of collected output so far after every PL/SQL or SQL query. NOTE:
130+
dbms_output is enabled by default.
131+
- :DBDisableSrvOut - new command (oracle+DBI specific), which disables
132+
dbms_output to be collected by oracle for our session and as well disables
133+
fetching
134+
- empty lines in output coming from the DB are now forced to stay - otherwise
135+
output of DDL (via dbms_output) is not useable
136+
137+
Bug Fixes
138+
---------
139+
140+
- dbext_dbi_debug - changed spelling to use 'g:' prefix
141+
- dbext_dbi.vim - several enhancements for the case of non-ASCII output
142+
- explicitly asking perl to use UTF8 in both source code and input/output
143+
encoding
144+
- get rid of $val =~tr/\x80-\xFF/ /; which replaced all non-ASCII things
145+
with blanks
146+
- dbext_dbi.vim - fixed bug in db_print_results() if empty line happened in
147+
the non-expected place
148+
- FuzzyFinder badly interact with dbext in that it creates global variable
149+
FuzzyFinderMode. Filter out this variable during check for upgrade of old
150+
profile.
151+
121152
Version 26.00 (Nov 17, 2017)
122153

123154
New Features
@@ -4317,6 +4348,52 @@ To specify the type of database you connect to most often, you can place the
43174348
read c:\temp\my.sql;
43184349
read /temp/my.sql;
43194350
<
4351+
**DDL extraction - oracle specific**
4352+
4353+
Given you have a Table like this: >
4354+
create table tmp_for_dbext (
4355+
c1 number,
4356+
s1 varchar2(20),
4357+
d1 date
4358+
);
4359+
<
4360+
You can use >
4361+
:DBXtractDdl tmp_for_dbext
4362+
<
4363+
to see the DDL of that table: >
4364+
CREATE TABLE "CAQCSM"."TMP_FOR_DBEXT"
4365+
( "C1" NUMBER,
4366+
"S1" VARCHAR2(20 CHAR),
4367+
"D1" DATE
4368+
) SEGMENT CREATION DEFERRED
4369+
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
4370+
NOCOMPRESS LOGGING
4371+
TABLESPACE "SOME_TABLESPACE" NO INMEMORY
4372+
<
4373+
(the output is coming as-is from DBMS_METADATA.GET_DDL() package and
4374+
displayed to the user with dbms_output oracle feature. Only for tables
4375+
additional query is used to fetch index data from user_indexes table). This
4376+
works as well for triggers/packages/types/views.
4377+
4378+
**Working with DBMS_OUTPUT - oracle specific**
4379+
4380+
After issue of: >
4381+
:DBEnableSrvOut
4382+
<
4383+
4384+
dbext will fetch collected dbms_output on server, e.g. if you execute for
4385+
example following PLSQL block: >
4386+
begin for i in 1..3 loop dbms_output.put_line('iteration #'||i); end loop; end;;
4387+
<
4388+
you will see as expected: >
4389+
iteration #1
4390+
iteration #2
4391+
iteration #3
4392+
<
4393+
After issue of: >
4394+
:DBDisableSrvOut
4395+
<
4396+
no output at all will come from PLSQL block above.
43204397

43214398
Summary
43224399
-------

0 commit comments

Comments
 (0)