blib/lib/DBD/Crate.pm

filter: Statementstmt 40.6% 91 / 224 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 56.1% 23 / 41 Podpod 0.0% 0 / 3 totaltotal 31.5% 114 / 361 CC 41 SCAR 42.2
CC
41
Cov
30%
_fetch_data
182.0
execute
156.0
connect
110.0
CRAP 67.9
↑ Summary
1 package DBD::Crate;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use vars qw($VERSION $REVISION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use vars qw($err $errstr $state $drh);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 $VERSION = "0.0.3";
9
10 5 $err = 0;
11 5 $errstr = "";
12 5 $state = "";
13 5 $drh = undef;
14 5 my $methods_are_installed = 0;
15 5 my ($HTTP, $JSON);
16
17 0 ▶sub driver {
Subroutine driver: not called CC=3 SCAR=24.8
Pod undocumented
18 0 ▶ return $drh if $drh;
Branch: 1 branch
BranchTrueFalseTotal
if $DBD::Crate::drh 0 0 0
19 0 my ($class, $attr) = @_;
20 0 $class .= "::dr";
21 0 ▶ $drh = DBI::_new_drh($class, {
Branch: 1 branch
BranchTrueFalseTotal
$DBD::Crate::drh = DBI::_new_drh($class, {"Name", "Crate", "Version", $DBD::Crate::VERSION, "Err", \$DBD::Crate::err, "Errstr", \$DBD::Crate::errstr, "State", \$DBD::Crate::state, "Attribution", "DBD::Crate by Mamod Mehyar", "AutoCommit", 1}) or return undef 0 0 0
22 'Name' => 'Crate',
23 'Version' => $VERSION,
24 'Err' => \$err,
25 'Errstr' => \$errstr,
26 'State' => \$state,
27 'Attribution' => 'DBD::Crate by Mamod Mehyar',
28 'AutoCommit' => 1
29 }) or return undef;
30 0 return $drh;
31 }
32
33 0 ▶sub http { $HTTP }
Subroutine http: not called CC=1 SCAR=6.9
Pod undocumented
34 0 ▶sub json { $JSON }
Subroutine json: not called CC=1 SCAR=6.9
Pod undocumented
35
36 #====================================================================
37 # DBD::Crate::dr
38 #====================================================================
39 package DBD::Crate::dr; {
40 5 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 ▶ use DBI qw(:sql_types);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 ▶ use vars qw($imp_data_size);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 5 ▶ use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 5 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 5 ▶ use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
46
47 5 $imp_data_size = 0;
48
49 0 ▶ sub connect {
Subroutine connect: not called CC=10 SCAR=47.0
50 0 my ($drh, $dburl, $user, $pass, $attr) = @_;
51 my $UTF8 = defined $attr->{utf8} ?
52 0 ▶ $attr->{utf8} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $attr->{'utf8'} ? : 0 0 0
53
54 0 $JSON = JSON::MaybeXS->new({ utf8 => $UTF8 });
55 0 $HTTP = HTTP::Tiny->new( keep_alive => 1 );
56
57 0 my @addresses = ($dburl);
58 0 my @addr;
59 0 ▶ if ($dburl =~ s/^\[(.*?)\]$/$1/){
Branch: 1 branch
BranchTrueFalseTotal
if ($dburl =~ s/^\[(.*?)\]$/$1/) 0 0 0
60 0 @addresses = split ',', $dburl;
61 }
62
63 0 foreach my $addr (@addresses){
64 0 $addr =~ s/\s+//;
65 0 ▶ if (!$addr){
Branch: 1 branch
BranchTrueFalseTotal
unless ($addr) 0 0 0
66 0 $addr = 'http://localhost:4200';
67 }
68
69 0 ▶ if ($addr !~ /^http/){
Branch: 1 branch
BranchTrueFalseTotal
unless ($addr =~ /^http/) 0 0 0
70 0 $addr = 'http://' . $addr;
71 }
72
73 0 ▶ if ($user || $pass){
Branch: 1 branch
BranchTrueFalseTotal
if ($user or $pass) 0 0 0
Condition0%0/3
$user or $pass
l!l&&r!l&&!r
000
MC/DC0%0/2
$user or $pass
$user $pass
Truth table0%0/3
A or B
A= $userB= $pass
ABresult
1X1
011
000
74 0 ▶ my $auth = ($user || '') . ':' . ($pass || '');
Condition0%0/4
$user || ''
l!l
00
$pass || ''
l!l
00
MC/DC0%0/1
$user || ''
$user
MC/DC0%0/1
$pass || ''
$pass
Truth table0%0/2
A
A= $user
Aresult
11
00
Truth table0%0/2
A
A= $pass
Aresult
11
00
75 0 $addr =~ s/^(http(?:.)?:\/\/)(.*?)/$1$auth\@$2/;
76 }
77 0 push @addr, $addr;
78 }
79
80 0 my ($t, $dbh) = DBI::_new_dbh($drh, {
81 'Name' => \@addr
82 });
83
84 0 return $dbh;
85 }
86
87 0 ▶ sub data_sources { return "Cratedb" }
Subroutine data_sources: not called CC=1 SCAR=6.9
88 0 ▶ sub disconnect_all { 1 }
Subroutine disconnect_all: not called CC=1 SCAR=6.9
89 5 };
90
91 #====================================================================
92 # DBD::Crate::db
93 #====================================================================
94 package DBD::Crate::db; {
95 5 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 5 ▶ use base qw(DBD::_::db);
Subroutine BEGIN: called CC=1 SCAR=0.0
97 5 ▶ use vars qw($imp_data_size);
Subroutine BEGIN: called CC=1 SCAR=0.0
98 5 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 5 ▶ use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 5 ▶ use Digest::SHA1 qw(sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
101
102 5 $imp_data_size = 0;
103
104 0 ▶ sub prepare {
Subroutine prepare: not called CC=1 SCAR=6.9
105 0 my ($dbh, $statement, @attr) = @_;
106 my $sth = DBI::_new_sth($dbh, {
107 'Statement' => $statement,
108 'ConnectionHOST' => $dbh->{Name}
109 0 });
110 0 return $sth;
111 }
112
113 #=============================================================
114 # blob methods
115 #=============================================================
116 0 ▶ sub crate_blob_insert {
Subroutine crate_blob_insert: not called CC=2 SCAR=17.9
117 0 my ($dbh, $table, $digest, $content) = @_;
118
119 0 ▶ if (!$content){
Branch: 1 branch
BranchTrueFalseTotal
unless ($content) 0 0 0
120 0 $content = $digest;
121 0 $digest = sha1_hex($content);
122 }
123
124 0 my $path = "/_blobs/$table/" . $digest;
125 my $sth = DBI::_new_sth($dbh, {
126 'REQUEST_PATH' => $path,
127 'REQUEST_METHOD' => 'PUT',
128 'Statement' => $content,
129 'ConnectionHOST' => $dbh->{Name},
130 0 'BLOB' => 1,
131 'DIGEST' => $digest
132 });
133
134 0 return $sth->execute();
135 };
136
137 0 ▶ sub crate_blob_get {
Subroutine crate_blob_get: not called CC=3 SCAR=24.8
138 0 my ($dbh, $table, $digest) = @_;
139
140 0 ▶ if (!$digest){
Branch: 1 branch
BranchTrueFalseTotal
unless ($digest) 0 0 0
141 0 $dbh->set_err(-1, "BLOB sha1 digest required");
142 0 return;
143 }
144
145 0 ▶ $digest ||= '';
Condition0%0/2
$digest ||= ""
l!l
00
MC/DC0%0/1
$digest ||= ""
$digest
Truth table0%0/2
A
A= $digest
Aresult
11
00
146
147 0 my $path = "/_blobs/$table/" . $digest;
148 my $sth = DBI::_new_sth($dbh, {
149 'REQUEST_PATH' => $path,
150 'REQUEST_METHOD' => 'GET',
151 'Statement' => '',
152 'ConnectionHOST' => $dbh->{Name},
153 0 'BLOB' => 1,
154 'DIGEST' => $digest
155 });
156 0 return $sth->execute();
157 }
158
159 0 ▶ sub crate_blob_delete {
Subroutine crate_blob_delete: not called CC=2 SCAR=17.9
160 0 my ($dbh, $table, $digest) = @_;
161
162 0 ▶ if (!$digest){
Branch: 1 branch
BranchTrueFalseTotal
unless ($digest) 0 0 0
163 0 $dbh->set_err(-1, "BLOB sha1 digest required");
164 0 return;
165 }
166
167 0 my $path = "/_blobs/$table/" . $digest;
168 my $sth = DBI::_new_sth($dbh, {
169 'REQUEST_PATH' => $path,
170 'REQUEST_METHOD' => 'DELETE',
171 'Statement' => '',
172 'ConnectionHOST' => $dbh->{Name},
173 0 'BLOB' => 1,
174 'DIGEST' => $digest
175 });
176
177 0 return $sth->execute();
178 }
179
180 #=============================================================
181 # table info methods
182 #=============================================================
183 #return columns information of provided table
184 0 ▶ sub crate_table_columns {
Subroutine crate_table_columns: not called CC=1 SCAR=6.9
185 0 my ($dbh, $table) = @_;
186 0 my $sth = $dbh->prepare(qq~
187 select column_name, data_type, ordinal_position
188 from information_schema.columns
189 where schema_name = 'doc'
190 AND table_name = ?
191 ~);
192
193 0 return $dbh->selectall_arrayref( $sth,
194 { Slice => {} },
195 $table);
196 }
197
198 #list all tables with information
199 0 ▶ sub crate_tables_list {
Subroutine crate_tables_list: not called CC=2 SCAR=17.9
200 0 my $dbh = shift;
201 0 ▶ my $schema = shift || 'doc';
Condition0%0/2
shift() || 'doc'
l!l
00
MC/DC0%0/1
shift() || 'doc'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
202 0 my $sth = $dbh->prepare(qq~
203 select number_of_replicas, partitioned_by, blobs_path, schema_name,
204 table_name, number_of_shards, clustered_by
205 from information_schema.tables
206 where schema_name = ?
207 ~);
208
209 0 return $dbh->selectall_arrayref( $sth,
210 { Slice => {} },
211 $schema);
212 }
213
214 #get table info
215 0 ▶ sub crate_table_info {
Subroutine crate_table_info: not called CC=1 SCAR=6.9
216 0 my $dbh = shift;
217 0 my $table = shift;
218 0 my $sth = $dbh->prepare(qq~
219 select number_of_replicas, partitioned_by, blobs_path, schema_name,
220 table_name, number_of_shards, clustered_by
221 from information_schema.tables
222 where schema_name = 'doc'
223 AND table_name = ?
224 ~);
225
226 0 return $dbh->selectrow_hashref( $sth,
227 undef,
228 $table);
229 }
230
231 #==================================================
232 # These should be removed once we get crate name
233 # space registered with DBI
234 #==================================================
235 5 *DBI::db::crate_blob_insert = \&crate_blob_insert;
236 5 *DBI::db::crate_blob_get = \&crate_blob_get;
237 5 *DBI::db::crate_blob_delete = \&crate_blob_delete;
238 5 *DBI::db::crate_table_columns = \&crate_table_columns;
239 5 *DBI::db::crate_tables_list = \&crate_tables_list;
240 5 *DBI::db::crate_table_info = \&crate_table_info;
241 };
242
243 #====================================================================
244 # DBD::Crate::st
245 #====================================================================
246 package DBD::Crate::st; {
247 5 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
248 5 ▶ use base qw(DBD::_::st);
Subroutine BEGIN: called CC=1 SCAR=0.0
249 5 ▶ use vars qw($imp_data_size);
Subroutine BEGIN: called CC=1 SCAR=0.0
250 5 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
251 5 ▶ use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
252
253 5 $imp_data_size = 0;
254
255 0 ▶ sub _fetch_data {
Subroutine _fetch_data: not called CC=13 SCAR=52.0
256 0 my $sth = shift;
257 0 my $statement = shift;
258 0 ▶ my $method = shift || 'POST';
Condition0%0/2
shift() || 'POST'
l!l
00
MC/DC0%0/1
shift() || 'POST'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
259 0 ▶ my $path = shift || '/_sql';
Condition0%0/2
shift() || '/_sql'
l!l
00
MC/DC0%0/1
shift() || '/_sql'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
260
261 0 my @hosts = @{ $sth->{ConnectionHOST} };
262
263 0 TRYAGAIN :
264 my $host = shift @hosts;
265 0 my $ret = DBD::Crate::http->request($method, $host . $path, {
266 content => $statement,
267 # headers => {'Content-Type' => 'application/json'}
268 });
269
270 0 ▶ if ( $ret->{status} == 599 && scalar @hosts){
Branch: 1 branch
BranchTrueFalseTotal
if ($ret->{"status"} == 599 and scalar @hosts) 0 0 0
Condition0%0/2
$ret->{"status"} == 599 and scalar @hosts
!ll
00
MC/DC0%0/1
$ret->{"status"} == 599 and scalar @hosts
$ret->{"status"} == 599
Truth table0%0/2
A
A= $ret->{"status"} == 599
Aresult
00
11
271 0 my $i = 0;
272 0 for (@{ $sth->{ConnectionHOST} }){
273 #put failing address to the end of hosts
274 #this will work only on persistant environments
275 0 ▶ if ( $host eq $_ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($host eq $_) 0 0 0
276 0 splice @{ $sth->{ConnectionHOST} }, $i, 1;
277 0 push @{ $sth->{ConnectionHOST} }, $_;
278 }
279 0 $i++;
280 }
281 0 goto TRYAGAIN;
282 }
283
284 0 ▶ if (!$ret->{success}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret->{"success"}) 0 0 0
285 0 my $olderr = $@;
286 0 my $data = eval { DBD::Crate::json->decode($ret->{content}) };
287 0 $@ = $olderr;
288
289 my $error = ref $data eq 'HASH' && ref $data->{error} ? $data->{error} : {
290 code => ref $data ? $data->{status} : $ret->{status},
291 message => ref $data ? $data->{error} : ($ret->{content} || $ret->{reason})
292 0 ▶ };
Branch: 3 branches
BranchTrueFalseTotal
ref $data eq 'HASH' && ref $data->{'error'} ? : 0 0 0
ref $data ? : 0 0 0
ref $data ? : 0 0 0
Condition0%0/6
ref $data eq "HASH" and ref $data->{"error"}
!ll&&!rl&&r
000
$ret->{"content"} or $ret->{"reason"}
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $data eq "HASH" and ref $data->{"error"}
ref $data eq "HASH" ref $data->{"error"}
MC/DC0%0/2
$ret->{"content"} or $ret->{"reason"}
$ret->{"content"} $ret->{"reason"}
Truth table0%0/3
A and B
A= ref $data eq "HASH"B= ref $data->{"error"}
ABresult
0X0
100
111
Truth table0%0/3
A or B
A= $ret->{"content"}B= $ret->{"reason"}
ABresult
1X1
011
000
293
294 0 $sth->set_err($error->{code}, $error->{message});
295 0 return;
296 }
297
298 0 return $ret;
299 }
300
301 0 ▶ sub execute {
Subroutine execute: not called CC=12 SCAR=50.5
302 0 my $sth = shift;
303 0 my $statement = $sth->{Statement};
304 0 my $ret;
305 0 ▶ if ($sth->{BLOB}){
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{'BLOB'}) { } 0 0 0
306 0 ▶ if (!$sth->{DIGEST}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->{"DIGEST"}) 0 0 0
307 0 $sth->set_err(-1, "BLOB sha1 digest required");
308 0 return;
309 }
310
311 $ret = _fetch_data($sth, $statement,
312 $sth->{REQUEST_METHOD},
313 0 ▶ $sth->{REQUEST_PATH}) or return;
Branch: 1 branch
BranchTrueFalseTotal
$ret = _fetch_data($sth, $statement, $sth->{"REQUEST_METHOD"}, $sth->{"REQUEST_PATH"}) or return 0 0 0
314 } else {
315 0 my $hash = {stmt => $statement };
316 0 ▶ if (@_){ $hash->{args} = \@_; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
317 0 my $json = DBD::Crate::json->encode($hash);
318 0 ▶ $ret = _fetch_data($sth, $json, 'POST', '/_sql') or return;
Branch: 1 branch
BranchTrueFalseTotal
$ret = _fetch_data($sth, $json, "POST", "/_sql") or return 0 0 0
319 }
320
321 0 $sth->{'driver_raw_data'} = $ret->{content};
322 0 ▶ if ($sth->{BLOB}){
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"BLOB"}) 0 0 0
323 0 ▶ if ($ret->{status} == 201){ # put success
Branch: 3 branches
BranchTrueFalseTotal
if ($ret->{'status'} == 201) { } 0 0 0
elsif ($ret->{'status'} == 200) { } 0 0 0
elsif ($ret->{'status'} == 204) { } 0 0 0
324 0 return $sth->{DIGEST};
325 } elsif ($ret->{status} == 200){ #get success
326 0 return $ret->{content};
327 } elsif ($ret->{status} == 204){ #delete success
328 0 return 1;
329 }
330 }
331
332 0 my $olderr = $@;
333 0 my $data = eval { DBD::Crate::json->decode($ret->{content}) };
334 0 ▶ if (!$data){
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
335 0 my $error = $@;
336 0 $@ = $olderr;
337 0 $sth->set_err(-1, $error);
338 0 return;
339 }
340
341
342 0 $sth->{'driver_data'} = $data->{rows};
343 0 $sth->{'driver_rows'} = $data->{rowcount};
344
345 0 $sth->{'NAME'} = $data->{cols};
346 0 $sth->STORE('NUM_OF_FIELDS', scalar @{ $data->{cols} });
347 0 ▶ return $data->{rowcount} || '0E0';
Condition0%0/2
$data->{'rowcount'} || '0E0'
l!l
00
MC/DC0%0/1
$data->{'rowcount'} || '0E0'
$data->{'rowcount'}
Truth table0%0/2
A
A= $data->{'rowcount'}
Aresult
11
00
348 }
349
350 5 *fetch = \&fetchrow_arrayref;
351 0 ▶ sub fetchrow_arrayref {
Subroutine fetchrow_arrayref: not called CC=2 SCAR=17.9
352 0 my $sth = shift;
353 0 my $data = $sth->FETCH('driver_data');
354 0 ▶ my $row = shift @$data or return;
Branch: 1 branch
BranchTrueFalseTotal
my $row = shift @$data or return 0 0 0
355 0 return $sth->_set_fbav($row);
356 }
357
358 0 ▶ sub raw {
Subroutine raw: not called CC=1 SCAR=6.9
359 0 my $sth = shift;
360 0 my $data = $sth->FETCH('driver_raw_data');
361 0 return $data;
362 }
363
364 5 *DBI::st::raw = \&raw;
365
366 #Nothing to close, crate is stateless
367 0 ▶ sub close {}
Subroutine close: not called CC=1 SCAR=0.0
368 5 };
369
370
371 5 1;
372
373 __END__
↑ Summary