Newer Older
199 lines | 4.953kb
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1
package DBIx::Custom::Where;
updatedd pod
Yuki Kimoto authored on 2011-06-12
2
use Object::Simple -base;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
3

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
4
use Carp 'croak';
5
use DBIx::Custom::Util '_subname';
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
6
use overload 'bool' => sub {1}, fallback => 1;
7
use overload '""' => sub { shift->to_string }, fallback => 1;
8

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

            
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
12
has [qw/dbi param/],
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
13
    clause => sub { [] };
14

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

            
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
28
sub to_string {
added test
Yuki Kimoto authored on 2011-01-19
29
    my $self = shift;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
30
    
many changed
Yuki Kimoto authored on 2011-01-23
31
    # Clause
added test
Yuki Kimoto authored on 2011-01-19
32
    my $clause = $self->clause;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
33
    $clause = ['and', $clause] unless ref $clause eq 'ARRAY';
added test
Yuki Kimoto authored on 2011-01-19
34
    $clause->[0] = 'and' unless @$clause;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
35
    
many changed
Yuki Kimoto authored on 2011-01-23
36
    # Parse
added test
Yuki Kimoto authored on 2011-01-19
37
    my $where = [];
38
    my $count = {};
micro optimization
Yuki Kimoto authored on 2011-10-24
39
    $self->{_query_builder} = $self->dbi->query_builder;
40
    $self->{_safety_character} = $self->dbi->safety_character;
41
    $self->{_quote} = $self->dbi->_quote;
micro optimization
Yuki Kimoto authored on 2011-10-31
42
    $self->{_tag_parse} = $self->dbi->tag_parse;
many changed
Yuki Kimoto authored on 2011-01-23
43
    $self->_parse($clause, $where, $count, 'and');
micro optimization
Yuki Kimoto authored on 2011-10-24
44

            
many changed
Yuki Kimoto authored on 2011-01-23
45
    # Stringify
46
    unshift @$where, 'where' if @$where;
added test
Yuki Kimoto authored on 2011-01-19
47
    return join(' ', @$where);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
48
}
49

            
added test
Yuki Kimoto authored on 2011-01-19
50
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/;
many changed
Yuki Kimoto authored on 2011-01-23
51
sub _parse {
micro optimization
Yuki Kimoto authored on 2011-10-24
52
    my ($self, $clause, $where, $count, $op, $info) = @_;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
53
    
many changed
Yuki Kimoto authored on 2011-01-23
54
    # Array
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
55
    if (ref $clause eq 'ARRAY') {
many changed
Yuki Kimoto authored on 2011-01-23
56
        
57
        # Start
added test
Yuki Kimoto authored on 2011-01-19
58
        push @$where, '(';
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
59
        
many changed
Yuki Kimoto authored on 2011-01-23
60
        # Operation
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
61
        my $op = $clause->[0] || '';
improved error message
Yuki Kimoto authored on 2011-06-13
62
        croak qq{First argument must be "and" or "or" in where clause } .
63
              qq{"$op" is passed} . _subname . ")"
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
64
          unless $VALID_OPERATIONS{$op};
many changed
Yuki Kimoto authored on 2011-01-23
65
        
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
66
        my $pushed_array;
many changed
Yuki Kimoto authored on 2011-01-23
67
        # Parse internal clause
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
68
        for (my $i = 1; $i < @$clause; $i++) {
many changed
Yuki Kimoto authored on 2011-01-23
69
            my $pushed = $self->_parse($clause->[$i], $where, $count, $op);
added test
Yuki Kimoto authored on 2011-01-19
70
            push @$where, $op if $pushed;
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
71
            $pushed_array = 1 if $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
72
        }
added test
Yuki Kimoto authored on 2011-01-19
73
        pop @$where if $where->[-1] eq $op;
74
        
many changed
Yuki Kimoto authored on 2011-01-23
75
        # Undo
added test
Yuki Kimoto authored on 2011-01-19
76
        if ($where->[-1] eq '(') {
77
            pop @$where;
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
78
            pop @$where if ($where->[-1] || '') eq $op;
added test
Yuki Kimoto authored on 2011-01-19
79
        }
many changed
Yuki Kimoto authored on 2011-01-23
80
        # End
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
81
        else { push @$where, ')' }
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
82
        
83
        return $pushed_array;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
84
    }
many changed
Yuki Kimoto authored on 2011-01-23
85
    
86
    # String
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
87
    else {
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
88
        # Pushed
89
        my $pushed;
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
90
        
added test
Yuki Kimoto authored on 2011-01-19
91
        # Column
micro optimization
Yuki Kimoto authored on 2011-10-31
92
        my $c = $self->{_safety_character};
93
        
94
        my $column;
fixed where clause parsing b...
Yuki Kimoto authored on 2011-11-01
95
        if ($self->{_tag_parse} && $clause =~ /(\s|^)\{/) {
micro optimization
Yuki Kimoto authored on 2011-10-31
96
            my $columns = $self->dbi->query_builder->build_query($clause)->{columns};
97
            $column = $columns->[0];
98
        }
fixed where clause parsing b...
Yuki Kimoto authored on 2011-11-01
99
        else {
100
            my $sql = $clause;
101
            $sql =~ s/([0-9]):/$1\\:/g;
102
            if ($sql =~ /[^\\]:([$c\.]+)/s || $sql =~ /^:([$c\.]+)/s) {
103
                ($column) = $1;
104
            }
105
        }
micro optimization
Yuki Kimoto authored on 2011-10-31
106
        unless (defined $column) {
DBIx::Custom::Where clause a...
Yuki Kimoto authored on 2011-04-18
107
            push @$where, $clause;
108
            $pushed = 1;
109
            return $pushed;
110
        }
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
111
        
many changed
Yuki Kimoto authored on 2011-01-23
112
        # Column count up
added test
Yuki Kimoto authored on 2011-01-19
113
        my $count = ++$count->{$column};
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
114
        
many changed
Yuki Kimoto authored on 2011-01-23
115
        # Push
micro optimization
Yuki Kimoto authored on 2011-10-24
116
        my $param = $self->{param};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
117
        if (ref $param eq 'HASH') {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
118
            if (exists $param->{$column}) {
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
119
                my $if = $self->{_if};
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
120
                
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
121
                if (ref $param->{$column} eq 'ARRAY') {
cleanup
Yuki Kimoto authored on 2011-08-09
122
                    $pushed = 1 if exists $param->{$column}->[$count - 1]
123
                      && ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists'
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
124
                }
cleanup
Yuki Kimoto authored on 2011-08-09
125
                elsif ($count == 1) { $pushed = 1 }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
126
            }
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
127
            push @$where, $clause if $pushed;
128
        }
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
129
        elsif (!defined $param) {
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
130
            push @$where, $clause;
131
            $pushed = 1;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
132
        }
improved error messages
Yuki Kimoto authored on 2011-04-18
133
        else {
cleanup
Yuki Kimoto authored on 2011-04-25
134
            croak "Parameter must be hash reference or undfined value ("
135
                . _subname . ")"
improved error messages
Yuki Kimoto authored on 2011-04-18
136
        }
added test
Yuki Kimoto authored on 2011-01-19
137
        return $pushed;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
138
    }
fixed DBIx::Custom::Where to...
Yuki Kimoto authored on 2011-06-27
139
    return;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
140
}
141
1;
142

            
143
=head1 NAME
144

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

            
147
=head1 SYNOPSYS
148

            
many changed
Yuki Kimoto authored on 2011-01-23
149
    my $where = DBIx::Custom::Where->new;
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
150
    my $string_where = "$where";
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
151

            
152
=head1 ATTRIBUTES
153

            
154
=head2 C<clause>
155

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
156
    my $clause = $where->clause;
157
    $where = $where->clause(
158
        ['and',
159
            'title = :title', 
160
            ['or', 'date < :date', 'date > :date']
161
        ]
added test
Yuki Kimoto authored on 2011-01-19
162
    );
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
163

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

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
167
    "where ( title = :title and ( date < :date or date > :date ) )"
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
168

            
map cleanup
Yuki Kimoto authored on 2011-08-09
169
=head2 C<param>
170

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

            
177
=head2 C<dbi>
178

            
179
    my $dbi = $where->dbi;
180
    $where = $where->dbi($dbi);
181

            
182
L<DBIx::Custom> object.
183

            
184
=head1 METHODS
185

            
186
L<DBIx::Custom::Where> inherits all methods from L<Object::Simple>
187
and implements the following new ones.
188

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

            
191
    $where->to_string;
added test
Yuki Kimoto authored on 2011-01-19
192

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
193
Convert where clause to string.
194

            
195
double quote is override to execute C<to_string> method.
196

            
197
    my $string_where = "$where";
added test
Yuki Kimoto authored on 2011-01-19
198

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