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( |
renamed DBIx::Custom::TagPro...
|
17 |
[qw/param query_builder/], |
added test
|
18 |
clause => sub { [] }, |
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/; |
changed DBIx::Custom::Where ...
|
40 | |
many changed
|
41 |
sub _parse { |
added test
|
42 |
my ($self, $clause, $where, $count, $op) = @_; |
added experimental DBIx::Cus...
|
43 |
|
many changed
|
44 |
# Array |
changed DBIx::Custom::Where ...
|
45 |
if (ref $clause eq 'ARRAY') { |
many changed
|
46 |
|
47 |
# Start |
|
added test
|
48 |
push @$where, '('; |
changed DBIx::Custom::Where ...
|
49 |
|
many changed
|
50 |
# Operation |
changed DBIx::Custom::Where ...
|
51 |
my $op = $clause->[0] || ''; |
52 |
croak qq{"$op" is invalid operation} |
|
53 |
unless $VALID_OPERATIONS{$op}; |
|
many changed
|
54 |
|
55 |
# Parse internal clause |
|
changed DBIx::Custom::Where ...
|
56 |
for (my $i = 1; $i < @$clause; $i++) { |
many changed
|
57 |
my $pushed = $self->_parse($clause->[$i], $where, $count, $op); |
added test
|
58 |
push @$where, $op if $pushed; |
changed DBIx::Custom::Where ...
|
59 |
} |
added test
|
60 |
pop @$where if $where->[-1] eq $op; |
61 |
|
|
many changed
|
62 |
# Undo |
added test
|
63 |
if ($where->[-1] eq '(') { |
64 |
pop @$where; |
|
65 |
pop @$where; |
|
66 |
} |
|
many changed
|
67 |
# End |
renamed DBIx::Custom::TagPro...
|
68 |
else { push @$where, ')' } |
changed DBIx::Custom::Where ...
|
69 |
} |
many changed
|
70 |
|
71 |
# String |
|
changed DBIx::Custom::Where ...
|
72 |
else { |
73 |
|
|
added test
|
74 |
# Column |
75 |
my $columns = $self->query_builder->build_query($clause)->columns; |
|
changed DBIx::Custom::Where ...
|
76 |
croak qq{each tag contains one column name: tag "$clause"} |
77 |
unless @$columns == 1; |
|
78 |
my $column = $columns->[0]; |
|
79 |
|
|
many changed
|
80 |
# Column count up |
added test
|
81 |
my $count = ++$count->{$column}; |
changed DBIx::Custom::Where ...
|
82 |
|
many changed
|
83 |
# Push |
84 |
my $param = $self->param; |
|
added test
|
85 |
my $pushed; |
renamed DBIx::Custom::TagPro...
|
86 |
if (defined $param) { |
87 |
if (exists $param->{$column}) { |
|
88 |
if (ref $param->{$column} eq 'ARRAY') { |
|
89 |
$pushed = 1 if exists $param->{$column}->[$count - 1]; |
|
90 |
} |
|
91 |
elsif ($count == 1) { |
|
92 |
$pushed = 1; |
|
93 |
} |
|
added experimental DBIx::Cus...
|
94 |
} |
renamed DBIx::Custom::TagPro...
|
95 |
push @$where, $clause if $pushed; |
96 |
} |
|
97 |
else { |
|
98 |
push @$where, $clause; |
|
99 |
$pushed = 1; |
|
added experimental DBIx::Cus...
|
100 |
} |
added test
|
101 |
|
102 |
return $pushed; |
|
added experimental DBIx::Cus...
|
103 |
} |
104 |
} |
|
105 | ||
106 |
1; |
|
107 | ||
108 |
=head1 NAME |
|
109 | ||
110 |
DBIx::Custom::Where - Where clause |
|
111 | ||
112 |
=head1 SYNOPSYS |
|
113 | ||
many changed
|
114 |
my $where = DBIx::Custom::Where->new; |
added experimental DBIx::Cus...
|
115 | |
116 |
=head1 ATTRIBUTES |
|
117 | ||
118 |
=head2 C<param> |
|
119 | ||
120 |
my $param = $where->param; |
|
121 |
$where = $where->param({title => 'Perl', |
|
122 |
date => ['2010-11-11', '2011-03-05']}, |
|
123 |
name => ['Ken', 'Taro']); |
|
124 |
=head1 METHODS |
|
125 | ||
126 |
=head2 C<clause> |
|
127 | ||
added test
|
128 |
$where->clause( |
129 |
['and', '{= title}', ['or', '{< date}', '{> date}']] |
|
130 |
); |
|
added experimental DBIx::Cus...
|
131 | |
many changed
|
132 |
Where clause. Above one is expanded to the following SQL by to_string |
133 |
If all parameter names is exists. |
|
134 | ||
135 |
"where ( {= title} and ( {< date} or {> date} ) )" |
|
added experimental DBIx::Cus...
|
136 | |
137 |
=head2 C<to_string> |
|
138 | ||
139 |
$where->to_string; |
|
added test
|
140 | |
141 |
Convert where clause to string correspoinding to param name. |
|
142 |