Newer Older
100 lines | 2.013kb
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
1
package DBIx::Custom::Order;
2
use Object::Simple -base;
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
3
use DBIx::Custom::Util '_deprecate';
4

            
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
5
use overload
6
  'bool'   => sub {1},
7
  '""'     => sub { shift->to_string },
8
  fallback => 1;
9

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

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

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

            
50
1;
51

            
52
=head1 NAME
53

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

            
56
=head1 SYNOPSIS
57

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

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

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

            
70
L<DBIx::Custom> object.
71

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

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

            
77
Parts of order by clause
78

            
79
=head1 METHODS
80

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

            
84
=head2 C<prepend>
85

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

            
88
Prepend order parts to C<orders>.
89

            
90
=head2 C<to_string>
91

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

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

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

            
99
=cut
100