Newer Older
98 lines | 2.097kb
- 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',
9
    orders => sub { [] };
added DBIx::Cusotm::Order pr...
Yuki Kimoto authored on 2011-07-11
10

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

            
30
sub to_string {
31
    my $self = shift;
32
    
33
    my $exists = {};
34
    my @orders;
cleanup
Yuki Kimoto authored on 2011-10-21
35
    for my $order (@{$self->orders}) {
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
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);
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

            
56
    # Result
57
    my $order = DBIx::Custom::Order->new;
58
    $order->prepend('title', 'author desc');
59
    my $order_by = "$order";
60
    
61
=head1 ATTRIBUTES
62

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

            
65
    my $dbi = $order->dbi;
66
    $order = $order->dbi($dbi);
67

            
68
L<DBIx::Custom> object.
69

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

            
72
    my $orders = $result->orders;
73
    $result = $result->orders(\%orders);
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

            
84
    $order->prepend('title', 'author desc');
85

            
86
Prepend order parts to C<orders>.
87

            
88
=head2 C<to_string>
89

            
90
    my $order_by = $order->to_string;
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

            
95
    my $order_by = "$order";
96

            
97
=cut
98