Newer Older
142 lines | 3.233kb
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(
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
17
  [qw/param query_builder/],
added test
Yuki Kimoto authored on 2011-01-19
18
  clause => sub { [] },
19
);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
20

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

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

            
added test
Yuki Kimoto authored on 2011-01-19
39
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
40

            
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 {
73
        
added test
Yuki Kimoto authored on 2011-01-19
74
        # Column
75
        my $columns = $self->query_builder->build_query($clause)->columns;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
76
        croak qq{each tag contains one column name: tag "$clause"}
77
          unless @$columns == 1;
78
        my $column = $columns->[0];
79
        
many changed
Yuki Kimoto authored on 2011-01-23
80
        # Column count up
added test
Yuki Kimoto authored on 2011-01-19
81
        my $count = ++$count->{$column};
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
82
        
many changed
Yuki Kimoto authored on 2011-01-23
83
        # Push
84
        my $param = $self->param;
added test
Yuki Kimoto authored on 2011-01-19
85
        my $pushed;
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
86
        if (defined $param) {
87
            if (exists $param->{$column}) {
88
                if (ref $param->{$column} eq 'ARRAY') {
89
                    $pushed = 1 if exists $param->{$column}->[$count - 1];
90
                }
91
                elsif ($count == 1) {
92
                    $pushed = 1;
93
                }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
94
            }
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
95
            push @$where, $clause if $pushed;
96
        }
97
        else {
98
            push @$where, $clause;
99
            $pushed = 1;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
100
        }
added test
Yuki Kimoto authored on 2011-01-19
101
        
102
        return $pushed;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
103
    }
104
}
105

            
106
1;
107

            
108
=head1 NAME
109

            
110
DBIx::Custom::Where - Where clause
111

            
112
=head1 SYNOPSYS
113

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

            
116
=head1 ATTRIBUTES
117

            
118
=head2 C<param>
119

            
120
    my $param = $where->param;
121
    $where    = $where->param({title => 'Perl',
122
                               date => ['2010-11-11', '2011-03-05']},
123
                               name => ['Ken', 'Taro']);
124
=head1 METHODS
125

            
126
=head2 C<clause>
127

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

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

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

            
137
=head2 C<to_string>
138

            
139
    $where->to_string;
added test
Yuki Kimoto authored on 2011-01-19
140

            
141
Convert where clause to string correspoinding to param name.
142