- added EXPERIMENTAL order m...
|
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...
|
9 |
has orders => sub { [] }, |
added quote method's two cha...
|
10 |
dbi => ''; |
added DBIx::Cusotm::Order pr...
|
11 | |
12 |
sub prepend { |
|
13 |
my $self = shift; |
|
14 |
|
|
15 |
foreach my $order (reverse @_) { |
|
16 |
if (ref $order eq 'ARRAY') { |
|
17 |
my $column = shift @$order; |
|
added quote method's two cha...
|
18 |
$column = $self->dbi->_q($column) if defined $column; |
added DBIx::Cusotm::Order pr...
|
19 |
my $derection = shift @$order; |
20 |
$order = $column; |
|
21 |
$order .= " $derection" if $derection; |
|
22 |
} |
|
23 |
unshift @{$self->orders}, $order; |
|
24 |
} |
|
25 |
|
|
26 |
return $self; |
|
27 |
} |
|
- added EXPERIMENTAL order m...
|
28 | |
29 |
sub to_string { |
|
30 |
my $self = shift; |
|
31 |
|
|
32 |
my $exists = {}; |
|
33 |
my @orders; |
|
34 |
foreach my $order (@{$self->orders}) { |
|
35 |
next unless defined $order; |
|
36 |
$order =~ s/^\s+//; |
|
37 |
$order =~ s/\s+$//; |
|
38 |
my ($column, $direction) = split /\s+/, $order; |
|
39 |
push @orders, $order unless $exists->{$column}; |
|
40 |
$exists->{$column} = 1; |
|
41 |
} |
|
42 |
|
|
43 |
return '' unless @orders; |
|
44 |
return 'order by ' . join(', ', @orders); |
|
45 |
} |
|
46 | ||
47 |
1; |
|
48 | ||
49 |
=head1 NAME |
|
50 | ||
51 |
DBIx::Custom::Order - Order by EXPERIMENTAL |
|
52 | ||
53 |
=head1 SYNOPSIS |
|
54 | ||
55 |
# Result |
|
56 |
my $order = DBIx::Custom::Order->new; |
|
57 |
$order->prepend('title', 'author desc'); |
|
58 |
my $order_by = "$order"; |
|
59 |
|
|
60 | ||
61 |
=head1 ATTRIBUTES |
|
62 | ||
63 |
=head2 C<orders> |
|
64 | ||
65 |
my $orders = $result->orders; |
|
66 |
$result = $result->orders(\%orders); |
|
67 | ||
68 |
Parts of order by clause |
|
69 | ||
70 |
=head1 METHODS |
|
71 | ||
72 |
L<DBIx::Custom::Result> inherits all methods from L<Object::Simple> |
|
73 |
and implements the following new ones. |
|
74 | ||
75 |
=head2 C<prepend> |
|
76 | ||
77 |
$order->prepend('title', 'author desc'); |
|
78 | ||
79 |
Prepend order parts to C<orders>. |
|
80 | ||
added DBIx::Cusotm::Order pr...
|
81 |
You can pass array reference, which contain column name and direction. |
82 |
Column name is quoted properly |
|
83 |
|
|
84 |
# Column name and direction |
|
85 |
$order->prepend(['book-title']); |
|
86 |
$order->prepend([qw/book-title desc/]); |
|
87 | ||
88 |
This is expanded to the following way. |
|
89 | ||
90 |
"book-title" |
|
91 |
"book-title" desc |
|
92 | ||
- added EXPERIMENTAL order m...
|
93 |
=head2 C<to_string> |
94 | ||
95 |
my $order_by = $order->to_string; |
|
96 | ||
97 |
Create order by clause. If column name is duplicated, First one is used. |
|
98 |
C<to_string> override stringification. so you can write the follwoing way. |
|
99 | ||
100 |
my $order_by = "$order"; |
|
101 | ||
102 |
=cut |
|
103 |