Newer Older
214 lines | 5.464kb
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
    
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
31
    # Check if column name is safety character;
cleanup
Yuki Kimoto authored on 2011-08-02
32
    my $safety = $self->dbi->safety_character;
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
33
    if (ref $self->param eq 'HASH') {
cleanup
Yuki Kimoto authored on 2011-10-21
34
        for my $column (keys %{$self->param}) {
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
35
            croak qq{"$column" is not safety column name (} . _subname . ")"
36
              unless $column =~ /^[$safety\.]+$/;
37
        }
38
    }
many changed
Yuki Kimoto authored on 2011-01-23
39
    # Clause
added test
Yuki Kimoto authored on 2011-01-19
40
    my $clause = $self->clause;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
41
    $clause = ['and', $clause] unless ref $clause eq 'ARRAY';
added test
Yuki Kimoto authored on 2011-01-19
42
    $clause->[0] = 'and' unless @$clause;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
43
    
many changed
Yuki Kimoto authored on 2011-01-23
44
    # Parse
added test
Yuki Kimoto authored on 2011-01-19
45
    my $where = [];
46
    my $count = {};
micro optimization
Yuki Kimoto authored on 2011-10-24
47
    $self->{_query_builder} = $self->dbi->query_builder;
48
    $self->{_safety_character} = $self->dbi->safety_character;
49
    $self->{_quote} = $self->dbi->_quote;
many changed
Yuki Kimoto authored on 2011-01-23
50
    $self->_parse($clause, $where, $count, 'and');
micro optimization
Yuki Kimoto authored on 2011-10-24
51

            
52
        
53
    # Check safety
54
    unless (join('', keys %$count) =~ /^[$self->{_safety_character}\.]+$/) {
55
        for my $column (keys %$count) {
56
            croak qq{"$column" is not safety column name (} . _subname . ")"
57
              unless $column =~ /^[$self->{_safety_character}\.]+$/;
58
        }
59
    }
60

            
many changed
Yuki Kimoto authored on 2011-01-23
61
    # Stringify
62
    unshift @$where, 'where' if @$where;
added test
Yuki Kimoto authored on 2011-01-19
63
    return join(' ', @$where);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
64
}
65

            
added test
Yuki Kimoto authored on 2011-01-19
66
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/;
many changed
Yuki Kimoto authored on 2011-01-23
67
sub _parse {
micro optimization
Yuki Kimoto authored on 2011-10-24
68
    my ($self, $clause, $where, $count, $op, $info) = @_;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
69
    
many changed
Yuki Kimoto authored on 2011-01-23
70
    # Array
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
71
    if (ref $clause eq 'ARRAY') {
many changed
Yuki Kimoto authored on 2011-01-23
72
        
73
        # Start
added test
Yuki Kimoto authored on 2011-01-19
74
        push @$where, '(';
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
75
        
many changed
Yuki Kimoto authored on 2011-01-23
76
        # Operation
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
77
        my $op = $clause->[0] || '';
improved error message
Yuki Kimoto authored on 2011-06-13
78
        croak qq{First argument must be "and" or "or" in where clause } .
79
              qq{"$op" is passed} . _subname . ")"
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
80
          unless $VALID_OPERATIONS{$op};
many changed
Yuki Kimoto authored on 2011-01-23
81
        
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
82
        my $pushed_array;
many changed
Yuki Kimoto authored on 2011-01-23
83
        # Parse internal clause
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
84
        for (my $i = 1; $i < @$clause; $i++) {
many changed
Yuki Kimoto authored on 2011-01-23
85
            my $pushed = $self->_parse($clause->[$i], $where, $count, $op);
added test
Yuki Kimoto authored on 2011-01-19
86
            push @$where, $op if $pushed;
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
87
            $pushed_array = 1 if $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
88
        }
added test
Yuki Kimoto authored on 2011-01-19
89
        pop @$where if $where->[-1] eq $op;
90
        
many changed
Yuki Kimoto authored on 2011-01-23
91
        # Undo
added test
Yuki Kimoto authored on 2011-01-19
92
        if ($where->[-1] eq '(') {
93
            pop @$where;
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
94
            pop @$where if ($where->[-1] || '') eq $op;
added test
Yuki Kimoto authored on 2011-01-19
95
        }
many changed
Yuki Kimoto authored on 2011-01-23
96
        # End
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
97
        else { push @$where, ')' }
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
98
        
99
        return $pushed_array;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
100
    }
many changed
Yuki Kimoto authored on 2011-01-23
101
    
102
    # String
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
103
    else {
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
104
        # Pushed
105
        my $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
106
        
added test
Yuki Kimoto authored on 2011-01-19
107
        # Column
micro optimization
Yuki Kimoto authored on 2011-10-24
108
        my $columns = $self->{_query_builder}->build_query($clause)->{columns};
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
109
        if (@$columns == 0) {
110
            push @$where, $clause;
111
            $pushed = 1;
112
            return $pushed;
113
        }
114
        elsif (@$columns != 1) {
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
115
            croak qq{Each part contains one column name: "$clause" (}
cleanup
Yuki Kimoto authored on 2011-04-25
116
                  . _subname . ")";
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
117
        }
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
118
        
119
        # Remove quote
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
120
        my $column = $columns->[0];
micro optimization
Yuki Kimoto authored on 2011-10-24
121
        if (my $q = $self->{_quote}) {
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
122
            $q = quotemeta($q);
123
            $column =~ s/[$q]//g;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
124
        }
125
        
many changed
Yuki Kimoto authored on 2011-01-23
126
        # Column count up
added test
Yuki Kimoto authored on 2011-01-19
127
        my $count = ++$count->{$column};
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
128
        
many changed
Yuki Kimoto authored on 2011-01-23
129
        # Push
micro optimization
Yuki Kimoto authored on 2011-10-24
130
        my $param = $self->{param};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
131
        if (ref $param eq 'HASH') {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
132
            if (exists $param->{$column}) {
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
133
                my $if = $self->{_if};
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
134
                
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
135
                if (ref $param->{$column} eq 'ARRAY') {
cleanup
Yuki Kimoto authored on 2011-08-09
136
                    $pushed = 1 if exists $param->{$column}->[$count - 1]
137
                      && ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists'
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
138
                }
cleanup
Yuki Kimoto authored on 2011-08-09
139
                elsif ($count == 1) { $pushed = 1 }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
140
            }
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
141
            push @$where, $clause if $pushed;
142
        }
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
143
        elsif (!defined $param) {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
144
            push @$where, $clause;
145
            $pushed = 1;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
146
        }
improved error messages
Yuki Kimoto authored on 2011-04-18
147
        else {
cleanup
Yuki Kimoto authored on 2011-04-25
148
            croak "Parameter must be hash reference or undfined value ("
149
                . _subname . ")"
improved error messages
Yuki Kimoto authored on 2011-04-18
150
        }
added test
Yuki Kimoto authored on 2011-01-19
151
        return $pushed;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
152
    }
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
153
    return;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
154
}
155

            
156
1;
157

            
158
=head1 NAME
159

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

            
162
=head1 SYNOPSYS
163

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

            
167
=head1 ATTRIBUTES
168

            
169
=head2 C<clause>
170

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
171
    my $clause = $where->clause;
172
    $where = $where->clause(
173
        ['and',
174
            'title = :title', 
175
            ['or', 'date < :date', 'date > :date']
176
        ]
added test
Yuki Kimoto authored on 2011-01-19
177
    );
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
178

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

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

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

            
186
    my $param = $where->param;
187
    $where = $where->param({
188
        title => 'Perl',
189
        date => ['2010-11-11', '2011-03-05'],
190
    });
191

            
192
=head2 C<dbi>
193

            
194
    my $dbi = $where->dbi;
195
    $where = $where->dbi($dbi);
196

            
197
L<DBIx::Custom> object.
198

            
199
=head1 METHODS
200

            
201
L<DBIx::Custom::Where> inherits all methods from L<Object::Simple>
202
and implements the following new ones.
203

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

            
206
    $where->to_string;
added test
Yuki Kimoto authored on 2011-01-19
207

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

            
210
double quote is override to execute C<to_string> method.
211

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

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