Newer Older
192 lines | 4.81kb
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1
package DBIx::Custom::Where;
2

            
3
use strict;
4
use warnings;
5

            
6
use base 'Object::Simple';
7

            
8
use overload 'bool' => sub {1}, fallback => 1;
9
use overload '""' => sub { shift->to_string }, fallback => 1;
10

            
cleanup
Yuki Kimoto authored on 2011-04-25
11
use DBIx::Custom::Util '_subname';
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
12
use Carp 'croak';
13

            
updated document
Yuki Kimoto authored on 2011-01-20
14
# Carp trust relationship
15
push @DBIx::Custom::CARP_NOT, __PACKAGE__;
16

            
added test
Yuki Kimoto authored on 2011-01-19
17
__PACKAGE__->attr(
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
18
    [qw/param query_builder safety_character/],
cleanup
Yuki Kimoto authored on 2011-01-25
19
    clause => sub { [] },
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
20
    reserved_word_quote => ''
added test
Yuki Kimoto authored on 2011-01-19
21
);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
22

            
improved error messages
Yuki Kimoto authored on 2011-04-18
23
sub new {
24
    my $self = shift->SUPER::new(@_);
25
    
26
    # Check attribute names
27
    my @attrs = keys %$self;
28
    foreach my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-25
29
        croak qq{"$attr" is invalid attribute name (} . _subname . ")"
improved error messages
Yuki Kimoto authored on 2011-04-18
30
          unless $self->can($attr);
31
    }
32
    
33
    return $self;
34
}
35

            
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
36
sub to_string {
added test
Yuki Kimoto authored on 2011-01-19
37
    my $self = shift;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
38
    
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
39
    # Check if column name is safety character;
40
    my $safety = $self->safety_character;
41
    if (ref $self->param eq 'HASH') {
42
        foreach my $column (keys %{$self->param}) {
43
            croak qq{"$column" is not safety column name (} . _subname . ")"
44
              unless $column =~ /^[$safety\.]+$/;
45
        }
46
    }
many changed
Yuki Kimoto authored on 2011-01-23
47
    # Clause
added test
Yuki Kimoto authored on 2011-01-19
48
    my $clause = $self->clause;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
49
    $clause = ['and', $clause] unless ref $clause eq 'ARRAY';
added test
Yuki Kimoto authored on 2011-01-19
50
    $clause->[0] = 'and' unless @$clause;
51

            
many changed
Yuki Kimoto authored on 2011-01-23
52
    # Parse
added test
Yuki Kimoto authored on 2011-01-19
53
    my $where = [];
54
    my $count = {};
many changed
Yuki Kimoto authored on 2011-01-23
55
    $self->_parse($clause, $where, $count, 'and');
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
56
    
many changed
Yuki Kimoto authored on 2011-01-23
57
    # Stringify
58
    unshift @$where, 'where' if @$where;
added test
Yuki Kimoto authored on 2011-01-19
59
    return join(' ', @$where);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
60
}
61

            
added test
Yuki Kimoto authored on 2011-01-19
62
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/;
many changed
Yuki Kimoto authored on 2011-01-23
63
sub _parse {
added test
Yuki Kimoto authored on 2011-01-19
64
    my ($self, $clause, $where, $count, $op) = @_;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
65
    
many changed
Yuki Kimoto authored on 2011-01-23
66
    # Array
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
67
    if (ref $clause eq 'ARRAY') {
many changed
Yuki Kimoto authored on 2011-01-23
68
        
69
        # Start
added test
Yuki Kimoto authored on 2011-01-19
70
        push @$where, '(';
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
71
        
many changed
Yuki Kimoto authored on 2011-01-23
72
        # Operation
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
73
        my $op = $clause->[0] || '';
cleanup
Yuki Kimoto authored on 2011-04-25
74
        croak qq{"$op" is invalid operation (} . _subname . ")"
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
75
          unless $VALID_OPERATIONS{$op};
many changed
Yuki Kimoto authored on 2011-01-23
76
        
77
        # Parse internal clause
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
78
        for (my $i = 1; $i < @$clause; $i++) {
many changed
Yuki Kimoto authored on 2011-01-23
79
            my $pushed = $self->_parse($clause->[$i], $where, $count, $op);
added test
Yuki Kimoto authored on 2011-01-19
80
            push @$where, $op if $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
81
        }
added test
Yuki Kimoto authored on 2011-01-19
82
        pop @$where if $where->[-1] eq $op;
83
        
many changed
Yuki Kimoto authored on 2011-01-23
84
        # Undo
added test
Yuki Kimoto authored on 2011-01-19
85
        if ($where->[-1] eq '(') {
86
            pop @$where;
87
            pop @$where;
88
        }
many changed
Yuki Kimoto authored on 2011-01-23
89
        # End
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
90
        else { push @$where, ')' }
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
91
    }
many changed
Yuki Kimoto authored on 2011-01-23
92
    
93
    # String
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
94
    else {
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
95
        # Pushed
96
        my $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
97
        
added test
Yuki Kimoto authored on 2011-01-19
98
        # Column
99
        my $columns = $self->query_builder->build_query($clause)->columns;
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
100
        if (@$columns == 0) {
101
            push @$where, $clause;
102
            $pushed = 1;
103
            return $pushed;
104
        }
105
        elsif (@$columns != 1) {
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
106
            croak qq{Each part contains one column name: "$clause" (}
cleanup
Yuki Kimoto authored on 2011-04-25
107
                  . _subname . ")";
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
108
        }
109

            
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
110
        my $column = $columns->[0];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
111
        if (my $q = $self->reserved_word_quote) {
112
            $column =~ s/$q//g;
113
        }
114
        
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
115
        my $safety = $self->safety_character;
cleanup
Yuki Kimoto authored on 2011-04-25
116
        croak qq{"$column" is not safety column name (} . _subname . ")"
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
117
          unless $column =~ /^[$safety\.]+$/;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
118
        
many changed
Yuki Kimoto authored on 2011-01-23
119
        # Column count up
added test
Yuki Kimoto authored on 2011-01-19
120
        my $count = ++$count->{$column};
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
121
        
many changed
Yuki Kimoto authored on 2011-01-23
122
        # Push
123
        my $param = $self->param;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
124
        if (ref $param eq 'HASH') {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
125
            if (exists $param->{$column}) {
126
                if (ref $param->{$column} eq 'ARRAY') {
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
127
                    $pushed = 1
128
                      if  exists $param->{$column}->[$count - 1]
129
                       && ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists';
130
                } 
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
131
                elsif ($count == 1) {
132
                    $pushed = 1;
133
                }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
134
            }
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
135
            push @$where, $clause if $pushed;
136
        }
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
137
        elsif (!defined $param) {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
138
            push @$where, $clause;
139
            $pushed = 1;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
140
        }
improved error messages
Yuki Kimoto authored on 2011-04-18
141
        else {
cleanup
Yuki Kimoto authored on 2011-04-25
142
            croak "Parameter must be hash reference or undfined value ("
143
                . _subname . ")"
improved error messages
Yuki Kimoto authored on 2011-04-18
144
        }
added test
Yuki Kimoto authored on 2011-01-19
145
        return $pushed;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
146
    }
147
}
148

            
149
1;
150

            
151
=head1 NAME
152

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

            
155
=head1 SYNOPSYS
156

            
many changed
Yuki Kimoto authored on 2011-01-23
157
    my $where = DBIx::Custom::Where->new;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
158

            
159
=head1 ATTRIBUTES
160

            
161
=head2 C<clause>
162

            
added test
Yuki Kimoto authored on 2011-01-19
163
    $where->clause(
164
        ['and', '{= title}', ['or', '{< date}', '{> date}']]
165
    );
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
166

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

            
170
    "where ( {= title} and ( {< date} or {> date} ) )"
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
171

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

            
174
    my $param = $where->param;
175
    $where    = $where->param({title => 'Perl',
176
                               date => ['2010-11-11', '2011-03-05']},
177
                               name => ['Ken', 'Taro']);
178

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
179
=head2 C<safety_character>
update pod
Yuki Kimoto authored on 2011-01-27
180

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
181
    my $safety_character = $self->safety_character;
182
    $dbi                 = $self->safety_character($name);
update pod
Yuki Kimoto authored on 2011-01-27
183

            
184
=head1 METHODS
185

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

            
188
    $where->to_string;
added test
Yuki Kimoto authored on 2011-01-19
189

            
190
Convert where clause to string correspoinding to param name.
191

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