- 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 | ||
sub module use DBIx::Custom ...
|
8 |
has 'dbi', |
cleanup
|
9 |
orders => sub { [] }; |
added DBIx::Cusotm::Order pr...
|
10 | |
11 |
sub prepend { |
|
cleanup
|
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...
|
23 |
} |
cleanup
|
24 |
unshift @{$self->orders}, $order; |
25 |
} |
|
26 |
|
|
27 |
return $self; |
|
added DBIx::Cusotm::Order pr...
|
28 |
} |
- added EXPERIMENTAL order m...
|
29 | |
30 |
sub to_string { |
|
cleanup
|
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...
|
46 |
} |
47 | ||
48 |
1; |
|
49 | ||
50 |
=head1 NAME |
|
51 | ||
- removed EXPERIMENTAL flag ...
|
52 |
DBIx::Custom::Order - Order by |
- added EXPERIMENTAL order m...
|
53 | |
54 |
=head1 SYNOPSIS |
|
55 | ||
cleanup
|
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...
|
61 |
=head1 ATTRIBUTES |
62 | ||
sub module use DBIx::Custom ...
|
63 |
=head2 C<dbi> |
64 | ||
cleanup
|
65 |
my $dbi = $order->dbi; |
66 |
$order = $order->dbi($dbi); |
|
sub module use DBIx::Custom ...
|
67 | |
68 |
L<DBIx::Custom> object. |
|
69 | ||
- added EXPERIMENTAL order m...
|
70 |
=head2 C<orders> |
71 | ||
cleanup
|
72 |
my $orders = $result->orders; |
73 |
$result = $result->orders(\%orders); |
|
- added EXPERIMENTAL order m...
|
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
|
84 |
$order->prepend('title', 'author desc'); |
- added EXPERIMENTAL order m...
|
85 | |
86 |
Prepend order parts to C<orders>. |
|
87 | ||
88 |
=head2 C<to_string> |
|
89 | ||
cleanup
|
90 |
my $order_by = $order->to_string; |
- added EXPERIMENTAL order m...
|
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
|
95 |
my $order_by = "$order"; |
- added EXPERIMENTAL order m...
|
96 | |
97 |
=cut |
|
98 |