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 { |
73 |
|
|
added test
|
74 |
# Column |
75 |
my $columns = $self->query_builder->build_query($clause)->columns; |
|
added experimental not_exist...
|
76 |
croak qq{Each tag contains one column name: tag "$clause"} |
changed DBIx::Custom::Where ...
|
77 |
unless @$columns == 1; |
78 |
my $column = $columns->[0]; |
|
added EXPERIMENTAL reserved_...
|
79 |
if (my $q = $self->reserved_word_quote) { |
80 |
$column =~ s/$q//g; |
|
81 |
} |
|
82 |
|
|
- remaned experimental safty...
|
83 |
my $safety = $self->safety_character; |
select() where can't receive...
|
84 |
croak qq{"$column" is not safety column name} |
- remaned experimental safty...
|
85 |
unless $column =~ /^[$safety\.]+$/; |
changed DBIx::Custom::Where ...
|
86 |
|
many changed
|
87 |
# Column count up |
added test
|
88 |
my $count = ++$count->{$column}; |
changed DBIx::Custom::Where ...
|
89 |
|
many changed
|
90 |
# Push |
91 |
my $param = $self->param; |
|
added test
|
92 |
my $pushed; |
added experimental not_exist...
|
93 |
if (ref $param eq 'HASH') { |
renamed DBIx::Custom::TagPro...
|
94 |
if (exists $param->{$column}) { |
95 |
if (ref $param->{$column} eq 'ARRAY') { |
|
added experimental not_exist...
|
96 |
$pushed = 1 |
97 |
if exists $param->{$column}->[$count - 1] |
|
98 |
&& ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists'; |
|
99 |
} |
|
renamed DBIx::Custom::TagPro...
|
100 |
elsif ($count == 1) { |
101 |
$pushed = 1; |
|
102 |
} |
|
added experimental DBIx::Cus...
|
103 |
} |
renamed DBIx::Custom::TagPro...
|
104 |
push @$where, $clause if $pushed; |
105 |
} |
|
added experimental not_exist...
|
106 |
elsif (!defined $param) { |
renamed DBIx::Custom::TagPro...
|
107 |
push @$where, $clause; |
108 |
$pushed = 1; |
|
added experimental DBIx::Cus...
|
109 |
} |
added experimental not_exist...
|
110 |
else { croak "Parameter must be hash reference or undfined value" } |
added test
|
111 |
|
112 |
return $pushed; |
|
added experimental DBIx::Cus...
|
113 |
} |
114 |
} |
|
115 | ||
116 |
1; |
|
117 | ||
118 |
=head1 NAME |
|
119 | ||
removed EXPERIMETNAL flag fr...
|
120 |
DBIx::Custom::Where - Where clause |
added experimental DBIx::Cus...
|
121 | |
122 |
=head1 SYNOPSYS |
|
123 | ||
many changed
|
124 |
my $where = DBIx::Custom::Where->new; |
added experimental DBIx::Cus...
|
125 | |
126 |
=head1 ATTRIBUTES |
|
127 | ||
128 |
=head2 C<clause> |
|
129 | ||
added test
|
130 |
$where->clause( |
131 |
['and', '{= title}', ['or', '{< date}', '{> date}']] |
|
132 |
); |
|
added experimental DBIx::Cus...
|
133 | |
many changed
|
134 |
Where clause. Above one is expanded to the following SQL by to_string |
135 |
If all parameter names is exists. |
|
136 | ||
137 |
"where ( {= title} and ( {< date} or {> date} ) )" |
|
added experimental DBIx::Cus...
|
138 | |
update pod
|
139 |
=head2 C<param> |
140 | ||
141 |
my $param = $where->param; |
|
142 |
$where = $where->param({title => 'Perl', |
|
143 |
date => ['2010-11-11', '2011-03-05']}, |
|
144 |
name => ['Ken', 'Taro']); |
|
145 | ||
- remaned experimental safty...
|
146 |
=head2 C<safety_character> |
update pod
|
147 | |
- remaned experimental safty...
|
148 |
my $safety_character = $self->safety_character; |
149 |
$dbi = $self->safety_character($name); |
|
update pod
|
150 | |
151 |
=head1 METHODS |
|
152 | ||
added experimental DBIx::Cus...
|
153 |
=head2 C<to_string> |
154 | ||
155 |
$where->to_string; |
|
added test
|
156 | |
157 |
Convert where clause to string correspoinding to param name. |
|
158 |