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

filter: Statementstmt 34.0% 30 / 88 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 53.3% 8 / 15 Podpod 85.7% 6 / 7 totaltotal 28.0% 44 / 157 CC 20 SCAR 34.5
CC
20
Cov
24%
build
90.0
limit
30.0
select
12.0
CRAP 31.6
« blib/lib/Google/Ads/Common/OAuth2ServiceAccountsHandler.pm ↑ Summary blib/lib/Google/Ads/Common/OAuth2ApplicationsHandler.pm »
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 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use base qw(Google::Ads::AdWords::Utilities::QueryBuilder);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # The following needs to be on one line because CPAN uses a particularly hacky
26 # eval() to determine module versions.
27 1 ▶use Google::Ads::AdWords::Constants; our $VERSION = ${Google::Ads::AdWords::Constants::VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Google::Ads::Common::Utilities::AdsUtilityRegistry;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 ▶use Class::Std::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 my %select_of : ATTR(:name<select> :default<[]>);
33 1 my %order_by_of : ATTR(:name<order_by> :default<[]>);
34 1 my %start_index_of : ATTR(:name<start_index> :default<>);
35 1 my %page_size_of : ATTR(:name<page_size> :default<>);
36  
37 0 ▶sub START {
Subroutine START: not called CC=2 SCAR=17.9
Pod undocumented
38 0 my ($self) = @_;
39  
40 0 Google::Ads::Common::Utilities::AdsUtilityRegistry->add_ads_utilities(
41 "ServiceQueryBuilder");
42  
43 # If a query builder was specified, then make a copy.
44 0 ▶ if ($self->get_query_builder()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_query_builder) 0 0 0
45 0 push(@{$self->get_select()}, @{$self->get_query_builder()->get_select()});
46 0 push(@{$self->get_order_by()},
47 0 @{$self->get_query_builder()->get_order_by()});
48 0 $self->set_start_index($self->get_query_builder()->get_start_index());
49 0 $self->set_page_size($self->get_query_builder()->get_page_size());
50 }
51  
52 0 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 0 ▶sub select {
Subroutine select: not called CC=3 SCAR=24.8
Pod documented
62 0 my ($self, $fields) = @_;
63 # Remove any duplicate fields.
64 0 my @unique_fields = ();
65 0 foreach my $field (@$fields) {
66 0 ▶ if ( !grep (/^$field$/, @unique_fields)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^$field$/, @unique_fields) 0 0 0
67 0 push @unique_fields, $field;
68 }
69 }
70 0 $self->set_select(\@unique_fields);
71 0 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 0 ▶sub order_by {
Subroutine order_by: not called CC=3 SCAR=24.8
Pod documented
81 0 my ($self, $field, $ascending) = @_;
82 0 ▶ $ascending = (defined $ascending) ? $ascending : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $ascending ? : 0 0 0
83 0 ▶ my $order_clause = sprintf("%s %s", $field, (($ascending) ? "ASC" : "DESC"));
Branch: 1 branch
BranchTrueFalseTotal
$ascending ? : 0 0 0
84 0 push(@{$self->get_order_by()}, $order_clause);
85 0 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 0 ▶sub limit {
Subroutine limit: not called CC=5 SCAR=34.0
Pod documented
94 0 my ($self, $start_index, $page_size) = @_;
95 0 ▶ if (defined($start_index) xor defined($page_size)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start_index xor defined $page_size) 0 0 0
Condition0%0/4
defined $start_index xor defined $page_size
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
defined $start_index xor defined $page_size
defined $start_index defined $page_size
Truth table0%0/4
A xor B
A= defined $start_indexB= defined $page_size
ABresult
110
101
011
000
96 0 $self->_log_error("Both start_index and page_size need to be set.");
97 }
98 0 ▶ if ($start_index < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_index < 0) 0 0 0
99 0 $self->_log_error(
100 sprintf("The start_index %s must be 0 or greater.", $start_index));
101 }
102 0 ▶ if ($page_size < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_size < 1) 0 0 0
103 0 $self->_log_error(
104 sprintf("The page_size %s must be greater than 0.", $page_size));
105 }
106 0 $self->set_start_index($start_index);
107 0 $self->set_page_size($page_size);
108 0 return $self;
109 }
110  
111 # Builds an AWQL query.
112 # Returns: The AWQL string.
113 0 ▶sub build {
Subroutine build: not called CC=9 SCAR=45.0
Pod documented
114 0 my ($self) = @_;
115 0 ▶ if (!$self->get_select() or (scalar @{$self->get_select()} eq 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->get_select or scalar @{$self->get_select;} eq 0) 0 0 0
Condition0%0/3
not $self->get_select or scalar @{$self->get_select;} eq 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->get_select or scalar @{$self->get_select;} eq 0
not $self->get_select scalar @{$self->get_select;} eq 0
Truth table0%0/3
not(A) or B
A= not $self->get_selectB= scalar @{$self->get_select;} eq 0
ABresult
1X1
011
000
116 0 $self->_log_error(
117 "Must use select() to specify SELECT clause for valid AWQL first.");
118 }
119 0 my $awql .= sprintf('SELECT %s', join(", ", @{$self->get_select()}));
120 0 my @where_builder_strings = ();
121 0 foreach my $where_builder (@{$self->get_where_builders()}) {
122 0 push(@where_builder_strings, ($where_builder->build()));
123 }
124 $awql .=
125 0 ▶ (scalar @where_builder_strings > 0)
Branch: 1 branch
BranchTrueFalseTotal
scalar @where_builder_strings > 0 ? : 0 0 0
126 ? sprintf(' WHERE %s', join(" AND ", @where_builder_strings))
127 : '';
128 $awql .=
129 0 ($self->get_order_by() and (scalar @{$self->get_order_by()} > 0))
130 0 ▶ ? sprintf(' ORDER BY %s', join(", ", @{$self->get_order_by()}))
Branch: 1 branch
BranchTrueFalseTotal
$self->get_order_by && scalar @{$self->get_order_by;} > 0 ? : 0 0 0
Condition0%0/3
$self->get_order_by and scalar @{$self->get_order_by;} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->get_order_by and scalar @{$self->get_order_by;} > 0
$self->get_order_by scalar @{$self->get_order_by;} > 0
Truth table0%0/3
A and B
A= $self->get_order_byB= scalar @{$self->get_order_by;} > 0
ABresult
0X0
100
111
131 : '';
132 0 ▶ $awql .=
Branch: 1 branch
BranchTrueFalseTotal
defined $self->get_start_index && defined $self->get_page_size ? : 0 0 0
Condition0%0/3
defined $self->get_start_index and defined $self->get_page_size
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->get_start_index and defined $self->get_page_size
defined $self->get_start_index defined $self->get_page_size
Truth table0%0/3
A and B
A= defined $self->get_start_indexB= defined $self->get_page_size
ABresult
0X0
100
111
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 0 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 0 ▶sub has_next {
Subroutine has_next: not called CC=2 SCAR=17.9
Pod documented
145 0 my ($self, $page, $total_entries) = @_;
146 0 ▶ if ($total_entries) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_entries) { } 0 0 0
147 0 return ($total_entries >= $self->get_page_size());
148 } else {
149 0 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 0 ▶sub next_page {
Subroutine next_page: not called CC=2 SCAR=17.9
Pod documented
160 0 my ($self, $other_page_size) = @_;
161 0 ▶ my $current_page_size = ($other_page_size)
Branch: 1 branch
BranchTrueFalseTotal
$other_page_size ? : 0 0 0
162 ? $other_page_size
163 : $self->get_page_size();
164 0 $self->set_start_index($self->get_start_index() + $current_page_size);
165 }
166  
167 1 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
« blib/lib/Google/Ads/Common/OAuth2ServiceAccountsHandler.pm ↑ Summary blib/lib/Google/Ads/Common/OAuth2ApplicationsHandler.pm »