Newer Older
364 lines | 9.337kb
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
    clause => sub { [] };
14

            
map cleanup
Yuki Kimoto authored on 2011-08-09
15
sub map {
16
    my ($self, %map) = @_;
17
    
18
    my $param = $self->_map_param($self->param, %map);
19
    $self->param($param);
20
    return $self;
21
}
22

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

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

            
93
sub if {
94
    my $self = shift;
95
    if (@_) {
96
        my $if = $_[0];
97
        
98
        $if = $if eq 'exists' ? $if
99
                : $if eq 'defined' ? sub { defined $_[0] }
100
                : $if eq 'length'  ? sub { length $_[0] }
101
                : ref $if eq 'CODE' ? $if
102
                : undef;
103

            
104
        croak "You can must specify right value to C<if> " . _subname
105
          unless $if;
106

            
107
        $self->{if} = $if;
108
        return $self;
109
    }
110
    $self->{if} = 'exists' unless exists $self->{if};
111
    return $self->{if};
112
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
113

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

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

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

            
260
1;
261

            
262
=head1 NAME
263

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

            
266
=head1 SYNOPSYS
267

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

            
271
=head1 ATTRIBUTES
272

            
273
=head2 C<clause>
274

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

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

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

            
map cleanup
Yuki Kimoto authored on 2011-08-09
288
=head2 C<param>
289

            
290
    my $param = $where->param;
291
    $where = $where->param({
292
        title => 'Perl',
293
        date => ['2010-11-11', '2011-03-05'],
294
    });
295

            
296
=head2 C<dbi>
297

            
298
    my $dbi = $where->dbi;
299
    $where = $where->dbi($dbi);
300

            
301
L<DBIx::Custom> object.
302

            
303
=head1 METHODS
304

            
305
L<DBIx::Custom::Where> inherits all methods from L<Object::Simple>
306
and implements the following new ones.
307

            
308
=head2 C<if EXPERIMENTAL>
309
    
310
    my $if = $where->if($condition);
311
    $where->if($condition);
312

            
313
C<if> is default of C<map> method C<if> option.
314

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

            
map cleanup
Yuki Kimoto authored on 2011-08-09
317
Mapping parameter key and value. C<param> is converted,
318
so this method must be called after C<param> is set.
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
319

            
map cleanup
Yuki Kimoto authored on 2011-08-09
320
    $where->map(
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
321
        'id' => 'book.id',
322
        'author' => ['book.author' => sub { '%' . $_[0] . '%' }],
323
        'price' => [
324
            'book.price', {if => sub { length $_[0] }
325
        ]
map cleanup
Yuki Kimoto authored on 2011-08-09
326
    );
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
327

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

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

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

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

            
338
In case C<defined> is specified, if the value is defined,
339
mapping is done.
340

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

            
343
In case C<length> is specified, the value is defined
344
and the length is bigger than 0, mappting is done.
345

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

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

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

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

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

            
356
    $where->to_string;
added test
Yuki Kimoto authored on 2011-01-19
357

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

            
360
double quote is override to execute C<to_string> method.
361

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

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