Newer Older
262 lines | 6.859kb
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;
cleanup
Yuki Kimoto authored on 2011-11-18
96
        if ($self->{_tag_parse} && ($clause =~ /\s\{/ || $clause =~ /^\{/)) {
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 {
micro optimization
Yuki Kimoto authored on 2011-11-18
101
            my $sql = " " . $clause || '';
fixed where clause parsing b...
Yuki Kimoto authored on 2011-11-01
102
            $sql =~ s/([0-9]):/$1\\:/g;
micro optimization
Yuki Kimoto authored on 2011-11-18
103
            ($column) = $sql =~ /[^\\]:([$c\.]+)/s
fixed where clause parsing b...
Yuki Kimoto authored on 2011-11-01
104
        }
micro optimization
Yuki Kimoto authored on 2011-10-31
105
        unless (defined $column) {
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
106
            push @$where, $clause;
107
            $pushed = 1;
108
            return $pushed;
109
        }
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
110
        
many changed
Yuki Kimoto authored on 2011-01-23
111
        # Column count up
added test
Yuki Kimoto authored on 2011-01-19
112
        my $count = ++$count->{$column};
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
113
        
many changed
Yuki Kimoto authored on 2011-01-23
114
        # Push
micro optimization
Yuki Kimoto authored on 2011-10-24
115
        my $param = $self->{param};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
116
        if (ref $param eq 'HASH') {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
117
            if (exists $param->{$column}) {
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
118
                my $if = $self->{_if};
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
119
                
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
120
                if (ref $param->{$column} eq 'ARRAY') {
cleanup
Yuki Kimoto authored on 2011-08-09
121
                    $pushed = 1 if exists $param->{$column}->[$count - 1]
122
                      && ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists'
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
123
                }
cleanup
Yuki Kimoto authored on 2011-08-09
124
                elsif ($count == 1) { $pushed = 1 }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
125
            }
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
126
            push @$where, $clause if $pushed;
127
        }
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
128
        elsif (!defined $param) {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
129
            push @$where, $clause;
130
            $pushed = 1;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
131
        }
improved error messages
Yuki Kimoto authored on 2011-04-18
132
        else {
cleanup
Yuki Kimoto authored on 2011-04-25
133
            croak "Parameter must be hash reference or undfined value ("
134
                . _subname . ")"
improved error messages
Yuki Kimoto authored on 2011-04-18
135
        }
added test
Yuki Kimoto authored on 2011-01-19
136
        return $pushed;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
137
    }
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
138
    return;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
139
}
140
1;
141

            
142
=head1 NAME
143

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

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

            
215
=head1 ATTRIBUTES
216

            
217
=head2 C<clause>
218

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

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

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

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

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

            
240
=head2 C<dbi>
241

            
242
    my $dbi = $where->dbi;
243
    $where = $where->dbi($dbi);
244

            
245
L<DBIx::Custom> object.
246

            
247
=head1 METHODS
248

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

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

            
254
    $where->to_string;
added test
Yuki Kimoto authored on 2011-01-19
255

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

            
258
double quote is override to execute C<to_string> method.
259

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

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