Newer Older
158 lines | 3.887kb
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 {
73
        
added test
Yuki Kimoto authored on 2011-01-19
74
        # Column
75
        my $columns = $self->query_builder->build_query($clause)->columns;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
76
        croak qq{Each tag contains one column name: tag "$clause"}
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
77
          unless @$columns == 1;
78
        my $column = $columns->[0];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
79
        if (my $q = $self->reserved_word_quote) {
80
            $column =~ s/$q//g;
81
        }
82
        
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
83
        my $safety = $self->safety_character;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
84
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
85
          unless $column =~ /^[$safety\.]+$/;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
86
        
many changed
Yuki Kimoto authored on 2011-01-23
87
        # Column count up
added test
Yuki Kimoto authored on 2011-01-19
88
        my $count = ++$count->{$column};
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
89
        
many changed
Yuki Kimoto authored on 2011-01-23
90
        # Push
91
        my $param = $self->param;
added test
Yuki Kimoto authored on 2011-01-19
92
        my $pushed;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
93
        if (ref $param eq 'HASH') {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
94
            if (exists $param->{$column}) {
95
                if (ref $param->{$column} eq 'ARRAY') {
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
96
                    $pushed = 1
97
                      if  exists $param->{$column}->[$count - 1]
98
                       && ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists';
99
                } 
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
100
                elsif ($count == 1) {
101
                    $pushed = 1;
102
                }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
103
            }
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
104
            push @$where, $clause if $pushed;
105
        }
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
106
        elsif (!defined $param) {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
107
            push @$where, $clause;
108
            $pushed = 1;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
109
        }
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
110
        else { croak "Parameter must be hash reference or undfined value" }
added test
Yuki Kimoto authored on 2011-01-19
111
        
112
        return $pushed;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
113
    }
114
}
115

            
116
1;
117

            
118
=head1 NAME
119

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

            
122
=head1 SYNOPSYS
123

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

            
126
=head1 ATTRIBUTES
127

            
128
=head2 C<clause>
129

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

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

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

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

            
141
    my $param = $where->param;
142
    $where    = $where->param({title => 'Perl',
143
                               date => ['2010-11-11', '2011-03-05']},
144
                               name => ['Ken', 'Taro']);
145

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

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

            
151
=head1 METHODS
152

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

            
155
    $where->to_string;
added test
Yuki Kimoto authored on 2011-01-19
156

            
157
Convert where clause to string correspoinding to param name.
158