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