Newer Older
264 lines | 6.896kb
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

            
improved error messages
Yuki Kimoto authored on 2011-04-18
15
sub new {
16
    my $self = shift->SUPER::new(@_);
17
    
18
    # Check attribute names
19
    my @attrs = keys %$self;
cleanup
Yuki Kimoto authored on 2011-10-21
20
    for my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-25
21
        croak qq{"$attr" is invalid attribute name (} . _subname . ")"
improved error messages
Yuki Kimoto authored on 2011-04-18
22
          unless $self->can($attr);
23
    }
24
    
25
    return $self;
26
}
27

            
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
28
sub to_string {
added test
Yuki Kimoto authored on 2011-01-19
29
    my $self = shift;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
30
    
many changed
Yuki Kimoto authored on 2011-01-23
31
    # Clause
added test
Yuki Kimoto authored on 2011-01-19
32
    my $clause = $self->clause;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
33
    $clause = ['and', $clause] unless ref $clause eq 'ARRAY';
added test
Yuki Kimoto authored on 2011-01-19
34
    $clause->[0] = 'and' unless @$clause;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
35
    
many changed
Yuki Kimoto authored on 2011-01-23
36
    # Parse
added test
Yuki Kimoto authored on 2011-01-19
37
    my $where = [];
38
    my $count = {};
micro optimization
Yuki Kimoto authored on 2011-10-24
39
    $self->{_query_builder} = $self->dbi->query_builder;
40
    $self->{_safety_character} = $self->dbi->safety_character;
41
    $self->{_quote} = $self->dbi->_quote;
cleanup
Yuki Kimoto authored on 2011-11-16
42
    $self->{_tag_parse} = exists $ENV{DBIX_CUSTOM_TAG_PARSE}
43
      ? $ENV{DBIX_CUSTOM_TAG_PARSE} : $self->dbi->{tag_parse};
many changed
Yuki Kimoto authored on 2011-01-23
44
    $self->_parse($clause, $where, $count, 'and');
micro optimization
Yuki Kimoto authored on 2011-10-24
45

            
many changed
Yuki Kimoto authored on 2011-01-23
46
    # Stringify
47
    unshift @$where, 'where' if @$where;
added test
Yuki Kimoto authored on 2011-01-19
48
    return join(' ', @$where);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
49
}
- DBIx::Custom::QueryBuilder...
Yuki Kimoto authored on 2011-11-04
50
    
