Newer Older
140 lines | 3.114kb
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(
17
  'query_builder',
18
  clause => sub { [] },
19
  param => sub { {} }
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/;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
41

            
many changed
Yuki Kimoto authored on 2011-01-23
42
sub _parse {
added test
Yuki Kimoto authored on 2011-01-19
43
    my ($self, $clause, $where, $count, $op) = @_;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
44
    
many changed
Yuki Kimoto authored on 2011-01-23
45
    # Array
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
46
    if (ref $clause eq 'ARRAY') {
many changed
Yuki Kimoto authored on 2011-01-23
47
        
48
        # Start
added test
Yuki Kimoto authored on 2011-01-19
49
        push @$where, '(';
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
50
        
many changed
Yuki Kimoto authored on 2011-01-23
51
        # Operation
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
52
        my $op = $clause->[0] || '';
53
        croak qq{"$op" is invalid operation}
54
          unless $VALID_OPERATIONS{$op};
many changed
Yuki Kimoto authored on 2011-01-23
55
        
56
        # Parse internal clause
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
57
        for (my $i = 1; $i < @$clause; $i++) {
many changed
Yuki Kimoto authored on 2011-01-23
58
            my $pushed = $self->_parse($clause->[$i], $where, $count, $op);
added test
Yuki Kimoto authored on 2011-01-19
59
            push @$where, $op if $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
60
        }
added test
Yuki Kimoto authored on 2011-01-19
61
        pop @$where if $where->[-1] eq $op;
62
        
many changed
Yuki Kimoto authored on 2011-01-23
63
        # Undo
added test
Yuki Kimoto authored on 2011-01-19
64
        if ($where->[-1] eq '(') {
65
            pop @$where;
66
            pop @$where;
67
        }
many changed
Yuki Kimoto authored on 2011-01-23
68
        
69
        # End
added test
Yuki Kimoto authored on 2011-01-19
70
        else {
71
            push @$where, ')';
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
72
        }
73
    }
many changed
Yuki Kimoto authored on 2011-01-23
74
    
75
    # String
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
76
    else {
77
        
added test
Yuki Kimoto authored on 2011-01-19
78
        # Column
79
        my $columns = $self->query_builder->build_query($clause)->columns;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
80
        croak qq{each tag contains one column name: tag "$clause"}
81
          unless @$columns == 1;
82
        my $column = $columns->[0];
83
        
many changed
Yuki Kimoto authored on 2011-01-23
84
        # Column count up
added test
Yuki Kimoto authored on 2011-01-19
85
        my $count = ++$count->{$column};
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
86
        
many changed
Yuki Kimoto authored on 2011-01-23
87
        # Push
88
        my $param = $self->param;
added test
Yuki Kimoto authored on 2011-01-19
89
        my $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
90
        if (exists $param->{$column}) {
added test
Yuki Kimoto authored on 2011-01-19
91
            if (ref $param->{$column} eq 'ARRAY') {
92
                $pushed = 1 if exists $param->{$column}->[$count - 1];
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
93
            }
added test
Yuki Kimoto authored on 2011-01-19
94
            elsif ($count == 1) {
95
                $pushed = 1;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
96
            }
97
        }
added test
Yuki Kimoto authored on 2011-01-19
98
        push @$where, $clause if $pushed;
99
        
100
        return $pushed;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
101
    }
102
}
103

            
104
1;
105

            
106
=head1 NAME
107

            
108
DBIx::Custom::Where - Where clause
109

            
110
=head1 SYNOPSYS
111

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

            
114
=head1 ATTRIBUTES
115

            
116
=head2 C<param>
117

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

            
124
=head2 C<clause>
125

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

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

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

            
135
=head2 C<to_string>
136

            
137
    $where->to_string;
added test
Yuki Kimoto authored on 2011-01-19
138

            
139
Convert where clause to string correspoinding to param name.
140