added experimental DBIx::Cus...
|
1 |
package DBIx::Custom::Where; |
2 | ||
updatedd pod
|
3 |
use Object::Simple -base; |
added experimental DBIx::Cus...
|
4 | |
5 |
use overload 'bool' => sub {1}, fallback => 1; |
|
6 |
use overload '""' => sub { shift->to_string }, fallback => 1; |
|
7 | ||
cleanup
|
8 |
use DBIx::Custom::Util '_subname'; |
added experimental DBIx::Cus...
|
9 |
use Carp 'croak'; |
10 | ||
updated document
|
11 |
# Carp trust relationship |
12 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
13 | ||
updatedd pod
|
14 |
has [qw/param query_builder safety_character/], |
cleanup
|
15 |
clause => sub { [] }, |
updatedd pod
|
16 |
reserved_word_quote => ''; |
added experimental DBIx::Cus...
|
17 | |
improved error messages
|
18 |
sub new { |
19 |
my $self = shift->SUPER::new(@_); |
|
20 |
|
|
21 |
# Check attribute names |
|
22 |
my @attrs = keys %$self; |
|
23 |
foreach my $attr (@attrs) { |
|
cleanup
|
24 |
croak qq{"$attr" is invalid attribute name (} . _subname . ")" |
improved error messages
|
25 |
unless $self->can($attr); |
26 |
} |
|
27 |
|
|
28 |
return $self; |
|
29 |
} |
|
30 | ||
changed DBIx::Custom::Where ...
|
31 |
sub to_string { |
added test
|
32 |
my $self = shift; |
added experimental DBIx::Cus...
|
33 |
|
- update_param_tag is DEPREC...
|
34 |
# Check if column name is safety character; |
35 |
my $safety = $self->safety_character; |
|
36 |
if (ref $self->param eq 'HASH') { |
|
37 |
foreach my $column (keys %{$self->param}) { |
|
38 |
croak qq{"$column" is not safety column name (} . _subname . ")" |
|
39 |
unless $column =~ /^[$safety\.]+$/; |
|
40 |
} |
|
41 |
} |
|
many changed
|
42 |
# Clause |
added test
|
43 |
my $clause = $self->clause; |
changed DBIx::Custom::Where ...
|
44 |
$clause = ['and', $clause] unless ref $clause eq 'ARRAY'; |
added test
|
45 |
$clause->[0] = 'and' unless @$clause; |
46 | ||
many changed
|
47 |
# Parse |
added test
|
48 |
my $where = []; |
49 |
my $count = {}; |
|
many changed
|
50 |
$self->_parse($clause, $where, $count, 'and'); |
changed DBIx::Custom::Where ...
|
51 |
|
many changed
|
52 |
# Stringify |
53 |
unshift @$where, 'where' if @$where; |
|
added test
|
54 |
return join(' ', @$where); |
added experimental DBIx::Cus...
|
55 |
} |
56 | ||
added test
|
57 |
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/; |
many changed
|
58 |
sub _parse { |
added test
|
59 |
my ($self, $clause, $where, $count, $op) = @_; |
added experimental DBIx::Cus...
|
60 |
|
many changed
|
61 |
# Array |
changed DBIx::Custom::Where ...
|
62 |
if (ref $clause eq 'ARRAY') { |
many changed
|
63 |
|
64 |
# Start |
|
added test
|
65 |
push @$where, '('; |
changed DBIx::Custom::Where ...
|
66 |
|
many changed
|
67 |
# Operation |
changed DBIx::Custom::Where ...
|
68 |
my $op = $clause->[0] || ''; |
improved error message
|
69 |
croak qq{First argument must be "and" or "or" in where clause } . |
70 |
qq{"$op" is passed} . _subname . ")" |
|
changed DBIx::Custom::Where ...
|
71 |
unless $VALID_OPERATIONS{$op}; |
many changed
|
72 |
|
73 |
# Parse internal clause |
|
changed DBIx::Custom::Where ...
|
74 |
for (my $i = 1; $i < @$clause; $i++) { |
many changed
|
75 |
my $pushed = $self->_parse($clause->[$i], $where, $count, $op); |
added test
|
76 |
push @$where, $op if $pushed; |
changed DBIx::Custom::Where ...
|
77 |
} |
added test
|
78 |
pop @$where if $where->[-1] eq $op; |
79 |
|
|
many changed
|
80 |
# Undo |
added test
|
81 |
if ($where->[-1] eq '(') { |
82 |
pop @$where; |
|
83 |
pop @$where; |
|
84 |
} |
|
many changed
|
85 |
# End |
renamed DBIx::Custom::TagPro...
|
86 |
else { push @$where, ')' } |
changed DBIx::Custom::Where ...
|
87 |
} |
many changed
|
88 |
|
89 |
# String |
|
changed DBIx::Custom::Where ...
|
90 |
else { |
DBIx::Custom::Where clause a...
|
91 |
# Pushed |
92 |
my $pushed; |
|
changed DBIx::Custom::Where ...
|
93 |
|
added test
|
94 |
# Column |
95 |
my $columns = $self->query_builder->build_query($clause)->columns; |
|
DBIx::Custom::Where clause a...
|
96 |
if (@$columns == 0) { |
97 |
push @$where, $clause; |
|
98 |
$pushed = 1; |
|
99 |
return $pushed; |
|
100 |
} |
|
101 |
elsif (@$columns != 1) { |
|
- update_param_tag is DEPREC...
|
102 |
croak qq{Each part contains one column name: "$clause" (} |
cleanup
|
103 |
. _subname . ")"; |
DBIx::Custom::Where clause a...
|
104 |
} |
105 | ||
changed DBIx::Custom::Where ...
|
106 |
my $column = $columns->[0]; |
added EXPERIMENTAL reserved_...
|
107 |
if (my $q = $self->reserved_word_quote) { |
108 |
$column =~ s/$q//g; |
|
109 |
} |
|
110 |
|
|
- 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; |
added experimental DBIx::Cus...
|
154 | |
155 |
=head1 ATTRIBUTES |
|
156 | ||
157 |
=head2 C<clause> |
|
158 | ||
added test
|
159 |
$where->clause( |
160 |
['and', '{= title}', ['or', '{< date}', '{> date}']] |
|
161 |
); |
|
added experimental DBIx::Cus...
|
162 | |
many changed
|
163 |
Where clause. Above one is expanded to the following SQL by to_string |
164 |
If all parameter names is exists. |
|
165 | ||
166 |
"where ( {= title} and ( {< date} or {> date} ) )" |
|
added experimental DBIx::Cus...
|
167 | |
update pod
|
168 |
=head2 C<param> |
169 | ||
170 |
my $param = $where->param; |
|
171 |
$where = $where->param({title => 'Perl', |
|
172 |
date => ['2010-11-11', '2011-03-05']}, |
|
173 |
name => ['Ken', 'Taro']); |
|
174 | ||
- remaned experimental safty...
|
175 |
=head2 C<safety_character> |
update pod
|
176 | |
- remaned experimental safty...
|
177 |
my $safety_character = $self->safety_character; |
178 |
$dbi = $self->safety_character($name); |
|
update pod
|
179 | |
180 |
=head1 METHODS |
|
181 | ||
added experimental DBIx::Cus...
|
182 |
=head2 C<to_string> |
183 | ||
184 |
$where->to_string; |
|
added test
|
185 | |
186 |
Convert where clause to string correspoinding to param name. |
|
187 | ||
- update_param_tag is DEPREC...
|
188 |
=cut |