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 | ||
sub module use DBIx::Custom ...
|
12 |
has [qw/dbi param/], |
reserved_word_quote is DEPRE...
|
13 |
clause => sub { [] }; |
added experimental DBIx::Cus...
|
14 | |
improved error messages
|
15 |
sub new { |
16 |
my $self = shift->SUPER::new(@_); |
|
17 |
|
|
18 |
# Check attribute names |
|
19 |
my @attrs = keys %$self; |
|
20 |
foreach my $attr (@attrs) { |
|
cleanup
|
21 |
croak qq{"$attr" is invalid attribute name (} . _subname . ")" |
improved error messages
|
22 |
unless $self->can($attr); |
23 |
} |
|
24 |
|
|
25 |
return $self; |
|
26 |
} |
|
27 | ||
changed DBIx::Custom::Where ...
|
28 |
sub to_string { |
added test
|
29 |
my $self = shift; |
added experimental DBIx::Cus...
|
30 |
|
- update_param_tag is DEPREC...
|
31 |
# Check if column name is safety character; |
cleanup
|
32 |
my $safety = $self->dbi->safety_character; |
- update_param_tag is DEPREC...
|
33 |
if (ref $self->param eq 'HASH') { |
34 |
foreach my $column (keys %{$self->param}) { |
|
35 |
croak qq{"$column" is not safety column name (} . _subname . ")" |
|
36 |
unless $column =~ /^[$safety\.]+$/; |
|
37 |
} |
|
38 |
} |
|
many changed
|
39 |
# Clause |
added test
|
40 |
my $clause = $self->clause; |
changed DBIx::Custom::Where ...
|
41 |
$clause = ['and', $clause] unless ref $clause eq 'ARRAY'; |
added test
|
42 |
$clause->[0] = 'and' unless @$clause; |
43 | ||
many changed
|
44 |
# Parse |
added test
|
45 |
my $where = []; |
46 |
my $count = {}; |
|
many changed
|
47 |
$self->_parse($clause, $where, $count, 'and'); |
changed DBIx::Custom::Where ...
|
48 |
|
many changed
|
49 |
# Stringify |
50 |
unshift @$where, 'where' if @$where; |
|
added test
|
51 |
return join(' ', @$where); |
added experimental DBIx::Cus...
|
52 |
} |
53 | ||
added test
|
54 |
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/; |
many changed
|
55 |
sub _parse { |
added test
|
56 |
my ($self, $clause, $where, $count, $op) = @_; |
added experimental DBIx::Cus...
|
57 |
|
many changed
|
58 |
# Array |
changed DBIx::Custom::Where ...
|
59 |
if (ref $clause eq 'ARRAY') { |
many changed
|
60 |
|
61 |
# Start |
|
added test
|
62 |
push @$where, '('; |
changed DBIx::Custom::Where ...
|
63 |
|
many changed
|
64 |
# Operation |
changed DBIx::Custom::Where ...
|
65 |
my $op = $clause->[0] || ''; |
improved error message
|
66 |
croak qq{First argument must be "and" or "or" in where clause } . |
67 |
qq{"$op" is passed} . _subname . ")" |
|
changed DBIx::Custom::Where ...
|
68 |
unless $VALID_OPERATIONS{$op}; |
many changed
|
69 |
|
fixed DBIx::Custom::Where to...
|
70 |
my $pushed_array; |
many changed
|
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; |
fixed DBIx::Custom::Where to...
|
75 |
$pushed_array = 1 if $pushed; |
changed DBIx::Custom::Where ...
|
76 |
} |
added test
|
77 |
pop @$where if $where->[-1] eq $op; |
78 |
|
|
many changed
|
79 |
# Undo |
added test
|
80 |
if ($where->[-1] eq '(') { |
81 |
pop @$where; |
|
fixed DBIx::Custom::Where to...
|
82 |
pop @$where if ($where->[-1] || '') eq $op; |
added test
|
83 |
} |
many changed
|
84 |
# End |
renamed DBIx::Custom::TagPro...
|
85 |
else { push @$where, ')' } |
fixed DBIx::Custom::Where to...
|
86 |
|
87 |
return $pushed_array; |
|
changed DBIx::Custom::Where ...
|
88 |
} |
many changed
|
89 |
|
90 |
# String |
|
changed DBIx::Custom::Where ...
|
91 |
else { |
DBIx::Custom::Where clause a...
|
92 |
# Pushed |
93 |
my $pushed; |
|
changed DBIx::Custom::Where ...
|
94 |
|
added test
|
95 |
# Column |
cleanup
|
96 |
my $columns = $self->dbi->query_builder->build_query($clause)->columns; |
DBIx::Custom::Where clause a...
|
97 |
if (@$columns == 0) { |
98 |
push @$where, $clause; |
|
99 |
$pushed = 1; |
|
100 |
return $pushed; |
|
101 |
} |
|
102 |
elsif (@$columns != 1) { |
|
- update_param_tag is DEPREC...
|
103 |
croak qq{Each part contains one column name: "$clause" (} |
cleanup
|
104 |
. _subname . ")"; |
DBIx::Custom::Where clause a...
|
105 |
} |
DBIx::Custom::Model type att...
|
106 |
|
107 |
# Remove quote |
|
changed DBIx::Custom::Where ...
|
108 |
my $column = $columns->[0]; |
sub module use DBIx::Custom ...
|
109 |
if (my $q = $self->dbi->_quote) { |
added quote method's two cha...
|
110 |
$q = quotemeta($q); |
111 |
$column =~ s/[$q]//g; |
|
added EXPERIMENTAL reserved_...
|
112 |
} |
113 |
|
|
DBIx::Custom::Model type att...
|
114 |
# Check safety |
cleanup
|
115 |
my $safety = $self->dbi->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 |
} |
fixed DBIx::Custom::Where to...
|
147 |
return; |
added experimental DBIx::Cus...
|
148 |
} |
149 | ||
150 |
1; |
|
151 | ||
152 |
=head1 NAME |
|
153 | ||
removed EXPERIMETNAL flag fr...
|
154 |
DBIx::Custom::Where - Where clause |
added experimental DBIx::Cus...
|
155 | |
156 |
=head1 SYNOPSYS |
|
157 | ||
many changed
|
158 |
my $where = DBIx::Custom::Where->new; |
DBIx::Custom::Model type att...
|
159 |
my $string_where = "$where"; |
added experimental DBIx::Cus...
|
160 | |
161 |
=head1 ATTRIBUTES |
|
162 | ||
163 |
=head2 C<clause> |
|
164 | ||
DBIx::Custom::Model type att...
|
165 |
my $clause = $where->clause; |
166 |
$where = $where->clause( |
|
167 |
['and', |
|
168 |
'title = :title', |
|
169 |
['or', 'date < :date', 'date > :date'] |
|
170 |
] |
|
added test
|
171 |
); |
added experimental DBIx::Cus...
|
172 | |
many changed
|
173 |
Where clause. Above one is expanded to the following SQL by to_string |
174 |
If all parameter names is exists. |
|
175 | ||
DBIx::Custom::Model type att...
|
176 |
"where ( title = :title and ( date < :date or date > :date ) )" |
added experimental DBIx::Cus...
|
177 | |
update pod
|
178 |
=head2 C<param> |
179 | ||
180 |
my $param = $where->param; |
|
DBIx::Custom::Model type att...
|
181 |
$where = $where->param({ |
182 |
title => 'Perl', |
|
183 |
date => ['2010-11-11', '2011-03-05'], |
|
184 |
}); |
|
update pod
|
185 | |
cleanup
|
186 |
=head2 C<dbi> |
update pod
|
187 | |
cleanup
|
188 |
my $dbi = $where->dbi; |
189 |
$where = $where->dbi($dbi); |
|
190 | ||
191 |
L<DBIx::Custom> object. |
|
update pod
|
192 | |
193 |
=head1 METHODS |
|
194 | ||
DBIx::Custom::Model type att...
|
195 |
L<DBIx::Custom::Where> inherits all methods from L<Object::Simple> |
196 |
and implements the following new ones. |
|
197 | ||
added experimental DBIx::Cus...
|
198 |
=head2 C<to_string> |
199 | ||
200 |
$where->to_string; |
|
added test
|
201 | |
DBIx::Custom::Model type att...
|
202 |
Convert where clause to string. |
203 | ||
204 |
double quote is override to execute C<to_string> method. |
|
205 | ||
206 |
my $string_where = "$where"; |
|
added test
|
207 | |
- update_param_tag is DEPREC...
|
208 |
=cut |