Newer Older
358 lines | 9.257kb
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1
package DBIx::Custom::Where;
updatedd pod
Yuki Kimoto authored on 2011-06-12
2
use Object::Simple -base;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
3

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
4
use Carp 'croak';
5
use DBIx::Custom::Util '_subname';
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
6
use overload 'bool' => sub {1}, fallback => 1;
7
use overload '""' => sub { shift->to_string }, fallback => 1;
8

            
updated document
Yuki Kimoto authored on 2011-01-20
9
# Carp trust relationship
10
push @DBIx::Custom::CARP_NOT, __PACKAGE__;
11

            
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
12
has [qw/dbi param/],
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
13
    map => sub { {} },
14
    clause => sub { [] };
15

            
16
sub _map_param {
17
    my $self = shift;
18
    my $param = shift;
19
    
20
    return $param if !defined $param;
21
    
22
    my %map = @_;
23
    
24
    # Mapping
25
    my $map_param = {};
26
    foreach my $key (keys %$param) {
27
    
28
        my $value_cb;
29
        my $condition;
30
        my $map_key;
31
        
32
        # Get mapping information
33
        if (ref $map{$key} eq 'ARRAY') {
34
            foreach my $some (@{$map{$key}}) {
35
                $map_key = $some unless ref $some;
36
                $condition = $some->{if} if ref $some eq 'HASH';
37
                $value_cb = $some if ref $some eq 'CODE';
38
            }
39
        }
cleanup
Yuki Kimoto authored on 2011-08-09
40
        elsif (defined $map{$key}) {
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
41
            $map_key = $map{$key};
42
        }
cleanup
Yuki Kimoto authored on 2011-08-09
43
        else {
44
            $map_key = $key;
45
        }
46
        
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
47
        $value_cb ||= sub { $_[0] };
48
        $condition ||= $self->if || 'exists';
49

            
50
        # Map parameter
51
        my $value;
52
        if (ref $condition eq 'CODE') {
53
            if (ref $param->{$key} eq 'ARRAY') {
54
                $map_param->{$map_key} = [];
55
                for (my $i = 0; $i < @{$param->{$key}}; $i++) {
56
                    $map_param->{$map_key}->[$i]
57
                      = $condition->($param->{$key}->[$i]) ? $param->{$key}->[$i]
58
                      : $self->dbi->not_exists;
59
                }
60
            }
61
            else {
62
                $map_param->{$map_key} = $value_cb->($param->{$key})
63
                  if $condition->($param->{$key});
64
            }
65
        }
66
        elsif ($condition eq 'exists') {
67
            if (ref $param->{$key} eq 'ARRAY') {
68
                $map_param->{$map_key} = [];
69
                for (my $i = 0; $i < @{$param->{$key}}; $i++) {
70
                    $map_param->{$map_key}->[$i]
71
                      = exists $param->{$key}->[$i] ? $param->{$key}->[$i]
72
                      : $self->dbi->not_exists;
73
                }
74
            }
75
            else {
76
                $map_param->{$map_key} = $value_cb->($param->{$key})
77
                  if exists $param->{$key};
78
            }
79
        }
80
        else { croak qq/Condition must be code reference or "exists" / . _subname }
81
    }
82
    
83
    return $map_param;
84
}
85

            
86
sub if {
87
    my $self = shift;
88
    if (@_) {
89
        my $if = $_[0];
90
        
91
        $if = $if eq 'exists' ? $if
92
                : $if eq 'defined' ? sub { defined $_[0] }
93
                : $if eq 'length'  ? sub { length $_[0] }
94
                : ref $if eq 'CODE' ? $if
95
                : undef;
96

            
97
        croak "You can must specify right value to C<if> " . _subname
98
          unless $if;
99

            
100
        $self->{if} = $if;
101
        return $self;
102
    }
103
    $self->{if} = 'exists' unless exists $self->{if};
104
    return $self->{if};
105
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
106

            
improved error messages
Yuki Kimoto authored on 2011-04-18
107
sub new {
108
    my $self = shift->SUPER::new(@_);
109
    
110
    # Check attribute names
111
    my @attrs = keys %$self;
112
    foreach my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-25
113
        croak qq{"$attr" is invalid attribute name (} . _subname . ")"
improved error messages
Yuki Kimoto authored on 2011-04-18
114
          unless $self->can($attr);
115
    }
116
    
117
    return $self;
118
}
119

            
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
120
sub to_string {
added test
Yuki Kimoto authored on 2011-01-19
121
    my $self = shift;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
122
    
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
123
    # Check if column name is safety character;
cleanup
Yuki Kimoto authored on 2011-08-02
124
    my $safety = $self->dbi->safety_character;
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
125
    if (ref $self->param eq 'HASH') {
126
        foreach my $column (keys %{$self->param}) {
127
            croak qq{"$column" is not safety column name (} . _subname . ")"
128
              unless $column =~ /^[$safety\.]+$/;
129
        }
130
    }
many changed
Yuki Kimoto authored on 2011-01-23
131
    # Clause
added test
Yuki Kimoto authored on 2011-01-19
132
    my $clause = $self->clause;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
133
    $clause = ['and', $clause] unless ref $clause eq 'ARRAY';
added test
Yuki Kimoto authored on 2011-01-19
134
    $clause->[0] = 'and' unless @$clause;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
135
    
136
    # Map condition
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
137
    my $if = $self->if || '';
138
    $if = $if eq 'exists' ? $if
139
            : $if eq 'defined' ? sub { defined $_[0] }
140
            : $if eq 'length'  ? sub { length $_[0] }
141
            : ref $if eq 'CODE' ? $if
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
142
            : undef;
143
    
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
144
    croak "You can must specify right value to C<if> " . _subname
145
      unless $if;
146
    $self->{_if} = $if;
147
    
148
    $self->{_param} = $self->_map_param($self->param, %{$self->map});
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
149
    
many changed
Yuki Kimoto authored on 2011-01-23
150
    # Parse
added test
Yuki Kimoto authored on 2011-01-19
151
    my $where = [];
152
    my $count = {};
many changed
Yuki Kimoto authored on 2011-01-23
153
    $self->_parse($clause, $where, $count, 'and');
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
154
    
many changed
Yuki Kimoto authored on 2011-01-23
155
    # Stringify
156
    unshift @$where, 'where' if @$where;
added test
Yuki Kimoto authored on 2011-01-19
157
    return join(' ', @$where);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
158
}
159

            
added test
Yuki Kimoto authored on 2011-01-19
160
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/;
many changed
Yuki Kimoto authored on 2011-01-23
161
sub _parse {
added test
Yuki Kimoto authored on 2011-01-19
162
    my ($self, $clause, $where, $count, $op) = @_;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
163
    
many changed
Yuki Kimoto authored on 2011-01-23
164
    # Array
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
165
    if (ref $clause eq 'ARRAY') {
many changed
Yuki Kimoto authored on 2011-01-23
166
        
167
        # Start
added test
Yuki Kimoto authored on 2011-01-19
168
        push @$where, '(';
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
169
        
many changed
Yuki Kimoto authored on 2011-01-23
170
        # Operation
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
171
        my $op = $clause->[0] || '';
improved error message
Yuki Kimoto authored on 2011-06-13
172
        croak qq{First argument must be "and" or "or" in where clause } .
173
              qq{"$op" is passed} . _subname . ")"
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
174
          unless $VALID_OPERATIONS{$op};
many changed
Yuki Kimoto authored on 2011-01-23
175
        
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
176
        my $pushed_array;
many changed
Yuki Kimoto authored on 2011-01-23
177
        # Parse internal clause
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
178
        for (my $i = 1; $i < @$clause; $i++) {
many changed
Yuki Kimoto authored on 2011-01-23
179
            my $pushed = $self->_parse($clause->[$i], $where, $count, $op);
added test
Yuki Kimoto authored on 2011-01-19
180
            push @$where, $op if $pushed;
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
181
            $pushed_array = 1 if $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
182
        }
added test
Yuki Kimoto authored on 2011-01-19
183
        pop @$where if $where->[-1] eq $op;
184
        
many changed
Yuki Kimoto authored on 2011-01-23
185
        # Undo
added test
Yuki Kimoto authored on 2011-01-19
186
        if ($where->[-1] eq '(') {
187
            pop @$where;
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
188
            pop @$where if ($where->[-1] || '') eq $op;
added test
Yuki Kimoto authored on 2011-01-19
189
        }
many changed
Yuki Kimoto authored on 2011-01-23
190
        # End
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
191
        else { push @$where, ')' }
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
192
        
193
        return $pushed_array;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
194
    }
many changed
Yuki Kimoto authored on 2011-01-23
195
    
196
    # String
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
197
    else {
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
198
        # Pushed
199
        my $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
200
        
added test
Yuki Kimoto authored on 2011-01-19
201
        # Column
cleanup
Yuki Kimoto authored on 2011-08-02
202
        my $columns = $self->dbi->query_builder->build_query($clause)->columns;
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
203
        if (@$columns == 0) {
204
            push @$where, $clause;
205
            $pushed = 1;
206
            return $pushed;
207
        }
208
        elsif (@$columns != 1) {
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
209
            croak qq{Each part contains one column name: "$clause" (}
cleanup
Yuki Kimoto authored on 2011-04-25
210
                  . _subname . ")";
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
211
        }
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
212
        
213
        # Remove quote
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
214
        my $column = $columns->[0];
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
215
        if (my $q = $self->dbi->_quote) {
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
216
            $q = quotemeta($q);
217
            $column =~ s/[$q]//g;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
218
        }
219
        
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
220
        # Check safety
cleanup
Yuki Kimoto authored on 2011-08-02
221
        my $safety = $self->dbi->safety_character;
cleanup
Yuki Kimoto authored on 2011-04-25
222
        croak qq{"$column" is not safety column name (} . _subname . ")"
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
223
          unless $column =~ /^[$safety\.]+$/;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
224
        
many changed
Yuki Kimoto authored on 2011-01-23
225
        # Column count up
added test
Yuki Kimoto authored on 2011-01-19
226
        my $count = ++$count->{$column};
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
227
        
many changed
Yuki Kimoto authored on 2011-01-23
228
        # Push
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
229
        my $param = $self->{_param};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
230
        if (ref $param eq 'HASH') {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
231
            if (exists $param->{$column}) {
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
232
                my $if = $self->{_if};
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
233
                
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
234
                if (ref $param->{$column} eq 'ARRAY') {
cleanup
Yuki Kimoto authored on 2011-08-09
235
                    $pushed = 1 if exists $param->{$column}->[$count - 1]
236
                      && ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists'
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
237
                }
cleanup
Yuki Kimoto authored on 2011-08-09
238
                elsif ($count == 1) { $pushed = 1 }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
239
            }
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
240
            push @$where, $clause if $pushed;
241
        }
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
242
        elsif (!defined $param) {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
243
            push @$where, $clause;
244
            $pushed = 1;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
245
        }
improved error messages
Yuki Kimoto authored on 2011-04-18
246
        else {
cleanup
Yuki Kimoto authored on 2011-04-25
247
            croak "Parameter must be hash reference or undfined value ("
248
                . _subname . ")"
improved error messages
Yuki Kimoto authored on 2011-04-18
249
        }
added test
Yuki Kimoto authored on 2011-01-19
250
        return $pushed;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
251
    }
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
252
    return;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
253
}
254

            
255
1;
256

            
257
=head1 NAME
258

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
259
DBIx::Custom::Where - Where clause
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
260

            
261
=head1 SYNOPSYS
262

            
many changed
Yuki Kimoto authored on 2011-01-23
263
    my $where = DBIx::Custom::Where->new;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
264
    my $string_where = "$where";
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
265

            
266
=head1 ATTRIBUTES
267

            
268
=head2 C<clause>
269

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
270
    my $clause = $where->clause;
271
    $where = $where->clause(
272
        ['and',
273
            'title = :title', 
274
            ['or', 'date < :date', 'date > :date']
275
        ]
added test
Yuki Kimoto authored on 2011-01-19
276
    );
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
277

            
many changed
Yuki Kimoto authored on 2011-01-23
278
Where clause. Above one is expanded to the following SQL by to_string
279
If all parameter names is exists.
280

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
281
    "where ( title = :title and ( date < :date or date > :date ) )"
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
282

            
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
283
=head2 C<map EXPERIMENTAL>
284

            
285
Mapping parameter key and value when C<to_stirng> method is executed.
286

            
287
    $where->map({
288
        'id' => 'book.id',
289
        'author' => ['book.author' => sub { '%' . $_[0] . '%' }],
290
        'price' => [
291
            'book.price', {if => sub { length $_[0] }
292
        ]
293
    });
294

            
295
The following option is available.
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
296

            
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
297
=over 4
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
298

            
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
299
=item * C<if>
300

            
301
By default, if parameter key is exists, mapping is done.
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
302
    
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
303
    if => 'exists';
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
304

            
305
In case C<defined> is specified, if the value is defined,
306
mapping is done.
307

            
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
308
    if => 'defined';
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
309

            
310
In case C<length> is specified, the value is defined
311
and the length is bigger than 0, mappting is done.
312

            
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
313
    if => 'length';
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
314

            
315
You can also subroutine like C<sub { defined $_[0] }> for mappging.
316

            
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
317
    if => sub { defined $_[0] }
318

            
319
=back
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
320

            
update pod
Yuki Kimoto authored on 2011-01-27
321
=head2 C<param>
322

            
323
    my $param = $where->param;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
324
    $where = $where->param({
325
        title => 'Perl',
326
        date => ['2010-11-11', '2011-03-05'],
327
    });
update pod
Yuki Kimoto authored on 2011-01-27
328

            
cleanup
Yuki Kimoto authored on 2011-08-02
329
=head2 C<dbi>
update pod
Yuki Kimoto authored on 2011-01-27
330

            
cleanup
Yuki Kimoto authored on 2011-08-02
331
    my $dbi = $where->dbi;
332
    $where = $where->dbi($dbi);
333

            
334
L<DBIx::Custom> object.
update pod
Yuki Kimoto authored on 2011-01-27
335

            
336
=head1 METHODS
337

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
338
L<DBIx::Custom::Where> inherits all methods from L<Object::Simple>
339
and implements the following new ones.
340

            
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
341
=head2 C<if EXPERIMENTAL>
342
    
343
    my $if = $where->if($condition);
344
    $where->if($condition);
345

            
346
C<if> is default of C<map> method C<if> option.
347

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
348
=head2 C<to_string>
349

            
350
    $where->to_string;
added test
Yuki Kimoto authored on 2011-01-19
351

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
352
Convert where clause to string.
353

            
354
double quote is override to execute C<to_string> method.
355

            
356
    my $string_where = "$where";
added test
Yuki Kimoto authored on 2011-01-19
357

            
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
358
=cut