Newer Older
104 lines | 2.211kb
- 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

            
8

            
added DBIx::Cusotm::Order pr...
Yuki Kimoto authored on 2011-07-11
9
has orders => sub { [] },
10
    quote => '';
11

            
12
sub prepend {
13
    my $self = shift;
14
    
15
    my $q = $self->quote;
16
    foreach my $order (reverse @_) {
17
        if (ref $order eq 'ARRAY') {
18
            my $column = shift @$order;
19
            $column = "$q$column$q" if defined $column;
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;
35
    foreach 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);
46
}
47

            
48
1;
49

            
50
=head1 NAME
51

            
52
DBIx::Custom::Order - Order by EXPERIMENTAL
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

            
62
=head1 ATTRIBUTES
63

            
64
=head2 C<orders>
65

            
66
    my $orders = $result->orders;
67
    $result = $result->orders(\%orders);
68

            
69
Parts of order by clause
70

            
71
=head1 METHODS
72

            
73
L<DBIx::Custom::Result> inherits all methods from L<Object::Simple>
74
and implements the following new ones.
75

            
76
=head2 C<prepend>
77

            
78
    $order->prepend('title', 'author desc');
79

            
80
Prepend order parts to C<orders>.
81

            
added DBIx::Cusotm::Order pr...
Yuki Kimoto authored on 2011-07-11
82
You can pass array reference, which contain column name and direction.
83
Column name is quoted properly
84
    
85
    # Column name and direction
86
    $order->prepend(['book-title']);
87
    $order->prepend([qw/book-title desc/]);
88

            
89
This is expanded to the following way.
90

            
91
    "book-title"
92
    "book-title" desc
93

            
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
94
=head2 C<to_string>
95

            
96
    my $order_by = $order->to_string;
97

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

            
101
    my $order_by = "$order";
102

            
103
=cut
104