ClickHouse/Introspect/Columns.pm

filter: Statementstmt 94.5% 35 / 37 Branchbran 93.7% 15 / 16 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 87.1% 61 / 70 CC 14 SCAR 21.8
CC
14
Cov
90%
fetch
12.3
_nonempty
3.0
BEGIN
1.0
CRAP 8.8
« ClickHouse/Test.pm ↑ Summary ClickHouse/Storage.pm »
1 package DBIO::ClickHouse::Introspect::Columns;
2 # ABSTRACT: Introspect ClickHouse columns via system.columns
3  
4 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 our $VERSION = '0.900007';
8  
9 34 ▶use DBIO::ClickHouse::Introspect::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12  
13 2 ▶sub fetch {
Subroutine fetch: called CC=12 SCAR=25.1
Pod documented
14 2 my ($class, $dbh, $tables, $database) = @_;
15  
16 2 my $sql = q{
17 SELECT
18 table,
19 name,
20 type,
21 position,
22 default_kind,
23 default_expression,
24 compression_codec,
25 comment,
26 is_in_sorting_key,
27 is_in_partition_key
28 FROM system.columns
29 WHERE database = %s
30 ORDER BY table, position
31 };
32 2 my @bind;
33 2 ▶ if (defined $database) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $database) { } 0 2 2
34 0 $sql = sprintf $sql, '?';
35 0 push @bind, $database;
36 }
37 else {
38 2 $sql = sprintf $sql, 'currentDatabase()';
39 }
40  
41 2 my $sth = $dbh->prepare($sql);
42 2 $sth->execute(@bind);
43  
44 # column name => 1-based key position, per table
45 2 my %pk_position;
46 2 for my $name (keys %$tables) {
47 4 ▶ my $key = $tables->{$name}{primary_key_columns} || [];
Condition50%1/2
$tables->{$name}{'primary_key_columns'} || []
l!l
40
MC/DC0%0/1
$tables->{$name}{'primary_key_columns'} || []
$tables->{$name}{'primary_key_columns'}
Truth table50%1/2
A
A= $tables->{$name}{'primary_key_columns'}
Aresult
11
00
48 4 $pk_position{$name}{ $key->[$_] } = $_ + 1 for 0 .. $#$key;
49 }
50  
51 2 my %columns;
52 2 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 8 2 10
53 8 ▶ my $table = $row->{table} // '';
Condition50%1/2
$row->{"table"} // ""
l!l
80
MC/DC0%0/1
$row->{"table"} // ""
$row->{"table"}
Truth table50%1/2
A
A= $row->{"table"}
Aresult
11
00
54 8 ▶ next unless exists $tables->{$table};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tables->{$table} 2 6 8
55  
56 6 my $name = $row->{name};
57 6 my $type = $row->{type};
58 6 my $position = $pk_position{$table}{$name};
59  
60 6 ▶ push @{ $columns{$table} }, {
Branch: 4 branches
BranchTrueFalseTotal
'DBIO::ClickHouse::Introspect::Util'->normalize_type($type)->{'is_nullable'} ? : 2 4 6
$position ? : 2 4 6
$row->{'is_in_sorting_key'} ? : 4 2 6
$row->{'is_in_partition_key'} ? : 2 4 6
61 column_name => $name,
62 data_type => $type,
63 not_null =>
64 DBIO::ClickHouse::Introspect::Util->normalize_type($type)->{is_nullable} ? 0 : 1,
65 default_kind => _nonempty($row->{default_kind}),
66 # The raw ClickHouse expression. DBIO::ClickHouse::Introspect turns it
67 # into a portable default_value / literal ref at contract time.
68 default_value => _nonempty($row->{default_expression}),
69 is_pk => $position ? 1 : 0,
70 pk_position => $position,
71 is_in_sorting_key => $row->{is_in_sorting_key} ? 1 : 0,
72 is_in_partition_key => $row->{is_in_partition_key} ? 1 : 0,
73 position => $row->{position},
74 comment => _nonempty($row->{comment}),
75 6 compression_codec => _nonempty($row->{compression_codec}),
76 };
77 }
78  
79 2 return \%columns;
80 }
81  
82 # ClickHouse returns '' (not NULL) for "absent" in these String columns, so an
83 # empty string has to be folded back to undef or every column would look like
84 # it carried a default, a comment and a codec.
85 24 ▶sub _nonempty {
Subroutine _nonempty: called CC=3 SCAR=11.0
86 24 my ($value) = @_;
87 24 ▶ return undef unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 16 8 24
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
0168
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
88 8 return $value;
89 }
90  
91  
92 34 1;
93  
94 __END__
« ClickHouse/Test.pm ↑ Summary ClickHouse/Storage.pm »