| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | for my $key (keys %$where) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | return 0 if ref($where->{$key}) || $key !~ m/^\w+(?:\.\w+)?\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
ref $where->{$key} or not $key =~ /^\w+(?:\.\w+)?\z/
ref $where->{$key} or not $key =~ /^\w+(?:\.\w+)?\z/
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/
Truth table0%0/3
A or not(B)
A= ref $where->{$key}B= not $key =~ /^\w+(?:\.\w+)?\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 306 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||