Newer Older
153 lines | 3.769kb
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 test
Yuki Kimoto authored on 2011-01-19
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/;
many changed
Yuki Kimoto authored on 2011-01-23
40
sub _parse {
added test
Yuki Kimoto authored on 2011-01-19
41
    my ($self, $clause, $where, $count, $op) = @_;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
42
    
many changed
Yuki Kimoto authored on 2011-01-23
43
    # Array
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
44
    if (ref $clause eq 'ARRAY') {
many changed
Yuki Kimoto authored on 2011-01-23
45
        
46
        # Start
added test
Yuki Kimoto authored on 2011-01-19
47
        push @$where, '(';
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
48
        
many changed
Yuki Kimoto authored on 2011-01-23
49
        # Operation
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
50
        my $op = $clause->[0] || '';
51
        croak qq{"$op" is invalid operation}
52
          unless $VALID_OPERATIONS{$op};
many changed
Yuki Kimoto authored on 2011-01-23
53
        
54
        # Parse internal clause
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
55
        for (my $i = 1; $i < @$clause; $i++) {
many changed
Yuki Kimoto authored on 2011-01-23
56
            my $pushed = $self->_parse($clause->[$i], $where, $count, $op);
added test
Yuki Kimoto authored on 2011-01-19
57
            push @$where, $op if $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
58
        }
added test
Yuki Kimoto authored on 2011-01-19
59
        pop @$where if $where->[-1] eq $op;
60
        
many changed
Yuki Kimoto authored on 2011-01-23
61
        # Undo
added test
Yuki Kimoto authored on 2011-01-19
62
        if ($where->[-1] eq '(') {
63
            pop @$where;
64
            pop @$where;
65
        }
many changed
Yuki Kimoto authored on 2011-01-23
66
        # End
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
67
        else { push @$where, ')' }
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
68
    }
many changed
Yuki Kimoto authored on 2011-01-23
69
    
70
    # String
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
71
    else {
72
        
added test
Yuki Kimoto authored on 2011-01-19
73
        # Column
74
        my $columns = $self->query_builder->build_query($clause)->columns;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
75
        croak qq{Each tag contains one column name: tag "$clause"}
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
76
          unless @$columns == 1;
77
        my $column = $columns->[0];
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
78
        my $safety = $self->safety_character;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
79
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
80
          unless $column =~ /^[$safety\.]+$/;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
81
        
many changed
Yuki Kimoto authored on 2011-01-23
82
        # Column count up
added test
Yuki Kimoto authored on 2011-01-19
83
        my $count = ++$count->{$column};
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
84
        
many changed
Yuki Kimoto authored on 2011-01-23
85
        # Push
86
        my $param = $self->param;
added test
Yuki Kimoto authored on 2011-01-19
87
        my $pushed;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
88
        if (ref $param eq 'HASH') {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
89
            if (exists $param->{$column}) {
90
                if (ref $param->{$column} eq 'ARRAY') {
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
91
                    $pushed = 1
92
                      if  exists $param->{$column}->[$count - 1]
93
                       && ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists';
94
                } 
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
95
                elsif ($count == 1) {
96
                    $pushed = 1;
97
                }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
98
            }
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
99
            push @$where, $clause if $pushed;
100
        }
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
101
        elsif (!defined $param) {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
102
            push @$where, $clause;
103
            $pushed = 1;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
104
        }
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
105
        else { croak "Parameter must be hash reference or undfined value" }
added test
Yuki Kimoto authored on 2011-01-19
106
        
107
        return $pushed;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
108
    }
109
}
110

            
111
1;
112

            
113
=head1 NAME
114

            
update pod
Yuki Kimoto authored on 2011-03-13
115
DBIx::Custom::Where - Where clause EXPERIMENTAL
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
116

            
117
=head1 SYNOPSYS
118

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

            
121
=head1 ATTRIBUTES
122

            
123
=head2 C<clause>
124

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

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

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

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

            
136
    my $param = $where->param;
137
    $where    = $where->param({title => 'Perl',
138
                               date => ['2010-11-11', '2011-03-05']},
139
                               name => ['Ken', 'Taro']);
140

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

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

            
146
=head1 METHODS
147

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

            
150
    $where->to_string;
added test
Yuki Kimoto authored on 2011-01-19
151

            
152
Convert where clause to string correspoinding to param name.
153