blib/lib/DBIx/QuickORM/WhereBuilder/Simple.pm

filter: Statementstmt 62.0% 31 / 50 Branchbran 50.0% 10 / 20 Conditioncond 50.0% 3 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 71.4% 5 / 7 Podpod 100% 4 / 4 totaltotal 60.4% 55 / 91 CC 17 SCAR 19.7
CC
17
Cov
58%
can_build_where
8.1
can_build_where
8.1
build_where
8.1
CRAP 7.2
« blib/lib/DBIx/QuickORM/Role/Row.pm ↑ Summary blib/lib/DBIx/QuickORM/Txn.pm »
1 package DBIx::QuickORM::WhereBuilder::Simple;
2 306 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 306 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 306 our $VERSION = '0.000032';
6  
7 306 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 306 with 'DBIx::QuickORM::Role::WhereBuilder';
9  
10 =pod
11  
12 =encoding UTF-8
13  
14 =head1 NAME
15  
16 DBIx::QuickORM::WhereBuilder::Simple - Fast equality conditions.
17  
18 =head1 DESCRIPTION
19  
20 Takes a C<where> that is a hashref of field names to plain values: each
21 field equals its value, and an undef value means C<IS NULL>. The SQL and
22 binds are exactly what L<DBIx::QuickORM::WhereBuilder::SQLAbstract> gives for
23 the same hashref, built without L<SQL::Abstract>.
24  
25 It declines anything else: a value that is a reference (including value
26 markers and operator hashes), and a key that is not a word or C<table.word>
27 (including C<-and> and C<-or>).
28  
29 =head1 SYNOPSIS
30  
31 my @rows = $orm->handle('users')
32 ->where({
33 name => 'Bob',
34 email => undef,
35 })
36 ->all;
37  
38 =head1 PUBLIC METHODS
39  
40 =over 4
41  
42 =item $bool = DBIx::QuickORM::WhereBuilder::Simple->can_build_where($where)
43  
44 =item ($sql, \@binds) = DBIx::QuickORM::WhereBuilder::Simple->build_where($where, $source, $dialect)
45  
46 As L<DBIx::QuickORM::Role::WhereBuilder> describes. Fields are sorted and
47 quoted through the dialect.
48  
49 =back
50  
51 =cut
52  
53 519 ▶sub can_build_where {
Subroutine can_build_where: called CC=5 SCAR=20.9
Pod documented
54 0 ▶ my $class = shift;
Subroutine can_build_where: not called CC=5 SCAR=20.9
Pod documented
55 0 my ($where) = @_;
56 0 ▶ return 0 unless ref($where) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $where eq "HASH" 6 513 519
unless ref $where eq "HASH" 0 0 0
57  
58 0 for my $key (keys %$where) {
59 0 ▶ return 0 if ref($where->{$key}) || $key !~ m/^\w+(?:\.\w+)?\z/;
Branch: 2 branches
BranchTrueFalseTotal
if ref $where->{$key} or not $key =~ /^\w+(?:\.\w+)?\z/ 58 750 808
if ref $where->{$key} or not $key =~ /^\w+(?:\.\w+)?\z/ 0 0 0
Condition50%3/6
ref $where->{$key} or not $key =~ /^\w+(?:\.\w+)?\z/
l!l&&r!l&&!r
544750
ref $where->{$key} or not $key =~ /^\w+(?:\.\w+)?\z/
l!l&&r!l&&!r
000
MC/DC100%2/2
ref $where->{$key} or not $key =~ /^\w+(?:\.\w+)?\z/
ref $where->{$key} not $key =~ /^\w+(?:\.\w+)?\z/
MC/DC0%0/2
ref $where->{$key} or not $key =~ /^\w+(?:\.\w+)?\z/
ref $where->{$key} not $key =~ /^\w+(?:\.\w+)?\z/
Truth table100%3/3
A or not(B)
A= ref $where->{$key}B= not $key =~ /^\w+(?:\.\w+)?\z/
ABresult
1X1
011
000
Truth table0%0/3
A or not(B)
A= ref $where->{$key}B= not $key =~ /^\w+(?:\.\w+)?\z/
ABresult
1X1
011
000
60 }
61  
62 0 return 1;
63 }
64  
65 454 ▶sub build_where {
Subroutine build_where: called CC=5 SCAR=20.9
Pod documented
66 0 ▶ my $class = shift;
Subroutine build_where: not called CC=5 SCAR=20.9
Pod documented
67 0 my ($where, $source, $dialect) = @_;
68  
69 0 my (@sql, @binds);
70 0 for my $key (sort keys %$where) {
71 0 my $name = join '.' => map { $dialect->quote_identifier($_) } split m/\./, $key;
72 0 my $value = $where->{$key};
73 0 ▶ if (defined $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value) { } 646 101 747
if (defined $value) { } 0 0 0
74 0 push @sql => "$name = ?";
75 0 push @binds => [$key => $value];
76 }
77 else {
78 0 push @sql => "$name IS NULL";
79 }
80 }
81  
82 0 ▶ return ('', []) unless @sql;
Branch: 2 branches
BranchTrueFalseTotal
unless @sql 8 446 454
unless @sql 0 0 0
83  
84 # The parentheses SQL::Abstract writes, so both give the same statement.
85 0 ▶ return (@sql == 1 ? "( $sql[0] )" : '( ( ' . join(' AND ' => @sql) . ' ) )', \@binds);
Branch: 2 branches
BranchTrueFalseTotal
@sql == 1 ? : 265 181 446
@sql == 1 ? : 0 0 0
86 }
87  
88 306 1;
89  
90 __END__
« blib/lib/DBIx/QuickORM/Role/Row.pm ↑ Summary blib/lib/DBIx/QuickORM/Txn.pm »