added experimental DBIx::Cus...
|
1 |
package DBIx::Custom::Where; |
2 | ||
3 |
use strict; |
|
4 |
use warnings; |
|
5 | ||
6 |
use base 'Object::Simple'; |
|
7 | ||
8 |
use overload 'bool' => sub {1}, fallback => 1; |
|
9 |
use overload '""' => sub { shift->to_string }, fallback => 1; |
|
10 | ||
11 |
use Carp 'croak'; |
|
12 | ||
updated document
|
13 |
# Carp trust relationship |
14 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
15 | ||
added test
|
16 |
__PACKAGE__->attr( |
cleanup
|
17 |
[qw/param query_builder/], |
18 |
clause => sub { [] }, |
|
added test
|
19 |
); |
added experimental DBIx::Cus...
|
20 | |
changed DBIx::Custom::Where ...
|
21 |
sub to_string { |
added test
|
22 |
my $self = shift; |
added experimental DBIx::Cus...
|
23 |
|
many changed
|
24 |
# Clause |
added test
|
25 |
my $clause = $self->clause; |
changed DBIx::Custom::Where ...
|
26 |
$clause = ['and', $clause] unless ref $clause eq 'ARRAY'; |
added test
|
27 |
$clause->[0] = 'and' unless @$clause; |
28 | ||
many changed
|
29 |
# Parse |
added test
|
30 |
my $where = []; |
31 |
my $count = {}; |
|
many changed
|
32 |
$self->_parse($clause, $where, $count, 'and'); |
changed DBIx::Custom::Where ...
|
33 |
|
many changed
|
34 |
# Stringify |
35 |
unshift @$where, 'where' if @$where; |
|
added test
|
36 |
return join(' ', @$where); |
added experimental DBIx::Cus...
|
37 |
} |
38 | ||
added test
|
39 |
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/; |
many changed
|
40 |
sub _parse { |
added test
|
41 |
my ($self, $clause, $where, $count, $op) = @_; |
added experimental DBIx::Cus...
|
42 |
|
many changed
|
43 |
# Array |
changed DBIx::Custom::Where ...
|
44 |
if (ref $clause eq 'ARRAY') { |
many changed
|
45 |
|
46 |
# Start |
|
added test
|
47 |
push @$where, '('; |
changed DBIx::Custom::Where ...
|
48 |
|
many changed
|
49 |
# Operation |
changed DBIx::Custom::Where ...
|
50 |
my $op = $clause->[0] || ''; |
51 |
croak qq{"$op" is invalid operation} |
|
52 |
unless $VALID_OPERATIONS{$op}; |
|
many changed
|
53 |
|
54 |
# Parse internal clause |
|
changed DBIx::Custom::Where ...
|
55 |
for (my $i = 1; $i < @$clause; $i++) { |
many changed
|
56 |
my $pushed = $self->_parse($clause->[$i], $where, $count, $op); |
added test
|
57 |
push @$where, $op if $pushed; |
changed DBIx::Custom::Where ...
|
58 |
} |
added test
|
59 |
pop @$where if $where->[-1] eq $op; |
60 |
|
|
many changed
|
61 |
# Undo |
added test
|
62 |
if ($where->[-1] eq '(') { |
63 |
pop @$where; |
|
64 |
pop @$where; |
|
65 |
} |
|
many changed
|
66 |
# End |
renamed DBIx::Custom::TagPro...
|
67 |
else { push @$where, ')' } |
changed DBIx::Custom::Where ...
|
68 |
} |
many changed
|
69 |
|
70 |
# String |
|
changed DBIx::Custom::Where ...
|
71 |
else { |
72 |
|
|
added test
|
73 |
# Column |
74 |
my $columns = $self->query_builder->build_query($clause)->columns; |
|
added experimental not_exist...
|
75 |
croak qq{Each tag contains one column name: tag "$clause"} |
changed DBIx::Custom::Where ...
|
76 |
unless @$columns == 1; |
77 |
my $column = $columns->[0]; |
|
78 |
|
|
many changed
|
79 |
# Column count up |
added test
|
80 |
my $count = ++$count->{$column}; |
changed DBIx::Custom::Where ...
|
81 |
|
many changed
|
82 |
# Push |
83 |
my $param = $self->param; |
|
added test
|
84 |
my $pushed; |
added experimental not_exist...
|
85 |
if (ref $param eq 'HASH') { |
renamed DBIx::Custom::TagPro...
|
86 |
if (exists $param->{$column}) { |
87 |
if (ref $param->{$column} eq 'ARRAY') { |
|
added experimental not_exist...
|
88 |
$pushed = 1 |
89 |
if exists $param->{$column}->[$count - 1] |
|
90 |
&& ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists'; |
|
91 |
} |
|
renamed DBIx::Custom::TagPro...
|
92 |
elsif ($count == 1) { |
93 |
$pushed = 1; |
|
94 |
} |
|
added experimental DBIx::Cus...
|
95 |
} |
renamed DBIx::Custom::TagPro...
|
96 |
push @$where, $clause if $pushed; |
97 |
} |
|
added experimental not_exist...
|
98 |
elsif (!defined $param) { |
renamed DBIx::Custom::TagPro...
|
99 |
push @$where, $clause; |
100 |
$pushed = 1; |
|
added experimental DBIx::Cus...
|
101 |
} |
added experimental not_exist...
|
102 |
else { croak "Parameter must be hash reference or undfined value" } |
added test
|
103 |
|
104 |
return $pushed; |
|
added experimental DBIx::Cus...
|
105 |
} |
106 |
} |
|
107 | ||
108 |
1; |
|
109 | ||
110 |
=head1 NAME |
|
111 | ||
112 |
DBIx::Custom::Where - Where clause |
|
113 | ||
114 |
=head1 SYNOPSYS |
|
115 | ||
many changed
|
116 |
my $where = DBIx::Custom::Where->new; |
added experimental DBIx::Cus...
|
117 | |
118 |
=head1 ATTRIBUTES |
|
119 | ||
120 |
=head2 C<param> |
|
121 | ||
122 |
my $param = $where->param; |
|
123 |
$where = $where->param({title => 'Perl', |
|
124 |
date => ['2010-11-11', '2011-03-05']}, |
|
125 |
name => ['Ken', 'Taro']); |
|
126 |
=head1 METHODS |
|
127 | ||
128 |
=head2 C<clause> |
|
129 | ||
added test
|
130 |
$where->clause( |
131 |
['and', '{= title}', ['or', '{< date}', '{> date}']] |
|
132 |
); |
|
added experimental DBIx::Cus...
|
133 | |
many changed
|
134 |
Where clause. Above one is expanded to the following SQL by to_string |
135 |
If all parameter names is exists. |
|
136 | ||
137 |
"where ( {= title} and ( {< date} or {> date} ) )" |
|
added experimental DBIx::Cus...
|
138 | |
139 |
=head2 C<to_string> |
|
140 | ||
141 |
$where->to_string; |
|
added test
|
142 | |
143 |
Convert where clause to string correspoinding to param name. |
|
144 |