Newer Older
188 lines | 4.822kb
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1
package DBIx::Custom::Where;
2

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
3
use Object::Simple -base;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
4

            
5
use overload 'bool' => sub {1}, fallback => 1;
6
use overload '""' => sub { shift->to_string }, fallback => 1;
7

            
cleanup
Yuki Kimoto authored on 2011-04-25
8
use DBIx::Custom::Util '_subname';
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
9
use Carp 'croak';
10

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

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
14
has [qw/param query_builder safety_character/],
cleanup
Yuki Kimoto authored on 2011-01-25
15
    clause => sub { [] },
updatedd pod
Yuki Kimoto authored on 2011-06-12
16
    reserved_word_quote => '';
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
17

            
improved error messages
Yuki Kimoto authored on 2011-04-18
18
sub new {
19
    my $self = shift->SUPER::new(@_);
20
    
21
    # Check attribute names
22
    my @attrs = keys %$self;
23
    foreach my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-25
24
        croak qq{"$attr" is invalid attribute name (} . _subname . ")"
improved error messages
Yuki Kimoto authored on 2011-04-18
25
          unless $self->can($attr);
26
    }
27
    
28
    return $self;
29
}
30

            
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
31
sub to_string {
added test
Yuki Kimoto authored on 2011-01-19
32
    my $self = shift;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
33
    
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
34
    # Check if column name is safety character;
35
    my $safety = $self->safety_character;
36
    if (ref $self->param eq 'HASH') {
37
        foreach my $column (keys %{$self->param}) {
38
            croak qq{"$column" is not safety column name (} . _subname . ")"
39
              unless $column =~ /^[$safety\.]+$/;
40
        }
41
    }
many changed
Yuki Kimoto authored on 2011-01-23
42
    # Clause
added test
Yuki Kimoto authored on 2011-01-19
43
    my $clause = $self->clause;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
44
    $clause = ['and', $clause] unless ref $clause eq 'ARRAY';
added test
Yuki Kimoto authored on 2011-01-19
45
    $clause->[0] = 'and' unless @$clause;
46

            
many changed
Yuki Kimoto authored on 2011-01-23
47
    # Parse
added test
Yuki Kimoto authored on 2011-01-19
48
    my $where = [];
49
    my $count = {};
many changed
Yuki Kimoto authored on 2011-01-23
50
    $self->_parse($clause, $where, $count, 'and');
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
51
    
many changed
Yuki Kimoto authored on 2011-01-23
52
    # Stringify
53
    unshift @$where, 'where' if @$where;
added test
Yuki Kimoto authored on 2011-01-19
54
    return join(' ', @$where);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
55
}
56

            
added test
Yuki Kimoto authored on 2011-01-19
57
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/;
many changed
Yuki Kimoto authored on 2011-01-23
58
sub _parse {
added test
Yuki Kimoto authored on 2011-01-19
59
    my ($self, $clause, $where, $count, $op) = @_;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
60
    
many changed
Yuki Kimoto authored on 2011-01-23
61
    # Array
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
62
    if (ref $clause eq 'ARRAY') {
many changed
Yuki Kimoto authored on 2011-01-23
63
        
64
        # Start
added test
Yuki Kimoto authored on 2011-01-19
65
        push @$where, '(';
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
66
        
many changed
Yuki Kimoto authored on 2011-01-23
67
        # Operation
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
68
        my $op = $clause->[0] || '';
improved error message
Yuki Kimoto authored on 2011-06-13
69
        croak qq{First argument must be "and" or "or" in where clause } .
70
              qq{"$op" is passed} . _subname . ")"
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
71
          unless $VALID_OPERATIONS{$op};
many changed
Yuki Kimoto authored on 2011-01-23
72
        
73
        # Parse internal clause
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
74
        for (my $i = 1; $i < @$clause; $i++) {
many changed
Yuki Kimoto authored on 2011-01-23
75
            my $pushed = $self->_parse($clause->[$i], $where, $count, $op);
added test
Yuki Kimoto authored on 2011-01-19
76
            push @$where, $op if $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
77
        }
added test
Yuki Kimoto authored on 2011-01-19
78
        pop @$where if $where->[-1] eq $op;
79
        
many changed
Yuki Kimoto authored on 2011-01-23
80
        # Undo
added test
Yuki Kimoto authored on 2011-01-19
81
        if ($where->[-1] eq '(') {
82
            pop @$where;
83
            pop @$where;
84
        }
many changed
Yuki Kimoto authored on 2011-01-23
85
        # End
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
86
        else { push @$where, ')' }
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
87
    }
many changed
Yuki Kimoto authored on 2011-01-23
88
    
89
    # String
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
90
    else {
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
91
        # Pushed
92
        my $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
93
        
added test
Yuki Kimoto authored on 2011-01-19
94
        # Column
95
        my $columns = $self->query_builder->build_query($clause)->columns;
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
96
        if (@$columns == 0) {
97
            push @$where, $clause;
98
            $pushed = 1;
99
            return $pushed;
100
        }
101
        elsif (@$columns != 1) {
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
102
            croak qq{Each part contains one column name: "$clause" (}
cleanup
Yuki Kimoto authored on 2011-04-25
103
                  . _subname . ")";
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
104
        }
105

            
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
106
        my $column = $columns->[0];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
107
        if (my $q = $self->reserved_word_quote) {
108
            $column =~ s/$q//g;
109
        }
110
        
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
111
        my $safety = $self->safety_character;
cleanup
Yuki Kimoto authored on 2011-04-25
112
        croak qq{"$column" is not safety column name (} . _subname . ")"
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
113
          unless $column =~ /^[$safety\.]+$/;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
114
        
many changed
Yuki Kimoto authored on 2011-01-23
115
        # Column count up
added test
Yuki Kimoto authored on 2011-01-19
116
        my $count = ++$count->{$column};
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
117
        
many changed
Yuki Kimoto authored on 2011-01-23
118
        # Push
119
        my $param = $self->param;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
120
        if (ref $param eq 'HASH') {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
121
            if (exists $param->{$column}) {
122
                if (ref $param->{$column} eq 'ARRAY') {
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
123
                    $pushed = 1
124
                      if  exists $param->{$column}->[$count - 1]
125
                       && ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists';
126
                } 
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
127
                elsif ($count == 1) {
128
                    $pushed = 1;
129
                }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
130
            }
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
131
            push @$where, $clause if $pushed;
132
        }
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
133
        elsif (!defined $param) {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
134
            push @$where, $clause;
135
            $pushed = 1;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
136
        }
improved error messages
Yuki Kimoto authored on 2011-04-18
137
        else {
cleanup
Yuki Kimoto authored on 2011-04-25
138
            croak "Parameter must be hash reference or undfined value ("
139
                . _subname . ")"
improved error messages
Yuki Kimoto authored on 2011-04-18
140
        }
added test
Yuki Kimoto authored on 2011-01-19
141
        return $pushed;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
142
    }
143
}
144

            
145
1;
146

            
147
=head1 NAME
148

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

            
151
=head1 SYNOPSYS
152

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

            
155
=head1 ATTRIBUTES
156

            
157
=head2 C<clause>
158

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

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

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

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

            
170
    my $param = $where->param;
171
    $where    = $where->param({title => 'Perl',
172
                               date => ['2010-11-11', '2011-03-05']},
173
                               name => ['Ken', 'Taro']);
174

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

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

            
180
=head1 METHODS
181

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

            
184
    $where->to_string;
added test
Yuki Kimoto authored on 2011-01-19
185

            
186
Convert where clause to string correspoinding to param name.
187

            
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
188
=cut