blib/lib/App/Netdisco/DB/ResultSet.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 158 CC 11 SCAR 22.8
CC
11
Cov
0%
_datatables_index_to_column
20.0
_with_datatables_where_clause
12.0
_with_datatables_paging
12.0
CRAP 9.8
1 package App::Netdisco::DB::ResultSet;
2  
3 use strict;
4 use warnings;
5  
6 use base 'DBIx::Class::ResultSet';
7  
8 __PACKAGE__->load_components(qw/
9 +App::Netdisco::DB::SetOperations
10 +App::Netdisco::DB::Upsert
11 Helper::ResultSet::Shortcut
12 Helper::ResultSet::CorrelateRelationship
13 /);
14  
15 =head1 ADDITIONAL METHODS
16  
17 =head2 get_distinct_col( $column )
18  
19 Returns an asciibetical sorted list of the distinct values in the given column
20 of the Device table. This is useful for web forms when you want to provide a
21 drop-down list of possible options.
22  
23 =cut
24  
25 sub get_distinct_col {
26 my ( $rs, $col ) = @_;
27 return $rs unless $col;
28  
29 return $rs->search(
30 {},
31 { columns => [$col],
32 order_by => $col,
33 distinct => 1
34 }
35 )->get_column($col)->all;
36 }
37  
38 =head2 get_datatables_data( $params )
39  
40 Returns a ResultSet for DataTables Server-side processing which populates
41 the displayed table. Evaluates the supplied query parameters for filtering,
42 paging, and ordering information. Note: query parameters are expected to be
43 passed as a reference to an expanded hash of hashes.
44  
45 Filtering if present, will generate simple LIKE matching conditions for each
46 searchable column (searchability indicated by query parameters) after each
47 column is casted to text. Conditions are combined as disjunction (OR).
48 Note: this does not match the built-in DataTables filtering which does it
49 word by word on any field.
50  
51 Both the filtering and the ordering clauses name their columns with the
52 C<columns[i][data]> query parameters, and each name is used as an SQL
53 identifier. A name must therefore be one this ResultSet resolves, that is,
54 one of its own columns or one brought in by a join. Anything else dies.
55  
56 =cut
57  
58 sub get_datatables_data {
59 my $rs = shift;
60 my $params = shift;
61 my $attrs = shift;
62  
63 die "condition parameter to search_by_field must be hashref\n"
64 if ref {} ne ref $params
65 or 0 == scalar keys %$params;
66  
67 # -- Paging
68 $rs = $rs->_with_datatables_paging($params);
69  
70 # -- Ordering
71 $rs = $rs->_with_datatables_order_clause($params);
72  
73 # -- Filtering
74 $rs = $rs->_with_datatables_where_clause($params);
75  
76 return $rs;
77 }
78  
79 =head2 get_datatables_filtered_count( $params )
80  
81 Returns the total records, after filtering (i.e. the total number of
82 records after filtering has been applied - not just the number of records
83 being returned for this page of data) for a datatables ResultSet and
84 query parameters. Note: query parameters are expected to be passed as a
85 reference to an expanded hash of hashes.
86  
87 =cut
88  
89 sub get_datatables_filtered_count {
90 my $rs = shift;
91 my $params = shift;
92  
93 return $rs->_with_datatables_where_clause($params)->count;
94  
95 }
96  
97 sub _with_datatables_order_clause {
98 my $rs = shift;
99 my $params = shift;
100 my $attrs = shift;
101  
102 my @order = ();
103  
104 if ( defined $params->{'order'}{0} ) {
105 for ( my $i = 0; $i < (scalar keys %{$params->{'order'}}); $i++ ) {
106  
107 # build direction, must be '-asc' or '-desc' (cf. SQL::Abstract)
108 # we only get 'asc' or 'desc', so they have to be prefixed with '-'
109 my $direction = '-' . $params->{'order'}{$i}{'dir'};
110  
111 # We only get the column index (starting from 0), so we have to
112 # translate the index into a column name.
113 my $column_name = $rs->_datatables_index_to_column( $params,
114 $params->{'order'}{$i}{'column'} );
115  
116 # Prefix with table alias if no prefix
117 my $csa = $rs->current_source_alias;
118 $column_name =~ s/^(\w+)$/$csa\.$1/x;
119 push @order, { $direction => $column_name };
120 }
121 }
122  
123 $rs = $rs->order_by( \@order );
124 return $rs;
125 }
126  
127 # NOTE this does not match the built-in DataTables filtering which does it
128 # word by word on any field.
129 #
130 # General filtering using LIKE, this will not be efficient as is will not
131 # be able to use indexes.
132  
133 sub _with_datatables_where_clause {
134 my $rs = shift;
135 my $params = shift;
136 my $attrs = shift;
137  
138 my %where = ();
139  
140 if ( defined $params->{'search'}{'value'}
141 && $params->{'search'}{'value'} )
142 {
143 my $search_string = $params->{'search'}{'value'};
144 for ( my $i = 0; $i < (scalar keys %{$params->{'columns'}}); $i++ ) {
145  
146 # Iterate over each column and check if it is searchable.
147 # If so, add a constraint to the where clause restricting the given
148 # column. In the query, the column is identified by it's index, we
149 # need to translate the index to the column name.
150 if ( $params->{'columns'}{$i}{'searchable'}
151 and $params->{'columns'}{$i}{'searchable'} eq 'true' )
152 {
153 my $column = $rs->_datatables_index_to_column( $params, $i );
154 my $csa = $rs->current_source_alias;
155 $column =~ s/^(\w+)$/$csa\.$1/x;
156  
157 # Cast everything to text for LIKE search
158 $column = $column . '::text';
159 push @{ $where{'-or'} },
160 { $column => { -like => '%' . $search_string . '%' } };
161 }
162 }
163 }
164  
165 $rs = $rs->search( \%where, $attrs );
166 return $rs;
167 }
168  
169 sub _with_datatables_paging {
170 my $rs = shift;
171 my $params = shift;
172 my $attrs = shift;
173  
174 my $limit = $params->{'length'};
175  
176 my $offset = 0;
177 if ( defined $params->{'start'} && $params->{'start'} ) {
178 $offset = $params->{'start'};
179 }
180 $attrs->{'offset'} = $offset;
181  
182 $rs = $rs->search( {}, $attrs );
183 $rs = $rs->limit($limit) if ($limit and $limit > 0);
184  
185 return $rs;
186 }
187  
188 # Use the DataTables columns.data definition to derive the column
189 # name from the index.
190 #
191 # The name comes from the request and both callers use it as an SQL
192 # identifier, so this is the one gate on it. The allow-list is the resolved
193 # 'as' list rather than the result source columns because a joined column
194 # such as manufacturer.abbrev is only in the former.
195  
196 sub _datatables_index_to_column {
197 my $rs = shift;
198 my $params = shift;
199 my $i = shift;
200  
201 my $field;
202  
203 if ( !defined($i) ) {
204 $i = 0;
205 }
206 $field = $params->{'columns'}{$i}{'data'};
207  
208 my @permitted = @{ $rs->_resolved_attrs->{'as'} || [] };
209 return $field
210 if defined $field and grep { $_ eq $field } @permitted;
211  
212 # the index is request data when ordering, so name it only when it can
213 # hold nothing but digits
214 my $at = ( $i =~ m/\A[0-9]+\z/ ) ? " at index $i" : '';
215 die "unknown DataTables column$at, expected one of: "
216 . join( ', ', @permitted ) . "\n";
217 }
218  
219 1;