| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | if ( ref $named_bind{$1} && ref $named_bind{$1} eq "ARRAY" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $named_bind{$1} and ref $named_bind{$1} eq "ARRAY"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||