blib/lib/ClickHouse.pm

filter: Statementstmt 19.7% 37 / 187 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 30.5% 11 / 36 Podpod 50.0% 3 / 6 totaltotal 16.6% 51 / 306 CC 36 SCAR 29.7
CC
36
Cov
15%
_prepare_query
72.0
_read_body
30.0
_escape_value
30.0
CRAP 19.5
↑ Summary
1 package ClickHouse;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.05';
8  
9 1 ▶use Net::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $AUTOLOAD;
18  
19 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
20 0 my ($class, %opts) = @_;
21 0 my $self = bless {}, $class;
22 0 $self->_init(%opts);
23 0 return $self;
24 }
25  
26 {
27 1 my %_attrs = (
28 '_host' => 'localhost',
29 '_port' => 8123,
30 '_database' => 'default',
31 '_user' => '',
32 '_password' => '',
33 '_keep_alive' => 1,
34 '_format' => 'TabSeparated',
35 '_socket' => undef,
36 '_uri' => undef,
37 '_timeout' => 30,
38 );
39  
40 #
41 # CLASS METHODS
42 #
43 # Returns a copy of the instance.
44 0 ▶ sub _clone {
Subroutine _clone: not called CC=1 SCAR=6.9
45 0 my ($self) = @_;
46 0 my ($clone) = {%$self};
47 0 bless( $clone, ref $self );
48 0 return ($clone);
49 }
50  
51 # Verify that an attribute is valid (called by the AUTOLOAD sub)
52 0 ▶ sub _accessible {
Subroutine _accessible: not called CC=2 SCAR=17.9
53 0 my ( $self, $name ) = @_;
54 0 ▶ if ( exists $_attrs{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_attrs{$name}) { } 0 0 0
55  
56 #$self->verbose("attribute $name is valid");
57 0 return 1;
58 }
59 0 else { return 0; }
60 }
61  
62 # Initialize the object (only called by the constructor)
63 0 ▶ sub _init {
Subroutine _init: not called CC=4 SCAR=30.0
64 0 my ( $self, %args ) = @_;
65  
66 0 foreach my $key ( keys %_attrs ) {
67 0 $key =~ s/^_+//;
68 0 ▶ if ( defined ($args{$key}) && $self->_accessible( "_$key" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$key} and $self->_accessible("_$key")) { } 0 0 0
Condition0%0/3
defined $args{$key} and $self->_accessible("_$key")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{$key} and $self->_accessible("_$key")
defined $args{$key} $self->_accessible("_$key")
Truth table0%0/3
A and B
A= defined $args{$key}B= $self->_accessible("_$key")
ABresult
0X0
100
111
69 0 $self->{"_$key"} = $args{$key};
70 }
71 else {
72 0 $self->{"_$key"} = $_attrs{"_$key"};
73 }
74 }
75 0 $self->{'_builder'} = \&_builder;
76  
77 0 $self->_connect();
78  
79 0 return 1;
80 }
81  
82 0 ▶ sub _builder {
Subroutine _builder: not called CC=4 SCAR=30.0
83 0 my ($self) = @_;
84 0 delete $self->{'_socket'};
85 0 delete $self->{'_uri'};
86  
87 # create Net::HTTP object
88 my $socket = Net::HTTP->new(
89 'Host' => $self->{'_host'},
90 'PeerPort' => $self->{'_port'},
91 'HTTPVersion' => '1.1',
92 'KeepAlive' => $self->{'_keep_alive'},
93 0 ▶ 'Timeout' => $self->{'_timeout'},
Branch: 1 branch
BranchTrueFalseTotal
my $socket = "Net::HTTP"->new("Host", $self->{"_host"}, "PeerPort", $self->{"_port"}, "HTTPVersion", "1.1", "KeepAlive", $self->{"_keep_alive"}, "Timeout", $self->{"_timeout"}) or die "Can't connect: $@" 0 0 0
94  
95 ) or die "Can't connect: $@";
96  
97 # create URI object
98 0 my $uri = URI->new(sprintf ("/?database=%s", $self->{'_database'}));
99 0 ▶ $uri->query_param('user' => $self->{'_user'}) if $self->{'_user'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_user"} 0 0 0
100 0 ▶ $uri->query_param('password' => $self->{'_password'}) if $self->{'_password'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_password"} 0 0 0
101  
102 0 $self->{'_socket'} = $socket;
103 0 $self->{'_uri'} = $uri;
104  
105 0 return 1;
106  
107 }
108  
109 0 ▶ sub _connect {
Subroutine _connect: not called CC=1 SCAR=6.9
110 0 my ($self) = @_;
111 0 $self->_builder($self);
112 0 return 1;
113 }
114  
115 0 ▶ sub _query {
Subroutine _query: not called CC=1 SCAR=6.9
116 0 my ($self, $cb) = @_;
117 return &try (
118 $cb,
119 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 $self->_connect();
121 0 $cb->();
122 }
123 0 );
124 }
125 }
126  
127 1 ▶sub ClickHouse::AUTOLOAD {
Subroutine AUTOLOAD: not called CC=5 SCAR=30.6
128 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
129 0 my ( $self, $value ) = @_;
130 0 ▶ if ( ( $AUTOLOAD =~ /.*::_get(_\w+)/ ) && ( $self->_accessible($1) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ClickHouse::AUTOLOAD =~ /.*::_get(_\w+)/ and $self->_accessible($1)) 0 0 0
Condition0%0/3
$ClickHouse::AUTOLOAD =~ /.*::_get(_\w+)/ and $self->_accessible($1)
!ll&&!rl&&r
000
MC/DC0%0/2
$ClickHouse::AUTOLOAD =~ /.*::_get(_\w+)/ and $self->_accessible($1)
$ClickHouse::AUTOLOAD =~ /.*::_get(_\w+)/ $self->_accessible($1)
Truth table0%0/3
A and B
A= $ClickHouse::AUTOLOAD =~ /.*::_get(_\w+)/B= $self->_accessible($1)
ABresult
0X0
100
111
131 0 my $attr_name = $1;
132 0 ▶ *{$AUTOLOAD} = sub { return $_[0]->{$attr_name} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 return ( $self->{$attr_name} );
134 }
135 0 ▶ if ( $AUTOLOAD =~ /.*::_set(_\w+)/ && $self->_accessible($1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ClickHouse::AUTOLOAD =~ /.*::_set(_\w+)/ and $self->_accessible($1)) 0 0 0
Condition0%0/3
$ClickHouse::AUTOLOAD =~ /.*::_set(_\w+)/ and $self->_accessible($1)
!ll&&!rl&&r
000
MC/DC0%0/2
$ClickHouse::AUTOLOAD =~ /.*::_set(_\w+)/ and $self->_accessible($1)
$ClickHouse::AUTOLOAD =~ /.*::_set(_\w+)/ $self->_accessible($1)
Truth table0%0/3
A and B
A= $ClickHouse::AUTOLOAD =~ /.*::_set(_\w+)/B= $self->_accessible($1)
ABresult
0X0
100
111
136 0 my $attr_name = $1;
137 0 ▶ *{$AUTOLOAD} = sub { $_[0]->{$attr_name} = $_[1]; return; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 $self->{$1} = $value;
139 0 return;
140 }
141 0 croak "No such method: $AUTOLOAD";
142 }
143  
144 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
145  
146 0 ▶sub disconnect {
Subroutine disconnect: not called CC=2 SCAR=17.9
Pod undocumented
147 0 my ($self) = @_;
148 0 ▶ if (my $socket = $self->_get_socket()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $socket = $self->_get_socket) 0 0 0
149 0 $socket->keep_alive(0);
150 0 $self->ping();
151 }
152 0 return 1;
153 }
154  
155  
156  
157 0 ▶sub select {
Subroutine select: not called CC=1 SCAR=6.9
Pod documented
158 0 my ($self, $query) = @_;
159 0 ▶ return $self->_query(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
160 0 my $method;
161 my $query_url;
162 0 my @post_data = ();
163 0 ▶ if (length ($query) <= 7000) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $query <= 7000) { } 0 0 0
164 0 $query_url = $self->_construct_query_uri( $query );
165 0 $method = 'GET';
166 }
167 else {
168 0 $query_url = $self->_get_uri()->clone();
169 0 $method = 'POST';
170 0 push @post_data, $query;
171 }
172  
173  
174 0 $self->_get_socket()->write_request( $method => $query_url, @post_data );
175 0 return $self->_parse_response($query);
176 0 });
177 }
178  
179 0 ▶sub select_value {
Subroutine select_value: not called CC=1 SCAR=6.9
Pod undocumented
180 0 my ($self, $query) = @_;
181  
182 0 my $arrayref = $self->select($query);
183 0 return $arrayref->[0]->[0];
184 }
185  
186 0 ▶sub do {
Subroutine do: not called CC=1 SCAR=6.9
Pod documented
187 0 my ($self, $query, @rows) = @_;
188 0 ▶ return $self->_query(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
189 0 my @prepared_rows = $self->_prepare_query(@rows);
190 0 my $query_url = $self->_construct_query_uri($query);
191 0 ▶ my $post_data = scalar @prepared_rows ? join (",", map { "(" . join (",", @{ $_ }) . ")" } @prepared_rows) : "\n" ;
Branch: 1 branch
BranchTrueFalseTotal
scalar @prepared_rows ? : 0 0 0
192  
193 0 $self->_get_socket()->write_request('POST' => $query_url, $post_data);
194 0 return $self->_parse_response($query);
195 0 });
196  
197 }
198  
199 0 ▶sub ping {
Subroutine ping: not called CC=4 SCAR=30.0
Pod undocumented
200 0 my ($self) = @_;
201  
202 0 my ($code) = eval {
203 0 $self->_get_socket()->write_request('GET' => '/');
204 0 $self->_get_socket()->read_response_headers();
205 };
206  
207 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
208 0 return 0;
209 }
210 0 ▶ unless ($code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code == 200) 0 0 0
211 0 return 0;
212 }
213 0 my $result = $self->_read_body();
214 0 ▶ unless ($result->[0] eq 'Ok.' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->[0] eq "Ok.") 0 0 0
215 0 return 0;
216 }
217 0 return 1;
218 }
219  
220 0 ▶sub _parse_response {
Subroutine _parse_response: not called CC=4 SCAR=30.0
221 0 my ($self, $query) = @_;
222 0 my ($code, $mess) = $self->_get_socket()->read_response_headers();
223 0 ▶ if ($code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 200) { } 0 0 0
224 0 return _formaty_query_result( $self->_read_body() );
225 }
226 else {
227 0 my $add_mess = _formaty_query_result( $self->_read_body() );
228 0 ▶ if (defined $add_mess) { $add_mess = $add_mess->[0]->[0] // '' };
Branch: 1 branch
BranchTrueFalseTotal
if (defined $add_mess) 0 0 0
Condition0%0/2
$add_mess->[0][0] // ""
l!l
00
MC/DC0%0/1
$add_mess->[0][0] // ""
$add_mess->[0][0]
Truth table0%0/2
A
A= $add_mess->[0][0]
Aresult
11
00
229 0 die "ClickHouse error: $mess ($add_mess)\n\t$query";
230 }
231 }
232  
233 0 ▶sub _read_body {
Subroutine _read_body: not called CC=5 SCAR=34.0
234 0 my ($self) = @_;
235  
236 0 my @response;
237 0 my $chunk = '';
238 0 while (1) {
239 0 my $buf;
240 0 my $n = $self->_get_socket()->read_entity_body($buf, 1024);
241 0 ▶ die "can't read response: $!" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
242 0 ▶ last unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
243 0 $buf = $chunk . $buf;
244 0 push @response, split (/\n/, $buf);
245 0 ▶ $chunk = substr ($buf,-1) eq "\n" ? '' : pop @response;
Branch: 1 branch
BranchTrueFalseTotal
substr($buf, -1) eq "\n" ? : 0 0 0
246 }
247 0 ▶ push @response, $chunk if $chunk;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk 0 0 0
248 0 return \@response;
249 }
250  
251 0 ▶sub _formaty_query_result {
Subroutine _formaty_query_result: not called CC=1 SCAR=6.9
252 0 my ($query_result) = @_;
253 0 return [ map { [ split (/\t/) ] } @{ $query_result } ];
254 }
255  
256  
257 0 ▶sub _construct_query_uri {
Subroutine _construct_query_uri: not called CC=1 SCAR=6.9
258 0 my ($self, $query) = @_;
259  
260 0 my $query_uri = $self->_get_uri()->clone();
261 0 $query_uri->query_param('query' => $query);
262  
263 0 return $query_uri->as_string();
264 }
265  
266 0 ▶sub _prepare_query {
Subroutine _prepare_query: not called CC=8 SCAR=42.8
267 0 my ($class, @rows) = @_;
268 0 my @clone_rows = map { [@$_] } @rows;
269 0 foreach my $row (@clone_rows) {
270 0 foreach my $value (@$row) {
271 0 my $type = 'NUMBER';
272 0 ▶ if (ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 0 0 0
273 0 $type = $value->{'TYPE'};
274 0 $value = $value->{'VALUE'};
275 }
276 0 ▶ unless (defined ($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
277 0 $type = 'NULL';
278 }
279 0 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 0 0 0
280 0 $type = 'ARRAY';
281 }
282 0 ▶ if ( defined ($value) && !looks_like_number ($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not &looks_like_number($value)) 0 0 0
Condition0%0/3
defined $value and not &looks_like_number($value)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and not &looks_like_number($value)
defined $value not &looks_like_number($value)
Truth table0%0/3
A and not(B)
A= defined $valueB= not &looks_like_number($value)
ABresult
0X0
100
111
283 0 $type = 'STRING';
284 }
285 0 $value = _escape_value($value, $type);
286 }
287 }
288 0 return @clone_rows;
289 }
290  
291 0 ▶sub _escape_value {
Subroutine _escape_value: not called CC=5 SCAR=34.0
292 0 my ($value, $type) = @_;
293 0 ▶ if ($type eq 'NULL') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'NULL') { } 0 0 0
elsif ($type eq 'STRING') { } 0 0 0
elsif ($type eq 'ARRAY') { } 0 0 0
294 0 $value = qq{''};
295 }
296 elsif ($type eq 'STRING') {
297 0 ▶ utf8::encode($value) if utf8::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($value) 0 0 0
298 0 $value =~ s{\\}{\\\\}g;
299 0 $value =~ s/'/\\'/g;
300 0 $value = qq{'$value'};
301 }
302 elsif ($type eq 'ARRAY') {
303 0 $value = q{'} . join ("','", @$value) . q{'};
304 }
305 0 return $value;
306 }
307  
308 1 1;
309  
310 __END__
↑ Summary