blib/lib/Google/Ads/AdWords/Utilities/ServiceQueryBuilder.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 13 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 151 CC 14 SCAR 24.7
CC
14
Cov
0%
build
30.0
limit
20.0
order_by
12.0
CRAP 11.8
1 # Copyright 2018, Google Inc. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # A query builder for building AWQL queries for services.
16  
17 package Google::Ads::AdWords::Utilities::ServiceQueryBuilder;
18  
19 use strict;
20 use warnings;
21 use utf8;
22 use version;
23 use base qw(Google::Ads::AdWords::Utilities::QueryBuilder);
24  
25 # The following needs to be on one line because CPAN uses a particularly hacky
26 # eval() to determine module versions.
27 use Google::Ads::AdWords::Constants; our $VERSION = ${Google::Ads::AdWords::Constants::VERSION};
28 use Google::Ads::Common::Utilities::AdsUtilityRegistry;
29  
30 use Class::Std::Fast;
31  
32 my %select_of : ATTR(:name<select> :default<[]>);
33 my %order_by_of : ATTR(:name<order_by> :default<[]>);
34 my %start_index_of : ATTR(:name<start_index> :default<>);
35 my %page_size_of : ATTR(:name<page_size> :default<>);
36  
37 sub START {
38 my ($self) = @_;
39  
40 Google::Ads::Common::Utilities::AdsUtilityRegistry->add_ads_utilities(
41 "ServiceQueryBuilder");
42  
43 # If a query builder was specified, then make a copy.
44 if ($self->get_query_builder()) {
45 push(@{$self->get_select()}, @{$self->get_query_builder()->get_select()});
46 push(@{$self->get_order_by()},
47 @{$self->get_query_builder()->get_order_by()});
48 $self->set_start_index($self->get_query_builder()->get_start_index());
49 $self->set_page_size($self->get_query_builder()->get_page_size());
50 }
51  
52 return $self;
53 }
54  
55 # Adds a provided list of fields as selected fields for the query in which
56 # order will be preserved. Calling the select will clear out any previous calls
57 # to select.
58 # Args:
59 # fields: The specified list of fields to be added to a SELECT clause.
60 # Returns: This service query builder.
61 sub select {
62 my ($self, $fields) = @_;
63 # Remove any duplicate fields.
64 my @unique_fields = ();
65 foreach my $field (@$fields) {
66 if ( !grep (/^$field$/, @unique_fields)) {
67 push @unique_fields, $field;
68 }
69 }
70 $self->set_select(\@unique_fields);
71 return $self;
72 }
73  
74 # Adds the provided field to the order-by list with the provided direction.
75 # Args:
76 # field: The specified field to be added to the order-by list.
77 # ascending: If true, the newly created order-by clause will be in ascending
78 # order. Otherwise, it will be in descending order.
79 # Returns: This service query builder.
80 sub order_by {
81 my ($self, $field, $ascending) = @_;
82 $ascending = (defined $ascending) ? $ascending : 1;
83 my $order_clause = sprintf("%s %s", $field, (($ascending) ? "ASC" : "DESC"));
84 push(@{$self->get_order_by()}, $order_clause);
85 return $self;
86 }
87  
88 # Sets the LIMIT clause using the provided start index and page size.
89 # Args:
90 # start_index: The specified start index for the LIMIT clause.
91 # page_size: The page size for the LIMIT clause.
92 # Returns: This service query builder.
93 sub limit {
94 my ($self, $start_index, $page_size) = @_;
95 if (defined($start_index) xor defined($page_size)) {
96 $self->_log_error("Both start_index and page_size need to be set.");
97 }
98 if ($start_index < 0) {
99 $self->_log_error(
100 sprintf("The start_index %s must be 0 or greater.", $start_index));
101 }
102 if ($page_size < 1) {
103 $self->_log_error(
104 sprintf("The page_size %s must be greater than 0.", $page_size));
105 }
106 $self->set_start_index($start_index);
107 $self->set_page_size($page_size);
108 return $self;
109 }
110  
111 # Builds an AWQL query.
112 # Returns: The AWQL string.
113 sub build {
114 my ($self) = @_;
115 if (!$self->get_select() or (scalar @{$self->get_select()} eq 0)) {
116 $self->_log_error(
117 "Must use select() to specify SELECT clause for valid AWQL first.");
118 }
119 my $awql .= sprintf('SELECT %s', join(", ", @{$self->get_select()}));
120 my @where_builder_strings = ();
121 foreach my $where_builder (@{$self->get_where_builders()}) {
122 push(@where_builder_strings, ($where_builder->build()));
123 }
124 $awql .=
125 (scalar @where_builder_strings > 0)
126 ? sprintf(' WHERE %s', join(" AND ", @where_builder_strings))
127 : '';
128 $awql .=
129 ($self->get_order_by() and (scalar @{$self->get_order_by()} > 0))
130 ? sprintf(' ORDER BY %s', join(", ", @{$self->get_order_by()}))
131 : '';
132 $awql .=
133 (defined($self->get_start_index()) and defined($self->get_page_size()))
134 ? sprintf(' LIMIT %s, %s', $self->get_start_index(), $self->get_page_size())
135 : '';
136 return $awql;
137 }
138  
139 # Checks if there is still a page left to query. This is used in conjunction
140 # with next_page. The page_size is necessary when using DataService, as its
141 # paging mechanism is different from other services. For details, see
142 # https://developers.google.com/adwords/api/docs/guides/bid-landscapes#paging_through_results.
143 # Returns 1 if there is a page left.
144 sub has_next {
145 my ($self, $page, $total_entries) = @_;
146 if ($total_entries) {
147 return ($total_entries >= $self->get_page_size());
148 } else {
149 return (($self->get_start_index() + $self->get_page_size())
150 < $page->get_totalNumEntries());
151 }
152 }
153  
154 # Sets the LIMIT clause of the AWQL to the next page.
155 # This is meant to be used with HasNext().
156 # The page_size is necessary when using DataService, as its paging mechanism
157 # is different from other services. For details, see
158 # https://developers.google.com/adwords/api/docs/guides/bid-landscapes#paging_through_results.
159 sub next_page {
160 my ($self, $other_page_size) = @_;
161 my $current_page_size = ($other_page_size)
162 ? $other_page_size
163 : $self->get_page_size();
164 $self->set_start_index($self->get_start_index() + $current_page_size);
165 }
166  
167 1;
168  
169 =pod
170  
171 =head1 NAME
172  
173 Google::Ads::AdWords::Utilities::ServiceQueryBuilder
174  
175 =head1 DESCRIPTION
176  
177 A query builder for building AWQL queries for services.
178  
179 =head1 ATTRIBUTES
180  
181 The attributes properties may be set in new() and accessed using get_ATTRIBUTE
182 methods:
183  
184 =head2 select
185  
186 An array of fields that are selected to be filtered on.
187  
188 =head2 order_by
189  
190 A hash of field names to ASC or DESC by which results will be ordered.
191  
192 =head2 start_index
193  
194 The start index of the results to be retunred.
195  
196 =head2 page_size
197  
198 The size of each page that to be returned.
199  
200 =head1 METHODS
201  
202 =head2 new
203  
204 Constructor. The following data structure may be passed to new():
205  
206 { # Google::Ads::AdWords::Utilities::ReportQueryBuilder
207 client # Required: Google::Ads::AdWords::Client
208 query_builder # Optional: the Google::Ads::AdWords::Utilities::QueryBuilder
209 to be copied
210 },
211  
212 =head1 METHODS
213  
214 =head2 select
215  
216 Sets the specified list of fields to be added to a SELECT clause.
217  
218 =head3 Parameters
219  
220 =over
221  
222 =item *
223  
224 A reference to an array of fields selected.
225  
226 =back
227  
228 =head3 Returns
229  
230 A query builder (this instance).
231  
232 =head2 order_by
233  
234 Adds the provided field to the order-by list with the provided direction.
235  
236 =head3 Parameters
237  
238 =over
239  
240 =item *
241  
242 The specified field to be added to the order-by list.
243  
244 =item *
245  
246 If true, the newly created order-by clause will be in ascending order.
247 Otherwise, it will be in descending order.
248  
249 =back
250  
251 =head3 Returns
252  
253 A query builder (this instance).
254  
255 =head2 limit
256  
257 Sets the LIMIT clause using the provided start index and page size.
258  
259 =head3 Parameters
260  
261 =over
262  
263 =item *
264  
265 The specified start index for the LIMIT clause.
266  
267 =item *
268  
269 The page size for the LIMIT clause.
270  
271 =back
272  
273 =head3 Returns
274  
275 A query builder (this instance).
276  
277 =head2 build
278  
279 Builds the AWQL query.
280  
281 =head3 Returns
282  
283 A string that is the AWQL query.
284  
285 =head2 has_next
286  
287 Checks if there is still a page left to query. This is used in conjunction
288 with next_page. The page_size is necessary when using DataService, as its
289 paging mechanism is different from other services. For details, see
290 https://developers.google.com/adwords/api/docs/guides/bid-landscapes#paging_through_results.
291  
292 =head3 Parameters
293  
294 =over
295  
296 =item *
297  
298 The current page.
299  
300 =item *
301  
302 Optional: The total number of entries left.
303  
304 =back
305  
306 =head3 Returns
307  
308 Returns 1 if there is another page to process.
309  
310 =head2 next_page
311  
312 Sets the LIMIT clause of the AWQL to the next page.
313 This is meant to be used with HasNext().
314 The page_size is necessary when using DataService, as its paging mechanism
315 is different from other services. For details, see
316 https://developers.google.com/adwords/api/docs/guides/bid-landscapes#paging_through_results.
317  
318 =head3 Parameters
319  
320 =over
321  
322 =item *
323  
324 Optional: A different size in which to increment other than the current page
325 size.
326  
327 =back
328  
329 =cut