blib/lib/DBI/Shell.pm

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