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] || ''; |
cleanup
|
69 |
croak qq{"$op" is invalid operation (} . _subname . ")" |
changed DBIx::Custom::Where ...
|
70 |
unless $VALID_OPERATIONS{$op}; |
many changed
|
71 |
|
72 |
# Parse internal clause |
|
changed DBIx::Custom::Where ...
|
73 |
for (my $i = 1; $i < @$clause; $i++) { |
many changed
|
74 |
my $pushed = $self->_parse($clause->[$i], $where, $count, $op); |
added test
|
75 |
push @$where, $op 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; |
|
82 |
pop @$where; |
|
83 |
} |
|
many changed
|
84 |
# End |
renamed DBIx::Custom::TagPro...
|
85 |
else { push @$where, ')' } |
changed DBIx::Custom::Where ...
|
86 |
} |
many changed
|
87 |
|
88 |
# String |
|
changed DBIx::Custom::Where ...
|
89 |
else { |
DBIx::Custom::Where clause a...
|
90 |
# Pushed |
91 |
my $pushed; |
|
changed DBIx::Custom::Where ...
|
92 |
|
added test
|
93 |
# Column |
94 |
my $columns = $self->query_builder->build_query($clause)->columns; |
|
DBIx::Custom::Where clause a...
|
95 |
if (@$columns == 0) { |
96 |
push @$where, $clause; |
|
97 |
$pushed = 1; |
|
98 |
return $pushed; |
|
99 |
} |
|
100 |
elsif (@$columns != 1) { |
|
- update_param_tag is DEPREC...
|
101 |
croak qq{Each part contains one column name: "$clause" (} |
cleanup
|
102 |
. _subname . ")"; |
DBIx::Custom::Where clause a...
|
103 |
} |
104 | ||
changed DBIx::Custom::Where ...
|
105 |
my $column = $columns->[0]; |
added EXPERIMENTAL reserved_...
|
106 |
if (my $q = $self->reserved_word_quote) { |
107 |
$column =~ s/$q//g; |
|
108 |
} |
|
109 |
|
|
- remaned experimental safty...
|
110 |
my $safety = $self->safety_character; |
cleanup
|
111 |
croak qq{"$column" is not safety column name (} . _subname . ")" |
- remaned experimental safty...
|
112 |
unless $column =~ /^[$safety\.]+$/; |
changed DBIx::Custom::Where ...
|
113 |
|
many changed
|
114 |
# Column count up |
added test
|
115 |
my $count = ++$count->{$column}; |
changed DBIx::Custom::Where ...
|
116 |
|
many changed
|
117 |
# Push |
118 |
my $param = $self->param; |
|
added experimental not_exist...
|
119 |
if (ref $param eq 'HASH') { |
renamed DBIx::Custom::TagPro...
|
120 |
if (exists $param->{$column}) { |
121 |
if (ref $param->{$column} eq 'ARRAY') { |
|
added experimental not_exist...
|
122 |
$pushed = 1 |
123 |
if exists $param->{$column}->[$count - 1] |
|
124 |
&& ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists'; |
|
125 |
} |
|
renamed DBIx::Custom::TagPro...
|
126 |
elsif ($count == 1) { |
127 |
$pushed = 1; |
|
128 |
} |
|
added experimental DBIx::Cus...
|
129 |
} |
renamed DBIx::Custom::TagPro...
|
130 |
push @$where, $clause if $pushed; |
131 |
} |
|
added experimental not_exist...
|
132 |
elsif (!defined $param) { |
renamed DBIx::Custom::TagPro...
|
133 |
push @$where, $clause; |
134 |
$pushed = 1; |
|
added experimental DBIx::Cus...
|
135 |
} |
improved error messages
|
136 |
else { |
cleanup
|
137 |
croak "Parameter must be hash reference or undfined value (" |
138 |
. _subname . ")" |
|
improved error messages
|
139 |
} |
added test
|
140 |
return $pushed; |
added experimental DBIx::Cus...
|
141 |
} |
142 |
} |
|
143 | ||
144 |
1; |
|
145 | ||
146 |
=head1 NAME |
|
147 | ||
removed EXPERIMETNAL flag fr...
|
148 |
DBIx::Custom::Where - Where clause |
added experimental DBIx::Cus...
|
149 | |
150 |
=head1 SYNOPSYS |
|
151 | ||
many changed
|
152 |
my $where = DBIx::Custom::Where->new; |
added experimental DBIx::Cus...
|
153 | |
154 |
=head1 ATTRIBUTES |
|
155 | ||
156 |
=head2 C<clause> |
|
157 | ||
added test
|
158 |
$where->clause( |
159 |
['and', '{= title}', ['or', '{< date}', '{> date}']] |
|
160 |
); |
|
added experimental DBIx::Cus...
|
161 | |
many changed
|
162 |
Where clause. Above one is expanded to the following SQL by to_string |
163 |
If all parameter names is exists. |
|
164 | ||
165 |
"where ( {= title} and ( {< date} or {> date} ) )" |
|
added experimental DBIx::Cus...
|
166 | |
update pod
|
167 |
=head2 C<param> |
168 | ||
169 |
my $param = $where->param; |
|
170 |
$where = $where->param({title => 'Perl', |
|
171 |
date => ['2010-11-11', '2011-03-05']}, |
|
172 |
name => ['Ken', 'Taro']); |
|
173 | ||
- remaned experimental safty...
|
174 |
=head2 C<safety_character> |
update pod
|
175 | |
- remaned experimental safty...
|
176 |
my $safety_character = $self->safety_character; |
177 |
$dbi = $self->safety_character($name); |
|
update pod
|
178 | |
179 |
=head1 METHODS |
|
180 | ||
added experimental DBIx::Cus...
|
181 |
=head2 C<to_string> |
182 | ||
183 |
$where->to_string; |
|
added test
|
184 | |
185 |
Convert where clause to string correspoinding to param name. |
|
186 | ||
- update_param_tag is DEPREC...
|
187 |
=cut |