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/WhereBuilder/RawSQL.pm ↑ Summary blib/lib/DBIx/QuickORM/SchemaBuilder/View.pm »
1 package DBIx::QuickORM::WhereBuilder::Simple;
2 261 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 261 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 261 our $VERSION = '0.000033';
6  
7 261 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 261 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 It is in the ORM's default chain, so a handle's C<where> reaches it:
32  
33 # SELECT ... FROM "users" WHERE ( ( "email" IS NULL AND "name" = ? ) )
34 my @rows = $orm->handle('users')
35 ->where({
36 name => 'Bob',
37 email => undef,
38 })
39 ->all;
40  
41 # declined, left to the next builder: a value is a reference
42 my $like = $orm->handle('users')->where({name => {-like => 'B%'}});
43  
44 The doubled parentheses are L<SQL::Abstract>'s, kept so both builders write
45 the same statement.
46  
47 =head1 PUBLIC METHODS
48  
49 =over 4
50  
51 =item $bool = DBIx::QuickORM::WhereBuilder::Simple->can_build_where($where)
52  
53 =item ($sql, \@binds) = DBIx::QuickORM::WhereBuilder::Simple->build_where($where, $source, $dialect)
54  
55 As L<DBIx::QuickORM::Role::WhereBuilder> describes. Fields are sorted and
56 quoted through the dialect.
57  
58 my $simple = 'DBIx::QuickORM::WhereBuilder::Simple';
59  
60 my $yes = $simple->can_build_where({'users.name' => 'Bob'}); # 1: table.field keys too
61 my $no = $simple->can_build_where({-or => [{name => 'Bob'}]}); # 0
62  
63 my ($sql, $binds) = $simple->build_where(
64 {
65 name => 'Bob',
66 email => undef,
67 },
68 $orm->handle('users')->source,
69 $orm->connection->dialect,
70 );
71 # $sql: ( ( "email" IS NULL AND "name" = ? ) )
72 # $binds: [['name', 'Bob']] # IS NULL takes no bind
73  
74 An empty hashref is accepted and is no condition: C<('', [])>.
75  
76 =back
77  
78 =cut
79  
80 573 ▶sub can_build_where {
Subroutine can_build_where: called CC=5 SCAR=20.9
Pod documented
81 0 ▶ my $class = shift;
Subroutine can_build_where: not called CC=5 SCAR=20.9
Pod documented
82 0 my ($where) = @_;
83 0 ▶ return 0 unless ref($where) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $where eq "HASH" 6 567 573
unless ref $where eq "HASH" 0 0 0
84  
85 0 for my $key (keys %$where) {
86 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/ 73 793 866
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
694793
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
87 }
88  
89 0 return 1;
90 }
91  
92 493 ▶sub build_where {
Subroutine build_where: called CC=5 SCAR=20.9
Pod documented
93 0 ▶ my $class = shift;
Subroutine build_where: not called CC=5 SCAR=20.9
Pod documented
94 0 my ($where, $source, $dialect) = @_;
95  
96 0 my (@sql, @binds);
97 0 for my $key (sort keys %$where) {
98 0 my $name = join '.' => map { $dialect->quote_identifier($_) } split m/\./, $key;
99 0 my $value = $where->{$key};
100 0 ▶ if (defined $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value) { } 690 101 791
if (defined $value) { } 0 0 0
101 0 push @sql => "$name = ?";
102 0 push @binds => [$key => $value];
103 }
104 else {
105 0 push @sql => "$name IS NULL";
106 }
107 }
108  
109 0 ▶ return ('', []) unless @sql;
Branch: 2 branches
BranchTrueFalseTotal
unless @sql 8 485 493
unless @sql 0 0 0
110  
111 # The parentheses SQL::Abstract writes, so both give the same statement.
112 0 ▶ return (@sql == 1 ? "( $sql[0] )" : '( ( ' . join(' AND ' => @sql) . ' ) )', \@binds);
Branch: 2 branches
BranchTrueFalseTotal
@sql == 1 ? : 299 186 485
@sql == 1 ? : 0 0 0
113 }
114  
115 261 1;
116  
117 __END__
« blib/lib/DBIx/QuickORM/WhereBuilder/RawSQL.pm ↑ Summary blib/lib/DBIx/QuickORM/SchemaBuilder/View.pm »