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