blib/lib/DBI/Shell/Completion.pm

filter: Statementstmt 13.2% 24 / 181 Branchbran 3.0% 3 / 100 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 38.4% 5 / 13 Podpod 0.0% 0 / 8 totaltotal 10.8% 35 / 322 CC 55 SCAR 52.1
CC
55
Cov
10%
sql_shell_completion
506.0
init
42.4
col_tab_gen
30.0
CRAP 182.9
« blib/lib/DBI/Shell.pm ↑ Summary blib/lib/DBI/Format/SQLMinus.pm »
1 package DBI::Shell::Completion;
2 # vim:ts=4:sw=4:ai:aw:nowrapscan
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 my ($loa, @matches, @tables, @table_list, $tbl_nm, $term, $history);
8  
9  
10 sub init {
11 4 ▶ my ($class, $sh, @args) = @_;
Subroutine init: called CC=9 SCAR=37.5
Pod undocumented
12 4 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
13 4 $loa = {
14 'catalogs' => undef,
15 'commands' => undef,
16 'sql' => [ sort qw(
17 select insert update delete
18 alter grant revoke
19 from where order by desc asc
20 join exists spool
21 set min max avg count
22 into values
23 ) ],
24 'select_func' => [ sort qw(
25 count(*) min max avg as distinct unique
26 ) ],
27 'schemas' => undef,
28 'system' => undef,
29 'tables' => undef,
30 'ntables' => undef, # Maintain a list of columns by table.
31 'sql_keywords' => undef,
32 'users' => undef,
33 'views' => undef,
34 'term' => undef, # Maintain a reference to the term type.
35 'history' => '.dbish_history',
36 'command_prefix' => undef,
37 'columns' => undef,
38 };
39  
40
41  
42 # Modify the history location to use the users home directory, if
43 # available.
44 # TODO: Change this to be less unix more perl
45 $loa->{history} = $sh->{home_dir} . '/' . $loa->{history}
46 4 ▶ if (exists $sh->{home_dir} and defined $sh->{home_dir});
Branch: 1 branch
BranchTrueFalseTotal
if exists $sh->{"home_dir"} and defined $sh->{"home_dir"} 4 0 4
Condition33%1/3
exists $sh->{"home_dir"} and defined $sh->{"home_dir"}
!ll&&!rl&&r
004
MC/DC0%0/2
exists $sh->{"home_dir"} and defined $sh->{"home_dir"}
exists $sh->{"home_dir"} defined $sh->{"home_dir"}
Truth table33%1/3
A and B
A= exists $sh->{"home_dir"}B= defined $sh->{"home_dir"}
ABresult
0X0
100
111
47  
48 4 $sh->log( "commandline history written to $loa->{history}" );
49  
50 4 my $pi = bless $loa, $class;
51  
52 # return if term is not defined.
53 4 ▶ return unless $sh->{term};
Branch: 1 branch
BranchTrueFalseTotal
unless $sh->{"term"} 4 0 4
54  
55 0 $term = $sh->{term};
56  
57 0 my $attribs = $term->Attribs();
58 0 $attribs->{history_length} = '500';
59  
60 0 $pi->{term} = \$sh->{term};
61 0 $pi->{dbh} = \$sh->{dbh};
62 0 $pi->{command_prefix} = \$sh->{command_prefix};
63  
64 0 ▶ if ($term->ReadLine eq "Term::ReadLine::Gnu") {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->ReadLine eq "Term::ReadLine::Gnu") 0 0 0
65 0 print "Using Term::ReadLine::Gnu\n";
66  
67 # Only source the current drivers Completion, if exists.
68 0 $sh->{completion} = $pi;
69  
70 # Define the completion function.
71 my $ssc = sub {
72 0 ▶ return $pi->sql_shell_completion(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 };
74 0 $attribs->{attempted_completion_function} = $ssc;
75  
76 # read in the history file.
77 0 ▶ if(-e $pi->{history}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $pi->{'history'}) { } 0 0 0
78 $sh->log ("History file $pi->{history} not restored!" )
79 0 ▶ unless($term->ReadHistory($pi->{history}));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->ReadHistory($pi->{"history"}) 0 0 0
80 } else {
81 0 print "Creating ${history} to store your command line history\n";
82 0 ▶ open(HISTORY, "> $pi->{history}")
Branch: 1 branch
BranchTrueFalseTotal
open HISTORY, qq[> $pi->{"history"}] or $sh->log(qq[Could not create $pi->{"history"}: $!]) 0 0 0
83 or $sh->log ("Could not create $pi->{history}: $!");
84 0 close(HISTORY);
85 }
86  
87 }
88  
89 0 return $pi;
90 }
91  
92 # sub load_completion {
93 # my $cpi = shift;
94 # my $sh = shift;
95 # my @pi;
96 # foreach my $where (qw(DBI/Shell/Completion DBI_Shell_Completion)) {
97 # my $mod = $where; $mod =~ s!/!::!g; #/ so vim see the syn correctly
98 # my @dir = map { -d "$_/$where" ? ("$_/$where") : () } @INC;
99 # foreach my $dir (@dir) {
100 # opendir DIR, $dir or warn "Unable to read $dir: $!\n";
101 # push @pi, map { s/\.pm$//; "${mod}::$_" } grep { /\.pm$/ }
102 # readdir DIR;
103 # closedir DIR;
104 # }
105 # }
106 # my $driver = $sh->{data_source};
107 # # print STDERR join( " ", @pi, $driver, "\n");
108 # foreach my $pi (sort @pi) {
109 # #local $DBI::Shell::SHELL = $sh; # publish the current shell
110 # eval qq{ use $pi };
111 # $sh->alert("Unable to load $pi: $@") if $@;
112 # }
113 # # plug-ins should remove options they recognise from (localized) @ARGV
114 # # by calling Getopt::Long::GetOptions (which is already in pass_through mode).
115 # foreach my $pi (@pi) {
116 # #local *ARGV = $sh->{unhandled_options};
117 # $pi->init($sh);
118 # }
119 # }
120  
121 sub populate {
122 0 ▶ my $sh = shift;
Subroutine populate: not called CC=4 SCAR=30.0
Pod undocumented
123 0 my $list = shift;
124  
125 0 ▶ return $loa unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
126 0 ▶ return undef unless exists $loa->{$list};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $loa->{$list} 0 0 0
127  
128 # print ( "$list populate ...", join " ", @_, "\n" );
129  
130 0 ▶ if (@_) { # User provided a list of values.
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
131 0 $loa->{$list} = [ @_ ];
132 }
133 0 return $loa->{$list};
134 }
135  
136 # Attempt to complete on the contents of TEXT. START and END bound
137 # the region of rl_line_buffer that contains the word to complete.
138 # TEXT is the word to complete. We can use the entire contents of
139 # rl_line_buffer in case we want to do some simple parsing. Return
140 # the array of matches, or NULL if there aren't any.
141 sub sql_shell_completion {
142 0 ▶ my $sh = shift;
Subroutine sql_shell_completion: not called CC=22 SCAR=62.3
Pod undocumented
143 0 my ($text, $line, $start, $end) = @_;
144  
145 0 my @matches = ();
146  
147 0 undef $tbl_nm;
148  
149 # Notes for future development. The $line is the complete line,
150 # start is where the text begins, end where text ends (looks like word
151 # boundies). I need to attempt to determine where I'm in the line, and
152 # what was the last key word given.
153  
154 # print STDERR "text:$text: line:$line: start:$start: end:$end:\n";
155 0 my $cmd_p = ${$sh->{command_prefix}};
156  
157 # Load the keywords.
158 0 ▶ unless (defined $loa->{sql_keywords}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $loa->{"sql_keywords"}) 0 0 0
159 0 eval {
160 # Not all drivers support the get_info function yet, so we
161 # need a fall back plan.
162 0 my $key_words = ${$sh->{dbh}}->get_info( 'SQL_KEYWORDS' );
163 0 ▶ die unless (defined $key_words);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key_words 0 0 0
164 0 my @key_words = split( /\s+/, $key_words);
165 0 ▶ die unless (@key_words); # Keywords not supported by driver, default
Branch: 1 branch
BranchTrueFalseTotal
unless @key_words 0 0 0
166 $sh->populate( q{sql_keywords}, @key_words )
167 0 ▶ unless (defined $loa->{sql_keywords});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $loa->{"sql_keywords"} 0 0 0
168 };
169  
170 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
171 0 $sh->populate( q{sql_keywords}, @{$sh->{sql}} );
172 }
173 }
174  
175 0 ▶ unless (defined $loa->{columns}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $loa->{"columns"}) 0 0 0
176 0 eval {
177 0 my $sth = ${$sh->{dbh}}->column_info( undef, undef, undef, undef );
178 0 ▶ die unless $sth; # column_info not supported by all drivers.
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 0 0 0
179 0 my (%catalogs, %schemas, %tables, %columns);
180 0 ▶ while ( my $row = $sth->fetchrow_arrayref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 0 0 0
181 0 ▶ $catalogs{$row->[0]}++ if defined $row->[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $row->[0] 0 0 0
182 0 ▶ $schemas{$row->[1]}++ if defined $row->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $row->[1] 0 0 0
183 0 ▶ $tables{$row->[2]}++ if defined $row->[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $row->[2] 0 0 0
184 0 ▶ $columns{$row->[3]}++ if defined $row->[3];
Branch: 1 branch
BranchTrueFalseTotal
if defined $row->[3] 0 0 0
185  
186 0 push ( @{$loa->{ntables}->{$row->[2]}}, $row->[3] );
187 }
188 0 push( @{$loa->{catalogs}}, sort keys %catalogs );
189 0 push( @{$loa->{schemas}}, sort keys %schemas );
190 0 push( @{$loa->{columns}}, sort keys %columns );
191 };
192  
193 0 push( @{$loa->{columns}}, @{$sh->{select_func}} );
194 }
195  
196 # print "line: $line - $cmd_p\n" if $line;
197 # Begin by loading all the key words, if available.
198 0 ▶ if ( $start == 0 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($start == 0) { } 0 0 0
elsif ($line =~ / \bfrom(?:\s*)?(?:['"])?$ | \bfrom(?:\s*)(?:['"])?(?:[\w.]+) | \binsert\s+into(?:\s+)?$ | \binsert\s+into\s+(?:['"])?(?:\w+|[\w+.]|\w+\.\w+)$ | \bupdate(?:\s*)?(?:['"])?(?:\w+)?$ | ^${cmd_p}desc(?:\s*)?(?:['"])?(?:\w+)? /xi) { } 0 0 0
elsif ($line =~ /select\s+?$|select\s+\w+?$/i) { } 0 0 0
elsif ($line =~ / ^insert\s+ into\s+ ((?:\w+|\w+\.\w+))\s+?\( # ) /xi) { } 0 0 0
199 # SQL_KEYWORDS
200 @matches =
201 0 ${$sh->{term}}->completion_matches($text,
202 \&sql_keywords_gen);
203 }
204 # If the last word is "from" attempt to match a schema or table name.
205 elsif(
206 $line=~ m/
207 \bfrom(?:\s*)?(?:['"])?$
208 |
209 \bfrom(?:\s*)(?:['"])?(?:[\w.]+)
210 |
211 \binsert\s+into(?:\s+)?$
212 |
213 \binsert\s+into\s+(?:['"])?(?:\w+|[\w+.]|\w+\.\w+)$
214 |
215 \bupdate(?:\s*)?(?:['"])?(?:\w+)?$
216 |
217 ^${cmd_p}desc(?:\s*)?(?:['"])?(?:\w+)?
218 /xi
219 ) {
220 $sh->populate(q{tables},
221 0 ▶ ${$sh->{dbh}}->tables) unless($loa->{tables});
Branch: 1 branch
BranchTrueFalseTotal
unless $loa->{"tables"} 0 0 0
222 0 @matches = ${$sh->{term}}->completion_matches($text, \&table_generator);
223 # |
224 # ^${cmd_p}desc(?:\s+)(?:['"])?\w+?$
225 }
226 # If we find a select on the line display a column list.
227 elsif( $line=~ m/select\s+?$|select\s+\w+?$/i ) {
228 0 @matches = ${$sh->{term}}->completion_matches($text,
229 \&column_generator);
230 }
231 elsif( $line=~ m/
232 ^insert\s+
233 into\s+
234 ((?:\w+|\w+\.\w+))\s+?\( # )
235 /xi ) {
236 0 $tbl_nm = $1;
237 0 ▶ unless( exists $loa->{ntables}->{$tbl_nm} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $loa->{"ntables"}{$tbl_nm}) 0 0 0
238 0 eval {
239 0 my $sth = ${$sh->{dbh}}->column_info( undef, undef, $tbl_nm, undef );
240 0 ▶ die unless $sth; # column_info not supported by all drivers.
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 0 0 0
241 0 push( @{$loa->{ntables}->{$tbl_nm}},
242 0 @{$sth->fetchall_arrayref( [3] )} );
243
244 };
245  
246 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
247 # Column Info not supported, do it the hard way.
248 {
249 0 local (${$sh->{dbh}}->{PrintError},
250 0 ${$sh->{dbh}}->{RaiseError});
251 0 ${$sh->{dbh}}->{PrintError} = 0;
252 0 ${$sh->{dbh}}->{RaiseError} = 0;
253 0 my $sth = ${$sh->{dbh}}->prepare( qq{select * from $tbl_nm where 1 = 2} );
254 0 $sth->execute;
255  
256 0 ▶ unless($sth->err) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->err) 0 0 0
257 0 push( @{$loa->{ntables}->{$tbl_nm}}, @{$sth->{NAME}} );
258 }
259 0 $sth->finish;
260 }
261 }
262  
263 }
264  
265 0 @matches = ${$sh->{term}}->completion_matches($text,
266 \&col_tab_gen );
267 }
268 else {
269 # match commands for now.
270 @matches =
271 0 ${$sh->{term}}->completion_matches($text, \&sql_keywords_gen);
272 }
273  
274 0 return @matches;
275 }
276  
277 # Generator function for command completion. STATE lets us know
278 # whether to start from scratch; without any state (i.e. STATE == 0),
279 # then we start at the top of the list.
280  
281 ## Term::ReadLine::Gnu has list_completion_function similar with this
282 ## function. I defined new one to be compared with original C version.
283 {
284 4 my $list_index;
285 4 my (@name, @columns, @tables);
286  
287 sub column_generator {
288 0 ▶ my ($text, $state) = @_;
Subroutine column_generator: not called CC=4 SCAR=30.0
Pod undocumented
289  
290 # If this is a new word to complete, initialize now. This
291 # includes saving the length of TEXT for efficiency, and
292 # initializing the index variable to 0.
293 0 ▶ unless ($state) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state) 0 0 0
294 0 $list_index = 0;
295 0 @columns = @{$loa->{columns}};
296 }
297  
298 # Return the next name which partially matches from the
299 # command list.
300 0 ▶ while ($list_index <= $#columns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_index <= $#columns) 0 0 0
301 0 $list_index++;
302 0 ▶ return $columns[$list_index - 1]
Branch: 1 branch
BranchTrueFalseTotal
if $columns[$list_index - 1] =~ /^$text/i 0 0 0
303 if ($columns[$list_index - 1] =~ /^$text/i);
304 }
305 # If no names matched, then return NULL.
306 0 return undef;
307 }
308  
309 sub col_tab_gen {
310 0 ▶ my ($text, $state) = @_;
Subroutine col_tab_gen: not called CC=5 SCAR=34.0
Pod undocumented
311  
312 # Just return undef for now.
313  
314 # If this is a new word to complete, initialize now. This
315 # includes saving the length of TEXT for efficiency, and
316 # initializing the index variable to 0.
317 0 ▶ unless ($state) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state) 0 0 0
318 0 $list_index = 0;
319 0 ▶ if (exists $loa->{ntables}->{$tbl_nm}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $loa->{'ntables'}{$tbl_nm}) { } 0 0 0
320 0 @columns = @{$loa->{ntables}->{$tbl_nm}};
321 }
322 else {
323 0 @columns = @{$loa->{columns}};
324 }
325 }
326  
327 # Return the next name which partially matches from the
328 # command list.
329 0 ▶ while ($list_index <= $#columns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_index <= $#columns) 0 0 0
330 0 $list_index++;
331 0 ▶ return $columns[$list_index - 1]
Branch: 1 branch
BranchTrueFalseTotal
if $columns[$list_index - 1] =~ /^$text/i 0 0 0
332 if ($columns[$list_index - 1] =~ /^$text/i);
333 }
334 # If no names matched, then return NULL.
335 0 return undef;
336 }
337  
338 sub sql_generator {
339 0 ▶ my ($text, $state) = @_;
Subroutine sql_generator: not called CC=4 SCAR=30.0
Pod undocumented
340  
341 # If this is a new word to complete, initialize now. This
342 # includes saving the length of TEXT for efficiency, and
343 # initializing the index variable to 0.
344 0 ▶ unless ($state) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state) 0 0 0
345 0 $list_index = 0;
346 0 @name = @{$loa->{sql}};
347 }
348  
349 # Return the next name which partially matches from the
350 # command list.
351 0 ▶ while ($list_index <= $#name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_index <= $#name) 0 0 0
352 0 $list_index++;
353 0 ▶ return $name[$list_index - 1]
Branch: 1 branch
BranchTrueFalseTotal
if $name[$list_index - 1] =~ /^$text/i 0 0 0
354 if ($name[$list_index - 1] =~ /^$text/i);
355 }
356 # If no names matched, then return NULL.
357 0 return undef;
358 }
359  
360 sub sql_keywords_gen {
361 0 ▶ my ($text, $state) = @_;
Subroutine sql_keywords_gen: not called CC=4 SCAR=30.0
Pod undocumented
362  
363 # If this is a new word to complete, initialize now. This
364 # includes saving the length of TEXT for efficiency, and
365 # initializing the index variable to 0.
366 0 ▶ unless ($state) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state) 0 0 0
367 0 $list_index = 0;
368 0 @name = @{$loa->{sql_keywords}};
369 }
370  
371 # Return the next name which partially matches from the
372 # command list.
373 0 ▶ while ($list_index <= $#name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_index <= $#name) 0 0 0
374 0 $list_index++;
375 0 ▶ return $name[$list_index - 1]
Branch: 1 branch
BranchTrueFalseTotal
if $name[$list_index - 1] =~ /^$text/i 0 0 0
376 if ($name[$list_index - 1] =~ /^$text/i);
377 }
378 # If no names matched, then return NULL.
379 0 return undef;
380 }
381 }
382  
383 {
384 4 my $list_index;
385  
386 sub table_generator {
387 0 ▶ my ($text, $state) = @_;
Subroutine table_generator: not called CC=4 SCAR=30.0
Pod undocumented
388  
389 # If this is a new table to complete, initialize now. This
390 # includes saving the length of TEXT for efficiency, and
391 # initializing the index variable to 0.
392 0 ▶ unless ($state) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state) 0 0 0
393 0 $list_index = 0;
394 0 @tables = @{$loa->{tables}};
395 }
396  
397 # Return the next name which partially matches from the
398 # command list.
399 0 ▶ while ($list_index <= $#tables) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_index <= $#tables) 0 0 0
400 0 $list_index++;
401 0 ▶ return $tables[$list_index - 1]
Branch: 1 branch
BranchTrueFalseTotal
if $tables[$list_index - 1] =~ /^$text/i 0 0 0
402 if ($tables[$list_index - 1] =~ /^$text/i);
403 }
404 # If no names matched, then return NULL.
405 0 return undef;
406 }
407 }
408  
409 4 DESTROY {
410 4 ▶ my $sh = shift;
Subroutine DESTROY: called CC=7 SCAR=32.9
411 # term is store as a package variable.
412 4 ▶ if ($term && $term->ReadLine eq "Term::ReadLine::Gnu") {
Branch: 1 branch
BranchTrueFalseTotal
if ($term and $term->ReadLine eq "Term::ReadLine::Gnu") 0 4 4
Condition33%1/3
$term and $term->ReadLine eq "Term::ReadLine::Gnu"
!ll&&!rl&&r
400
MC/DC0%0/2
$term and $term->ReadLine eq "Term::ReadLine::Gnu"
$term $term->ReadLine eq "Term::ReadLine::Gnu"
Truth table33%1/3
A and B
A= $termB= $term->ReadLine eq "Term::ReadLine::Gnu"
ABresult
0X0
100
111
413 0 ▶ if($term && $term->history_total_bytes()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term and $term->history_total_bytes) 0 0 0
Condition0%0/3
$term and $term->history_total_bytes
!ll&&!rl&&r
000
MC/DC0%0/2
$term and $term->history_total_bytes
$term $term->history_total_bytes
Truth table0%0/3
A and B
A= $termB= $term->history_total_bytes
ABresult
0X0
100
111
414 0 my $history = $sh->{completion}->{history};
415 0 ▶ if ($history) {
Branch: 1 branch
BranchTrueFalseTotal
if ($history) 0 0 0
416 0 ▶ unless($term->WriteHistory($history)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($term->WriteHistory($history)) 0 0 0
417 0 carp ("Could not write history file $history to history_file}. ");
418 }
419 }
420 }
421 }
422  
423 4 $term = undef; $sh->{term} = undef;
424 }
425  
426 ▶END { }
Subroutine END: called CC=1 SCAR=0.0
427  
428 4 1;
429 __END__
« blib/lib/DBI/Shell.pm ↑ Summary blib/lib/DBI/Format/SQLMinus.pm »