CohortExplorer/Command/Find.pm

filter: Statementstmt 26.5% 17 / 64 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 55.5% 5 / 9 Podpod 100% 4 / 4 totaltotal 22.4% 26 / 116 CC 20 SCAR 55.6
CC
20
Cov
17%
run
380.0
validate
6.0
usage_text
2.0
CRAP 258.6
« CohortExplorer/Command/Query.pm ↑ Summary CohortExplorer/Datasource.pm »
1 package CohortExplorer::Command::Find;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = 0.14;
7  
8 1 ▶use base qw(CLI::Framework::Command);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use CLI::Framework::Exceptions qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Exception::Class::TryCatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 #-------
13 0 ▶sub usage_text {
Subroutine usage_text: not called CC=1 SCAR=6.9
Pod documented
14 0 q{
15 find [--fuzzy|f] [--ignore-case|i] [--and|a] [keyword] : find variables using keywords
16  
17
18 By default, the command returns variables that contain either of the keywords in at least one of their attributes.
19 The user can override this setting by specifying 'and' option.
20  
21 EXAMPLES
22
23 find --fuzzy --ignore-case cancer diabetes mmhg (fuzzy and case insensitive search)
24  
25 find Demographics (exact search)
26  
27 find -fia mmse total (using AND operation with bundling and aliases)
28 };
29 }
30  
31 0 ▶sub option_spec {
Subroutine option_spec: not called CC=1 SCAR=6.9
Pod documented
32 (
33 0 [],
34 [ 'ignore-case|i' => 'ignore case' ],
35 [ 'fuzzy|f' => 'fuzzy search' ],
36 [],
37 [ 'and|a' => 'Join keywords using AND (default OR)' ],
38 []
39 );
40 }
41  
42 0 ▶sub validate {
Subroutine validate: not called CC=2 SCAR=17.9
Pod documented
43 0 my ( $self, $opts, @args ) = @_;
44 0 ▶ if ( @args == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 0) 0 0 0
45 0 throw_cmd_validation_exception(
46 error => 'At least one argument/keyword is required to run this command' );
47 }
48 }
49  
50 0 ▶sub run {
Subroutine run: not called CC=19 SCAR=59.4
Pod documented
51 0 my ( $self, $opts, @args ) = @_;
52 0 my ( $ds, $verbose ) = @{ $self->cache->get('cache') }{qw/datasource verbose/};
53 0 ▶ my $oper = $opts->{ignore_case} ? ( $ds->dialect->ilike || 'ilike' ) : ( $ds->dialect->like || 'like' );
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'ignore_case'} ? : 0 0 0
Condition0%0/4
$ds->dialect->ilike or "ilike"
l!l
00
$ds->dialect->like or "like"
l!l
00
MC/DC0%0/1
$ds->dialect->ilike or "ilike"
$ds->dialect->ilike
MC/DC0%0/1
$ds->dialect->like or "like"
$ds->dialect->like
Truth table0%0/2
A
A= $ds->dialect->ilike
Aresult
11
00
Truth table0%0/2
A
A= $ds->dialect->like
Aresult
11
00
54  
55 # Case insensitive matching
56 0 ▶ @args = map { uc $_ } @args if $opts->{ignore_case};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"ignore_case"} 0 0 0
57  
58 # Fuzzy matching
59 0 ▶ @args = map { '%' . $_ . '%' } @args if $opts->{fuzzy};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"fuzzy"} 0 0 0
60 0 my $struct = $ds->variable_structure;
61 0 my %map = @{ $struct->{-columns} };
62  
63 # Set the condition key
64 0 ▶ my $cond_key = $struct->{-group_by} ? -having : -where;
Branch: 1 branch
BranchTrueFalseTotal
$struct->{'-group_by'} ? : 0 0 0
65  
66 # Get the existing 'OR' within $struct (if any)
67 0 my $OR_cond = $struct->{$cond_key}{-or};
68 0 my @conds;
69  
70 # By default, assume the user is interested in finding variables which contain at least one of the
71 # supplied keywords in at least one of the variable attributes
72 # If there is already an 'OR' within $struct->{$condition_key} then merge two conditions
73 0 for my $val ( values %map ) {
74 0 ▶ push @conds, { $val => [ ( $opts->{'and'} ? '-and' : '-or' ) => map { { $oper => $_ } } @args ] };
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'and'} ? : 0 0 0
75 }
76  
77 0 ▶ $struct->{ $struct->{-group_by} ? -having : -where }{-or} = \@conds;
Branch: 1 branch
BranchTrueFalseTotal
$struct->{'-group_by'} ? : 0 0 0
78  
79 # Do a merger of old and new 'OR'
80 0 ▶ if ($OR_cond) {
Branch: 1 branch
BranchTrueFalseTotal
if ($OR_cond) 0 0 0
81 $struct->{$cond_key}{-or} =
82 $struct->{$cond_key}
83 0 { [ -or => $struct->{$cond_key}{-or}, $OR_cond ] };
84 }
85  
86 # Set table and variable as first two columns
87 0 push my @cols, qw/variable table/;
88 0 for ( keys %map ) {
89 0 ▶ if ( $_ ne 'variable' && $_ ne 'table' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ ne "variable" and $_ ne "table") 0 0 0
Condition0%0/3
$_ ne "variable" and $_ ne "table"
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne "variable" and $_ ne "table"
$_ ne "variable" $_ ne "table"
Truth table0%0/3
A and B
A= $_ ne "variable"B= $_ ne "table"
ABresult
0X0
100
111
90 0 push @cols, $_;
91 }
92 }
93  
94 0 my $dbh = $ds->dbh;
95  
96 # Format column name-SQL pairs as per SQL::Abstract::More
97 0 $struct->{-columns} = [ map { $map{$_} . ' AS ' . $dbh->quote_identifier($_) } @cols ];
98
99 ##----- SQL TO FETCH VARIABLE DATA -----##
100 0 my ( $stmt, @bind, $sth, $rows );
101
102 0 eval { ( $stmt, @bind ) = $ds->sqla->select(%$struct); };
103 0 ▶ if ( catch my $e ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&catch(my $e)) 0 0 0
104 0 throw_cmd_run_exception( error => $e );
105 }
106  
107 0 eval { $rows = $dbh->selectall_arrayref( $stmt, undef, @bind ); };
108 0 ▶ if ( catch my $e ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&catch(my $e)) 0 0 0
109 0 throw_cmd_run_exception( error => $e );
110 }
111
112 0 ▶ if (@$rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rows) { } 0 0 0
113 # Add column names
114 0 unshift @$rows, \@cols;
115 0 ▶ print STDERR
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
116 "\nFound $#$rows variable(s) matching the find query criteria ...\n\n"
117 . "Rendering variable description ...\n\n"
118 if $verbose;
119 return {
120 0 headingText => 'variable description',
121 rows => $rows
122 };
123 }
124 else {
125 0 ▶ print STDERR "\nFound 0 variable(s) matching the find query criteria ...\n\n"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
126 if $verbose;
127 0 return;
128 }
129 }
130  
131 #-------
132 1 1;
133 __END__
« CohortExplorer/Command/Query.pm ↑ Summary CohortExplorer/Datasource.pm »