| 1 | package Class::DBI::Search::Basic; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | Class::DBI::Search::Basic - Simple Class::DBI search | ||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | my $searcher = Class::DBI::Search::Basic->new( | ||||||||||||||||||||||||||||||||||
| 10 | $cdbi_class, @search_args | ||||||||||||||||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | my @results = $searcher->run_search; | ||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | # Over in your Class::DBI subclass: | ||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | __PACKAGE__->add_searcher( | ||||||||||||||||||||||||||||||||||
| 18 | search => "Class::DBI::Search::Basic", | ||||||||||||||||||||||||||||||||||
| 19 | isearch => "Class::DBI::Search::Plugin::CaseInsensitive", | ||||||||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | This is the start of a pluggable Search infrastructure for Class::DBI. | ||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | At the minute Class::DBI::Search::Basic doubles up as both the default | ||||||||||||||||||||||||||||||||||
| 27 | search within Class::DBI as well as the search base class. We will | ||||||||||||||||||||||||||||||||||
| 28 | probably need to tease this apart more later and create an abstract base | ||||||||||||||||||||||||||||||||||
| 29 | class for search plugins. | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | =head1 METHODS | ||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | =head2 new | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | my $searcher = Class::DBI::Search::Basic->new( | ||||||||||||||||||||||||||||||||||
| 36 | $cdbi_class, @search_args | ||||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | A Searcher is created with the class to which the results will belong, | ||||||||||||||||||||||||||||||||||
| 40 | and the arguments passed to the search call by the user. | ||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | =head2 opt | ||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | if (my $order = $self->opt('order_by')) { ... } | ||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | The arguments passed to search may contain an options hash. This will | ||||||||||||||||||||||||||||||||||
| 47 | return the value of a given option. | ||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | =head2 run_search | ||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | my @results = $searcher->run_search; | ||||||||||||||||||||||||||||||||||
| 52 | my $iterator = $searcher->run_search; | ||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | Actually run the search. | ||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | =head1 SUBCLASSING | ||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | =head2 sql / bind / fragment | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | The actual mechanics of generating the SQL and executing it split up | ||||||||||||||||||||||||||||||||||
| 61 | into a variety of methods for you to override. | ||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||
| 63 | run_search() is implemented as: | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | return $cdbi->sth_to_objects($self->sql, $self->bind); | ||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||
| 67 | Where sql() is | ||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | $cdbi->sql_Retrieve($self->fragment); | ||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | There are also a variety of private methods underneath this that could | ||||||||||||||||||||||||||||||||||
| 73 | be overriden in a pinch, but if you need to do this I'd rather you let | ||||||||||||||||||||||||||||||||||
| 74 | me know so that I can make them public, or at least so that I don't | ||||||||||||||||||||||||||||||||||
| 75 | remove them from under your feet. | ||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||
| 77 | =cut | ||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 80 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | 5 | ▶ | use base 'Class::Accessor::Fast'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 83 | 5 | __PACKAGE__->mk_accessors(qw/class args opts type/); | |||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 86 | 0 | my ($me, $proto, @args) = @_; | |||||||||||||||||||||||||||||||||
| 87 | 0 | my ($args, $opts) = $me->_unpack_args(@args); | |||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | bless { | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||
| 89 | class => ref $proto || $proto, | ||||||||||||||||||||||||||||||||||
| 90 | args => $args, | ||||||||||||||||||||||||||||||||||
| 91 | opts => $opts, | ||||||||||||||||||||||||||||||||||
| 92 | type => "=", | ||||||||||||||||||||||||||||||||||
| 93 | } => $me; | ||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub opt { | ||||||||||||||||||||||||||||||||
|
Subroutine
opt: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 97 | 0 | my ($self, $option) = @_; | |||||||||||||||||||||||||||||||||
| 98 | 0 | $self->{opts}->{$option}; | |||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub _unpack_args { | ||||||||||||||||||||||||||||||||
|
Subroutine
_unpack_args: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||
| 102 | 0 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | @args = %{ $args[0] } if ref $args[0] eq "HASH"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | my $opts = @args % 2 ? pop @args : {}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 105 | 0 | return (\@args, $opts); | |||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub _search_for { | ||||||||||||||||||||||||||||||||
|
Subroutine
_search_for: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||
| 109 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 110 | 0 | my @args = @{ $self->{args} }; | |||||||||||||||||||||||||||||||||
| 111 | 0 | my $class = $self->{class}; | |||||||||||||||||||||||||||||||||
| 112 | 0 | my %search_for; | |||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | while (my ($col, $val) = splice @args, 0, 2) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 114 | my $column = $class->find_column($col) | ||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | || (List::Util::first { $_->accessor eq $col } $class->columns) | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/6
$class->find_column($col) || &List::Util::first(sub {
$_->accessor eq $col;
}
, $class->columns) || $class->_croak("$col is not a column of $class")
$class->find_column($col) or &List::Util::first(sub {
$_->accessor eq $col;
}
, $class->columns)
MC/DC0%0/3
$class->find_column($col) || &List::Util::first(sub {
$_->accessor eq $col;
}
, $class->columns) || $class->_croak("$col is not a column of $class")
$class->find_column($col)
&List::Util::first(sub {
$_->accessor eq $col;
}
, $class->columns)
$class->_croak("$col is not a column of $class")
Truth table0%0/4
A or B || C
A= $class->find_column($col)B= &List::Util::first(sub {
$_->accessor eq $col;
}
, $class->columns)C= $class->_croak("$col is not a column of $class")
| |||||||||||||||||||||||||||||||||||
| 116 | || $class->_croak("$col is not a column of $class"); | ||||||||||||||||||||||||||||||||||
| 117 | 0 | $search_for{$column} = $class->_deflated_column($column, $val); | |||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||
| 119 | 0 | return \%search_for; | |||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub _qual_bind { | ||||||||||||||||||||||||||||||||
|
Subroutine
_qual_bind: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||
| 123 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | $self->{_qual_bind} ||= do { | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"_qual_bind"} ||= do {
my $search_for = $self->_search_for;
my $type = $self->type;
my(@qual, @bind);
foreach my $column (sort keys %$search_for) {
if (defined(my $value = $search_for->{$column})) {
push @qual, "$column $type ?";
push @bind, $value;
}
else {
push @qual, "$column IS NULL";
};
};
[\@qual, \@bind]
}
MC/DC0%0/2
$self->{"_qual_bind"} ||= do {
my $search_for = $self->_search_for;
my $type = $self->type;
my(@qual, @bind);
foreach my $column (sort keys %$search_for) {
if (defined(my $value = $search_for->{$column})) {
push @qual, "$column $type ?";
push @bind, $value;
}
else {
push @qual, "$column IS NULL";
};
};
[\@qual, \@bind]
}
$self->{"_qual_bind"}
do {
my $search_for = $self->_search_for;
my $type = $self->type;
my(@qual, @bind);
foreach my $column (sort keys %$search_for) {
if (defined(my $value = $search_for->{$column})) {
push @qual, "$column $type ?";
push @bind, $value;
}
else {
push @qual, "$column IS NULL";
};
};
[\@qual, \@bind]
}
Truth table0%0/3
A ||= B
A= $self->{"_qual_bind"}B= do {
my $search_for = $self->_search_for;
my $type = $self->type;
my(@qual, @bind);
foreach my $column (sort keys %$search_for) {
if (defined(my $value = $search_for->{$column})) {
push @qual, "$column $type ?";
push @bind, $value;
}
else {
push @qual, "$column IS NULL";
};
};
[\@qual, \@bind]
}
| |||||||||||||||||||||||||||||||||||
| 125 | 0 | my $search_for = $self->_search_for; | |||||||||||||||||||||||||||||||||
| 126 | 0 | my $type = $self->type; | |||||||||||||||||||||||||||||||||
| 127 | 0 | my (@qual, @bind); | |||||||||||||||||||||||||||||||||
| 128 | 0 | for my $column (sort keys %$search_for) { # sort for prepare_cached | |||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if (defined(my $value = $search_for->{$column})) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 130 | 0 | push @qual, "$column $type ?"; | |||||||||||||||||||||||||||||||||
| 131 | 0 | push @bind, $value; | |||||||||||||||||||||||||||||||||
| 132 | } else { | ||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||
| 134 | # perhaps _carp if $type ne "=" | ||||||||||||||||||||||||||||||||||
| 135 | 0 | push @qual, "$column IS NULL"; | |||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||
| 138 | 0 | [ \@qual, \@bind ]; | |||||||||||||||||||||||||||||||||
| 139 | }; | ||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub _qual { | ||||||||||||||||||||||||||||||||
|
Subroutine
_qual: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 143 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | $self->{_qual} ||= $self->_qual_bind->[0]; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"_qual"} ||= $self->_qual_bind->[0]
MC/DC0%0/2
$self->{"_qual"} ||= $self->_qual_bind->[0]
$self->{"_qual"}
$self->_qual_bind->[0]
Truth table0%0/3
A ||= B
A= $self->{"_qual"}B= $self->_qual_bind->[0]
| |||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | sub bind { | ||||||||||||||||||||||||||||||||
|
Subroutine
bind: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 148 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | $self->{_bind} ||= $self->_qual_bind->[1]; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"_bind"} ||= $self->_qual_bind->[1]
MC/DC0%0/2
$self->{"_bind"} ||= $self->_qual_bind->[1]
$self->{"_bind"}
$self->_qual_bind->[1]
Truth table0%0/3
A ||= B
A= $self->{"_bind"}B= $self->_qual_bind->[1]
| |||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | sub fragment { | ||||||||||||||||||||||||||||||||
|
Subroutine
fragment: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 153 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 154 | 0 | my $frag = join " AND ", @{ $self->_qual }; | |||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if (my $order = $self->opt('order_by')) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 156 | 0 | $frag .= " ORDER BY $order"; | |||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||
| 158 | 0 | return $frag; | |||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | sub sql { | ||||||||||||||||||||||||||||||||
|
Subroutine
sql: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 162 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 163 | 0 | return $self->class->sql_Retrieve($self->fragment); | |||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub run_search { | ||||||||||||||||||||||||||||||||
|
Subroutine
run_search: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 167 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 168 | 0 | my $cdbi = $self->class; | |||||||||||||||||||||||||||||||||
| 169 | 0 | return $cdbi->sth_to_objects($self->sql, $self->bind); | |||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||
| 172 | 5 | 1; | |||||||||||||||||||||||||||||||||