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

filter: Statementstmt 73.3% 22 / 30 Branchbran n/a Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 83.3% 5 / 6 Podpod 100% 3 / 3 totaltotal 65.3% 32 / 49 CC 3 SCAR 6.0
CC
3
Cov
67%
build_where
2.6
build_where
2.6
BEGIN
1.0
CRAP 1.8
« blib/lib/DBIx/QuickORM/Dialect/MariaDB.pm ↑ Summary blib/lib/DBIx/QuickORM/Role/Type.pm »
1 package DBIx::QuickORM::WhereBuilder::SQLAbstract;
2 263 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 263 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 263 our $VERSION = '0.000033';
6  
7 263 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 263 with 'DBIx::QuickORM::Role::WhereBuilder';
9  
10 =pod
11  
12 =encoding UTF-8
13  
14 =head1 NAME
15  
16 DBIx::QuickORM::WhereBuilder::SQLAbstract - Any where, through SQL::Abstract.
17  
18 =head1 DESCRIPTION
19  
20 Takes every C<where> and renders it as
21 L<DBIx::QuickORM::SQLBuilder::SQLAbstract> documents: L<SQL::Abstract>'s
22 syntax, with value markers anywhere. It is the last where builder in the
23 default chain.
24  
25 =head1 SYNOPSIS
26  
27 It is last in the ORM's default chain, so a handle's C<where> reaches it when
28 nothing before it takes the C<where>. Each of these reaches it:
29  
30 use DBIx::QuickORM::Value qw/sql/;
31  
32 my $users = $orm->handle('users');
33  
34 my $like = $users->where({name => {-like => 'B%'}});
35 my $range = $users->where({
36 id => {
37 '>' => 10,
38 '<' => 20,
39 },
40 });
41 my $in = $users->where({id => {-in => [1, 2, 3]}});
42 my $any = $users->where([{name => 'Bob'}, {email => undef}]); # an arrayref is OR
43 my $mixed = $users->where({
44 name => 'Bob',
45 -or => [
46 {id => 2},
47 {id => {'>' => 10}},
48 ],
49 });
50  
51 # value markers anywhere, and SQL::Abstract's literal SQL
52 my $marker = $users->where({id => {'>' => sql('? * 2', 5)}});
53 my $literal = $users->where({id => \['> ?', [id => 10]]}); # binds as [$field, $value]
54  
55 To render every C<where> through it, make it the whole chain:
56  
57 my $orm = DBIx::QuickORM->new(
58 credentials => \%credentials,
59 where_builders => ['DBIx::QuickORM::WhereBuilder::SQLAbstract'],
60 );
61  
62 =head1 PUBLIC METHODS
63  
64 =over 4
65  
66 =item $bool = DBIx::QuickORM::WhereBuilder::SQLAbstract->can_build_where($where)
67  
68 Always true.
69  
70 =item ($sql, \@binds) = DBIx::QuickORM::WhereBuilder::SQLAbstract->build_where($where, $source, $dialect)
71  
72 As L<DBIx::QuickORM::Role::WhereBuilder> describes.
73  
74 my ($sql, $binds) = DBIx::QuickORM::WhereBuilder::SQLAbstract->build_where(
75 {name => {-like => 'B%'}},
76 $orm->handle('users')->source,
77 $orm->connection->dialect,
78 );
79 # $sql: ( "name" LIKE ? )
80 # $binds: [['name', 'B%']]
81  
82 =back
83  
84 =cut
85  
86 # One shared SQL builder: its convert_where turns value markers into SQL::Abstract's forms, and it keeps an
87 # SQL::Abstract per quote character. Loaded at first use, since that builder loads this class.
88 263 my $SQL_BUILDER;
89  
90 92 ▶sub can_build_where { 1 }
Subroutine can_build_where: called CC=1 SCAR=0.0
Pod documented
91  
92 397 ▶sub build_where {
Subroutine build_where: called CC=2 SCAR=9.7
Pod documented
93 0 ▶ my $class = shift;
Subroutine build_where: not called CC=2 SCAR=9.7
Pod documented
94 0 my ($where, $source, $dialect) = @_;
95  
96 0 ▶ my $builder = $SQL_BUILDER //= do { require DBIx::QuickORM::SQLBuilder::SQLAbstract; DBIx::QuickORM::SQLBuilder::SQLAbstract->new };
Condition33%2/6
$SQL_BUILDER //= do { require DBIx::QuickORM::SQLBuilder::SQLAbstract; "DBIx::QuickORM::SQLBuilder::SQLAbstract"->new }
l!l&&r!l&&!r
377200
$SQL_BUILDER //= do { require DBIx::QuickORM::SQLBuilder::SQLAbstract; "DBIx::QuickORM::SQLBuilder::SQLAbstract"->new }
l!l&&r!l&&!r
000
MC/DC0%0/2
$SQL_BUILDER //= do { require DBIx::QuickORM::SQLBuilder::SQLAbstract; "DBIx::QuickORM::SQLBuilder::SQLAbstract"->new }
$SQL_BUILDER do { require DBIx::QuickORM::SQLBuilder::SQLAbstract; "DBIx::QuickORM::SQLBuilder::SQLAbstract"->new }
MC/DC0%0/2
$SQL_BUILDER //= do { require DBIx::QuickORM::SQLBuilder::SQLAbstract; "DBIx::QuickORM::SQLBuilder::SQLAbstract"->new }
$SQL_BUILDER do { require DBIx::QuickORM::SQLBuilder::SQLAbstract; "DBIx::QuickORM::SQLBuilder::SQLAbstract"->new }
Truth table66%2/3
A //= B
A= $SQL_BUILDERB= do { require DBIx::QuickORM::SQLBuilder::SQLAbstract; "DBIx::QuickORM::SQLBuilder::SQLAbstract"->new }
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $SQL_BUILDERB= do { require DBIx::QuickORM::SQLBuilder::SQLAbstract; "DBIx::QuickORM::SQLBuilder::SQLAbstract"->new }
ABresult
1X1
011
000
97 0 my ($sql, @binds) = $builder->sqla($dialect)->where($builder->convert_where($where));
98 0 $sql =~ s/^ WHERE //;
99 0 return ($sql, \@binds);
100 }
101  
102 263 1;
103  
104 __END__
« blib/lib/DBIx/QuickORM/Dialect/MariaDB.pm ↑ Summary blib/lib/DBIx/QuickORM/Role/Type.pm »