Newer Older
166 lines | 4.047kb
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

            
11
use Carp 'croak';
12

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

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

            
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
22
sub to_string {
added test
Yuki Kimoto authored on 2011-01-19
23
    my $self = shift;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
24
    
many changed
Yuki Kimoto authored on 2011-01-23
25
    # Clause
added test
Yuki Kimoto authored on 2011-01-19
26
    my $clause = $self->clause;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
27
    $clause = ['and', $clause] unless ref $clause eq 'ARRAY';
added test
Yuki Kimoto authored on 2011-01-19
28
    $clause->[0] = 'and' unless @$clause;
29

            
many changed
Yuki Kimoto authored on 2011-01-23
30
    # Parse
added test
Yuki Kimoto authored on 2011-01-19
31
    my $where = [];
32
    my $count = {};
many changed
Yuki Kimoto authored on 2011-01-23
33
    $self->_parse($clause, $where, $count, 'and');
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
34
    
many changed
Yuki Kimoto authored on 2011-01-23
35
    # Stringify
36
    unshift @$where, 'where' if @$where;
added test
Yuki Kimoto authored on 2011-01-19
37
    return join(' ', @$where);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
38
}
39

            
added test
Yuki Kimoto authored on 2011-01-19
40
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/;
many changed
Yuki Kimoto authored on 2011-01-23
41
sub _parse {
added test
Yuki Kimoto authored on 2011-01-19
42
    my ($self, $clause, $where, $count, $op) = @_;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
43
    
many changed
Yuki Kimoto authored on 2011-01-23
44
    # Array
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
45
    if (ref $clause eq 'ARRAY') {
many changed
Yuki Kimoto authored on 2011-01-23
46
        
47
        # Start
added test
Yuki Kimoto authored on 2011-01-19
48
        push @$where, '(';
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
49
        
many changed
Yuki Kimoto authored on 2011-01-23
50
        # Operation
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
51
        my $op = $clause->[0] || '';
52
        croak qq{"$op" is invalid operation}
53
          unless $VALID_OPERATIONS{$op};
many changed
Yuki Kimoto authored on 2011-01-23
54
        
55
        # Parse internal clause
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
56
        for (my $i = 1; $i < @$clause; $i++) {
many changed
Yuki Kimoto authored on 2011-01-23
57
            my $pushed = $self->_parse($clause->[$i], $where, $count, $op);
added test
Yuki Kimoto authored on 2011-01-19
58
            push @$where, $op if $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
59
        }
added test
Yuki Kimoto authored on 2011-01-19
60
        pop @$where if $where->[-1] eq $op;
61
        
many changed
Yuki Kimoto authored on 2011-01-23
62
        # Undo
added test
Yuki Kimoto authored on 2011-01-19
63
        if ($where->[-1] eq '(') {
64
            pop @$where;
65
            pop @$where;
66
        }
many changed
Yuki Kimoto authored on 2011-01-23
67
        # End
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
68
        else { push @$where, ')' }
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
69
    }
many changed
Yuki Kimoto authored on 2011-01-23
70
    
71
    # String
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
72
    else {
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
73
        # Pushed
74
        my $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
75
        
added test
Yuki Kimoto authored on 2011-01-19
76
        # Column
77
        my $columns = $self->query_builder->build_query($clause)->columns;
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
78
        if (@$columns == 0) {
79
            push @$where, $clause;
80
            $pushed = 1;
81
            return $pushed;
82
        }
83
        elsif (@$columns != 1) {
84
            croak qq{Each tag contains one column name: tag "$clause"}
85
        }
86

            
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
87
        my $column = $columns->[0];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
88
        if (my $q = $self->reserved_word_quote) {
89
            $column =~ s/$q//g;
90
        }
91
        
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
92
        my $safety = $self->safety_character;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
93
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
94
          unless $column =~ /^[$safety\.]+$/;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
95
        
many changed
Yuki Kimoto authored on 2011-01-23
96
        # Column count up
added test
Yuki Kimoto authored on 2011-01-19
97
        my $count = ++$count->{$column};
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
98
        
many changed
Yuki Kimoto authored on 2011-01-23
99
        # Push
100
        my $param = $self->param;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
101
        if (ref $param eq 'HASH') {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
102
            if (exists $param->{$column}) {
103
                if (ref $param->{$column} eq 'ARRAY') {
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
104
                    $pushed = 1
105
                      if  exists $param->{$column}->[$count - 1]
106
                       && ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists';
107
                } 
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
108
                elsif ($count == 1) {
109
                    $pushed = 1;
110
                }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
111
            }
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
112
            push @$where, $clause if $pushed;
113
        }
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
114
        elsif (!defined $param) {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
115
            push @$where, $clause;
116
            $pushed = 1;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
117
        }
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
118
        else { croak "Parameter must be hash reference or undfined value" }
added test
Yuki Kimoto authored on 2011-01-19
119
        
120
        return $pushed;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
121
    }
122
}
123

            
124
1;
125

            
126
=head1 NAME
127

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

            
130
=head1 SYNOPSYS
131

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

            
134
=head1 ATTRIBUTES
135

            
136
=head2 C<clause>
137

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

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

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

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

            
149
    my $param = $where->param;
150
    $where    = $where->param({title => 'Perl',
151
                               date => ['2010-11-11', '2011-03-05']},
152
                               name => ['Ken', 'Taro']);
153

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

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

            
159
=head1 METHODS
160

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

            
163
    $where->to_string;
added test
Yuki Kimoto authored on 2011-01-19
164

            
165
Convert where clause to string correspoinding to param name.
166