added experimental DBIx::Cus...
|
1 |
package DBIx::Custom::Where; |
2 | ||
3 |
use strict; |
|
4 |
use warnings; |
|
5 | ||
6 |
use base 'Object::Simple'; |
|
7 | ||
8 |
use overload 'bool' => sub {1}, fallback => 1; |
|
9 |
use overload '""' => sub { shift->to_string }, fallback => 1; |
|
10 | ||
11 |
use Carp 'croak'; |
|
12 | ||
updated document
|
13 |
# Carp trust relationship |
14 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
15 | ||
added test
|
16 |
__PACKAGE__->attr( |
- remaned experimental safty...
|
17 |
[qw/param query_builder safety_character/], |
cleanup
|
18 |
clause => sub { [] }, |
added EXPERIMENTAL reserved_...
|
19 |
reserved_word_quote => '' |
added test
|
20 |
); |
added experimental DBIx::Cus...
|
21 | |
changed DBIx::Custom::Where ...
|
22 |
sub to_string { |
added test
|
23 |
my $self = shift; |
added experimental DBIx::Cus...
|
24 |
|
many changed
|
25 |
# Clause |
added test
|
26 |
my $clause = $self->clause; |
changed DBIx::Custom::Where ...
|
27 |
$clause = ['and', $clause] unless ref $clause eq 'ARRAY'; |
added test
|
28 |
$clause->[0] = 'and' unless @$clause; |
29 | ||
many changed
|
30 |
# Parse |
added test
|
31 |
my $where = []; |
32 |
my $count = {}; |
|
many changed
|
33 |
$self->_parse($clause, $where, $count, 'and'); |
changed DBIx::Custom::Where ...
|
34 |
|
many changed
|
35 |
# Stringify |
36 |
unshift @$where, 'where' if @$where; |
|
added test
|
37 |
return join(' ', @$where); |
added experimental DBIx::Cus...
|
38 |
} |
39 | ||
added test
|
40 |
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/; |
many changed
|
41 |
sub _parse { |
added test
|
42 |
my ($self, $clause, $where, $count, $op) = @_; |
added experimental DBIx::Cus...
|
43 |
|
many changed
|
44 |
# Array |
changed DBIx::Custom::Where ...
|
45 |
if (ref $clause eq 'ARRAY') { |
many changed
|
46 |
|
47 |
# Start |
|
added test
|
48 |
push @$where, '('; |
changed DBIx::Custom::Where ...
|
49 |
|
many changed
|
50 |
# Operation |
changed DBIx::Custom::Where ...
|
51 |
my $op = $clause->[0] || ''; |
52 |
croak qq{"$op" is invalid operation} |
|
53 |
unless $VALID_OPERATIONS{$op}; |
|
many changed
|
54 |
|
55 |
# Parse internal clause |
|
changed DBIx::Custom::Where ...
|
56 |
for (my $i = 1; $i < @$clause; $i++) { |
many changed
|
57 |
my $pushed = $self->_parse($clause->[$i], $where, $count, $op); |
added test
|
58 |
push @$where, $op if $pushed; |
changed DBIx::Custom::Where ...
|
59 |
} |
added test
|
60 |
pop @$where if $where->[-1] eq $op; |
61 |
|
|
many changed
|
62 |
# Undo |
added test
|
63 |
if ($where->[-1] eq '(') { |
64 |
pop @$where; |
|
65 |
pop @$where; |
|
66 |
} |
|
many changed
|
67 |
# End |
renamed DBIx::Custom::TagPro...
|
68 |
else { push @$where, ')' } |
changed DBIx::Custom::Where ...
|
69 |
} |
many changed
|
70 |
|
71 |
# String |
|
changed DBIx::Custom::Where ...
|
72 |
else { |
DBIx::Custom::Where clause a...
|
73 |
# Pushed |
74 |
my $pushed; |
|
changed DBIx::Custom::Where ...
|
75 |
|
added test
|
76 |
# Column |
77 |
my $columns = $self->query_builder->build_query($clause)->columns; |
|
DBIx::Custom::Where clause a...
|
78 |
if (@$columns == 0) { |
79 |
push @$where, $clause; |
|
80 |
$pushed = 1; |
|
81 |
return $pushed; |
|
82 |
} |
|
83 |
elsif (@$columns != 1) { |
|
84 |
croak qq{Each tag contains one column name: tag "$clause"} |
|
85 |
} |
|
86 | ||
changed DBIx::Custom::Where ...
|
87 |
my $column = $columns->[0]; |
added EXPERIMENTAL reserved_...
|
88 |
if (my $q = $self->reserved_word_quote) { |
89 |
$column =~ s/$q//g; |
|
90 |
} |
|
91 |
|
|
- remaned experimental safty...
|
92 |
my $safety = $self->safety_character; |
select() where can't receive...
|
93 |
croak qq{"$column" is not safety column name} |
- remaned experimental safty...
|
94 |
unless $column =~ /^[$safety\.]+$/; |
changed DBIx::Custom::Where ...
|
95 |
|
many changed
|
96 |
# Column count up |
added test
|
97 |
my $count = ++$count->{$column}; |
changed DBIx::Custom::Where ...
|
98 |
|
many changed
|
99 |
# Push |
100 |
my $param = $self->param; |
|
added experimental not_exist...
|
101 |
if (ref $param eq 'HASH') { |
renamed DBIx::Custom::TagPro...
|
102 |
if (exists $param->{$column}) { |
103 |
if (ref $param->{$column} eq 'ARRAY') { |
|
added experimental not_exist...
|
104 |
$pushed = 1 |
105 |
if exists $param->{$column}->[$count - 1] |
|
106 |
&& ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists'; |
|
107 |
} |
|
renamed DBIx::Custom::TagPro...
|
108 |
elsif ($count == 1) { |
109 |
$pushed = 1; |
|
110 |
} |
|
added experimental DBIx::Cus...
|
111 |
} |
renamed DBIx::Custom::TagPro...
|
112 |
push @$where, $clause if $pushed; |
113 |
} |
|
added experimental not_exist...
|
114 |
elsif (!defined $param) { |
renamed DBIx::Custom::TagPro...
|
115 |
push @$where, $clause; |
116 |
$pushed = 1; |
|
added experimental DBIx::Cus...
|
117 |
} |
added experimental not_exist...
|
118 |
else { croak "Parameter must be hash reference or undfined value" } |
added test
|
119 |
|
120 |
return $pushed; |
|
added experimental DBIx::Cus...
|
121 |
} |
122 |
} |
|
123 | ||
124 |
1; |
|
125 | ||
126 |
=head1 NAME |
|
127 | ||
removed EXPERIMETNAL flag fr...
|
128 |
DBIx::Custom::Where - Where clause |
added experimental DBIx::Cus...
|
129 | |
130 |
=head1 SYNOPSYS |
|
131 | ||
many changed
|
132 |
my $where = DBIx::Custom::Where->new; |
added experimental DBIx::Cus...
|
133 | |
134 |
=head1 ATTRIBUTES |
|
135 | ||
136 |
=head2 C<clause> |
|
137 | ||
added test
|
138 |
$where->clause( |
139 |
['and', '{= title}', ['or', '{< date}', '{> date}']] |
|
140 |
); |
|
added experimental DBIx::Cus...
|
141 | |
many changed
|
142 |
Where clause. Above one is expanded to the following SQL by to_string |
143 |
If all parameter names is exists. |
|
144 | ||
145 |
"where ( {= title} and ( {< date} or {> date} ) )" |
|
added experimental DBIx::Cus...
|
146 | |
update pod
|
147 |
=head2 C<param> |
148 | ||
149 |
my $param = $where->param; |
|
150 |
$where = $where->param({title => 'Perl', |
|
151 |
date => ['2010-11-11', '2011-03-05']}, |
|
152 |
name => ['Ken', 'Taro']); |
|
153 | ||
- remaned experimental safty...
|
154 |
=head2 C<safety_character> |
update pod
|
155 | |
- remaned experimental safty...
|
156 |
my $safety_character = $self->safety_character; |
157 |
$dbi = $self->safety_character($name); |
|
update pod
|
158 | |
159 |
=head1 METHODS |
|
160 | ||
added experimental DBIx::Cus...
|
161 |
=head2 C<to_string> |
162 | ||
163 |
$where->to_string; |
|
added test
|
164 | |
165 |
Convert where clause to string correspoinding to param name. |
|
166 |