| 1 | package DBIO::ClickHouse::Result; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: ClickHouse-specific Result component for DBIO | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 27 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 27 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 27 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 27 | ▶ | use base 'DBIO::Base'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 27 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 27 | __PACKAGE__->mk_classdata('_clickhouse_table_options'); | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 27 | my %VALID_OPTION = map { $_ => 1 } qw( | |||||||||||||||||||||||||||||||
| 17 | engine order_by primary_key partition_by settings | ||||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 72 | ▶ | sub clickhouse_table { | ||||||||||||||||||||||||||||||
|
Subroutine
clickhouse_table: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 21 | 72 | my ($class, $options) = @_; | |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 72 | ▶ | $class->_clickhouse_table_options(_clickhouse_validate_table_options($class, $options)) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 24 | if defined $options; | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 62 | my $stored = $class->_clickhouse_table_options; | |||||||||||||||||||||||||||||||
| 27 | 62 | ▶ | return $stored ? { %$stored } : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 61 | ▶ | sub _clickhouse_validate_table_options { | ||||||||||||||||||||||||||||||
|
Subroutine
_clickhouse_validate_table_options: called CC=21 SCAR=30.5
| |||||||||||||||||||||||||||||||||
| 31 | 61 | my ($class, $options) = @_; | |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 61 | ▶ | _clickhouse_throw($class, 'clickhouse_table() expects a hashref of table options') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 34 | unless ref $options eq 'HASH'; | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 59 | my %clean; | |||||||||||||||||||||||||||||||
| 37 | 59 | for my $key (sort keys %$options) { | |||||||||||||||||||||||||||||||
| 38 | _clickhouse_throw($class, "clickhouse_table(): unknown option '$key'") | ||||||||||||||||||||||||||||||||
| 39 | 162 | ▶ | unless $VALID_OPTION{$key}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 161 | my $value = $options->{$key}; | |||||||||||||||||||||||||||||||
| 42 | 161 | ▶ | next unless defined $value; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 161 | ▶ | if ($key eq 'engine' || $key eq 'partition_by') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$key eq "engine" or $key eq "partition_by"
MC/DC100%2/2
$key eq "engine" or $key eq "partition_by"
$key eq "engine"
$key eq "partition_by"
Truth table100%3/3
A or B
A= $key eq "engine"B= $key eq "partition_by"
| |||||||||||||||||||||||||||||||||
| 45 | 79 | ▶ | _clickhouse_throw($class, "clickhouse_table(): '$key' must be a non-empty string") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $value or not length $value
MC/DC100%2/2
ref $value or not length $value
ref $value
not length $value
Truth table100%3/3
A or not(B)
A= ref $valueB= not length $value
| |||||||||||||||||||||||||||||||||
| 46 | if ref $value || !length $value; | ||||||||||||||||||||||||||||||||
| 47 | 76 | $clean{$key} = $value; | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | ▶ | elsif ($key eq 'order_by' || $key eq 'primary_key') { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
$key eq "order_by" or $key eq "primary_key"
MC/DC100%2/2
$key eq "order_by" or $key eq "primary_key"
$key eq "order_by"
$key eq "primary_key"
Truth table100%3/3
A or B
A= $key eq "order_by"B= $key eq "primary_key"
| |||||||||||||||||||||||||||||||||
| 50 | 78 | ▶ | my @entries = ref $value eq 'ARRAY' ? @$value : ($value); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 78 | for my $entry (@entries) { | |||||||||||||||||||||||||||||||
| 52 | 103 | ▶ | _clickhouse_throw($class, | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $entry and ref $entry ne "SCALAR"
MC/DC50%1/2
ref $entry and ref $entry ne "SCALAR"
ref $entry
ref $entry ne "SCALAR"
Truth table66%2/3
A and B
A= ref $entryB= ref $entry ne "SCALAR"
| |||||||||||||||||||||||||||||||||
| 53 | "clickhouse_table(): '$key' entries must be column names or scalar refs of raw SQL") | ||||||||||||||||||||||||||||||||
| 54 | if ref $entry && ref $entry ne 'SCALAR'; | ||||||||||||||||||||||||||||||||
| 55 | 102 | ▶ | _clickhouse_throw($class, "clickhouse_table(): '$key' entries must not be empty") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $entry or length $entry
MC/DC50%1/2
ref $entry or length $entry
ref $entry
length $entry
Truth table66%2/3
A or B
A= ref $entryB= length $entry
| |||||||||||||||||||||||||||||||||
| 56 | if !ref $entry && !length $entry; | ||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | 76 | $clean{$key} = \@entries; | |||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||||||
| 61 | 4 | ▶ | _clickhouse_throw($class, "clickhouse_table(): 'settings' must be a hashref") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 62 | unless ref $value eq 'HASH'; | ||||||||||||||||||||||||||||||||
| 63 | 3 | for my $name (sort keys %$value) { | |||||||||||||||||||||||||||||||
| 64 | _clickhouse_throw($class, | ||||||||||||||||||||||||||||||||
| 65 | "clickhouse_table(): setting '$name' must be a scalar or scalar ref") | ||||||||||||||||||||||||||||||||
| 66 | 3 | ▶ | if ref $value->{$name} && ref $value->{$name} ne 'SCALAR'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $value->{$name} and ref $value->{$name} ne "SCALAR"
MC/DC50%1/2
ref $value->{$name} and ref $value->{$name} ne "SCALAR"
ref $value->{$name}
ref $value->{$name} ne "SCALAR"
Truth table66%2/3
A and B
A= ref $value->{$name}B= ref $value->{$name} ne "SCALAR"
| |||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | 2 | $clean{$key} = { %$value }; | |||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 51 | return \%clean; | |||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | # Route errors through the result class's own throw_exception when the | ||||||||||||||||||||||||||||||||
| 76 | # component is mixed into one (the family rule), and fall back to a plain | ||||||||||||||||||||||||||||||||
| 77 | # DBIO::Exception when it is not -- DBIO::Base has no throw_exception, and a | ||||||||||||||||||||||||||||||||
| 78 | # component must never install one, which would shadow the result class's. | ||||||||||||||||||||||||||||||||
| 79 | 10 | ▶ | sub _clickhouse_throw { | ||||||||||||||||||||||||||||||
|
Subroutine
_clickhouse_throw: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 80 | 10 | my ($class, $message) = @_; | |||||||||||||||||||||||||||||||
| 81 | 10 | ▶ | return $class->throw_exception($message) if $class->can('throw_exception'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | 1 | DBIO::Exception->throw($message); | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 27 | 1; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||||||||||||||