blib/lib/DBI/Shell.pm

filter: Statementstmt 53.6% 430 / 802 Branchbran 31.1% 129 / 414 Conditioncond 21.8% 28 / 128 MC/DCmcdc 4.3% 3 / 69 Subroutinesub 74.7% 68 / 91 Podpod 33.3% 1 / 3 totaltotal 43.7% 659 / 1507 CC 277 SCAR 55.2
CC
277
Cov
44%
do_describe
1722.0
run
420.0
do_edit
306.0
CRAP 250.4
« blib/lib/DBI/Shell/SQLMinus.pm ↑ Summary blib/lib/DBI/Shell/Completion.pm »
1 package DBI::Shell;
2 # vim:ts=4:sw=4:ai:aw:nowrapscan
3  
4 =head1 NAME
5  
6 DBI::Shell - Interactive command shell for the DBI
7  
8 =head1 SYNOPSIS
9  
10 perl -MDBI::Shell -e shell [<DBI data source> [<user> [<password>]]]
11  
12 or
13  
14 dbish [<DBI data source> [<user> [<password>]]]
15 dbish --debug [<DBI data source> [<user> [<password>]]]
16 dbish --batch [<DBI data source> [<user> [<password>]]] < batch file
17  
18 =head1 DESCRIPTION
19  
20 The DBI::Shell module (and dbish command, if installed) provide a
21 simple but effective command line interface for the Perl L<DBI> module.
22  
23 =cut
24  
25 ###
26 ### See TO DO section in the docs at the end.
27 ###
28  
29  
30 7 ▶BEGIN { require 5.004 }
Subroutine BEGIN: called CC=1 SCAR=0.0
31 7 ▶BEGIN { $^W = 1 }
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 7 ▶use vars qw(@ISA @EXPORT $VERSION $SHELL);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 7 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 7 @ISA = qw(Exporter DBI::Shell::Std);
39 7 @EXPORT = qw(shell);
40  
41 7 our $VERSION = '11.98'; # VERSION
42  
43 5 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
44 5 my $class = shift;
45 5 ▶ my @args = @_ ? @_ : @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 5 0 5
46 #my $sh = bless {}, $class;
47 5 my $sh = $class->SUPER::new(@args);
48 # Load configuration files, system and user. The user configuration may
49 # over ride the system configuration.
50 5 my $myconfig = $sh->configuration;
51 # Save the configuration file for this instance.
52 5 $sh->{myconfig} = $myconfig;
53 # Pre-init plugins.
54 5 $sh->load_plugins($myconfig->{'plug-ins'}->{'pre-init'});
55 # Post-init plugins.
56 #$sh->SUPER::init(@args);
57 5 $sh->load_plugins($myconfig->{'plug-ins'}->{'post-init'});
58  
59 # do_format is already run in DBI::Shell::Base::new, but that is
60 # before the user config is loaded so we need to run it again if
61 # the user overrides the format.
62 5 $sh->do_format($sh->{format});
63 5 return $sh;
64 }
65  
66 0 ▶sub shell {
Subroutine shell: not called CC=2 SCAR=17.9
Pod undocumented
67 0 ▶ my @args = @_ ? @_ : @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
68 0 $SHELL = DBI::Shell::Std->new(@args);
69 0 $SHELL->load_plugins;
70 0 $SHELL->run;
71 }
72  
73 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
74 0 my $sh = shift;
75 0 die "Unrecognised options: @{$sh->{unhandled_options}}\n"
76 0 ▶ if @{$sh->{unhandled_options}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$sh->{"unhandled_options"};} 0 0 0
77  
78 # Use valid "dbi:driver:..." to connect with source.
79 0 $sh->do_connect( $sh->{data_source} );
80  
81 #
82 # Main loop
83 #
84 0 $sh->{abbrev} = undef;
85 0 $sh->{abbrev} = Text::Abbrev::abbrev(keys %{$sh->{commands}});
86 # unless $sh->{batch};
87 0 $sh->{current_buffer} = '';
88 0 $sh->SUPER::run;
89  
90 }
91  
92  
93 # -------------------------------------------------------------
94 package DBI::Shell::Std;
95  
96 7 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
97 7 @ISA = qw(DBI::Shell::Base);
98  
99 # XXX this package might be used to override commands etc.
100 9 ▶sub do_connect {
Subroutine do_connect: called CC=3 SCAR=11.0
101 9 my $sh = shift;
102 $sh->load_plugins($sh->{myconfig}->{'plug-ins'}->{'pre-connect'})
103 9 ▶ if exists $sh->{myconfig}->{'plug-ins'}->{'pre-connect'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sh->{"myconfig"}{"plug-ins"}{"pre-connect"} 4 5 9
104 9 $sh->SUPER::do_connect(@_);
105 $sh->load_plugins($sh->{myconfig}->{'plug-ins'}->{'post-connect'})
106 9 ▶ if exists $sh->{myconfig}->{'plug-ins'}->{'post-connect'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sh->{"myconfig"}{"plug-ins"}{"post-connect"} 4 5 9
107 9 return;
108 }
109  
110 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
111 0 my $sh = shift;
112 0 return;
113 }
114  
115  
116 # -------------------------------------------------------------
117 package DBI::Shell::Base;
118  
119 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
120 7 ▶use Text::Abbrev ();
Subroutine BEGIN: called CC=1 SCAR=0.0
121 7 ▶use Term::ReadLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 7 ▶use Getopt::Long 2.17; # upgrade from CPAN if needed: http://www.perl.com/CPAN
Subroutine BEGIN: called CC=1 SCAR=0.0
123 7 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 7 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
125 7 ▶use File::HomeDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
126  
127 7 ▶use DBI 1.00 qw(:sql_types :utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
128 7 ▶use DBI::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
129  
130 7 ▶use DBI::Shell::FindSqlFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
131 7 ▶use IO::Interactive qw/ is_interactive /;
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 7 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
134 7 @ISA = qw(DBI::Shell::FindSqlFile);
135  
136 7 ▶use constant ADD_RH => 1; # Add the results, to rhistory.
Subroutine BEGIN: called CC=1 SCAR=0.0
137 7 ▶use constant NO_RH => 0; # Do not add results, to rhistory.
Subroutine BEGIN: called CC=1 SCAR=0.0
138  
139 # History saving is also provided by DBI::Shell::Completion.
140 7 ▶use constant HISTORY_FILE => '.dbish-builtin-history';
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 7 my $haveTermReadKey;
143 my $term;
144  
145  
146 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
147 0 warn <<USAGE;
148 Usage: perl -MDBI::Shell -e shell [<DBI data source> [<user> [<password>]]]
149 USAGE
150 }
151  
152 168 ▶sub log {
Subroutine log: called CC=2 SCAR=7.2
153 168 my $sh = shift;
154 168 ▶ return ($sh->{batch}) ? warn @_,"\n" : $sh->print_buffer_nop(@_,"\n"); # XXX maybe
Branch: 1 branch
BranchTrueFalseTotal
$sh->{'batch'} ? : 168 0 168
155 }
156  
157 0 ▶sub alert { # XXX not quite sure how alert and err relate
Subroutine alert: not called CC=1 SCAR=6.9
158 # for msgs that would pop-up an alert dialog if this was a Tk app
159 0 my $sh = shift;
160 0 return warn @_,"\n";
161 }
162  
163 0 ▶sub err { # XXX not quite sure how alert and err relate
Subroutine err: not called CC=2 SCAR=17.9
164 0 my ($sh, $msg, $die) = @_;
165 0 $msg = "DBI::Shell: $msg\n";
166 0 ▶ die $msg if $die;
Branch: 1 branch
BranchTrueFalseTotal
if $die 0 0 0
167 0 return $sh->alert($msg);
168 }
169  
170  
171  
172 222 ▶sub add_option {
Subroutine add_option: called CC=2 SCAR=6.9
173 222 my ($sh, $opt, $default) = @_;
174 222 (my $opt_name = $opt) =~ s/[|=].*//;
175 croak "Can't add_option '$opt_name', already defined"
176 222 ▶ if exists $sh->{$opt_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sh->{$opt_name} 20 202 222
177 202 $sh->{options}->{$opt_name} = $opt;
178 202 $sh->{$opt_name} = $default;
179 }
180  
181 22 ▶sub load_plugins {
Subroutine load_plugins: called CC=17 SCAR=29.7
182 22 my ($sh, @ppi) = @_;
183 # Output must not appear while loading plugins:
184 # It might happen, that batch mode is entered
185 # later!
186 22 my @pi;
187 22 ▶ return unless(@ppi);
Branch: 1 branch
BranchTrueFalseTotal
unless @ppi 0 22 22
188 22 foreach my $n (0 .. $#ppi) {
189 22 ▶ next unless ($ppi[$n]);
Branch: 1 branch
BranchTrueFalseTotal
unless $ppi[$n] 10 12 22
190 12 my $pi = $ppi[$n];
191  
192 12 ▶ if ( ref $pi eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $pi eq 'HASH') { } 4 8 12
elsif (ref $pi eq 'ARRAY') { } 8 0 8
193 # As we descend down the hash reference,
194 # we're looking for an array of modules to source in.
195 4 my @mpi = keys %$pi;
196 4 foreach my $opt (@mpi) {
197 #print "Working with $opt\n";
198 12 ▶ if ($opt =~ /^option/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opt =~ /^option/i) { } 4 8 12
elsif ($opt =~ /^database/i) { } 4 4 8
elsif ($opt =~ /^non-database/i) { } 4 0 4
199 # Call the option handling.
200 4 $sh->install_options( @{$pi->{$opt}} );
201 4 next;
202 } elsif ( $opt =~ /^database/i ) {
203 # Handle plugs for a named # type of database.
204 4 ▶ next unless $sh->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $sh->{"dbh"} 0 4 4
205 # Determine what type of database connection.
206 4 my $db = $sh->{dbh}->{Driver}->{Name};
207 $sh->load_plugins( $pi->{$opt}->{$db} )
208 4 ▶ if (exists $pi->{$opt}->{$db});
Branch: 1 branch
BranchTrueFalseTotal
if exists $pi->{$opt}{$db} 0 4 4
209 4 next;
210 } elsif ( $opt =~ /^non-database/i ) {
211 4 $sh->load_plugins( $pi->{$opt} );
212 } else {
213 0 $sh->load_plugins( $pi->{$opt} );
214 }
215 }
216 } elsif ( ref $pi eq 'ARRAY' ) {
217 8 @pi = @$pi;
218 } else {
219 0 ▶ next unless $pi;
Branch: 1 branch
BranchTrueFalseTotal
unless $pi 0 0 0
220 0 push(@pi, $pi);
221 }
222 12 foreach my $pi (@pi) {
223 16 my $mod = $pi;
224 16 $mod =~ s/\.pm$//;
225 #print "Module: $mod\n";
226 16 unshift @DBI::Shell::Std::ISA, $mod;
227 16 ▶ eval qq{ use $pi };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
228 16 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 16 16
229 0 warn "Failed: $@";
230 0 shift @DBI::Shell::Std::ISA;
231 0 shift @pi;
232 } else {
233 $sh->print_buffer_nop("Loaded plugins $mod\n")
234 16 ▶ unless $sh->{batch};
Branch: 1 branch
BranchTrueFalseTotal
unless $sh->{"batch"} 0 16 16
235 }
236 }
237 }
238 22 local ($|) = 1;
239 # plug-ins should remove options they recognise from (localized) @ARGV
240 # by calling Getopt::Long::GetOptions (which is already in pass_through mode).
241 22 foreach my $pi (@pi) {
242 16 local *ARGV = $sh->{unhandled_options};
243 16 $pi->init($sh);
244 }
245 22 return @pi;
246 }
247  
248 7 ▶sub default_config {
Subroutine default_config: called CC=6 SCAR=22.6
249 7 my $sh = shift;
250 #
251 # Set default configuration options
252 #
253 7 ▶ foreach my $opt_ref (
Condition40%4/10
$ENV{'VISUAL'} || $ENV{'EDITOR'} || 'vi'
l!l
07
$ENV{"VISUAL"} or $ENV{"EDITOR"}
l!l&&r!l&&!r
007
$ENV{'DBISH_DEBUG'} || 0
l!l
07
$ENV{'HOME'} || "$ENV{'HOMEDRIVE'}$ENV{'HOMEPATH'}"
l!l&&r!l&&!r
700
MC/DC0%0/2
$ENV{'VISUAL'} || $ENV{'EDITOR'} || 'vi'
$ENV{"VISUAL"} $ENV{"EDITOR"}
MC/DC0%0/1
$ENV{'DBISH_DEBUG'} || 0
$ENV{'DBISH_DEBUG'}
MC/DC0%0/2
$ENV{'HOME'} || "$ENV{'HOMEDRIVE'}$ENV{'HOMEPATH'}"
$ENV{'HOME'} "$ENV{'HOMEDRIVE'}$ENV{'HOMEPATH'}"
Truth table33%1/3
A or B
A= $ENV{"VISUAL"}B= $ENV{"EDITOR"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $ENV{'DBISH_DEBUG'}
Aresult
11
00
Truth table33%1/3
A || B
A= $ENV{'HOME'}B= "$ENV{'HOMEDRIVE'}$ENV{'HOMEPATH'}"
ABresult
1X1
011
000
254 [ 'command_prefix_line=s' => '/' ],
255 [ 'command_prefix_end=s' => ';' ],
256 [ 'command_prefix=s' => '[/;]' ],
257 [ 'chistory_size=i' => 50 ],
258 [ 'rhistory_size=i' => 50 ],
259 [ 'rhistory_head=i' => 5 ],
260 [ 'rhistory_tail=i' => 5 ],
261 [ 'user_level=i' => 1 ],
262 [ 'editor|ed=s' => ($ENV{VISUAL} || $ENV{EDITOR} || 'vi') ],
263 [ 'batch' => 0 ],
264 [ 'format=s' => 'neat' ],
265 [ 'prompt=s' => undef ],
266 # defaults for each new database connect:
267 [ 'init_trace|trace=i' => 0 ],
268 [ 'init_autocommit|autocommit=i' => 1 ],
269 [ 'debug|d=i' => ($ENV{DBISH_DEBUG} || 0) ],
270 [ 'seperator|sep=s' => ',' ],
271 [ 'sqlpath|sql=s' => '.' ],
272 [ 'tmp_dir|tmp_d=s' => $ENV{DBISH_TMP} ],
273 [ 'tmp_file|tmp_f=s' => qq{dbish$$.sql} ],
274 [ 'home_dir|home_d=s' => $ENV{HOME} || "$ENV{HOMEDRIVE}$ENV{HOMEPATH}" ],
275 [ 'desc_show_remarks|show_remarks' => 1 ],
276 [ 'desc_show_long|show_long' => 1 ],
277 [ 'desc_format=s' => q{partbox} ],
278 [ 'desc_show_columns=s' => q{COLUMN_NAME,DATA_TYPE,TYPE_NAME,COLUMN_SIZE,PK,NULLABLE,COLUMN_DEF,IS_NULLABLE,REMARKS} ],
279 [ 'null_format=s' => '(NULL)' ],
280 [ 'bool_format=s' => q{Y,N} ],
281 @_,
282 ) {
283 182 $sh->add_option(@$opt_ref);
284 }
285  
286 }
287
288  
289 7 ▶sub default_commands {
Subroutine default_commands: called CC=1 SCAR=0.0
290 7 my $sh = shift;
291 #
292 # Install default commands
293 #
294 # The sub is passed a reference to the shell and the @ARGV-style
295 # args it was invoked with.
296 #
297 $sh->{commands} = {
298 7 'help' => {
299 hint => "display this list of commands",
300 },
301 'quit' => {
302 hint => "exit",
303 },
304 'exit' => {
305 hint => "exit",
306 },
307 'trace' => {
308 hint => "set DBI trace level for current database",
309 },
310 'connect' => {
311 hint => "connect to another data source/DSN",
312 },
313 'prompt' => {
314 hint => "change the displayed prompt",
315 },
316 # --- execute commands
317 'go' => {
318 hint => "execute the current statement",
319 },
320 'count' => {
321 hint => "execute 'select count(*) from table' (on each table listed).",
322 },
323 'do' => {
324 hint => "execute the current (non-select) statement",
325 },
326 'perl' => {
327 hint => "evaluate the current statement as perl code",
328 },
329 'ping' => {
330 hint => "ping the current connection",
331 },
332 'commit' => {
333 hint => "commit changes to the database",
334 },
335 'rollback' => {
336 hint => "rollback changes to the database",
337 },
338 # --- information commands
339 'primary_key_info' => {
340 hint => "display primary keys that exist in current database",
341 },
342 'col_info' => {
343 hint => "display columns that exist in current database",
344 },
345 'table_info' => {
346 hint => "display tables that exist in current database",
347 },
348 'type_info' => {
349 hint => "display data types supported by current server",
350 },
351 'drivers' => {
352 hint => "display available DBI drivers",
353 },
354  
355 # --- statement/history management commands
356 'clear' => {
357 hint => "erase the current statement",
358 },
359 'redo' => {
360 hint => "re-execute the previously executed statement",
361 },
362 'get' => {
363 hint => "make a previous statement current again",
364 },
365 'current' => {
366 hint => "display current statement",
367 },
368 'edit' => {
369 hint => "edit current statement in an external editor",
370 },
371 'chistory' => {
372 hint => "display command history",
373 },
374 'rhistory' => {
375 hint => "display result history",
376 },
377 'format' => {
378 hint => "set display format for selected data (Neat|Box)",
379 },
380 'history' => {
381 hint => "display combined command and result history",
382 },
383 'option' => {
384 hint => "display or set an option value",
385 },
386 'describe' => {
387 hint => "display information about a table (columns, data types).",
388 },
389 'load' => {
390 hint => "load a file from disk to the current buffer.",
391 },
392 'run' => {
393 hint => "load a file from disk to current buffer, then executes.",
394 },
395 'save' => {
396 hint => "save the current buffer to a disk file.",
397 },
398 'spool' => {
399 hint => "send all output to a disk file. usage: spool file name or spool off.",
400 },
401  
402 };
403  
404 }
405  
406 7 ▶sub default_term {
Subroutine default_term: called CC=3 SCAR=14.2
407 7 my ($sh, $class) = @_;
408 #
409 # Setup Term
410 #
411 7 my $mode;
412 7 ▶ if (!is_interactive()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not is_interactive()) { } 7 0 7
413 7 $sh->{batch} = 1;
414 7 $mode = "in batch mode";
415 } else {
416 0 $sh->{term} = new Term::ReadLine($class);
417 0 ▶ if ($sh->{term}->Features->{readHistory}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->{"term"}->Features->{"readHistory"}) 0 0 0
418 0 $sh->{term}->ReadHistory(File::Spec->catfile(File::HomeDir->my_home, HISTORY_FILE));
419 }
420 0 $mode = "";
421 }
422  
423 7 return( $mode );
424 }
425  
426 7 ▶sub new {
Subroutine new: called CC=12 SCAR=27.3
427 7 my ($class, @args) = @_;
428  
429 7 my $sh = bless {}, $class;
430
431 7 $sh->default_config;
432 7 $sh->default_commands;
433  
434 #
435 # Handle command line parameters
436 #
437 # data_source and user command line parameters overrides both
438 # environment and config settings.
439 #
440  
441 7 $DB::single = 1;
442  
443 7 local (@ARGV) = @args;
444 7 my @options = values %{ $sh->{options} };
445 7 Getopt::Long::config('pass_through'); # for plug-ins
446 7 ▶ unless (GetOptions($sh, 'help|h', @options)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&GetOptions($sh, "help|h", @options)) 0 7 7
447 0 $class->usage;
448 0 croak "DBI::Shell aborted.\n";
449 }
450 7 ▶ if ($sh->{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->{"help"}) 0 7 7
451 0 $class->usage;
452 0 return;
453 }
454  
455 7 $sh->{unhandled_options} = [];
456 7 @args = ();
457 7 foreach my $arg (@ARGV) {
458 7 ▶ if ($arg =~ /^-/) { # expected to be in "--opt=value" format
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^-/) { } 0 7 7
459 0 push @{$sh->{unhandled_options}}, $arg;
460 }
461 else {
462 7 push @args, $arg;
463 }
464 }
465  
466 # This may be obsolete since it is run again in DBI::Shell::new.
467 7 $sh->do_format($sh->{format});
468  
469 7 ▶ $sh->{data_source} = shift(@args) || $ENV{DBI_DSN} || '';
Condition40%2/5
shift @args || $ENV{'DBI_DSN'} || ''
l!l
70
shift @args or $ENV{"DBI_DSN"}
l!l&&r!l&&!r
700
MC/DC0%0/2
shift @args || $ENV{'DBI_DSN'} || ''
shift @args $ENV{"DBI_DSN"}
Truth table33%1/3
A or B
A= shift @argsB= $ENV{"DBI_DSN"}
ABresult
1X1
011
000
470  
471 7 my $user = shift(@args);
472 7 ▶ $sh->{user} = defined $user ? $user : $ENV{DBI_USER} || '';
Branch: 1 branch
BranchTrueFalseTotal
defined $user ? : 0 7 7
Condition50%1/2
$ENV{"DBI_USER"} or ""
l!l
07
MC/DC0%0/1
$ENV{"DBI_USER"} or ""
$ENV{"DBI_USER"}
Truth table50%1/2
A
A= $ENV{"DBI_USER"}
Aresult
11
00
473 7 my $password = shift(@args);
474 7 ▶ $sh->{password} = defined $password ? $password : $ENV{DBI_PASS} || undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $password ? : 0 7 7
Condition50%1/2
$ENV{"DBI_PASS"} or undef
l!l
07
MC/DC0%0/1
$ENV{"DBI_PASS"} or undef
$ENV{"DBI_PASS"}
Truth table50%1/2
A
A= $ENV{"DBI_PASS"}
Aresult
11
00
475  
476 7 $sh->{chistory} = []; # command history
477 7 $sh->{rhistory} = []; # result history
478 7 $sh->{prompt} = $sh->{data_source};
479  
480 # set the default io handle.
481 7 $sh->{out_fh} = \*STDOUT;
482  
483 # support for spool command ...
484 7 $sh->{spooling} = 0; $sh->{spool_file} = undef; $sh->{spool_fh} = undef;
485  
486 7 my $mode = $sh->default_term($class);
487  
488 7 $sh->log("DBI::Shell $DBI::Shell::VERSION using DBI $DBI::VERSION $mode");
489 7 ▶ $sh->log("DBI::Shell loaded from $INC{'DBI/Shell.pm'}") if $sh->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"debug"} 0 7 7
490  
491 7 return $sh;
492 }
493  
494 # Used to install, configure, or change an option or command.
495 52 ▶sub install_options {
Subroutine install_options: called CC=12 SCAR=30.4
496 52 my ($sh, $options) = @_;
497  
498 52 my @po;
499 $sh->log( "reference type: " . ref $options )
500 52 ▶ if $sh->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"debug"} 0 52 52
501  
502 52 ▶ if ( ref $options eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $options eq 'ARRAY') { } 52 0 52
elsif (ref $options eq 'HASH') { } 0 0 0
elsif (ref $options eq 'SCALAR') { } 0 0 0
503  
504 52 foreach my $opt_ref ( @$options )
505 #[ 'debug|d=i' => ($ENV{DBISH_DEBUG} || 0) ],
506 #[ 'seperator|sep=s' => ',' ],)
507 {
508 116 ▶ if ( ref $opt_ref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt_ref eq 'ARRAY') { } 36 80 116
509 36 $sh->install_options( $opt_ref );
510 } else {
511 80 push( @po, $opt_ref );
512 }
513 }
514 } elsif ( ref $options eq 'HASH' ) {
515 0 foreach (keys %{$options}) {
516 0 push(@po, $_, $options->{$_});
517 }
518 } elsif ( ref $options eq 'SCALAR' ) {
519 0 push( @po, $$options );
520 } else {
521 0 ▶ return unless $options;
Branch: 1 branch
BranchTrueFalseTotal
unless $options 0 0 0
522 0 push( @po, $options );
523 }
524  
525 52 ▶ return unless @po;
Branch: 1 branch
BranchTrueFalseTotal
unless @po 12 40 52
526  
527 40 eval{ $sh->add_option(@po) };
528 # Option exists, just change it.
529 40 ▶ if ($@ =~ /add_option/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /add_option/) { } 20 20 40
530 20 $sh->do_option( join( '=',@po ) );
531 } else {
532 20 ▶ croak "configuration: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 20 20
533 }
534 }
535  
536 5 ▶sub configuration {
Subroutine configuration: called CC=6 SCAR=18.3
537 5 my $sh = shift;
538  
539 # Source config file which may override the defaults.
540 # Default is $ENV{HOME}/.dbish_config.
541 # Can be overridden with $ENV{DBISH_CONFIG}.
542 # Make $ENV{DBISH_CONFIG} empty to prevent sourcing config file.
543 # XXX all this will change
544 my $homedir = $ENV{HOME} # unix
545 5 ▶ || "$ENV{HOMEDRIVE}$ENV{HOMEPATH}"; # NT
Condition33%1/3
$ENV{'HOME'} || "$ENV{'HOMEDRIVE'}$ENV{'HOMEPATH'}"
l!l&&r!l&&!r
500
MC/DC0%0/2
$ENV{'HOME'} || "$ENV{'HOMEDRIVE'}$ENV{'HOMEPATH'}"
$ENV{'HOME'} "$ENV{'HOMEDRIVE'}$ENV{'HOMEPATH'}"
Truth table33%1/3
A || B
A= $ENV{'HOME'}B= "$ENV{'HOMEDRIVE'}$ENV{'HOMEPATH'}"
ABresult
1X1
011
000
546 5 ▶ $sh->{config_file} = $ENV{DBISH_CONFIG} || "$homedir/.dbish_config";
Condition100%2/2
$ENV{'DBISH_CONFIG'} || "$homedir/.dbish_config"
l!l
41
MC/DC100%1/1
$ENV{'DBISH_CONFIG'} || "$homedir/.dbish_config"
$ENV{'DBISH_CONFIG'}
Truth table100%2/2
A
A= $ENV{'DBISH_CONFIG'}
Aresult
11
00
547 5 my $config;
548 5 ▶ if ($sh->{config_file} && -f $sh->{config_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->{"config_file"} and -f $sh->{"config_file"}) 4 1 5
Condition66%2/3
$sh->{"config_file"} and -f $sh->{"config_file"}
!ll&&!rl&&r
014
MC/DC50%1/2
$sh->{"config_file"} and -f $sh->{"config_file"}
$sh->{"config_file"} -f $sh->{"config_file"}
Truth table66%2/3
A and B
A= $sh->{"config_file"}B= -f $sh->{"config_file"}
ABresult
0X0
100
111
549 4 my $full = File::Spec->rel2abs( $sh->{config_file} );
550 4 $config = require $full;
551 # allow for custom configuration options.
552 4 ▶ if (exists $config->{'options'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"options"}) 4 0 4
553 4 $sh->install_options( $config->{'options'} );
554 }
555 }
556 5 return $config;
557 }
558  
559  
560 0 ▶sub run {
Subroutine run: not called CC=20 SCAR=60.4
561 0 my $sh = shift;
562  
563 0 my $current_line = '';
564  
565 0 while (1) {
566 0 my $prefix = $sh->{command_prefix};
567  
568 0 $current_line = $sh->readline($sh->prompt());
569 0 ▶ $current_line = "/quit" unless defined $current_line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current_line 0 0 0
570  
571 0 my $copy_cline = $current_line; my $eat_line = 0;
572 # move past command prefix contained within quotes
573 0 ▶ while( $copy_cline =~ s/(['"][^'"]*(?:$prefix).*?['"])//og ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($copy_cline =~ s/(['"][^'"]*(?:$prefix).*?['"])//go) 0 0 0
574 0 $eat_line = $+[0];
575 }
576  
577 # What's left to check?
578 0 my $line;
579 0 ▶ if ($eat_line > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eat_line > 0) { } 0 0 0
580 0 $sh->{current_buffer} .= substr( $current_line, 0, $eat_line ) . "\n";
581 0 ▶ $current_line = substr( $current_line, $eat_line )
Branch: 1 branch
BranchTrueFalseTotal
if length $current_line >= $eat_line 0 0 0
582 if (length($current_line) >= $eat_line );
583 } else {
584 0 $current_line = $copy_cline;
585 }
586  
587  
588 0 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($current_line =~ / ^(.*?) (?<!\\) $prefix (?:(\w*) ([^\|><]*))? ((?:\||>>?|<<?).+)? $ /x) { } 0 0 0
elsif ($current_line ne '') { } 0 0 0
589 $current_line =~ m/
590 ^(.*?)
591 (?<!\\)
592 $prefix
593 (?:(\w*)
594 ([^\|><]*))?
595 ((?:\||>>?|<<?).+)?
596 $
597 /x) {
598 0 ▶ my ($stmt, $cmd, $args_string, $output) = ($1, $2, $3, $4||'');
Condition0%0/2
$4 || ''
l!l
00
MC/DC0%0/1
$4 || ''
$4
Truth table0%0/2
A
A= $4
Aresult
11
00
599  
600 # print "$stmt -- $cmd -- $args_string -- $output\n";
601 # $sh->{current_buffer} .= "$stmt\n" if length $stmt;
602 0 ▶ if (length $stmt) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $stmt) 0 0 0
603 0 $stmt =~ s/\\$prefix/$prefix/g;
604 0 $sh->{current_buffer} .= "$stmt\n";
605 0 ▶ if ($sh->is_spooling) { print ${$sh->{spool_fh}} ($stmt, "\n\n") }
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->is_spooling) 0 0 0
606 }
607  
608 0 ▶ $cmd = 'go' if $cmd eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "" 0 0 0
609 0 ▶ my @args = split ' ', $args_string||'';
Condition0%0/2
$args_string || ''
l!l
00
MC/DC0%0/1
$args_string || ''
$args_string
Truth table0%0/2
A
A= $args_string
Aresult
11
00
610  
611 warn("command='$cmd' args='$args_string' output='$output'")
612 0 ▶ if $sh->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"debug"} 0 0 0
613  
614 0 my $command;
615 0 ▶ if ($sh->{abbrev}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->{'abbrev'}) { } 0 0 0
616 0 $command = $sh->{abbrev}->{$cmd};
617 }
618 else {
619 0 ▶ $command = ($sh->{command}->{$cmd}) ? $cmd : undef;
Branch: 1 branch
BranchTrueFalseTotal
$sh->{'command'}{$cmd} ? : 0 0 0
620 }
621 0 ▶ if ($command) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command) { } 0 0 0
622 0 $sh->run_command($command, $output, @args);
623 }
624 else {
625 0 ▶ if ($sh->{batch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->{"batch"}) 0 0 0
626 0 die "Command '$cmd' not recognised";
627 }
628  
629 0 my $additional = "";
630 0 ▶ if ($current_line =~ /$prefix(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_line =~ /$prefix(.+)/) 0 0 0
631 0 $additional = ", does your statement contain the separator '$prefix'? See https://rt.cpan.org/Ticket/Display.html?id=21200";
632 }
633  
634 0 $sh->alert("Command '$cmd' not recognised$additional ",
635 "(enter ${prefix}help for help).");
636 }
637  
638 }
639 elsif ($current_line ne "") {
640 0 ▶ if ($sh->is_spooling) { print ${$sh->{spool_fh}} ($current_line, "\n") }
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->is_spooling) 0 0 0
641 0 $sh->{current_buffer} .= $current_line . "\n";
642 # print whole buffer here so user can see it as
643 # it grows (and new users might guess that unrecognised
644 # inputs are treated as commands)
645 0 ▶ unless ($sh->{user_level}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sh->{"user_level"}) 0 0 0
646 0 $sh->run_command('current', undef,
647 "(enter '$prefix' to execute or '${prefix}help' for help)");
648 }
649 }
650 }
651 }
652
653  
654 #
655 # Internal methods
656 #
657  
658 0 ▶sub readline {
Subroutine readline: not called CC=2 SCAR=17.9
659 0 my ($sh, $prompt) = @_;
660 0 my $rv;
661 0 ▶ if ($sh->{term}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->{'term'}) { } 0 0 0
662 0 $rv = $sh->{term}->readline($prompt);
663 }
664 else {
665 0 chomp($rv = <STDIN>);
666 }
667  
668 0 return $rv;
669 }
670  
671  
672 9 ▶sub run_command {
Subroutine run_command: called CC=10 SCAR=30.3
673 9 my ($sh, $command, $output, @args) = @_;
674 9 ▶ return unless $command;
Branch: 1 branch
BranchTrueFalseTotal
unless $command 0 9 9
675  
676 9 my $code = "do_$command";
677 9 ▶ if ($sh->can("$code")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->can("$code")) { } 9 0 9
678 9 ▶ local(*STDOUT) if $output;
Branch: 1 branch
BranchTrueFalseTotal
if $output 0 9 9
679 9 ▶ local(*OUTPUT) if $output;
Branch: 1 branch
BranchTrueFalseTotal
if $output 0 9 9
680 9 ▶ if ($output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output) 0 9 9
681 0 ▶ if (open(OUTPUT, $output)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open OUTPUT, $output) { } 0 0 0
682 0 *STDOUT = *OUTPUT;
683 } else {
684 0 $sh->err("Couldn't open output '$output': $!");
685 0 $sh->run_command('current', undef, '');
686 }
687 }
688  
689 9 local $@;
690  
691 9 eval {
692 9 $sh->$code(@args);
693 };
694 9 ▶ close OUTPUT if $output;
Branch: 1 branch
BranchTrueFalseTotal
if $output 0 9 9
695 9 ▶ $sh->err("$command failed: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
696 }
697 else {
698 0 ▶ if ($command eq 'spool') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq 'spool') { } 0 0 0
699 0 $sh->err("The DBI::Shell:Spool plug in needs to be installed. See https://rt.cpan.org/Ticket/Display.html?id=24538#txn-813176")
700 }
701 else {
702 0 $sh->err("$command does not exist, does a plug-in need to be installed?")
703 }
704 }
705 9 return;
706 }
707  
708  
709 1 ▶sub print_list {
Subroutine print_list: called CC=2 SCAR=8.4
710 1 my ($sh, $list_ref) = @_;
711 1 ▶ for(my $i = 0; $i < @$list_ref; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$list_ref) 0 1 1
712 0 print ${$sh->{out_fh}} ($i+1,": $$list_ref[$i]\n");
713 }
714 1 return;
715 }
716  
717  
718 #-------------------------------------------------------------------
719 #
720 # Print Buffer adding a prompt.
721 #
722 #-------------------------------------------------------------------
723 72 ▶sub print_buffer {
Subroutine print_buffer: called CC=1 SCAR=0.0
724 72 my $sh = shift;
725 {
726 72 local ($,) = q{ };
727 72 my @out = @_;
728 72 chomp $out[-1]; # Remove any added newline.
729 72 return print ($sh->prompt(), @out,"\n");
730 }
731 }
732  
733 #-------------------------------------------------------------------
734 #
735 # Print Buffer without adding a prompt.
736 #
737 #-------------------------------------------------------------------
738 98 ▶sub print_buffer_nop {
Subroutine print_buffer_nop: called CC=1 SCAR=0.0
739 98 my $sh = shift;
740 {
741 98 local ($,) = q{ };
742 98 my @out = @_;
743 98 chomp $out[-1]; # Remove any added newline.
744 98 return print (@out,"\n");
745 }
746 }
747  
748 9 ▶sub get_data_source {
Subroutine get_data_source: called CC=17 SCAR=54.4
749 9 my ($sh, $dsn, @args) = @_;
750 9 my $driver;
751  
752 9 ▶ if ($dsn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn) 9 0 9
753 9 ▶ if ($dsn =~ m/^dbi:.*:/i) { # has second colon
Branch: 2 branches
BranchTrueFalseTotal
if ($dsn =~ /^dbi:.*:/i) { } 9 0 9
elsif ($dsn =~ /^dbi:([^:]*)/i) { } 0 0 0
754 9 return $dsn; # assumed to be full DSN
755 }
756 elsif ($dsn =~ m/^dbi:([^:]*)/i) {
757 0 $driver = $1 # use DriverName part
758 }
759 else {
760 0 $sh->print_buffer_nop ("Ignored unrecognised DBI DSN '$dsn'.\n");
761 }
762 }
763  
764 0 ▶ if ($sh->{batch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->{"batch"}) 0 0 0
765 0 die "Missing or unrecognised DBI DSN.";
766 }
767  
768 0 $sh->print_buffer_nop("\n");
769  
770 0 ▶ while (!$driver) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($driver) 0 0 0
771 0 $sh->print_buffer_nop("Available DBI drivers:\n");
772 0 my @drivers = DBI->available_drivers;
773 0 ▶ for( my $cnt = 0; $cnt <= $#drivers; $cnt++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt <= $#drivers) 0 0 0
774 0 $sh->print_buffer_nop(sprintf ("%2d: dbi:%s\n", $cnt+1, $drivers[$cnt]));
775 }
776 0 $driver = $sh->readline(
777 "Enter driver name or number, or full 'dbi:...:...' DSN: ");
778 0 ▶ exit unless defined $driver; # detect ^D / EOF
Branch: 1 branch
BranchTrueFalseTotal
unless defined $driver 0 0 0
779 0 $sh->print_buffer_nop("\n");
780  
781 0 ▶ return $driver if $driver =~ /^dbi:.*:/i; # second colon entered
Branch: 1 branch
BranchTrueFalseTotal
if $driver =~ /^dbi:.*:/i 0 0 0
782  
783 0 ▶ if ( $driver =~ /^\s*(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($driver =~ /^\s*(\d+)/) { } 0 0 0
784 0 $driver = $drivers[$1-1];
785 } else {
786 0 $driver = $1;
787 0 ▶ $driver =~ s/^dbi://i if $driver # incase they entered 'dbi:Name'
Branch: 1 branch
BranchTrueFalseTotal
if $driver 0 0 0
788 }
789 # XXX try to install $driver (if true)
790 # unset $driver if install fails.
791 }
792  
793 0 my $source;
794 0 ▶ while (!defined $source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $source) 0 0 0
795 0 my $prompt;
796 0 my @data_sources = DBI->data_sources($driver);
797 0 ▶ if (@data_sources) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data_sources) { } 0 0 0
798 0 $sh->print_buffer_nop("Enter data source to connect to: \n");
799 0 ▶ for( my $cnt = 0; $cnt <= $#data_sources; $cnt++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt <= $#data_sources) 0 0 0
800 0 $sh->print_buffer_nop(sprintf ("%2d: %s\n", $cnt+1, $data_sources[$cnt]));
801 }
802 0 $prompt = "Enter data source or number,";
803 }
804 else {
805 0 $sh->print_buffer_nop ("(The data_sources method returned nothing.)\n");
806 0 $prompt = "Enter data source";
807 }
808 0 $source = $sh->readline(
809 "$prompt or full 'dbi:...:...' DSN: ");
810 0 ▶ return if !defined $source; # detect ^D / EOF
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 0 0 0
811 0 ▶ if ($source =~ /^\s*(\d+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($source =~ /^\s*(\d+)/) { } 0 0 0
elsif ($source =~ /^dbi:([^:]+)$/) { } 0 0 0
812 0 $source = $data_sources[$1-1]
813 }
814 elsif ($source =~ /^dbi:([^:]+)$/) { # no second colon
815 0 $driver = $1; # possibly new driver
816 0 $source = undef;
817 }
818 0 $sh->print_buffer_nop("\n");
819 }
820  
821 0 return $source;
822 }
823  
824  
825 0 ▶sub prompt_for_password {
Subroutine prompt_for_password: not called CC=6 SCAR=37.4
826 0 my ($sh) = @_;
827  
828 # no prompts in batch mode.
829  
830 0 ▶ return if ($sh->{batch});
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"batch"} 0 0 0
831  
832 0 ▶ if (!defined($haveTermReadKey)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $haveTermReadKey) 0 0 0
833 0 ▶ $haveTermReadKey = eval { require Term::ReadKey } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey } } ? : 0 0 0
834 }
835 0 local $| = 1;
836 0 ▶ $sh->print_buffer_nop ("Password for $sh->{user} (",
Branch: 1 branch
BranchTrueFalseTotal
$haveTermReadKey ? : 0 0 0
837 ($haveTermReadKey ? "not " : "Warning: "),
838 "echoed to screen): ");
839 0 ▶ if ($haveTermReadKey) {
Branch: 1 branch
BranchTrueFalseTotal
if ($haveTermReadKey) { } 0 0 0
840 0 Term::ReadKey::ReadMode('noecho');
841 0 $sh->{password} = Term::ReadKey::ReadLine(0);
842 0 Term::ReadKey::ReadMode('restore');
843 } else {
844 0 $sh->{password} = <STDIN>;
845 }
846 0 chomp $sh->{password};
847 0 $sh->print_buffer_nop ("\n");
848 }
849  
850 72 ▶sub prompt {
Subroutine prompt: called CC=6 SCAR=33.4
851 72 my ($sh) = @_;
852 72 ▶ return "" if $sh->{batch};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"batch"} 72 0 72
853 0 ▶ return "(not connected)> " unless $sh->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $sh->{"dbh"} 0 0 0
854  
855 0 ▶ if ( ref $sh->{prompt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sh->{'prompt'}) { } 0 0 0
856 0 foreach (@{$sh->{prompt}} ) {
857 0 ▶ if ( ref $_ eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'CODE') { } 0 0 0
858 0 $sh->{prompt} .= &$_;
859 } else {
860 0 $sh->{prompt} .= $_;
861 }
862 }
863 0 return "$sh->{user}\@$sh->{prompt}> ";
864 } else {
865 0 return "$sh->{user}\@$sh->{prompt}> ";
866 }
867 0 return;
868 }
869  
870  
871 52 ▶sub push_chistory {
Subroutine push_chistory: called CC=3 SCAR=11.2
872 52 my ($sh, $cmd) = @_;
873 52 ▶ $cmd = $sh->{current_buffer} unless defined $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmd 52 0 52
874 52 $sh->{prev_buffer} = $cmd;
875 52 my $chist = $sh->{chistory};
876 52 ▶ shift @$chist if @$chist >= $sh->{chistory_size};
Branch: 1 branch
BranchTrueFalseTotal
if @$chist >= $sh->{"chistory_size"} 0 52 52
877 52 push @$chist, $cmd;
878 52 return;
879 }
880  
881  
882 #
883 # Command methods
884 #
885  
886 1 ▶sub do_help {
Subroutine do_help: called CC=5 SCAR=29.1
887 1 my ($sh, @args) = @_;
888  
889 1 ▶ return "" if $sh->{batch};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"batch"} 1 0 1
890  
891 0 my $prefix = $sh->{command_prefix};
892 0 my $commands = $sh->{commands};
893 0 $sh->print_buffer_nop ("Defined commands, in alphabetical order:\n");
894 0 foreach my $cmd (sort keys %$commands) {
895 0 ▶ my $hint = $commands->{$cmd}->{hint} || '';
Condition0%0/2
$commands->{$cmd}{'hint'} || ''
l!l
00
MC/DC0%0/1
$commands->{$cmd}{'hint'} || ''
$commands->{$cmd}{'hint'}
Truth table0%0/2
A
A= $commands->{$cmd}{'hint'}
Aresult
11
00
896 0 $sh->print_buffer_nop(sprintf (" %s%-10s %s\n", $prefix, $cmd, $hint));
897 }
898 0 ▶ $sh->print_buffer_nop ("Commands can be abbreviated.\n") if $sh->{abbrev};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"abbrev"} 0 0 0
899 0 return;
900 }
901  
902  
903 22 ▶sub do_format {
Subroutine do_format: called CC=4 SCAR=14.6
904 22 my ($sh, @args) = @_;
905 22 ▶ my $mode = $args[0] || '';
Condition50%1/2
$args[0] || ''
l!l
220
MC/DC0%0/1
$args[0] || ''
$args[0]
Truth table50%1/2
A
A= $args[0]
Aresult
11
00
906 22 my $class = eval { DBI::Format->formatter($mode,1) };
907 22 ▶ unless ($class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 0 22 22
908 0 return $sh->alert("Unable to select '$mode': $@");
909 }
910 22 ▶ $sh->log("Using formatter class '$class'") if $sh->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"debug"} 0 22 22
911 22 $sh->{format} = $mode;
912 22 return $sh->{display} = $class->new($sh);
913 }
914  
915  
916 52 ▶sub do_go {
Subroutine do_go: called CC=7 SCAR=19.8
917 52 my ($sh, @args) = @_;
918  
919 # print "do_go\n";
920  
921 # Modify go to get the last executed statement if called on an
922 # empty buffer.
923  
924 52 ▶ if ($sh->{current_buffer} eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->{"current_buffer"} eq "") 2 50 52
925 2 $sh->do_get;
926 2 ▶ return if $sh->{current_buffer} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"current_buffer"} eq "" 0 2 2
927 }
928  
929 52 $sh->{prev_buffer} = $sh->{current_buffer};
930  
931 52 $sh->push_chistory;
932
933 52 eval {
934 # Determine if the single quotes are out of balance.
935 52 my $count = ($sh->{current_buffer} =~ tr/'/'/);
936 52 ▶ warn "Quotes out of balance: $count" unless (($count % 2) == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $count % 2 == 0 0 52 52
937  
938 52 my $sth = $sh->{dbh}->prepare($sh->{current_buffer});
939  
940 51 $sh->sth_go($sth, 1);
941 };
942 52 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 51 52
943 1 my $err = $@;
944 $err =~ s: at \S*DBI/Shell.pm line \d+(,.*?chunk \d+)?::
945 1 ▶ if !$sh->{debug} && $err =~ /^DBD::\w+::\w+ \w+/;
Branch: 1 branch
BranchTrueFalseTotal
if not $sh->{"debug"} and $err =~ /^DBD::\w+::\w+ \w+/ 1 0 1
Condition33%1/3
not $sh->{"debug"} and $err =~ /^DBD::\w+::\w+ \w+/
!ll&&!rl&&r
001
MC/DC0%0/2
not $sh->{"debug"} and $err =~ /^DBD::\w+::\w+ \w+/
not $sh->{"debug"} $err =~ /^DBD::\w+::\w+ \w+/
Truth table33%1/3
not(A) and B
A= not $sh->{"debug"}B= $err =~ /^DBD::\w+::\w+ \w+/
ABresult
0X0
100
111
946 1 print STDERR "$err";
947 }
948 # There need to be a better way, maybe clearing the
949 # buffer when the next non command is typed.
950 # Or sprinkle <$sh->{current_buffer} ||= $sh->{prev_buffer};>
951 # around in the code.
952 52 return $sh->{current_buffer} = '';
953 }
954  
955  
956 53 ▶sub sth_go {
Subroutine sth_go: called CC=24 SCAR=37.2
957 53 my ($sh, $sth, $execute, $rh) = @_;
958  
959 53 ▶ $rh = 1 unless defined $rh; # Add to results history. Default 1, Yes.
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rh 51 2 53
960 53 my $rv;
961 53 ▶ if ($execute || !$sth->{Active}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($execute or not $sth->{"Active"}) 53 0 53
Condition66%2/3
$execute or not $sth->{"Active"}
l!l&&r!l&&!r
5120
MC/DC0%0/2
$execute or not $sth->{"Active"}
$execute not $sth->{"Active"}
Truth table66%2/3
A or not(B)
A= $executeB= not $sth->{"Active"}
ABresult
1X1
011
000
962 53 my @params;
963 53 ▶ my $params = $sth->{NUM_OF_PARAMS} || 0;
Condition50%1/2
$sth->{'NUM_OF_PARAMS'} || 0
l!l
053
MC/DC0%0/1
$sth->{'NUM_OF_PARAMS'} || 0
$sth->{'NUM_OF_PARAMS'}
Truth table50%1/2
A
A= $sth->{'NUM_OF_PARAMS'}
Aresult
11
00
964 53 ▶ $sh->print_buffer_nop("Statement has $params parameters:\n") if $params;
Branch: 1 branch
BranchTrueFalseTotal
if $params 0 53 53
965 53 foreach(1..$params) {
966 0 my $val = $sh->readline("Parameter $_ value: ");
967 0 push @params, $val;
968 }
969 53 $rv = $sth->execute(@params);
970 }
971
972 53 ▶ if (!$sth->{'NUM_OF_FIELDS'}) { # not a select statement
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->{"NUM_OF_FIELDS"}) 0 53 53
973 0 local $^W=0;
974 0 ▶ $rv = "undefined number of" unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
975 0 ▶ $rv = "unknown number of" if $rv == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == -1 0 0 0
976 0 ▶ $sh->print_buffer_nop ("[$rv row" . ($rv==1 ? "" : "s") . " affected]\n");
Branch: 1 branch
BranchTrueFalseTotal
$rv == 1 ? : 0 0 0
977 0 return;
978 }
979  
980 53 $sh->{sth} = $sth;
981  
982 #
983 # Remove oldest result from history if reached limit
984 #
985 53 my $rhist = $sh->{rhistory};
986 53 ▶ if ($rh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rh) 51 2 53
987 51 ▶ shift @$rhist if @$rhist >= $sh->{rhistory_size};
Branch: 1 branch
BranchTrueFalseTotal
if @$rhist >= $sh->{"rhistory_size"} 0 51 51
988 51 push @$rhist, [];
989 }
990  
991 #
992 # Keep a buffer of $sh->{rhistory_tail} many rows,
993 # when done with result add those to rhistory buffer.
994 # Could use $sth->rows(), but not all DBD's support it.
995 #
996 53 my @rtail;
997 53 my $i = 0;
998 53 ▶ my $display = $sh->{display} || die "panic: no display set";
Condition50%1/2
$sh->{'display'} || die('panic: no display set')
l!l
530
MC/DC0%0/1
$sh->{'display'} || die('panic: no display set')
$sh->{'display'}
Truth table50%1/2
A
A= $sh->{'display'}
Aresult
11
00
999 53 ▶ $display->header($sth, $sh->{out_fh}||\*STDOUT, $sh->{seperator});
Condition50%1/2
$sh->{'out_fh'} || \*STDOUT
l!l
530
MC/DC0%0/1
$sh->{'out_fh'} || \*STDOUT
$sh->{'out_fh'}
Truth table50%1/2
A
A= $sh->{'out_fh'}
Aresult
11
00
1000  
1001 OUT_ROWS:
1002 53 ▶ while (my $rowref = $sth->fetchrow_arrayref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rowref = $sth->fetchrow_arrayref) 519 53 572
1003 519 $i++;
1004  
1005 519 my $rslt = $display->row($rowref);
1006  
1007 519 ▶ if($rh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rh) 510 9 519
1008 510 ▶ if ($i <= $sh->{rhistory_head}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $sh->{'rhistory_head'}) { } 255 255 510
1009 255 push @{$rhist->[-1]}, [@$rowref];
1010 }
1011 else {
1012 255 ▶ shift @rtail if @rtail == $sh->{rhistory_tail};
Branch: 1 branch
BranchTrueFalseTotal
if @rtail == $sh->{"rhistory_tail"} 0 255 255
1013 255 push @rtail, [@$rowref];
1014 }
1015 }
1016  
1017 519 ▶ unless(defined $rslt) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rslt) 0 519 519
1018 0 $sh->print_buffer_nop( "row limit reached" );
1019 0 last OUT_ROWS;
1020 }
1021 }
1022  
1023 53 $display->trailer($i);
1024  
1025 53 ▶ if($rh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rh) 51 2 53
1026 51 ▶ if (@rtail) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rtail) 51 0 51
1027 51 my $rows = $i;
1028 51 my $ommitted = $i - $sh->{rhistory_head} - @rtail;
1029 # Only include the omitted message if results are omitted.
1030 51 ▶ if ($ommitted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ommitted) 0 51 51
1031 0 push(@{$rhist->[-1]},
1032 [ "[...$ommitted rows out of $rows ommitted...]"]);
1033 }
1034 51 foreach my $rowref (@rtail) {
1035 255 push @{$rhist->[-1]}, $rowref;
1036 }
1037 }
1038 }
1039  
1040 53 return;
1041 }
1042  
1043 #------------------------------------------------------------------
1044 #
1045 # Generate a select count(*) from table for each table in list.
1046 #
1047 #------------------------------------------------------------------
1048  
1049 0 ▶sub do_count {
Subroutine do_count: not called CC=2 SCAR=17.9
1050 0 my ($sh, @args) = @_;
1051 0 foreach my $tab (@args) {
1052 0 $sh->print_buffer_nop ("Counting: $tab\n");
1053 0 $sh->{current_buffer} = "select count(*) as cnt_$tab from $tab";
1054 0 $sh->do_go();
1055 }
1056 0 return $sh->{current_buffer} = '';
1057 }
1058  
1059 0 ▶sub do_do {
Subroutine do_do: not called CC=3 SCAR=24.8
1060 0 my ($sh, @args) = @_;
1061 0 $sh->push_chistory;
1062 0 my $rv = $sh->{dbh}->do($sh->{current_buffer});
1063 0 ▶ $sh->print_buffer_nop ("[$rv row" . ($rv==1 ? "" : "s") . " affected]\n")
Branch: 2 branches
BranchTrueFalseTotal
if defined $rv 0 0 0
$rv == 1 ? : 0 0 0
1064 if defined $rv;
1065  
1066 # XXX I question setting the buffer to '' here.
1067 # I may want to edit my line without having to scroll back.
1068 0 return $sh->{current_buffer} = '';
1069 }
1070  
1071  
1072 9 ▶sub do_disconnect {
Subroutine do_disconnect: called CC=5 SCAR=16.3
1073 9 my ($sh, @args) = @_;
1074 9 ▶ return unless $sh->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $sh->{"dbh"} 0 9 9
1075 9 $sh->log("Disconnecting from $sh->{data_source}.");
1076 9 eval {
1077 9 ▶ $sh->{sth}->finish if $sh->{sth};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"sth"} 6 3 9
1078 9 ▶ $sh->{dbh}->rollback unless $sh->{dbh}->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $sh->{"dbh"}{"AutoCommit"} 0 9 9
1079 9 $sh->{dbh}->disconnect;
1080 };
1081 9 ▶ $sh->alert("Error during disconnect: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
1082 9 $sh->{sth} = undef;
1083 9 $sh->{dbh} = undef;
1084 9 return;
1085 }
1086  
1087  
1088 9 ▶sub do_connect {
Subroutine do_connect: called CC=8 SCAR=23.6
1089 9 my ($sh, $dsn, $user, $pass) = @_;
1090  
1091 9 $dsn = $sh->get_data_source($dsn);
1092 9 ▶ return unless $dsn;
Branch: 1 branch
BranchTrueFalseTotal
unless $dsn 0 9 9
1093  
1094 9 ▶ $sh->do_disconnect if $sh->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"dbh"} 2 7 9
1095  
1096 # Change from Jeff Zucker, convert literal slash and letter n to newline.
1097 9 $dsn =~ s/\\n/\n/g;
1098 9 $dsn =~ s/\\t/\t/g;
1099  
1100  
1101 9 $sh->{data_source} = $dsn;
1102 9 ▶ if (defined $user and length $user) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user and length $user) 0 9 9
Condition33%1/3
defined $user and length $user
!ll&&!rl&&r
900
MC/DC0%0/2
defined $user and length $user
defined $user length $user
Truth table33%1/3
A and B
A= defined $userB= length $user
ABresult
0X0
100
111
1103 0 $sh->{user} = $user;
1104 0 $sh->{password} = undef; # force prompt below
1105 }
1106  
1107 9 $sh->log("Connecting to '$sh->{data_source}' as '$sh->{user}'...");
1108 9 ▶ if ($sh->{user} and !defined $sh->{password}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->{"user"} and not defined $sh->{"password"}) 0 9 9
Condition33%1/3
$sh->{"user"} and not defined $sh->{"password"}
!ll&&!rl&&r
900
MC/DC0%0/2
$sh->{"user"} and not defined $sh->{"password"}
$sh->{"user"} not defined $sh->{"password"}
Truth table33%1/3
A and not(B)
A= $sh->{"user"}B= not defined $sh->{"password"}
ABresult
0X0
100
111
1109 0 $sh->prompt_for_password();
1110 }
1111 $sh->{dbh} = DBI->connect(
1112 $sh->{data_source}, $sh->{user}, $sh->{password}, {
1113 AutoCommit => $sh->{init_autocommit},
1114 9 PrintError => 0,
1115 RaiseError => 1,
1116 LongTruncOk => 1, # XXX
1117 });
1118 9 ▶ $sh->{dbh}->trace($sh->{init_trace}) if $sh->{init_trace};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"init_trace"} 0 9 9
1119 9 return;
1120 }
1121  
1122  
1123 9 ▶sub do_current {
Subroutine do_current: called CC=2 SCAR=7.1
1124 9 my ($sh, $msg, @args) = @_;
1125 9 ▶ $msg = $msg ? " $msg" : "";
Branch: 1 branch
BranchTrueFalseTotal
$msg ? : 0 9 9
1126 return
1127 9 $sh->log("Current statement buffer$msg:\n" . $sh->{current_buffer});
1128 }
1129  
1130 0 ▶sub do_autoflush {
Subroutine do_autoflush: not called CC=1 SCAR=6.9
1131  
1132 0 return;
1133 }
1134  
1135 0 ▶sub do_trace {
Subroutine do_trace: not called CC=1 SCAR=6.9
1136 0 return shift->{dbh}->trace(@_);
1137 }
1138  
1139 1 ▶sub do_commit {
Subroutine do_commit: called CC=1 SCAR=0.0
1140 1 return shift->{dbh}->commit(@_);
1141 }
1142  
1143 1 ▶sub do_rollback {
Subroutine do_rollback: called CC=1 SCAR=0.0
1144 1 return shift->{dbh}->rollback(@_);
1145 }
1146  
1147  
1148 0 ▶sub do_quit {
Subroutine do_quit: not called CC=4 SCAR=30.0
1149 0 my ($sh, @args) = @_;
1150 0 ▶ $sh->do_disconnect if $sh->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"dbh"} 0 0 0
1151  
1152 0 ▶ if ($sh->{term}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->{"term"}) 0 0 0
1153 0 ▶ if ($sh->{term}->Features()->{writeHistory}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->{"term"}->Features->{"writeHistory"}) 0 0 0
1154 0 $sh->{term}->WriteHistory(File::Spec->catfile(File::HomeDir->my_home, HISTORY_FILE));
1155 }
1156 }
1157  
1158 0 undef $sh->{term};
1159 0 exit 0;
1160 }
1161  
1162 # Until the alias command is working each command requires definition.
1163 0 ▶sub do_exit { shift->do_quit(@_); }
Subroutine do_exit: not called CC=1 SCAR=6.9
1164  
1165 1 ▶sub do_clear {
Subroutine do_clear: called CC=1 SCAR=0.0
1166 1 my ($sh, @args) = @_;
1167 1 return $sh->{current_buffer} = '';
1168 }
1169  
1170  
1171 0 ▶sub do_redo {
Subroutine do_redo: not called CC=3 SCAR=24.8
1172 0 my ($sh, @args) = @_;
1173 0 ▶ $sh->{current_buffer} = $sh->{prev_buffer} || '';
Condition0%0/2
$sh->{'prev_buffer'} || ''
l!l
00
MC/DC0%0/1
$sh->{'prev_buffer'} || ''
$sh->{'prev_buffer'}
Truth table0%0/2
A
A= $sh->{'prev_buffer'}
Aresult
11
00
1174 0 ▶ $sh->run_command('go') if $sh->{current_buffer};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"current_buffer"} 0 0 0
1175 0 return;
1176 }
1177  
1178  
1179 1 ▶sub do_chistory {
Subroutine do_chistory: called CC=1 SCAR=0.0
1180 1 my ($sh, @args) = @_;
1181 1 return $sh->print_list($sh->{chistory});
1182 }
1183  
1184 1 ▶sub do_history {
Subroutine do_history: called CC=4 SCAR=20.4
1185 1 my ($sh, @args) = @_;
1186 1 ▶ for(my $i = 0; $i < @{$sh->{chistory}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$sh->{"chistory"};}) 0 1 1
1187 0 $sh->print_buffer_nop ($i+1, ":\n", $sh->{chistory}->[$i], "--------\n");
1188 0 foreach my $rowref (@{$sh->{rhistory}[$i]}) {
1189 0 ▶ $sh->print_buffer_nop(" ", join(", ", map { defined $_ ? $_ : q{undef} }@$rowref), "\n");
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
1190 }
1191 }
1192 1 return;
1193 }
1194  
1195 1 ▶sub do_rhistory {
Subroutine do_rhistory: called CC=4 SCAR=20.4
1196 1 my ($sh, @args) = @_;
1197 1 ▶ for(my $i = 0; $i < @{$sh->{rhistory}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$sh->{"rhistory"};}) 0 1 1
1198 0 $sh->print_buffer_nop ($i+1, ":\n");
1199 0 foreach my $rowref (@{$sh->{rhistory}[$i]}) {
1200 0 ▶ $sh->print_buffer_nop (" ", join(", ", map { defined $_ ? $_ : q{undef} }@$rowref), "\n");
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
1201 }
1202 }
1203 1 return;
1204 }
1205  
1206  
1207 26 ▶sub do_get {
Subroutine do_get: called CC=7 SCAR=20.4
1208 26 my ($sh, $num, @args) = @_;
1209 # If get is called without a number, retrieve the last command.
1210 26 ▶ unless( $num ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num) 23 3 26
1211 23 $num = ($#{$sh->{chistory}} + 1);
1212  
1213 }
1214 # Allow for negative history. If called with -1, get the second
1215 # to last command execute, -2 third to last, ...
1216 26 ▶ if ($num and $num =~ /^\-\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num and $num =~ /^\-\d+$/) 2 24 26
Condition66%2/3
$num and $num =~ /^\-\d+$/
!ll&&!rl&&r
0242
MC/DC50%1/2
$num and $num =~ /^\-\d+$/
$num $num =~ /^\-\d+$/
Truth table66%2/3
A and B
A= $numB= $num =~ /^\-\d+$/
ABresult
0X0
100
111
1217 2 $sh->print_buffer_nop("Negative number $num: \n");
1218 2 $num = ($#{$sh->{chistory}} + 1) + $num;
1219 2 $sh->print_buffer_nop("Changed number $num: \n");
1220 }
1221  
1222 26 ▶ if (!$num or $num !~ /^\d+$/ or !defined($sh->{chistory}->[$num-1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num and $num =~ /^\d+$/ and defined $sh->{"chistory"}[$num - 1]) 0 26 26
Condition33%2/6
$num and $num =~ /^\d+$/ and defined $sh->{"chistory"}[$num - 1]
!ll&&!rl&&r
0026
$num and $num =~ /^\d+$/
!ll&&!rl&&r
0026
MC/DC0%0/3
$num and $num =~ /^\d+$/ and defined $sh->{"chistory"}[$num - 1]
$num $num =~ /^\d+$/ defined $sh->{"chistory"}[$num - 1]
Truth table25%1/4
A and B and C
A= $numB= $num =~ /^\d+$/C= defined $sh->{"chistory"}[$num - 1]
ABCresult
0XX0
10X0
1100
1111
1223 0 return $sh->err("No such command number '$num'. Use /chistory to list previous commands.");
1224 }
1225 26 $sh->{current_buffer} = $sh->{chistory}->[$num-1];
1226 26 $sh->print_buffer($sh->{current_buffer});
1227 26 return $num;
1228 }
1229  
1230  
1231 0 ▶sub do_perl {
Subroutine do_perl: not called CC=2 SCAR=17.9
1232 0 my ($sh, @args) = @_;
1233 0 $DBI::Shell::eval::dbh = $sh->{dbh};
1234 0 eval "package DBI::Shell::eval; $sh->{current_buffer}";
1235 0 ▶ if ($@) { $sh->err("Perl failed: $@") }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1236 0 return $sh->run_command('clear');
1237 }
1238  
1239 #-------------------------------------------------------------
1240 # Ping the current database connection.
1241 #-------------------------------------------------------------
1242 0 ▶sub do_ping {
Subroutine do_ping: not called CC=2 SCAR=17.9
1243 0 my ($sh, @args) = @_;
1244 return $sh->print_buffer_nop (
1245 "Connection "
1246 0 ▶ , $sh->{dbh}->ping() == '0' ? 'Is' : 'Is Not'
Branch: 1 branch
BranchTrueFalseTotal
$sh->{'dbh'}->ping == '0' ? : 0 0 0
1247 , " alive\n" );
1248 }
1249  
1250 0 ▶sub do_edit {
Subroutine do_edit: not called CC=17 SCAR=57.2
1251 0 my ($sh, @args) = @_;
1252  
1253 0 ▶ $sh->run_command('get', '', $&) if @args and $args[0] =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if @args and $args[0] =~ /^\d+$/ 0 0 0
Condition0%0/3
@args and $args[0] =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
@args and $args[0] =~ /^\d+$/
@args $args[0] =~ /^\d+$/
Truth table0%0/3
A and B
A= @argsB= $args[0] =~ /^\d+$/
ABresult
0X0
100
111
1254 0 ▶ $sh->{current_buffer} ||= $sh->{prev_buffer};
Condition0%0/3
$sh->{"current_buffer"} ||= $sh->{"prev_buffer"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$sh->{"current_buffer"} ||= $sh->{"prev_buffer"}
$sh->{"current_buffer"} $sh->{"prev_buffer"}
Truth table0%0/3
A ||= B
A= $sh->{"current_buffer"}B= $sh->{"prev_buffer"}
ABresult
1X1
011
000
1255
1256 # Find an area to write a temp file into.
1257 my $tmp_dir = $sh->{tmp_dir} ||
1258 $ENV{DBISH_TMP} || # Give people the choice.
1259 $ENV{TMP} || # Is TMP set?
1260 $ENV{TEMP} || # How about TEMP?
1261 $ENV{HOME} || # Look for HOME?
1262 $ENV{HOMEDRIVE} . $ENV{HOMEPATH} || # Last env checked.
1263 0 ▶ "."; # fallback: try to write in current directory.
Condition0%0/17
$sh->{'tmp_dir'} || $ENV{'DBISH_TMP'} || $ENV{'TMP'} || $ENV{'TEMP'} || $ENV{'HOME'} || $ENV{'HOMEDRIVE'} . $ENV{'HOMEPATH'} || '.'
l!l
00
$sh->{"tmp_dir"} or $ENV{"DBISH_TMP"} or $ENV{"TMP"} or $ENV{"TEMP"} or $ENV{"HOME"} or $ENV{"HOMEDRIVE"} . $ENV{"HOMEPATH"}
l!l&&r!l&&!r
000
$sh->{"tmp_dir"} or $ENV{"DBISH_TMP"} or $ENV{"TMP"} or $ENV{"TEMP"} or $ENV{"HOME"}
l!l&&r!l&&!r
000
$sh->{"tmp_dir"} or $ENV{"DBISH_TMP"} or $ENV{"TMP"} or $ENV{"TEMP"}
l!l&&r!l&&!r
000
$sh->{"tmp_dir"} or $ENV{"DBISH_TMP"} or $ENV{"TMP"}
l!l&&r!l&&!r
000
$sh->{"tmp_dir"} or $ENV{"DBISH_TMP"}
l!l&&r!l&&!r
000
MC/DC0%0/6
$sh->{'tmp_dir'} || $ENV{'DBISH_TMP'} || $ENV{'TMP'} || $ENV{'TEMP'} || $ENV{'HOME'} || $ENV{'HOMEDRIVE'} . $ENV{'HOMEPATH'} || '.'
$sh->{"tmp_dir"} $ENV{"DBISH_TMP"} $ENV{"TMP"} $ENV{"TEMP"} $ENV{"HOME"} $ENV{"HOMEDRIVE"} . $ENV{"HOMEPATH"}
Truth table0%0/7
A or B or C or D or E or F
A= $sh->{"tmp_dir"}B= $ENV{"DBISH_TMP"}C= $ENV{"TMP"}D= $ENV{"TEMP"}E= $ENV{"HOME"}F= $ENV{"HOMEDRIVE"} . $ENV{"HOMEPATH"}
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
1264  
1265 0 ▶ my $tmp_file = "$tmp_dir/" . ($sh->{tmp_file} || qq{dbish$$.sql});
Condition0%0/2
$sh->{'tmp_file'} || "dbish${$}.sql"
l!l
00
MC/DC0%0/1
$sh->{'tmp_file'} || "dbish${$}.sql"
$sh->{'tmp_file'}
Truth table0%0/2
A
A= $sh->{'tmp_file'}
Aresult
11
00
1266  
1267 0 ▶ $sh->log( "using tmp file: $tmp_file" ) if $sh->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"debug"} 0 0 0
1268  
1269 0 local (*FH);
1270 0 ▶ open(FH, ">$tmp_file") or
Branch: 1 branch
BranchTrueFalseTotal
open FH, ">$tmp_file" or $sh->err("Can't create ${tmp_file}: $!\n", 1) 0 0 0
1271 $sh->err("Can't create $tmp_file: $!\n", 1);
1272 0 ▶ print FH $sh->{current_buffer} if defined $sh->{current_buffer};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sh->{"current_buffer"} 0 0 0
1273 0 ▶ close(FH) or $sh->err("Can't write $tmp_file: $!\n", 1);
Branch: 1 branch
BranchTrueFalseTotal
close FH or $sh->err("Can't write ${tmp_file}: $!\n", 1) 0 0 0
1274  
1275 0 my $command = "$sh->{editor} $tmp_file";
1276 0 system($command);
1277  
1278 # Read changes back in (editor may have deleted and rewritten file)
1279 0 ▶ open(FH, "<$tmp_file") or $sh->err("Can't open $tmp_file: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open FH, "<$tmp_file" or $sh->err("Can't open ${tmp_file}: $!\n") 0 0 0
1280 0 $sh->{current_buffer} = join "", <FH>;
1281 0 ▶ close(FH) or $sh->err( "Close failed: $tmp_file: $!\n" );
Branch: 1 branch
BranchTrueFalseTotal
close FH or $sh->err("Close failed: ${tmp_file}: $!\n") 0 0 0
1282 0 unlink $tmp_file;
1283  
1284 0 return $sh->run_command('current');
1285 }
1286  
1287  
1288 #
1289 # Load a command/file from disk to the current buffer. Currently this
1290 # overwrites the current buffer with the file loaded. This may change
1291 # in the future.
1292 #
1293 8 ▶sub do_load {
Subroutine do_load: called CC=8 SCAR=26.8
1294 8 my ($sh, $ufile, @args) = @_;
1295  
1296 8 ▶ unless( $ufile ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ufile) 0 8 8
1297 0 $sh->err ( qq{load what file?} );
1298 0 return;
1299 }
1300  
1301 # Load file for from sqlpath.
1302 8 my $file = $sh->look_for_file($ufile);
1303  
1304 8 ▶ unless( $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 8 8
1305 0 $sh->err( qq{Unable to locate file $ufile} );
1306 0 return;
1307 }
1308  
1309 8 ▶ unless( -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 8 8
1310 0 ▶ $file = q{'undef'} unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
1311 0 $sh->err( qq{Can't load $file: $!} );
1312 0 return;
1313 }
1314  
1315 8 ▶ $sh->log("Loading: $ufile : $file") if $sh->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"debug"} 0 8 8
1316 8 local (*FH);
1317 8 ▶ open(FH, "$file") or $sh->err("Can't open $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open FH, "$file" or $sh->err("Can't open ${file}: $!") 8 0 8
1318 8 $sh->{current_buffer} = join "", <FH>;
1319 8 ▶ close(FH) or $sh->err( "close$file failed: $!" );
Branch: 1 branch
BranchTrueFalseTotal
close FH or $sh->err("close$file failed: $!") 8 0 8
1320  
1321 8 return $sh->run_command('current');
1322 }
1323  
1324 1 ▶sub do_save {
Subroutine do_save: called CC=6 SCAR=19.9
1325 1 my ($sh, $file, @args) = @_;
1326  
1327 1 ▶ unless( $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 1 1
1328 0 $sh->err ( qq{save to what file?} );
1329 0 return;
1330 }
1331  
1332 1 ▶ $sh->log("Saving... ") if $sh->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"debug"} 0 1 1
1333 1 local (*FH);
1334 1 ▶ open(FH, "> $file") or $sh->err("Can't open $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open FH, "> $file" or $sh->err("Can't open ${file}: $!") 1 0 1
1335 1 print FH $sh->{current_buffer};
1336 1 ▶ close(FH) or $sh->err( "close$file failed: $!" );
Branch: 1 branch
BranchTrueFalseTotal
close FH or $sh->err("close$file failed: $!") 1 0 1
1337  
1338 1 ▶ $sh->log(" $file") if $sh->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"debug"} 0 1 1
1339 1 return $sh->run_command('current');
1340 }
1341  
1342 #
1343 # run: combines load and go.
1344 #
1345 0 ▶sub do_run {
Subroutine do_run: not called CC=4 SCAR=30.0
1346 0 my ($sh, $file, @args) = @_;
1347 0 ▶ return unless( ! $sh->do_load( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
if $sh->do_load($file) 0 0 0
1348 0 ▶ $sh->log( "running $file" ) if $sh->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"debug"} 0 0 0
1349 0 ▶ $sh->run_command('go') if $sh->{current_buffer};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"current_buffer"} 0 0 0
1350 0 return;
1351 }
1352  
1353 1 ▶sub do_drivers {
Subroutine do_drivers: called CC=2 SCAR=6.9
1354 1 my ($sh, @args) = @_;
1355 1 $sh->log("Available drivers:");
1356 1 my @drivers = DBI->available_drivers;
1357 1 foreach my $driver (sort @drivers) {
1358 7 $sh->log("\t$driver");
1359 }
1360 1 return;
1361 }
1362  
1363  
1364 # $sth = $dbh->column_info( $catalog, $schema, $table, $column );
1365  
1366 0 ▶sub do_col_info {
Subroutine do_col_info: not called CC=3 SCAR=24.8
1367 0 my ($sh, @args) = @_;
1368 0 my $dbh = $sh->{dbh};
1369  
1370 0 ▶ $sh->log( "col_info( " . join( " ", @args ) . ")" ) if $sh->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sh->{"debug"} 0 0 0
1371  
1372 0 my $sth = $dbh->column_info(@args);
1373 0 ▶ unless(ref $sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $sth) 0 0 0
1374 0 $sh->print_buffer_nop ("Driver has not implemented the column_info() method\n");
1375 0 $sth = undef;
1376 0 return;
1377 }
1378 0 return $sh->sth_go($sth, 0, NO_RH);
1379 }
1380  
1381  
1382 1 ▶sub do_type_info {
Subroutine do_type_info: called CC=1 SCAR=0.0
1383 1 my ($sh, @args) = @_;
1384 1 my $dbh = $sh->{dbh};
1385 1 my $ti = $dbh->type_info_all;
1386 1 my $ti_cols = shift @$ti;
1387 1 my @names = sort { $ti_cols->{$a} <=> $ti_cols->{$b} } keys %$ti_cols;
1388 1 my $sth = $sh->prepare_from_data("type_info", $ti, \@names);
1389 1 return $sh->sth_go($sth, 0, NO_RH);
1390 }
1391  
1392 0 ▶sub do_describe {
Subroutine do_describe: not called CC=41 SCAR=74.5
1393 0 my ($sh, $tab, @argv) = @_;
1394 0 my $dbh = $sh->{dbh};
1395  
1396 # Table to describe?
1397 0 ▶ return $sh->print_buffer_nop( "Describe what?\n" ) unless (defined $tab);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tab 0 0 0
1398  
1399 # First attempt the advanced describe using column_info
1400 # $sth = $dbh->column_info( $catalog, $schema, $table, $column );
1401 #$sh->log( "col_info( " . join( " ", @args ) . ")" ) if $sh->{debug};
1402  
1403 # Need to determine which columns to include with the describe command.
1404 # TABLE_CAT,TABLE_SCHEM,TABLE_NAME,COLUMN_NAME,
1405 # DATA_TYPE,TYPE_NAME,COLUMN_SIZE,BUFFER_LENGTH,
1406 # DECIMAL_DIGITS,NUM_PREC_RADIX,NULLABLE,
1407 # REMARKS,COLUMN_DEF,SQL_DATA_TYPE,
1408 # SQL_DATETIME_SUB,CHAR_OCTET_LENGTH,ORDINAL_POSITION,
1409 # IS_NULLABLE
1410 #
1411 # desc_format: partbox
1412 # desc_show_long: 1
1413 # desc_show_remarks: 1
1414  
1415 0 my $schema;
1416 0 ▶ if ($tab =~ /^([^.]+)\.([^.]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tab =~ /^([^.]+)\.([^.]+)$/) 0 0 0
1417 0 $schema = $1;
1418 0 $tab = $2;
1419 }
1420 0 my @names = ();
1421  
1422 # Determine if the short or long display type is used
1423 0 ▶ if (exists $sh->{desc_show_long} and $sh->{desc_show_long} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sh->{'desc_show_long'} and $sh->{'desc_show_long'} == 1) { } 0 0 0
Condition0%0/3
exists $sh->{"desc_show_long"} and $sh->{"desc_show_long"} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
exists $sh->{"desc_show_long"} and $sh->{"desc_show_long"} == 1
exists $sh->{"desc_show_long"} $sh->{"desc_show_long"} == 1
Truth table0%0/3
A and B
A= exists $sh->{"desc_show_long"}B= $sh->{"desc_show_long"} == 1
ABresult
0X0
100
111
1424  
1425 0 ▶ if (exists $sh->{desc_show_columns} and defined
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sh->{'desc_show_columns'} and defined $sh->{'desc_show_columns'}) { } 0 0 0
Condition0%0/3
exists $sh->{"desc_show_columns"} and defined $sh->{"desc_show_columns"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $sh->{"desc_show_columns"} and defined $sh->{"desc_show_columns"}
exists $sh->{"desc_show_columns"} defined $sh->{"desc_show_columns"}
Truth table0%0/3
A and B
A= exists $sh->{"desc_show_columns"}B= defined $sh->{"desc_show_columns"}
ABresult
0X0
100
111
1426 $sh->{desc_show_columns}) {
1427 0 ▶ @names = map { defined $_ ? uc $_ : () } split( /[,\s+]/, $sh->{desc_show_columns});
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
1428 0 ▶ unless (@names) { # List of columns is empty
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 0 0 0
1429 0 $sh->err ( qq{option desc_show_columns contains an empty list, using default} );
1430 # set the empty list to undef
1431 0 $sh->{desc_show_columns} = undef;
1432 0 @names = ();
1433 0 push @names, qw/COLUMN_NAME DATA_TYPE TYPE_NAME COLUMN_SIZE PK
1434 NULLABLE COLUMN_DEF IS_NULLABLE/;
1435 }
1436 } else {
1437 0 push @names, qw/COLUMN_NAME DATA_TYPE TYPE_NAME COLUMN_SIZE PK
1438 NULLABLE COLUMN_DEF IS_NULLABLE/;
1439 }
1440 } else {
1441 0 push @names, qw/COLUMN_NAME TYPE_NAME NULLABLE PK/;
1442 }
1443  
1444 # my @names = qw/COLUMN_NAME DATA_TYPE NULLABLE PK/;
1445 push @names, q{REMARKS}
1446 if (exists $sh->{desc_show_remarks}
1447 and $sh->{desc_show_remarks} == 1
1448 0 ▶ and (not grep { m/REMARK/i } @names));
Branch: 1 branch
BranchTrueFalseTotal
if exists $sh->{"desc_show_remarks"} and $sh->{"desc_show_remarks"} == 1 and not grep({/REMARK/i;} @names) 0 0 0
Condition0%0/6
exists $sh->{"desc_show_remarks"} and $sh->{"desc_show_remarks"} == 1 and not grep({/REMARK/i;} @names)
!ll&&!rl&&r
000
exists $sh->{"desc_show_remarks"} and $sh->{"desc_show_remarks"} == 1
!ll&&!rl&&r
000
MC/DC0%0/3
exists $sh->{"desc_show_remarks"} and $sh->{"desc_show_remarks"} == 1 and not grep({/REMARK/i;} @names)
exists $sh->{"desc_show_remarks"} $sh->{"desc_show_remarks"} == 1 not grep({/REMARK/i;} @names)
Truth table0%0/4
A and B and not(C)
A= exists $sh->{"desc_show_remarks"}B= $sh->{"desc_show_remarks"} == 1C= not grep({/REMARK/i;} @names)
ABCresult
0XX0
10X0
1100
1111
1449  
1450 0 my $sth = $dbh->column_info(undef, $schema, $tab, '%');
1451  
1452 0 ▶ if (ref $sth) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sth) 0 0 0
1453
1454 # Only attempt the primary_key lookup if using the column_info call.
1455  
1456 0 my %pk_cols;
1457 0 eval {
1458 0 my @key_column_names = $dbh->primary_key( undef, undef, $tab );
1459 # Convert the column names to lower case for matching
1460 0 foreach my $idx (0 ..$#key_column_names) {
1461 0 $pk_cols{lc($key_column_names[$idx])} = $idx;
1462 }
1463 };
1464  
1465 0 my @t_data = (); # An array of arrays
1466
1467 0 ▶ while (my $row = $sth->fetchrow_hashref() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
1468  
1469 0 my $col_name = $row->{COLUMN_NAME};
1470 0 my $col_name_lc = lc $col_name;
1471  
1472 # Use the Type name, unless undefined, they use the data type
1473 # value. TODO: Change to resolve the data_type to an ANSI data
1474 # type ... SQL_
1475 0 ▶ my $type = $row->{TYPE_NAME} || $row->{DATA_TYPE};
Condition0%0/3
$row->{'TYPE_NAME'} || $row->{'DATA_TYPE'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$row->{'TYPE_NAME'} || $row->{'DATA_TYPE'}
$row->{'TYPE_NAME'} $row->{'DATA_TYPE'}
Truth table0%0/3
A || B
A= $row->{'TYPE_NAME'}B= $row->{'DATA_TYPE'}
ABresult
1X1
011
000
1476  
1477 0 ▶ if (defined $row->{COLUMN_SIZE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{"COLUMN_SIZE"}) 0 0 0
1478 0 $type .= "(" . $row->{COLUMN_SIZE} . ")";
1479 }
1480 0 ▶ my $is_pk = $pk_cols{$col_name_lc} if exists $pk_cols{$col_name_lc};
Branch: 1 branch
BranchTrueFalseTotal
if exists $pk_cols{$col_name_lc} 0 0 0
1481  
1482 0 my @out_row;
1483 0 foreach my $dcol (@names) {
1484  
1485 # Add primary key
1486 0 ▶ if ($dcol eq q{PK}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dcol eq "PK") 0 0 0
1487 0 ▶ push @out_row, defined $is_pk ? $is_pk : q{};
Branch: 1 branch
BranchTrueFalseTotal
defined $is_pk ? : 0 0 0
1488 0 next;
1489 }
1490 0 ▶ if ($dcol eq q{TYPE_NAME} and
Branch: 1 branch
BranchTrueFalseTotal
if ($dcol eq "TYPE_NAME" and (exists $sh->{"desc_show_long"} and $sh->{"desc_show_long"} == 0)) 0 0 0
Condition0%0/6
$dcol eq "TYPE_NAME" and (exists $sh->{"desc_show_long"} and $sh->{"desc_show_long"} == 0)
!ll&&!rl&&r
000
exists $sh->{"desc_show_long"} and $sh->{"desc_show_long"} == 0
!ll&&!rl&&r
000
MC/DC0%0/3
$dcol eq "TYPE_NAME" and (exists $sh->{"desc_show_long"} and $sh->{"desc_show_long"} == 0)
$dcol eq "TYPE_NAME" exists $sh->{"desc_show_long"} $sh->{"desc_show_long"} == 0
Truth table0%0/4
A and B and C
A= $dcol eq "TYPE_NAME"B= exists $sh->{"desc_show_long"}C= $sh->{"desc_show_long"} == 0
ABCresult
0XX0
10X0
1100
1111
1491 (exists $sh->{desc_show_long} and $sh->{desc_show_long} == 0)) {
1492 0 ▶ my $type = $row->{TYPE_NAME} || $row->{DATA_TYPE};
Condition0%0/3
$row->{'TYPE_NAME'} || $row->{'DATA_TYPE'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$row->{'TYPE_NAME'} || $row->{'DATA_TYPE'}
$row->{'TYPE_NAME'} $row->{'DATA_TYPE'}
Truth table0%0/3
A || B
A= $row->{'TYPE_NAME'}B= $row->{'DATA_TYPE'}
ABresult
1X1
011
000
1493 0 ▶ if (defined $row->{COLUMN_SIZE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{"COLUMN_SIZE"}) 0 0 0
1494 0 $type .= "(" . $row->{COLUMN_SIZE} . ")";
1495 }
1496 0 push @out_row, $type;
1497 0 next;
1498 }
1499  
1500 # Put a blank if not defined.
1501 0 ▶ push @out_row, defined $row->{$dcol} ? $row->{$dcol} : q{};
Branch: 1 branch
BranchTrueFalseTotal
defined $row->{$dcol} ? : 0 0 0
1502  
1503 # push(my @out_row
1504 # , $col_name
1505 # , $type
1506 # , sprintf( "%4s", ($row->{NULLABLE} eq 0 ? q{N}: q{Y}))
1507 # );
1508  
1509 # push @out_row, defined $row->{REMARKS} ? $row->{REMARKS} : q{}
1510 # if (exists $sh->{desc_show_remarks}
1511 # and $sh->{desc_show_remarks} == 1);
1512 }
1513  
1514 0 push @t_data, \@out_row;
1515 }
1516  
1517 0 $sth->finish; # Complete the handler from column_info
1518  
1519  
1520 # Create a new statement handler from the data and names.
1521 0 $sth = $sh->prepare_from_data("describe", \@t_data, \@names);
1522  
1523 # Use the built in formatter to handle data.
1524  
1525 0 ▶ my $mode = exists $sh->{desc_format} ? $sh->{desc_format} : 'partbox';
Branch: 1 branch
BranchTrueFalseTotal
exists $sh->{'desc_format'} ? : 0 0 0
1526 0 my $class = eval { DBI::Format->formatter($mode,1) };
1527 0 ▶ unless ($class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 0 0 0
1528 0 return $sh->alert("Unable to select '$mode': $@");
1529 }
1530  
1531 0 my $display = $class->new($sh);
1532  
1533 0 ▶ $display->header($sth, $sh->{out_fh}||\*STDOUT, $sh->{seperator});
Condition0%0/2
$sh->{'out_fh'} || \*STDOUT
l!l
00
MC/DC0%0/1
$sh->{'out_fh'} || \*STDOUT
$sh->{'out_fh'}
Truth table0%0/2
A
A= $sh->{'out_fh'}
Aresult
11
00
1534  
1535 0 my $i = 0;
1536 OUT_ROWS:
1537 0 ▶ while (my $rowref = $sth->fetchrow_arrayref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rowref = $sth->fetchrow_arrayref) 0 0 0
1538 0 $i++;
1539 0 my $rslt = $display->row($rowref);
1540 }
1541  
1542 0 $display->trailer($i);
1543 }
1544  
1545 #
1546 # This is the old method, if the driver doesn't support the DBI column_info
1547 # meta data.
1548 #
1549 0 ▶ $tab = "$schema.$tab" if defined $schema;
Branch: 1 branch
BranchTrueFalseTotal
if defined $schema 0 0 0
1550 0 my $sql = qq{select * from $tab where 1 = 0};
1551 0 $sth = $dbh->prepare( $sql );
1552 0 $sth->execute;
1553 0 my $cnt = $#{$sth->{NAME}}; #
1554 0 @names = qw{NAME TYPE NULLABLE};
1555 0 my @ti;
1556 0 ▶ for ( my $c = 0; $c <= $cnt; $c++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c <= $cnt) 0 0 0
1557 0 ▶ push( my @j, $sth->{NAME}->[$c] || 0 );
Condition0%0/2
$sth->{'NAME'}[$c] || 0
l!l
00
MC/DC0%0/1
$sth->{'NAME'}[$c] || 0
$sth->{'NAME'}[$c]
Truth table0%0/2
A
A= $sth->{'NAME'}[$c]
Aresult
11
00
1558 0 my $m = $dbh->type_info($sth->{TYPE}->[$c]);
1559 0 my $s;
1560 #print "desc: $c ", $sth->{NAME}->[$c], " ",
1561 #$sth->{TYPE}->[$c], "\n";
1562 0 ▶ if (ref $m eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $m eq 'HASH') { } 0 0 0
elsif (not defined $m) { } 0 0 0
1563 0 $s = $m->{TYPE_NAME}; # . q{ } . $sth->{TYPE}->[$c];
1564 } elsif (not defined $m) {
1565 # $s = q{undef } . $sth->{TYPE}->[$c];
1566 0 $s = $sth->{TYPE}->[$c];
1567 } else {
1568 0 warn "describe: not good. Not good at all!";
1569 }
1570  
1571 0 ▶ if (defined $sth->{PRECISION}->[$c]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sth->{"PRECISION"}[$c]) 0 0 0
1572 0 ▶ $s .= "(" . $sth->{PRECISION}->[$c] || '';
Condition0%0/2
'(' . $sth->{'PRECISION'}[$c] || ''
l!l
00
MC/DC0%0/1
'(' . $sth->{'PRECISION'}[$c] || ''
'(' . $sth->{'PRECISION'}[$c]
Truth table0%0/2
A
A= '(' . $sth->{'PRECISION'}[$c]
Aresult
11
00
1573 $s .= "," . $sth->{SCALE}->[$c]
1574 if ( defined $sth->{SCALE}->[$c]
1575 0 ▶ and $sth->{SCALE}->[$c] ne 0);
Branch: 1 branch
BranchTrueFalseTotal
if defined $sth->{"SCALE"}[$c] and $sth->{"SCALE"}[$c] ne 0 0 0 0
Condition0%0/3
defined $sth->{"SCALE"}[$c] and $sth->{"SCALE"}[$c] ne 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sth->{"SCALE"}[$c] and $sth->{"SCALE"}[$c] ne 0
defined $sth->{"SCALE"}[$c] $sth->{"SCALE"}[$c] ne 0
Truth table0%0/3
A and B
A= defined $sth->{"SCALE"}[$c]B= $sth->{"SCALE"}[$c] ne 0
ABresult
0X0
100
111
1576 0 $s .= ")";
1577 }
1578 push(@j, $s,
1579 0 ▶ $sth->{NULLABLE}->[$c] ne 1? qq{N}: qq{Y} );
Branch: 1 branch
BranchTrueFalseTotal
$sth->{'NULLABLE'}[$c] ne 1 ? : 0 0 0
1580 0 push(@ti,\@j);
1581 }
1582  
1583 0 $sth->finish;
1584 0 $sth = $sh->prepare_from_data("describe", \@ti, \@names);
1585 0 return $sh->sth_go($sth, 0, NO_RH);
1586 }
1587  
1588  
1589 1 ▶sub prepare_from_data {
Subroutine prepare_from_data: called CC=1 SCAR=0.0
1590 1 my ($sh, $statement, $data, $names, %attr) = @_;
1591 1 my $sponge = DBI->connect("dbi:Sponge:","","",{ RaiseError => 1 });
1592 1 my $sth = $sponge->prepare($statement, { rows=>$data, NAME=>$names, %attr });
1593 1 return $sth;
1594 }
1595  
1596  
1597 # Do option: sets or gets an option
1598 48 ▶sub do_option {
Subroutine do_option: called CC=14 SCAR=27.9
1599 48 my ($sh, @args) = @_;
1600  
1601 48 my $value;
1602 48 ▶ unless (@args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 1 47 48
1603 1 foreach my $opt (sort keys %{ $sh->{options}}) {
1604 26 ▶ $value = (defined $sh->{$opt}) ? $sh->{$opt} : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $sh->{$opt} ? : 25 1 26
1605 26 $sh->log(sprintf("%20s: %s", $opt, $value));
1606 }
1607 1 return;
1608 }
1609  
1610 47 my $options = Text::Abbrev::abbrev(keys %{$sh->{options}});
1611  
1612 # Expecting the form [option=value] [option=] [option]
1613 47 foreach my $opt (@args) {
1614 47 my ($opt_name);
1615 47 ($opt_name, $value) = $opt =~ /^\s*(\w+)(?:=(.*))?/;
1616 47 ▶ $opt_name = $options->{$opt_name} || $opt_name if $opt_name;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_name 47 0 47
Condition33%1/3
$options->{$opt_name} || $opt_name
l!l&&r!l&&!r
4700
MC/DC0%0/2
$options->{$opt_name} || $opt_name
$options->{$opt_name} $opt_name
Truth table33%1/3
A || B
A= $options->{$opt_name}B= $opt_name
ABresult
1X1
011
000
1617 47 ▶ if (!$opt_name || !$sh->{options}->{$opt_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt_name and $sh->{"options"}{$opt_name}) 0 47 47
Condition33%1/3
$opt_name and $sh->{"options"}{$opt_name}
!ll&&!rl&&r
0047
MC/DC0%0/2
$opt_name and $sh->{"options"}{$opt_name}
$opt_name $sh->{"options"}{$opt_name}
Truth table33%1/3
A and B
A= $opt_nameB= $sh->{"options"}{$opt_name}
ABresult
0X0
100
111
1618 0 $sh->log("Unknown or ambiguous option name '$opt_name'");
1619 0 next;
1620 }
1621 47 ▶ my $crnt = (defined $sh->{$opt_name}) ? $sh->{$opt_name} : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $sh->{$opt_name} ? : 43 4 47
1622 47 ▶ if (not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value) { } 5 42 47
1623 5 $sh->log("/option $opt_name=$crnt");
1624 5 $value = $crnt;
1625 }
1626 else {
1627 # Need to deal with quoted strings.
1628 # 1 while ( $value =~ s/[^\\]?["']//g ); #"'
1629 $sh->log("/option $opt_name=$value (was $crnt)")
1630 42 ▶ unless $sh->{batch};
Branch: 1 branch
BranchTrueFalseTotal
unless $sh->{"batch"} 0 42 42
1631 42 ▶ $sh->{$opt_name} = ($value eq 'undef' ) ? undef : $value;
Branch: 1 branch
BranchTrueFalseTotal
$value eq 'undef' ? : 0 42 42
1632 }
1633 }
1634 47 ▶return (defined $value ? $value : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 47 0 47
1635 }
1636  
1637 #
1638 # Do prompt: sets or gets a prompt
1639 #
1640 0 ▶sub do_prompt {
Subroutine do_prompt: not called CC=2 SCAR=17.9
1641 0 my ($sh, @args) = @_;
1642  
1643 0 ▶ return $sh->log( $sh->{prompt} ) unless (@args);
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 0 0
1644 0 return $sh->{prompt} = join( '', @args );
1645 }
1646  
1647  
1648 1 ▶sub do_table_info {
Subroutine do_table_info: called CC=3 SCAR=16.5
1649 1 my ($sh, @args) = @_;
1650 1 my $dbh = $sh->{dbh};
1651 1 my $sth = $dbh->table_info(@args);
1652 1 ▶ unless(ref $sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $sth) 0 1 1
1653 0 $sh->log("Driver has not implemented the table_info() method, ",
1654 "trying tables()\n");
1655 0 my @tables = $dbh->tables(@args); # else try list context
1656 0 ▶ unless (@tables) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@tables) 0 0 0
1657 0 $sh->print_buffer_nop ("No tables exist ",
1658 "(or driver hasn't implemented the tables method)\n");
1659 0 return;
1660 }
1661 $sth = $sh->prepare_from_data("tables",
1662 0 [ map { [ $_ ] } @tables ],
1663 [ "TABLE_NAME" ]
1664 );
1665 }
1666 1 return $sh->sth_go($sth, 0, NO_RH);
1667 }
1668  
1669 # Support functions.
1670 3 ▶sub is_spooling ( ) { return shift->{spooling} }
Subroutine is_spooling: called CC=1 SCAR=0.0
1671 1 ▶sub spool_on ( ) { return shift->{spooling} = 1 }
Subroutine spool_on: called CC=1 SCAR=0.0
1672 1 ▶sub spool_off ( ) { return shift->{spooling} = 0 }
Subroutine spool_off: called CC=1 SCAR=0.0
1673  
1674 7 1;
1675 __END__
« blib/lib/DBI/Shell/SQLMinus.pm ↑ Summary blib/lib/DBI/Shell/Completion.pm »