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 | ||
cleanup
|
11 |
use DBIx::Custom::Util '_subname'; |
added experimental DBIx::Cus...
|
12 |
use Carp 'croak'; |
13 | ||
updated document
|
14 |
# Carp trust relationship |
15 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
16 | ||
added test
|
17 |
__PACKAGE__->attr( |
- remaned experimental safty...
|
18 |
[qw/param query_builder safety_character/], |
cleanup
|
19 |
clause => sub { [] }, |
added EXPERIMENTAL reserved_...
|
20 |
reserved_word_quote => '' |
added test
|
21 |
); |
added experimental DBIx::Cus...
|
22 | |
improved error messages
|
23 |
sub new { |
24 |
my $self = shift->SUPER::new(@_); |
|
25 |
|
|
26 |
# Check attribute names |
|
27 |
my @attrs = keys %$self; |
|
28 |
foreach my $attr (@attrs) { |
|
cleanup
|
29 |
croak qq{"$attr" is invalid attribute name (} . _subname . ")" |
improved error messages
|
30 |
unless $self->can($attr); |
31 |
} |
|
32 |
|
|
33 |
return $self; |
|
34 |
} |
|
35 | ||
changed DBIx::Custom::Where ...
|
36 |
sub to_string { |
added test
|
37 |
my $self = shift; |
added experimental DBIx::Cus...
|
38 |
|
- update_param_tag is DEPREC...
|
39 |
# Check if column name is safety character; |
40 |
my $safety = $self->safety_character; |
|
41 |
if (ref $self->param eq 'HASH') { |
|
42 |
foreach my $column (keys %{$self->param}) { |
|
43 |
croak qq{"$column" is not safety column name (} . _subname . ")" |
|
44 |
unless $column =~ /^[$safety\.]+$/; |
|
45 |
} |
|
46 |
} |
|
many changed
|
47 |
# Clause |
added test
|
48 |
my $clause = $self->clause; |
changed DBIx::Custom::Where ...
|
49 |
$clause = ['and', $clause] unless ref $clause eq 'ARRAY'; |
added test
|
50 |
$clause->[0] = 'and' unless @$clause; |
51 | ||
many changed
|
52 |
# Parse |
added test
|
53 |
my $where = []; |
54 |
my $count = {}; |
|
many changed
|
55 |
$self->_parse($clause, $where, $count, 'and'); |
changed DBIx::Custom::Where ...
|
56 |
|
many changed
|
57 |
# Stringify |
58 |
unshift @$where, 'where' if @$where; |
|
added test
|
59 |
return join(' ', @$where); |
added experimental DBIx::Cus...
|
60 |
} |
61 | ||
added test
|
62 |
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/; |
many changed
|
63 |
sub _parse { |
added test
|
64 |
my ($self, $clause, $where, $count, $op) = @_; |
added experimental DBIx::Cus...
|
65 |
|
many changed
|
66 |
# Array |
changed DBIx::Custom::Where ...
|
67 |
if (ref $clause eq 'ARRAY') { |
many changed
|
68 |
|
69 |
# Start |
|
added test
|
70 |
push @$where, '('; |
changed DBIx::Custom::Where ...
|
71 |
|
many changed
|
72 |
# Operation |
changed DBIx::Custom::Where ...
|
73 |
my $op = $clause->[0] || ''; |
cleanup
|
74 |
croak qq{"$op" is invalid operation (} . _subname . ")" |
changed DBIx::Custom::Where ...
|
75 |
unless $VALID_OPERATIONS{$op}; |
many changed
|
76 |
|
77 |
# Parse internal clause |
|
changed DBIx::Custom::Where ...
|
78 |
for (my $i = 1; $i < @$clause; $i++) { |
many changed
|
79 |
my $pushed = $self->_parse($clause->[$i], $where, $count, $op); |
added test
|
80 |
push @$where, $op if $pushed; |
changed DBIx::Custom::Where ...
|
81 |
} |
added test
|
82 |
pop @$where if $where->[-1] eq $op; |
83 |
|
|
many changed
|
84 |
# Undo |
added test
|
85 |
if ($where->[-1] eq '(') { |
86 |
pop @$where; |
|
87 |
pop @$where; |
|
88 |
} |
|
many changed
|
89 |
# End |
renamed DBIx::Custom::TagPro...
|
90 |
else { push @$where, ')' } |
changed DBIx::Custom::Where ...
|
91 |
} |
many changed
|
92 |
|
93 |
# String |
|
changed DBIx::Custom::Where ...
|
94 |
else { |
DBIx::Custom::Where clause a...
|
95 |
# Pushed |
96 |
my $pushed; |
|
changed DBIx::Custom::Where ...
|
97 |
|
added test
|
98 |
# Column |
99 |
my $columns = $self->query_builder->build_query($clause)->columns; |
|
DBIx::Custom::Where clause a...
|
100 |
if (@$columns == 0) { |
101 |
push @$where, $clause; |
|
102 |
$pushed = 1; |
|
103 |
return $pushed; |
|
104 |
} |
|
105 |
elsif (@$columns != 1) { |
|
- update_param_tag is DEPREC...
|
106 |
croak qq{Each part contains one column name: "$clause" (} |
cleanup
|
107 |
. _subname . ")"; |
DBIx::Custom::Where clause a...
|
108 |
} |
109 | ||
changed DBIx::Custom::Where ...
|
110 |
my $column = $columns->[0]; |
added EXPERIMENTAL reserved_...
|
111 |
if (my $q = $self->reserved_word_quote) { |
112 |
$column =~ s/$q//g; |
|
113 |
} |
|
114 |
|
|
- remaned experimental safty...
|
115 |
my $safety = $self->safety_character; |
cleanup
|
116 |
croak qq{"$column" is not safety column name (} . _subname . ")" |
- remaned experimental safty...
|
117 |
unless $column =~ /^[$safety\.]+$/; |
changed DBIx::Custom::Where ...
|
118 |
|
many changed
|
119 |
# Column count up |
added test
|
120 |
my $count = ++$count->{$column}; |
changed DBIx::Custom::Where ...
|
121 |
|
many changed
|
122 |
# Push |
123 |
my $param = $self->param; |
|
added experimental not_exist...
|
124 |
if (ref $param eq 'HASH') { |
renamed DBIx::Custom::TagPro...
|
125 |
if (exists $param->{$column}) { |
126 |
if (ref $param->{$column} eq 'ARRAY') { |
|
added experimental not_exist...
|
127 |
$pushed = 1 |
128 |
if exists $param->{$column}->[$count - 1] |
|
129 |
&& ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists'; |
|
130 |
} |
|
renamed DBIx::Custom::TagPro...
|
131 |
elsif ($count == 1) { |
132 |
$pushed = 1; |
|
133 |
} |
|
added experimental DBIx::Cus...
|
134 |
} |
renamed DBIx::Custom::TagPro...
|
135 |
push @$where, $clause if $pushed; |
136 |
} |
|
added experimental not_exist...
|
137 |
elsif (!defined $param) { |
renamed DBIx::Custom::TagPro...
|
138 |
push @$where, $clause; |
139 |
$pushed = 1; |
|
added experimental DBIx::Cus...
|
140 |
} |
improved error messages
|
141 |
else { |
cleanup
|
142 |
croak "Parameter must be hash reference or undfined value (" |
143 |
. _subname . ")" |
|
improved error messages
|
144 |
} |
added test
|
145 |
return $pushed; |
added experimental DBIx::Cus...
|
146 |
} |
147 |
} |
|
148 | ||
149 |
1; |
|
150 | ||
151 |
=head1 NAME |
|
152 | ||
removed EXPERIMETNAL flag fr...
|
153 |
DBIx::Custom::Where - Where clause |
added experimental DBIx::Cus...
|
154 | |
155 |
=head1 SYNOPSYS |
|
156 | ||
many changed
|
157 |
my $where = DBIx::Custom::Where->new; |
added experimental DBIx::Cus...
|
158 | |
159 |
=head1 ATTRIBUTES |
|
160 | ||
161 |
=head2 C<clause> |
|
162 | ||
added test
|
163 |
$where->clause( |
164 |
['and', '{= title}', ['or', '{< date}', '{> date}']] |
|
165 |
); |
|
added experimental DBIx::Cus...
|
166 | |
many changed
|
167 |
Where clause. Above one is expanded to the following SQL by to_string |
168 |
If all parameter names is exists. |
|
169 | ||
170 |
"where ( {= title} and ( {< date} or {> date} ) )" |
|
added experimental DBIx::Cus...
|
171 | |
update pod
|
172 |
=head2 C<param> |
173 | ||
174 |
my $param = $where->param; |
|
175 |
$where = $where->param({title => 'Perl', |
|
176 |
date => ['2010-11-11', '2011-03-05']}, |
|
177 |
name => ['Ken', 'Taro']); |
|
178 | ||
- remaned experimental safty...
|
179 |
=head2 C<safety_character> |
update pod
|
180 | |
- remaned experimental safty...
|
181 |
my $safety_character = $self->safety_character; |
182 |
$dbi = $self->safety_character($name); |
|
update pod
|
183 | |
184 |
=head1 METHODS |
|
185 | ||
added experimental DBIx::Cus...
|
186 |
=head2 C<to_string> |
187 | ||
188 |
$where->to_string; |
|
added test
|
189 | |
190 |
Convert where clause to string correspoinding to param name. |
|
191 | ||
- update_param_tag is DEPREC...
|
192 |
=cut |