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