line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::ReluctantORM::SQL::OrderBy; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Class::ReluctantORM::SQL::OrderBy - Represent an ORDER BY clause in a SQL statement |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my $ob = Class::ReluctantORM::SQL::OrderBy->new(); |
10
|
|
|
|
|
|
|
$ob->add($col); |
11
|
|
|
|
|
|
|
$ob->add($col, 'DESC'); |
12
|
|
|
|
|
|
|
@cols = $ob->columns(); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 METHODS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=cut |
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
26
|
|
19
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
464
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
#use base 'Class::Accessor'; |
22
|
|
|
|
|
|
|
#__PACKAGE__->mk_accessors(qw(table alias column)); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head2 new() |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Constructor. No arguments. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=cut |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub new { |
31
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
32
|
0
|
|
|
|
|
|
return bless { cols => [] }, $class; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head2 @cols = $ob->columns(); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
Lists the Class::ReluctantORM::SQL::Columns in the order by clause, in order of occurence. No sort direction is provided. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=cut |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub columns { |
43
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
44
|
0
|
|
|
|
|
|
return map {$_->[0]} $self->columns_with_directions; |
|
0
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 @tables = $ob->tables(); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Returns a list of (non-unique) tables referenced in the clause. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub tables { |
54
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
55
|
0
|
|
|
|
|
|
return map { $_->table() } $self->columns; |
|
0
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head2 @col_pairs = $ob->columns_with_directions(); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Returns an array of two-element arrays. In each subarry, the first element is the Class::ReluctantORM::SQL::Column, and the second is the sort direction (either 'ASC' or 'DESC'). |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=cut |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub columns_with_directions { |
65
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
66
|
0
|
|
|
|
|
|
return @{$self->{cols}}; |
|
0
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 $ob->add($col); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head2 $ob->add($col, $direction); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Adds a sort criteria to the clause. $col is a Class::ReluctantORM::SQL::Column. $direction is either of the strings 'ASC' or 'DESC', default 'ASC'. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=cut |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub add { |
78
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
79
|
0
|
|
|
|
|
|
my $col = shift; |
80
|
0
|
0
|
|
|
|
|
unless ($col) { Class::ReluctantORM::Exception::Param::Missing->croak(param => 'column'); } |
|
0
|
|
|
|
|
|
|
81
|
0
|
0
|
0
|
|
|
|
unless (ref($col) && $col->isa('Class::ReluctantORM::SQL::Column')) { |
82
|
0
|
|
|
|
|
|
Class::ReluctantORM::Exception::Param::WrongType->croak( |
83
|
|
|
|
|
|
|
param => 'column', |
84
|
|
|
|
|
|
|
expected => 'Class::ReluctantORM::SQL::Column', |
85
|
|
|
|
|
|
|
value => $col, |
86
|
|
|
|
|
|
|
); |
87
|
|
|
|
|
|
|
} |
88
|
0
|
|
0
|
|
|
|
my $dir = shift || 'ASC'; |
89
|
0
|
|
|
|
|
|
$dir = uc($dir); |
90
|
0
|
|
|
|
|
|
my %acceptable = map {$_ => 1} qw(ASC DESC); |
|
0
|
|
|
|
|
|
|
91
|
0
|
0
|
|
|
|
|
unless (exists $acceptable{$dir}) { |
92
|
0
|
|
|
|
|
|
Class::ReluctantORM::Exception::Param::BadValue->( |
93
|
|
|
|
|
|
|
error => 'Driection must be one of ' . (join ',', keys %acceptable), |
94
|
|
|
|
|
|
|
param => 'direction', |
95
|
|
|
|
|
|
|
value => $dir, |
96
|
|
|
|
|
|
|
); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
push @{$self->{cols}}, [$col, $dir]; |
|
0
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 $str = $ob->pretty_print(); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Outputs the clause as a human-readable, driver-neutral string. Useless for SQL execution. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=cut |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub pretty_print { |
110
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
111
|
0
|
|
|
|
|
|
my %args = @_; |
112
|
0
|
|
0
|
|
|
|
my $prefix = $args{prefix} || ''; |
113
|
0
|
|
|
|
|
|
my $str = $prefix . "ORDER BY:\n"; |
114
|
0
|
|
|
|
|
|
foreach my $cd ($self->columns_with_directions) { |
115
|
0
|
|
|
|
|
|
$str .= $prefix . ' '; |
116
|
0
|
|
|
|
|
|
$str .= $cd->[0]->pretty_print(one_line => 1); |
117
|
0
|
|
|
|
|
|
$str .= ' '; |
118
|
0
|
|
|
|
|
|
$str .= $cd->[1]; |
119
|
0
|
|
|
|
|
|
$str .= "\n"; |
120
|
|
|
|
|
|
|
} |
121
|
0
|
|
|
|
|
|
return $str; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 $clone = $ob->clone(); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Deeply clones each sort expression, and copies each direction. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub clone { |
131
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
132
|
0
|
|
|
|
|
|
my $class = ref $self; |
133
|
0
|
|
|
|
|
|
my $other = $class->new(); |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
foreach my $sort_term ($self->columns_with_directions) { |
136
|
0
|
|
|
|
|
|
$other->add( |
137
|
|
|
|
|
|
|
$sort_term->[0]->clone(), |
138
|
|
|
|
|
|
|
$sort_term->[1], |
139
|
|
|
|
|
|
|
); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
return $other; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |