- added EXPERIMENTAL order m...
|
1 |
package DBIx::Custom::Order; |
2 |
use Object::Simple -base; |
|
- added DBIX_CUSTOM_SUPPRESS...
|
3 |
use DBIx::Custom::Util '_deprecate'; |
4 | ||
- added EXPERIMENTAL order m...
|
5 |
use overload |
6 |
'bool' => sub {1}, |
|
7 |
'""' => sub { shift->to_string }, |
|
8 |
fallback => 1; |
|
9 | ||
sub module use DBIx::Custom ...
|
10 |
has 'dbi', |
cleanup
|
11 |
orders => sub { [] }; |
added DBIx::Cusotm::Order pr...
|
12 | |
13 |
sub prepend { |
|
cleanup
|
14 |
my $self = shift; |
15 |
|
|
16 |
for my $order (reverse @_) { |
|
17 |
if (ref $order eq 'ARRAY') { |
|
- added DBIX_CUSTOM_SUPPRESS...
|
18 |
_deprecate('0.24', "prepend method receiving array reference is DEPRECATED! " . |
19 |
"use q method to quote column name."); |
|
cleanup
|
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...
|
25 |
} |
cleanup
|
26 |
unshift @{$self->orders}, $order; |
27 |
} |
|
28 |
|
|
29 |
return $self; |
|
added DBIx::Cusotm::Order pr...
|
30 |
} |
- added EXPERIMENTAL order m...
|
31 | |
32 |
sub to_string { |
|
cleanup
|
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...
|
48 |
} |
49 | ||
50 |
1; |
|
51 | ||
52 |
=head1 NAME |
|
53 | ||
fixed documentation miss abo...
|
54 |
DBIx::Custom::Order - Order by clause |
- added EXPERIMENTAL order m...
|
55 | |
56 |
=head1 SYNOPSIS |
|
57 | ||
cleanup
|
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...
|
63 |
=head1 ATTRIBUTES |
64 | ||
cleanup and added deprecated...
|
65 |
=head2 dbi |
sub module use DBIx::Custom ...
|
66 | |
cleanup
|
67 |
my $dbi = $order->dbi; |
68 |
$order = $order->dbi($dbi); |
|
sub module use DBIx::Custom ...
|
69 | |
70 |
L<DBIx::Custom> object. |
|
71 | ||
cleanup and added deprecated...
|
72 |
=head2 orders |
- added EXPERIMENTAL order m...
|
73 | |
cleanup
|
74 |
my $orders = $result->orders; |
75 |
$result = $result->orders(\%orders); |
|
- added EXPERIMENTAL order m...
|
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 | ||
cleanup and added deprecated...
|
84 |
=head2 prepend |
- added EXPERIMENTAL order m...
|
85 | |
cleanup
|
86 |
$order->prepend('title', 'author desc'); |
- added EXPERIMENTAL order m...
|
87 | |
88 |
Prepend order parts to C<orders>. |
|
89 | ||
cleanup and added deprecated...
|
90 |
=head2 to_string |
- added EXPERIMENTAL order m...
|
91 | |
cleanup
|
92 |
my $order_by = $order->to_string; |
- added EXPERIMENTAL order m...
|
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
|
97 |
my $order_by = "$order"; |
- added EXPERIMENTAL order m...
|
98 | |
99 |
=cut |
|
100 |