blib/lib/Text/ANSITable/SQLStyleSheet.pm

filter: Statementstmt 93.1% 95 / 102 Branchbran 68.7% 22 / 32 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 15 / 15 Podpod 100% 1 / 1 totaltotal 86.4% 134 / 155 CC 21 SCAR 14.1
CC
21
Cov
86%
_sqlite_type_from_sth_ix
7.6
from_sth
6.0
_column_style
5.2
CRAP 4.1
↑ Summary
1 package Text::ANSITable::SQLStyleSheet;
2 4 ▶use 5.018000;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Text::ANSITable;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use DBD::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $VERSION = '0.05';
12  
13 4 our $Json = JSON->new;
14  
15 2 ▶sub _sqlite_type_from_sth_ix {
Subroutine _sqlite_type_from_sth_ix: called CC=5 SCAR=20.3
16  
17 2 my ($dbh, $sth, $ix) = @_;
18  
19 2 my $sth_type = $sth->{TYPE}[ $ix ];
20  
21 # NOTE: It seems that as of 2018-10-06 DBD::SQLite returns the
22 # wrong kind of value for TYPE. It also does not implement the
23 # type_info method.
24 #
25 # Worse, for something like `SELECT 1, TYPEOF(1)` SQLite itself
26 # considers the first to be an `integer`, but DBD::SQLite returns
27 # `VARCHAR` in $sth->{TYPE}.
28 #
29 # Reported in <https://github.com/DBD-SQLite/DBD-SQLite/issues/36>.
30 #
31 # Other drivers may have similar issues, so this is best-effort
32 # guesswork.
33 #
34 # SQLite will do further guesswork on the type we pass to it, per
35 # <https://www.sqlite.org/datatype3.html>.
36  
37 2 ▶ if (defined $sth_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sth_type) 2 0 2
38  
39 2 my $type_info = $dbh->type_info($sth_type);
40  
41 2 ▶ if ( $type_info and exists $type_info->{TYPE_NAME} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type_info and exists $type_info->{'TYPE_NAME'}) { } 2 0 2
elsif (not $sth_type =~ /^\d+$/u) { } 0 0 0
Condition33%1/3
$type_info and exists $type_info->{"TYPE_NAME"}
!ll&&!rl&&r
002
MC/DC0%0/2
$type_info and exists $type_info->{"TYPE_NAME"}
$type_info exists $type_info->{"TYPE_NAME"}
Truth table33%1/3
A and B
A= $type_infoB= exists $type_info->{"TYPE_NAME"}
ABresult
0X0
100
111
42  
43 2 return $type_info->{TYPE_NAME};
44  
45 } elsif ($sth_type !~ /^\d+$/) {
46  
47 0 return $sth_type;
48  
49 }
50  
51 }
52  
53 0 return 'TEXT';
54  
55 }
56  
57 2 ▶sub _from_sth_sql {
Subroutine _from_sth_sql: called CC=3 SCAR=11.0
58  
59 2 my ($class, $sth, $sql_or_code) = @_;
60  
61 2 my $dbh = DBI->connect('dbi:SQLite:dbname=:memory:');
62  
63 2 local $dbh->{sqlite_allow_multiple_statements} = 1;
64  
65 2 $dbh->do(q{
66 PRAGMA synchronous = OFF;
67 PRAGMA journal_mode = OFF;
68 PRAGMA locking_mode = EXCLUSIVE;
69 });
70  
71 my $fields = join(',', map {
72 sprintf(
73 'CAST(NULL AS %s) AS %s',
74 _sqlite_type_from_sth_ix($dbh, $sth, $_),
75 2 $dbh->quote_identifier(undef, undef, $sth->{NAME}[$_]))
76 2 } 0 .. @{ $sth->{NAME} } - 1);
77  
78 2 my $create_sth = $dbh->prepare(sprintf(q{
79 CREATE TABLE data AS SELECT %s LIMIT 0
80 }, $fields));
81  
82 2 $create_sth->execute();
83  
84 my $insert_sth = $dbh->prepare(sprintf(q{
85 INSERT INTO data VALUES(%s)
86 2 }, join(',', map { '?' } 0 .. @{ $sth->{NAME} } - 1)));
87  
88 2 $dbh->begin_work;
89  
90 2 ▶ while (my $row = $sth->fetchrow_arrayref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 20 2 22
91 20 $insert_sth->execute(@$row);
92 }
93  
94 2 $dbh->commit;
95  
96 # $dbh->sqlite_backup_to_file('DEBUG.sqlite');
97  
98 2 ▶ if ( 'CODE' eq ref( $sql_or_code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('CODE' eq ref $sql_or_code) { } 1 1 2
99  
100 1 return $class->from_sth( $sql_or_code->($dbh) );
101  
102 } else {
103  
104 1 my $final_sth = $dbh->prepare( $sql_or_code );
105 1 $final_sth->execute();
106 1 return $class->from_sth( $final_sth );
107  
108 }
109  
110 }
111  
112 4 ▶sub from_sth {
Subroutine from_sth: called CC=6 SCAR=17.9
Pod documented
113  
114 4 my ($class, $sth, $sql_or_code) = @_;
115  
116 4 ▶ return _from_sth_sql(@_) if defined $sql_or_code;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sql_or_code 2 2 4
117  
118 2 my %col2ix;
119 $col2ix{$_} = keys %col2ix
120 2 for @{ $sth->{NAME} };
121  
122 # filter out __*_style columns
123 0 my @wanted = sort { $a <=> $b } map {
124 2 ▶ /^__(\w+)_style$/ ? () : $col2ix{$_}
Branch: 1 branch
BranchTrueFalseTotal
/^__(\w+)_style$/u ? : 4 2 6
125 } keys %col2ix;
126  
127 2 my $t = Text::ANSITable->new;
128  
129 2 $t->columns([ map { $sth->{NAME}[$_] } @wanted ]);
130  
131 2 my $nth = 0;
132
133 2 ▶ while (my $row = $sth->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 20 2 22
134  
135 20 $t->add_row([ @{ $row }[ @wanted ] ]);
136
137 20 ▶ if ( $nth == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nth == 0) 2 18 20
138 2 _column_style( $t, $row, \%col2ix, $col2ix{__column_style} );
139 }
140
141 20 _row_style( $t, $nth, $row, \%col2ix, $col2ix{__row_style} );
142 20 _cell_styles( $t, $nth, $row, \%col2ix, $col2ix{__cell_style} );
143
144 20 $nth += 1;
145  
146 }
147
148 2 return $t;
149  
150 }
151  
152 42 ▶sub _get_hashref {
Subroutine _get_hashref: called CC=4 SCAR=14.0
153 42 my ($row, $ix) = @_;
154  
155 42 ▶ return unless defined $ix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ix 2 40 42
156 40 ▶ return unless defined $row->[ $ix ];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row->[$ix] 0 40 40
157  
158 40 my $h = $Json->decode( $row->[ $ix ] );
159  
160 40 ▶ return unless 'HASH' eq ref( $h );
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $h 0 40 40
161  
162 40 delete @$h{ grep !defined($h->{$_}), keys %$h };
163
164 40 return $h;
165  
166 }
167  
168 2 ▶sub _column_style {
Subroutine _column_style: called CC=3 SCAR=16.5
169
170 2 my ($t, $row, $col2ix, $ix) = @_;
171  
172 2 ▶ return unless my $style = _get_hashref( $row, $ix );
Branch: 1 branch
BranchTrueFalseTotal
unless my $style = _get_hashref($row, $ix) 2 0 2
173  
174 0 for my $column ( grep { exists $col2ix->{$_} } keys %$style ) {
175 0 $t->set_column_style( $column, %{ $style->{ $column } } );
176 }
177  
178 }
179  
180 20 ▶sub _row_style {
Subroutine _row_style: called CC=2 SCAR=7.1
181
182 20 my ($t, $nth_row, $row, $col2ix, $ix) = @_;
183  
184 20 ▶ return unless my $style = _get_hashref( $row, $ix );
Branch: 1 branch
BranchTrueFalseTotal
unless my $style = _get_hashref($row, $ix) 0 20 20
185  
186 20 $t->set_row_style( $nth_row, %$style );
187  
188 }
189  
190 20 ▶sub _cell_styles {
Subroutine _cell_styles: called CC=4 SCAR=14.0
191
192 20 my ($t, $nth_row, $row, $col2ix, $ix) = @_;
193  
194 20 ▶ return unless my $style = _get_hashref( $row, $ix );
Branch: 1 branch
BranchTrueFalseTotal
unless my $style = _get_hashref($row, $ix) 0 20 20
195  
196 20 for my $column ( grep { exists $col2ix->{$_} } keys %$style ) {
197  
198 20 my %cell_style = %{ $style->{ $column } };
199  
200 # override cell contents if pseudo-style `text` is specified
201 20 ▶ if ( exists $cell_style{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cell_style{"value"}) 20 0 20
202 20 $t->set_cell( $nth_row, $column, $cell_style{value} );
203 20 delete $cell_style{value};
204 }
205  
206 $t->set_cell_style(
207 20 $nth_row,
208 $column,
209 %cell_style
210 );
211  
212 }
213  
214 }
215  
216 4 1;
217  
218 __END__
↑ Summary