Newer Older
98 lines | 1.963kb
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
1
package DBIx::Custom::Order;
2
use Object::Simple -base;
3
use overload
4
  'bool'   => sub {1},
5
  '""'     => sub { shift->to_string },
6
  fallback => 1;
7

            
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
8
has 'dbi',
cleanup
Yuki Kimoto authored on 2012-01-20
9
  orders => sub { [] };
added DBIx::Cusotm::Order pr...
Yuki Kimoto authored on 2011-07-11
10

            
11
sub prepend {
cleanup
Yuki Kimoto authored on 2012-01-20
12
  my $self = shift;
13
  
14
  for my $order (reverse @_) {
15
    if (ref $order eq 'ARRAY') {
16
      warn "prepend method receiving array reference is DEPRECATED! " .
17
           "use q method to quote column name.";
18
      my $column = shift @$order;
19
      $column = $self->dbi->q($column) if defined $column;
20
      my $derection = shift @$order;
21
      $order = $column;
22
      $order .= " $derection" if $derection;
added DBIx::Cusotm::Order pr...
Yuki Kimoto authored on 2011-07-11
23
    }
cleanup
Yuki Kimoto authored on 2012-01-20
24
    unshift @{$self->orders}, $order;
25
  }
26
  
27
  return $self;
added DBIx::Cusotm::Order pr...
Yuki Kimoto authored on 2011-07-11
28
}
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
29

            
30
sub to_string {
cleanup
Yuki Kimoto authored on 2012-01-20
31
  my $self = shift;
32
  
33
  my $exists = {};
34
  my @orders;
35
  for my $order (@{$self->orders}) {
36
    next unless defined $order;
37
    $order =~ s/^\s+//;
38
    $order =~ s/\s+$//;
39
    my ($column, $direction) = split /\s+/, $order;
40
    push @orders, $order unless $exists->{$column};
41
    $exists->{$column} = 1;
42
  }
43
  
44
  return '' unless @orders;
45
  return 'order by ' . join(', ', @orders);
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
46
}
47

            
48
1;
49

            
50
=head1 NAME
51

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
52
DBIx::Custom::Order - Order by
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
53

            
54
=head1 SYNOPSIS
55

            
cleanup
Yuki Kimoto authored on 2012-01-20
56
  # Result
57
  my $order = DBIx::Custom::Order->new;
58
  $order->prepend('title', 'author desc');
59
  my $order_by = "$order";
60
  
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
61
=head1 ATTRIBUTES
62

            
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
63
=head2 C<dbi>
64

            
cleanup
Yuki Kimoto authored on 2012-01-20
65
  my $dbi = $order->dbi;
66
  $order = $order->dbi($dbi);
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
67

            
68
L<DBIx::Custom> object.
69

            
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
70
=head2 C<orders>
71

            
cleanup
Yuki Kimoto authored on 2012-01-20
72
  my $orders = $result->orders;
73
  $result = $result->orders(\%orders);
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
74

            
75
Parts of order by clause
76

            
77
=head1 METHODS
78

            
79
L<DBIx::Custom::Result> inherits all methods from L<Object::Simple>
80
and implements the following new ones.
81

            
82
=head2 C<prepend>
83

            
cleanup
Yuki Kimoto authored on 2012-01-20
84
  $order->prepend('title', 'author desc');
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
85

            
86
Prepend order parts to C<orders>.
87

            
88
=head2 C<to_string>
89

            
cleanup
Yuki Kimoto authored on 2012-01-20
90
  my $order_by = $order->to_string;
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
91

            
92
Create order by clause. If column name is duplicated, First one is used.
93
C<to_string> override stringification. so you can write the follwoing way.
94

            
cleanup
Yuki Kimoto authored on 2012-01-20
95
  my $order_by = "$order";
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
96

            
97
=cut
98