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