blib/lib/SQL/Object.pm

filter: Statementstmt 83.9% 68 / 81 Branchbran 64.2% 9 / 14 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 88.0% 22 / 25 Podpod 81.8% 9 / 11 totaltotal 80.1% 109 / 136 CC 9 SCAR 9.4
CC
9
Cov
80%
sql_obj
8.0
new
2.1
new
2.1
CRAP 2.6
↑ Summary
1 package SQL::Object;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our @EXPORT_OK = qw/sql_obj sql_type/;
8  
9 use overload
10 0 ▶ '&' => sub { $_[0]->compose_and($_[1]) },
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=6.9
11 2 ▶ '|' => sub { $_[0]->compose_or($_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
12 1 ▶ '+' => sub { $_[0]->join($_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 0 ▶ '""' => sub { $_[0]->as_sql },
Subroutine __ANON__: not called CC=1 SCAR=6.9
14 2 fallback => 1
15 2 ;
16  
17 2 our $VERSION = '0.01';
18  
19 5 ▶sub sql_obj {
Subroutine sql_obj: called CC=6 SCAR=20.8
Pod undocumented
20 5 my ($sql, $args) = @_;
21  
22 5 my $bind;
23 5 ▶ if (ref($args) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq 'HASH') { } 1 4 5
24 1 my %named_bind = %{$args};
25 1 $sql =~ s{:(\w+)}{
26 1 ▶ Carp::croak("$1 does not exists in hash") if !exists $named_bind{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $named_bind{$1} 0 1 1
27 1 ▶ if ( ref $named_bind{$1} && ref $named_bind{$1} eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $named_bind{$1} and ref $named_bind{$1} eq 'ARRAY') { } 0 1 1
Condition33%1/3
ref $named_bind{$1} and ref $named_bind{$1} eq "ARRAY"
!ll&&!rl&&r
100
MC/DC0%0/2
ref $named_bind{$1} and ref $named_bind{$1} eq "ARRAY"
ref $named_bind{$1} ref $named_bind{$1} eq "ARRAY"
Truth table33%1/3
A and B
A= ref $named_bind{$1}B= ref $named_bind{$1} eq "ARRAY"
ABresult
0X0
100
111
28 0 push @$bind, @{ $named_bind{$1} };
29 0 my $tmp = join ',', map { '?' } @{ $named_bind{$1} };
30 0 "($tmp)";
31 } else {
32 1 push @$bind, $named_bind{$1};
33 1 '?'
34 }
35 }ge;
36 }
37 else {
38 4 ▶ $bind = ref($args) eq 'ARRAY' ? $args : [$args];
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'ARRAY' ? : 0 4 4
39 }
40  
41 5 SQL::Object->new(sql => $sql, bind => $bind);
42 }
43  
44 1 ▶sub sql_type {
Subroutine sql_type: called CC=1 SCAR=0.0
Pod documented
45 1 my ($value_ref, $type) = @_;
46 1 SQL::Object::Type->new(value_ref => $value_ref, type => $type);
47 }
48  
49 5 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
50 5 my $class = shift;
51 0 ▶ my %args = @_==1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 5 5
52 5 bless {%args}, $class;
53 }
54  
55 4 ▶sub _compose {
Subroutine _compose: called CC=1 SCAR=0.0
56 4 my ($self, $op, $sql, $bind) = @_;
57  
58 4 $self->{sql} = $self->{sql} . " $op " . $sql;
59 4 $self->{bind} = [@{$self->{bind}}, @$bind];
60 4 $self;
61 }
62  
63 1 ▶sub and {
Subroutine and: called CC=1 SCAR=0.0
Pod documented
64 1 my ($self, $sql, @bind) = @_;
65 1 $self->_compose('AND', $sql, \@bind);
66 }
67  
68 3 ▶sub or {
Subroutine or: called CC=1 SCAR=0.0
Pod documented
69 3 my ($self, $sql, @bind) = @_;
70 3 $self->add_parens->_compose('OR', $sql, \@bind);
71 }
72  
73 1 ▶sub join {
Subroutine join: called CC=1 SCAR=0.0
Pod undocumented
74 1 my ($self, $other) = @_;
75 1 $self->{sql} = $self->{sql} . $other->{sql};
76 1 $self->{bind} = [@{$self->{bind}}, @{$other->{bind}}];
77 1 $self;
78 }
79  
80 0 ▶sub compose_and {
Subroutine compose_and: not called CC=1 SCAR=6.9
Pod documented
81 0 my ($self, $other) = @_;
82 0 $self->and($other->{sql}, @{$other->{bind}});
83 }
84  
85 2 ▶sub compose_or {
Subroutine compose_or: called CC=1 SCAR=0.0
Pod documented
86 2 my ($self, $other) = @_;
87 2 $self->or($other->add_parens->as_sql, @{$other->{bind}});
88 }
89  
90 5 ▶sub add_parens {
Subroutine add_parens: called CC=1 SCAR=0.0
Pod documented
91 5 my $self = shift;
92 5 $self->{sql} = '('.$self->{sql}.')';
93 5 $self;
94 }
95  
96 10 ▶sub as_sql { $_[0]->{sql} }
Subroutine as_sql: called CC=1 SCAR=0.0
Pod documented
97 6 ▶sub bind { wantarray ? @{$_[0]->{bind}} : $_[0]->{bind} }
Subroutine bind: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 1 6
98  
99 package # hide from PAUSE
100 SQL::Object::Type;
101  
102 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
103 1 my $class = shift;
104 0 ▶ my %args = @_==1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 1 1
105 1 bless {%args}, $class;
106 }
107  
108 1 ▶sub value { ${$_[0]->{value_ref}} }
Subroutine value: called CC=1 SCAR=0.0
109 1 ▶sub value_ref { $_[0]->{value_ref} }
Subroutine value_ref: called CC=1 SCAR=0.0
110 1 ▶sub type { $_[0]->{type} }
Subroutine type: called CC=1 SCAR=0.0
111  
112 2 1;
113 __END__
↑ Summary