Abstract/Driver/SQL.pm

filter: Statementstmt 98.4% 65 / 66 Branchbran 75.0% 12 / 16 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 4 totaltotal 91.8% 102 / 111 CC 11 SCAR 8.1
CC
11
Cov
94%
convert_sort
5.0
convert_filter
4.0
convert_query
3.0
CRAP 2.3
« Abstract.pm ↑ Summary Abstract/Driver/ArrayOfHashes.pm »
1 package Query::Abstract::Driver::SQL;
2  
3 1 our $VERSION = '0.01';
4  
5 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use base 'Query::Abstract::Driver::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 my %CONVERTORS = (
15 3 ▶ 'eq' => sub {"$_[0] = ?" },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 2 ▶ 'ne' => sub {"$_[0] <> ?" },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 1 ▶ 'lt' => sub {"$_[0] < ?" },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 1 ▶ 'le' => sub {"$_[0] <= ?" },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 1 ▶ 'gt' => sub {"$_[0] > ?" },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 1 ▶ 'ge' => sub {"$_[0] >= ?" },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 2 ▶ '<' => sub {"$_[0] < ?" },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 1 ▶ '>' => sub {"$_[0] > ?" },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 1 ▶ '<=' => sub {"$_[0] <= ?" },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 1 ▶ '>=' => sub {"$_[0] >= ?" },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 4 ▶ 'in' => sub {"$_[0] IN (" . join(', ', ('?') x @{$_[1]} ) . ")"},
Subroutine __ANON__: called CC=1 SCAR=0.0
26 1 ▶ 'like' => sub {"$_[0] LIKE ?" },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 1 );
28  
29 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
30 1 my ( $class, %args ) = @_;
31 1 my $self = $class->SUPER::new(%args);
32 1 ▶ croak "Table name is required!" unless $self->{table};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"table"} 0 1 1
33  
34 1 return $self;
35 }
36  
37 15 ▶sub convert_query {
Subroutine convert_query: called CC=3 SCAR=11.0
Pod undocumented
38 15 my ($self, %query) = @_;
39  
40 15 my $select_sql = "SELECT * FROM $self->{table}";
41  
42 15 my ($where_sql, $bind_values) = $self->convert_filter( $query{where} );
43 15 ▶ $select_sql .= " $where_sql" if $where_sql;
Branch: 1 branch
BranchTrueFalseTotal
if $where_sql 15 0 15
44  
45 15 my $sort_sql = $self->convert_sort( $query{sort_by} );
46 15 ▶ $select_sql .= " $sort_sql" if $sort_sql;
Branch: 1 branch
BranchTrueFalseTotal
if $sort_sql 2 13 15
47  
48 15 return( $select_sql, $bind_values );
49 }
50  
51  
52 15 ▶sub convert_filter {
Subroutine convert_filter: called CC=4 SCAR=13.9
Pod undocumented
53 15 my ( $self, $where ) = @_;
54  
55 15 my @rules;
56 my @bind_values;
57 15 ▶ for ( my $i = 0; $i < @$where; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$where) 19 15 34
58 19 my $field = $where->[$i];
59 19 my $condition = $where->[$i+1];
60 19 my ($oper, $values) = %$condition;
61  
62 19 push @rules, $CONVERTORS{$oper}->($field, $values);
63 19 ▶ push @bind_values, @{ ref($values) ? $values : [$values] };
Branch: 1 branch
BranchTrueFalseTotal
ref $values ? : 4 15 19
64 }
65  
66 15 ▶ if (@rules) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rules) { } 15 0 15
67 15 my $where_str = 'WHERE ' . join(' AND ', @rules);
68 15 return( $where_str, \@bind_values );
69 } else {
70 0 return '';
71 }
72 }
73  
74 15 ▶sub convert_sort {
Subroutine convert_sort: called CC=5 SCAR=16.1
Pod undocumented
75 15 my ( $self, $sort_by ) = @_;
76 15 my @rules;
77  
78 15 foreach my $sort_rule ( @$sort_by ) {
79 3 my ($field, $order) = split(/\s+/, $sort_rule, 2);
80 3 ▶ $order ||='ASC';
Condition100%2/2
$order ||= "ASC"
l!l
21
MC/DC100%1/1
$order ||= "ASC"
$order
Truth table100%2/2
A
A= $order
Aresult
11
00
81  
82 3 push @rules, "$field \U$order\E";
83 }
84  
85 15 ▶ if (@rules) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rules) { } 2 13 15
86 2 ▶ return @rules ? 'ORDER BY ' . join(' ,', @rules) : '';
Branch: 1 branch
BranchTrueFalseTotal
@rules ? : 2 0 2
87 } else {
88 13 return '';
89 }
90 }
91  
92 1 1;
« Abstract.pm ↑ Summary Abstract/Driver/ArrayOfHashes.pm »