Newer Older
257 lines | 6.187kb
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/],
cleanup
Yuki Kimoto authored on 2012-01-20
13
  clause => sub { [] };
added map method(not complet...
Yuki Kimoto authored on 2011-08-09
14

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

            
changed DBIx::Custom::Where ...
Yuki Kimoto authored on 2011-01-19
28
sub to_string {
cleanup
Yuki Kimoto authored on 2012-01-20
29
  my $self = shift;
30
  
31
  # Clause
32
  my $clause = $self->clause;
33
  $clause = ['and', $clause] unless ref $clause eq 'ARRAY';
34
  $clause->[0] = 'and' unless @$clause;
35
  
36
  # Parse
37
  my $where = [];
38
  my $count = {};
39
  $self->{_query_builder} = $self->dbi->query_builder;
40
  my $c = $self->dbi->safety_character;
41
  $self->{_re} = $c eq 'a-zA-Z0-9_' ?
42
    qr/[^\\]:([$c\.]+)/so : qr/[^\\]:([$c\.]+)/s;
43
  
44
  $self->{_quote} = $self->dbi->_quote;
45
  $self->{_tag_parse} = exists $ENV{DBIX_CUSTOM_TAG_PARSE}
46
    ? $ENV{DBIX_CUSTOM_TAG_PARSE} : $self->dbi->{tag_parse};
47
  $self->_parse($clause, $where, $count, 'and');
micro optimization
Yuki Kimoto authored on 2011-10-24
48

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

            
144
=head1 NAME
145

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

            
148
=head1 SYNOPSYS
cleanup
Yuki Kimoto authored on 2012-01-20
149
  
150
  # Create DBIx::Custom::Where object
151
  my $where = $dbi->where;
152
  
improved where document
Yuki Kimoto authored on 2012-03-01
153
  # Clause
154
  $where->clause(['and', 'title like :title', 'price = :price']);
cleanup
Yuki Kimoto authored on 2012-01-20
155
  $where->clause(['and', ':title{like}', ':price{=}']);
156
  
improved where document
Yuki Kimoto authored on 2012-03-01
157
  # Stringify where clause
cleanup
Yuki Kimoto authored on 2012-01-20
158
  my $where_clause = "$where";
improved where document
Yuki Kimoto authored on 2012-03-01
159
  my $where_clause = $where->to_string;
160
    # -> where title like :title and price = :price
cleanup
Yuki Kimoto authored on 2012-01-20
161
  
162
  # Only price condition
163
  $where->clause(['and', ':title{like}', ':price{=}']);
164
  $where->param({price => 1900});
improved where document
Yuki Kimoto authored on 2012-03-01
165
    # -> where price = :price
cleanup
Yuki Kimoto authored on 2012-01-20
166
  
167
  # Only title condition
168
  $where->clause(['and', ':title{like}', ':price{=}']);
169
  $where->param({title => 'Perl'});
improved where document
Yuki Kimoto authored on 2012-03-01
170
    # -> where title like :title
cleanup
Yuki Kimoto authored on 2012-01-20
171
  
172
  # Nothing
173
  $where->clause(['and', ':title{like}', ':price{=}']);
174
  $where->param({});
improved where document
Yuki Kimoto authored on 2012-03-01
175
    # => Nothing
cleanup
Yuki Kimoto authored on 2012-01-20
176
  
177
  # or condition
178
  $where->clause(['or', ':title{like}', ':price{=}']);
improved where document
Yuki Kimoto authored on 2012-03-01
179
    # -> where title = :title or price like :price
cleanup
Yuki Kimoto authored on 2012-01-20
180
  
181
  # More than one parameter
182
  $where->clause(['and', ':price{>}', ':price{<}']);
183
  $where->param({price => [1000, 2000]});
improved where document
Yuki Kimoto authored on 2012-03-01
184
    # -> where price > :price and price < :price
cleanup
Yuki Kimoto authored on 2012-01-20
185
  
186
  # Only first condition
187
  $where->clause(['and', ':price{>}', ':price{<}']);
188
  $where->param({price => [1000, $dbi->not_exists]});
improved where document
Yuki Kimoto authored on 2012-03-01
189
    # -> where price > :price
cleanup
Yuki Kimoto authored on 2012-01-20
190
  
191
  # Only second condition
192
  $where->clause(['and', ':price{>}', ':price{<}']);
193
  $where->param({price => [$dbi->not_exists, 2000]});
improved where document
Yuki Kimoto authored on 2012-03-01
194
    # -> where price < :price
cleanup
Yuki Kimoto authored on 2012-01-20
195
  
196
  # More complex condition
197
  $where->clause(
198
    [
199
      'and',
200
      ':price{=}',
201
      ['or', ':title{=}', ':title{=}', ':title{=}']
202
    ]
203
  );
improved where document
Yuki Kimoto authored on 2012-03-01
204
    # -> pirce = :price and (title = :title or title = :title or tilte = :title)
cleanup
Yuki Kimoto authored on 2012-01-20
205
  
206
  # Using Full-qualified column name
207
  $where->clause(['and', ':book.title{like}', ':book.price{=}']);
improved where document
Yuki Kimoto authored on 2012-03-01
208
    # -> book.title like :book.title and book.price = :book.price
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
209

            
210
=head1 ATTRIBUTES
211

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
212
=head2 clause
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
213

            
cleanup
Yuki Kimoto authored on 2012-01-20
214
  my $clause = $where->clause;
215
  $where = $where->clause(
216
    ['and',
217
      ':title{=}', 
218
      ['or', ':date{<}', ':date{>}']
219
    ]
220
  );
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
221

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
225
  where title = :title and ( date < :date or date > :date )
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
226

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
227
=head2 param
map cleanup
Yuki Kimoto authored on 2011-08-09
228

            
cleanup
Yuki Kimoto authored on 2012-01-20
229
  my $param = $where->param;
230
  $where = $where->param({
231
    title => 'Perl',
232
    date => ['2010-11-11', '2011-03-05'],
233
  });
map cleanup
Yuki Kimoto authored on 2011-08-09
234

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
235
=head2 dbi
map cleanup
Yuki Kimoto authored on 2011-08-09
236

            
cleanup
Yuki Kimoto authored on 2012-01-20
237
  my $dbi = $where->dbi;
238
  $where = $where->dbi($dbi);
map cleanup
Yuki Kimoto authored on 2011-08-09
239

            
240
L<DBIx::Custom> object.
241

            
242
=head1 METHODS
243

            
244
L<DBIx::Custom::Where> inherits all methods from L<Object::Simple>
245
and implements the following new ones.
246

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
247
=head2 to_string
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
248

            
cleanup
Yuki Kimoto authored on 2012-01-20
249
  $where->to_string;
added test
Yuki Kimoto authored on 2011-01-19
250

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

            
253
double quote is override to execute C<to_string> method.
254

            
cleanup
Yuki Kimoto authored on 2012-01-20
255
  my $string_where = "$where";
added test
Yuki Kimoto authored on 2011-01-19
256

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