added test
Yuki Kimoto authored on 2011-01-19
51
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/;
many changed
Yuki Kimoto authored on 2011-01-23
52
sub _parse {
micro optimization
Yuki Kimoto authored on 2011-10-24
53
    my ($self, $clause, $where, $count, $op, $info) = @_;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
54
    
many changed
Yuki Kimoto authored on 2011-01-23
55
    # Array
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
56
    if (ref $clause eq 'ARRAY') {
many changed
Yuki Kimoto authored on 2011-01-23
57
        
58
        # Start
added test
Yuki Kimoto authored on 2011-01-19
59
        push @$where, '(';
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
60
        
many changed
Yuki Kimoto authored on 2011-01-23
61
        # Operation
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
62
        my $op = $clause->[0] || '';
improved error message
Yuki Kimoto authored on 2011-06-13
63
        croak qq{First argument must be "and" or "or" in where clause } .
64
              qq{"$op" is passed} . _subname . ")"
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
65
          unless $VALID_OPERATIONS{$op};
many changed
Yuki Kimoto authored on 2011-01-23
66
        
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
67
        my $pushed_array;
many changed
Yuki Kimoto authored on 2011-01-23
68
        # Parse internal clause
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
69
        for (my $i = 1; $i < @$clause; $i++) {
many changed
Yuki Kimoto authored on 2011-01-23
70
            my $pushed = $self->_parse($clause->[$i], $where, $count, $op);
added test
Yuki Kimoto authored on 2011-01-19
71
            push @$where, $op if $pushed;
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
72
            $pushed_array = 1 if $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
73
        }
added test
Yuki Kimoto authored on 2011-01-19
74
        pop @$where if $where->[-1] eq $op;
75
        
many changed
Yuki Kimoto authored on 2011-01-23
76
        # Undo
added test
Yuki Kimoto authored on 2011-01-19
77
        if ($where->[-1] eq '(') {
78
            pop @$where;
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
79
            pop @$where if ($where->[-1] || '') eq $op;
added test
Yuki Kimoto authored on 2011-01-19
80
        }
many changed
Yuki Kimoto authored on 2011-01-23
81
        # End
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
82
        else { push @$where, ')' }
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
83
        
84
        return $pushed_array;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
85
    }
many changed
Yuki Kimoto authored on 2011-01-23
86
    
87
    # String
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
88
    else {
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
89
        # Pushed
90
        my $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
91
        
added test
Yuki Kimoto authored on 2011-01-19
92
        # Column
micro optimization
Yuki Kimoto authored on 2011-10-31
93
        my $c = $self->{_safety_character};
94
        
95
        my $column;
fixed where clause parsing b...
Yuki Kimoto authored on 2011-11-01
96
        if ($self->{_tag_parse} && $clause =~ /(\s|^)\{/) {
micro optimization
Yuki Kimoto authored on 2011-10-31
97
            my $columns = $self->dbi->query_builder->build_query($clause)->{columns};
98
            $column = $columns->[0];
99
        }
fixed where clause parsing b...
Yuki Kimoto authored on 2011-11-01
100
        else {
101
            my $sql = $clause;
102
            $sql =~ s/([0-9]):/$1\\:/g;
103
            if ($sql =~ /[^\\]:([$c\.]+)/s || $sql =~ /^:([$c\.]+)/s) {
104
                ($column) = $1;
105
            }
106
        }
micro optimization
Yuki Kimoto authored on 2011-10-31
107
        unless (defined $column) {
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
108
            push @$where, $clause;
109
            $pushed = 1;
110
            return $pushed;
111
        }
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
112
        
many changed
Yuki Kimoto authored on 2011-01-23
113
        # Column count up
added test
Yuki Kimoto authored on 2011-01-19
114
        my $count = ++$count->{$column};
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
115
        
many changed
Yuki Kimoto authored on 2011-01-23
116
        # Push
micro optimization
Yuki Kimoto authored on 2011-10-24
117
        my $param = $self->{param};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
118
        if (ref $param eq 'HASH') {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
119
            if (exists $param->{$column}) {
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
120
                my $if = $self->{_if};
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
121
                
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
122
                if (ref $param->{$column} eq 'ARRAY') {
cleanup
Yuki Kimoto authored on 2011-08-09
123
                    $pushed = 1 if exists $param->{$column}->[$count - 1]
124
                      && ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists'
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
125
                }
cleanup
Yuki Kimoto authored on 2011-08-09
126
                elsif ($count == 1) { $pushed = 1 }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
127
            }
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
128
            push @$where, $clause if $pushed;
129
        }
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
130
        elsif (!defined $param) {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
131
            push @$where, $clause;
132
            $pushed = 1;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
133
        }
improved error messages
Yuki Kimoto authored on 2011-04-18
134
        else {
cleanup
Yuki Kimoto authored on 2011-04-25
135
            croak "Parameter must be hash reference or undfined value ("
136
                . _subname . ")"
improved error messages
Yuki Kimoto authored on 2011-04-18
137
        }
added test
Yuki Kimoto authored on 2011-01-19
138
        return $pushed;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
139
    }
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
140
    return;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
141
}
142
1;
143

            
144
=head1 NAME
145

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

            
148
=head1 SYNOPSYS
- id option work if id count...
Yuki Kimoto authored on 2011-11-03
149
    
150
    # Create DBIx::Custom::Where object
151
    my $where = $dbi->where;
152
    
153
    # Set clause and parameter
154
    $where->clause(['and', ':title{like}', ':price{=}']);
155
    
156
    # Create where clause by to_string method
157
    my $where_clause = $where->to_string;
158
    
159
    # Create where clause by stringify
160
    my $where_clause = "$where";
161
    
162
    # Created where clause in the above way
163
    where :title{=} and :price{like}
164
    
165
    # Only price condition
166
    $where->clause(['and', ':title{like}', ':price{=}']);
167
    $where->param({price => 1900});
168
    my $where_clause = "$where";
169
    
170
    # Created where clause in the above way
171
    where :price{=}
172
    
173
    # Only title condition
174
    $where->clause(['and', ':title{like}', ':price{=}']);
175
    $where->param({title => 'Perl'});
176
    my $where_clause = "$where";
177
    
178
    # Created where clause in the above way
179
    where :title{like}
180
    
181
    # Nothing
182
    $where->clause(['and', ':title{like}', ':price{=}']);
183
    $where->param({});
184
    my $where_clause = "$where";
185
    
186
    # or condition
187
    $where->clause(['or', ':title{like}', ':price{=}']);
188
    
189
    # More than one parameter
190
    $where->clause(['and', ':price{>}', ':price{<}']);
191
    $where->param({price => [1000, 2000]});
192
    
193
    # Only first condition
194
    $where->clause(['and', ':price{>}', ':price{<}']);
195
    $where->param({price => [1000, $dbi->not_exists]});
196
    
197
    # Only second condition
198
    $where->clause(['and', ':price{>}', ':price{<}']);
199
    $where->param({price => [$dbi->not_exists, 2000]});
200
    
201
    # More complex condition
202
    $where->clause(
203
        [
204
            'and',
205
            ':price{=}',
206
            ['or', ':title{=}', ':title{=}', ':title{=}']
207
        ]
208
    );
209
    my $where_clause = "$where";
210
    
211
    # Created where clause in the above way
212
    where :price{=} and (:title{=} or :title{=} or :title{=})
213
    
214
    # Using Full-qualified column name
215
    $where->clause(['and', ':book.title{like}', ':book.price{=}']);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
216

            
217
=head1 ATTRIBUTES
218

            
219
=head2 C<clause>
220

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
221
    my $clause = $where->clause;
222
    $where = $where->clause(
223
        ['and',
- id option work if id count...
Yuki Kimoto authored on 2011-11-03
224
            ':title{=}', 
225
            ['or', ':date{<}', ':date{>}']
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
226
        ]
added test
Yuki Kimoto authored on 2011-01-19
227
    );
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
228

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

            
- id option work if id count...
Yuki Kimoto authored on 2011-11-03
232
    where title = :title and ( date < :date or date > :date )
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
233

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

            
236
    my $param = $where->param;
237
    $where = $where->param({
238
        title => 'Perl',
239
        date => ['2010-11-11', '2011-03-05'],
240
    });
241

            
242
=head2 C<dbi>
243

            
244
    my $dbi = $where->dbi;
245
    $where = $where->dbi($dbi);
246

            
247
L<DBIx::Custom> object.
248

            
249
=head1 METHODS
250

            
251
L<DBIx::Custom::Where> inherits all methods from L<Object::Simple>
252
and implements the following new ones.
253

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

            
256
    $where->to_string;
added test
Yuki Kimoto authored on 2011-01-19
257

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

            
260
double quote is override to execute C<to_string> method.
261

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

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