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 = {}; |
|
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 |
map cleanup
|
123 |
my $param = $self->param; |
added experimental not_exist...
|
124 |
if (ref $param eq 'HASH') { |
renamed DBIx::Custom::TagPro...
|
125 |
if (exists $param->{$column}) { |
added map method(not complet...
|
126 |
my $if = $self->{_if}; |
added EXPERIMENTAL DBIx::Cus...
|
127 |
|
renamed DBIx::Custom::TagPro...
|
128 |
if (ref $param->{$column} eq 'ARRAY') { |
cleanup
|
129 |
$pushed = 1 if exists $param->{$column}->[$count - 1] |
130 |
&& ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists' |
|
renamed DBIx::Custom::TagPro...
|
131 |
} |
cleanup
|
132 |
elsif ($count == 1) { $pushed = 1 } |
added experimental DBIx::Cus...
|
133 |
} |
renamed DBIx::Custom::TagPro...
|
134 |
push @$where, $clause if $pushed; |
135 |
} |
|
added experimental not_exist...
|
136 |
elsif (!defined $param) { |
renamed DBIx::Custom::TagPro...
|
137 |
push @$where, $clause; |
138 |
$pushed = 1; |
|
added experimental DBIx::Cus...
|
139 |
} |
improved error messages
|
140 |
else { |
cleanup
|
141 |
croak "Parameter must be hash reference or undfined value (" |
142 |
. _subname . ")" |
|
improved error messages
|
143 |
} |
added test
|
144 |
return $pushed; |
added experimental DBIx::Cus...
|
145 |
} |
fixed DBIx::Custom::Where to...
|
146 |
return; |
added experimental DBIx::Cus...
|
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; |
DBIx::Custom::Model type att...
|
158 |
my $string_where = "$where"; |
added experimental DBIx::Cus...
|
159 | |
160 |
=head1 ATTRIBUTES |
|
161 | ||
162 |
=head2 C<clause> |
|
163 | ||
DBIx::Custom::Model type att...
|
164 |
my $clause = $where->clause; |
165 |
$where = $where->clause( |
|
166 |
['and', |
|
167 |
'title = :title', |
|
168 |
['or', 'date < :date', 'date > :date'] |
|
169 |
] |
|
added test
|
170 |
); |
added experimental DBIx::Cus...
|
171 | |
many changed
|
172 |
Where clause. Above one is expanded to the following SQL by to_string |
173 |
If all parameter names is exists. |
|
174 | ||
DBIx::Custom::Model type att...
|
175 |
"where ( title = :title and ( date < :date or date > :date ) )" |
added experimental DBIx::Cus...
|
176 | |
map cleanup
|
177 |
=head2 C<param> |
178 | ||
179 |
my $param = $where->param; |
|
180 |
$where = $where->param({ |
|
181 |
title => 'Perl', |
|
182 |
date => ['2010-11-11', '2011-03-05'], |
|
183 |
}); |
|
184 | ||
185 |
=head2 C<dbi> |
|
186 | ||
187 |
my $dbi = $where->dbi; |
|
188 |
$where = $where->dbi($dbi); |
|
189 | ||
190 |
L<DBIx::Custom> object. |
|
191 | ||
192 |
=head1 METHODS |
|
193 | ||
194 |
L<DBIx::Custom::Where> inherits all methods from L<Object::Simple> |
|
195 |
and implements the following new ones. |
|
196 | ||
added experimental DBIx::Cus...
|
197 |
=head2 C<to_string> |
198 | ||
199 |
$where->to_string; |
|
added test
|
200 | |
DBIx::Custom::Model type att...
|
201 |
Convert where clause to string. |
202 | ||
203 |
double quote is override to execute C<to_string> method. |
|
204 | ||
205 |
my $string_where = "$where"; |
|
added test
|
206 | |
- update_param_tag is DEPREC...
|
207 |
=cut |