| 1 | |||
| 2 | =head1 Name | ||
| 3 | |||
| 4 | QBit::Application::Model::DB::Filter | ||
| 5 | |||
| 6 | =head1 Description | ||
| 7 | |||
| 8 | Base class for DB filters. | ||
| 9 | |||
| 10 | =cut | ||
| 11 | |||
| 12 | package QBit::Application::Model::DB::Filter; | ||
| 13 | $QBit::Application::Model::DB::Filter::VERSION = '0.030'; | ||
| 14 | use qbit; | ||
| 15 | |||
| 16 | use base qw(QBit::Application::Model::DB::Class); | ||
| 17 | |||
| 18 | =head1 Package methods | ||
| 19 | |||
| 20 | =head2 new | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | my ($class, $filter, %opts) = @_; | ||
| 26 | |||
| 27 | return $filter if blessed($filter) && $filter->isa(__PACKAGE__); | ||
| 28 | |||
| 29 | my $self = $class->SUPER::new(%opts); | ||
| 30 | |||
| 31 | $self->_add($filter) if defined($filter); | ||
| 32 | |||
| 33 | return $self; | ||
| 34 | } | ||
| 35 | |||
| 36 | =head2 and | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub and { | ||
| 41 | $_[0]->_add($_[1], type => 'AND'); | ||
| 42 | } | ||
| 43 | |||
| 44 | =head2 or | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub or { | ||
| 49 | $_[0]->_add($_[1], type => 'OR'); | ||
| 50 | } | ||
| 51 | |||
| 52 | =head2 and_not | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub and_not { | ||
| 57 | $_[0]->_add($_[1], type => 'AND NOT'); | ||
| 58 | } | ||
| 59 | |||
| 60 | =head2 or_not | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub or_not { | ||
| 65 | $_[0]->_add($_[1], type => 'OR NOT'); | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 expression | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub expression { | ||
| 73 | my ($self) = @_; | ||
| 74 | |||
| 75 | return exists($self->{'__FILTER__'}) ? $self->{'__FILTER__'} : (); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _add { | ||
| 79 | my ($self, $filter, %opts) = @_; | ||
| 80 | |||
| 81 | if (ref($filter) eq 'HASH') { | ||
| 82 | $filter = clone($filter); | ||
| 83 | my @cmprs = (); | ||
| 84 | push(@cmprs, [$_ => '=' => \$filter->{$_}]) foreach sort keys(%$filter); | ||
| 85 | $filter = [AND => \@cmprs]; | ||
| 86 | } | ||
| 87 | |||
| 88 | if (ref($filter) eq 'ARRAY' && @$filter == 3) { | ||
| 89 | $filter = [AND => [$filter]]; | ||
| 90 | } | ||
| 91 | |||
| 92 | if (blessed($filter)) { | ||
| 93 | throw gettext('Bad filter object: %s is not %s descendant', ref($filter), __PACKAGE__) | ||
| 94 | unless $filter->isa(__PACKAGE__); | ||
| 95 | $filter = $filter->{'__FILTER__'}; | ||
| 96 | } | ||
| 97 | |||
| 98 | return $self unless defined($filter); | ||
| 99 | |||
| 100 | my ($type, $not) = split(/\s+/, $opts{'type'} || 'AND'); | ||
| 101 | |||
| 102 | if (exists($self->{'__FILTER__'})) { | ||
| 103 | if ($self->{'__FILTER__'}[0] eq $type) { | ||
| 104 | push( | ||
| 105 | @{$self->{'__FILTER__'}[1]}, | ||
| 106 | $not ? {NOT => [$filter]} : ($filter->[0] eq $type ? @{$filter->[1]} : $filter) | ||
| 107 | ); | ||
| 108 | } else { | ||
| 109 | $self->{'__FILTER__'} = [$type => [$self->{'__FILTER__'}, $not ? {NOT => [$filter]} : $filter]]; | ||
| 110 | } | ||
| 111 | } else { | ||
| 112 | $self->{'__FILTER__'} = $filter; | ||
| 113 | } | ||
| 114 | |||
| 115 | return $self; | ||
| 116 | } | ||
| 117 | |||
| 118 | TRUE; | ||
| 119 | |||
| 120 | =pod | ||
| 121 | |||
| 122 | For more information see code and test. | ||
| 123 | |||
| 124 | =cut |