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

filter: Statementstmt 39.7% 31 / 78 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 61.5% 8 / 13 Podpod 80.0% 4 / 5 totaltotal 33.3% 43 / 129 CC 14 SCAR 36.3
CC
14
Cov
30%
build
90.0
during
30.0
START
6.0
CRAP 37.8
« lib/Google/Ads/Common/ErrorUtils.pm ↑ Summary blib/lib/Google/Ads/Common/OAuth2ServiceAccountsHandler.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 reporting.
16  
17 package Google::Ads::AdWords::Utilities::ReportQueryBuilder;
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 %from_report_of : ATTR(:name<from_report> :default<>);
34 1 my %date_range_of : ATTR(:name<date_range> :default<>);
35 1 my %start_date_of : ATTR(:name<start_date> :default<>);
36 1 my %end_date_of : ATTR(:name<end_date> :default<>);
37  
38 0 ▶sub START {
Subroutine START: not called CC=2 SCAR=17.9
Pod undocumented
39 0 my ($self) = @_;
40  
41 0 Google::Ads::Common::Utilities::AdsUtilityRegistry->add_ads_utilities(
42 "ReportQueryBuilder");
43  
44 # If a query builder was specified, then make a copy.
45 0 ▶ if ($self->get_query_builder()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_query_builder) 0 0 0
46 0 push(@{$self->get_select()}, @{$self->get_query_builder()->get_select()});
47 0 $self->set_from_report($self->get_query_builder()->get_from_report());
48 0 $self->set_date_range($self->get_query_builder()->get_date_range());
49 0 $self->set_start_date($self->get_query_builder()->get_start_date());
50 0 $self->set_end_date($self->get_query_builder()->get_end_date());
51 }
52  
53 0 return $self;
54 }
55  
56 # Adds a provided list of fields as selected fields for the query. Each call to
57 # this method will replace the list of fields from previous calls.
58 # Args:
59 # fields: The specified list of fields to be added to a SELECT clause.
60 # Returns: This report query builder.
61 0 ▶sub select {
Subroutine select: not called CC=1 SCAR=6.9
Pod documented
62 0 my ($self, $fields) = @_;
63 0 @{$self->get_select()} = ();
64 0 push(@{$self->get_select()}, @$fields);
65 0 return $self;
66 }
67  
68 # Sets a provided report name as the argument to the FROM clause.
69 # A list of report names can be found at:
70 # https://developers.google.com/adwords/api/docs/appendix/reports
71 # Args:
72 # report_name: The specified report name.
73 # Returns: This report query builder.
74 0 ▶sub from {
Subroutine from: not called CC=1 SCAR=6.9
Pod documented
75 0 my ($self, $report_name) = @_;
76 0 $self->set_from_report($report_name);
77 0 return $self;
78 }
79  
80 # Sets arguments for the DURING clause using the provided date range or start
81 # and end dates. Only the date range or start and end date should be
82 # specified. If both are supplied, an error will be thrown. A valid date range
83 # string can be found at
84 # https://developers.google.com/adwords/api/docs/guides/reporting#date_ranges.
85 # Start and end dates should be in 'YYYYMMDD' format.
86 # Args:
87 # date_range_or_start_date: The specified date range string, e.g. YESTERDAY
88 # or the start date in 'YYYYMMDD' format when the end date is specified.
89 # end_date: The end date string in 'YYYYMMDD' format.
90 # Returns: This report query builder.
91 0 ▶sub during {
Subroutine during: not called CC=5 SCAR=34.0
Pod documented
92 0 my ($self, $date_range_or_start_date, $end_date) = @_;
93  
94 0 ▶ if (!$date_range_or_start_date) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($date_range_or_start_date) 0 0 0
95 0 $self->_log_error("Pass in either 1 argument with a date range as a " .
96 "string e.g. YESTERDAY or pass in 2 arguments with a start date and an " .
97 "end date with the format 'YYYYMMDD'");
98 }
99  
100 # If the first argument is a number, then it must be a start date. Therefore,
101 # an end date must be specified.
102 0 ▶ if ($date_range_or_start_date =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date_range_or_start_date =~ /^\d+$/) { } 0 0 0
103 0 ▶ if (!$end_date or !($end_date =~ /^\d+$/)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($end_date and $end_date =~ /^\d+$/) 0 0 0
Condition0%0/3
$end_date and $end_date =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$end_date and $end_date =~ /^\d+$/
$end_date $end_date =~ /^\d+$/
Truth table0%0/3
A and B
A= $end_dateB= $end_date =~ /^\d+$/
ABresult
0X0
100
111
104 0 $self->_log_error("Pass in 2 arguments with a start date and an " .
105 "end date with the format 'YYYYMMDD'");
106 }
107 0 $self->set_start_date($date_range_or_start_date);
108 0 $self->set_end_date($end_date);
109 }
110 else {
111 0 $self->set_date_range($date_range_or_start_date);
112 }
113 0 return $self;
114 }
115  
116 # Builds an AWQL query.
117 # Returns: The AWQL string.
118 0 ▶sub build {
Subroutine build: not called CC=9 SCAR=45.0
Pod documented
119 0 my ($self) = @_;
120 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
121 0 $self->_log_error(
122 "Must use select() to specify SELECT clause for valid AWQL first.");
123 }
124 0 ▶ if (!$self->get_from_report()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_from_report) 0 0 0
125 0 $self->_log_error(
126 "Must use from() to specify FROM clause for valid AWQL first.");
127 }
128 0 my $awql .= sprintf('SELECT %s', join(", ", @{$self->get_select()}));
129 0 $awql .= sprintf(' FROM %s', $self->get_from_report());
130 0 my @where_builder_strings = ();
131 0 foreach my $where_builder (@{$self->get_where_builders()}) {
132 0 push(@where_builder_strings, ($where_builder->build()));
133 }
134 $awql .=
135 0 ▶ (scalar @where_builder_strings > 0)
Branch: 1 branch
BranchTrueFalseTotal
scalar @where_builder_strings > 0 ? : 0 0 0
136 ? sprintf(' WHERE %s', join(" AND ", @where_builder_strings))
137 : '';
138 0 ▶ $awql .=
Branch: 1 branch
BranchTrueFalseTotal
$self->get_date_range ? : 0 0 0
139 ($self->get_date_range())
140 ? sprintf(' DURING %s', $self->get_date_range())
141 : '';
142 0 ▶ $awql .=
Branch: 1 branch
BranchTrueFalseTotal
$self->get_start_date && $self->get_end_date ? : 0 0 0
Condition0%0/3
$self->get_start_date and $self->get_end_date
!ll&&!rl&&r
000
MC/DC0%0/2
$self->get_start_date and $self->get_end_date
$self->get_start_date $self->get_end_date
Truth table0%0/3
A and B
A= $self->get_start_dateB= $self->get_end_date
ABresult
0X0
100
111
143 ($self->get_start_date() and $self->get_end_date())
144 ? sprintf(' DURING %s,%s', $self->get_start_date(), $self->get_end_date())
145 : '';
146 0 return $awql;
147 }
148  
149 1 1;
150  
151 =pod
152  
153 =head1 NAME
154  
155 Google::Ads::AdWords::Utilities::ReportQueryBuilder
156  
157 =head1 DESCRIPTION
158  
159 A query builder for building AWQL queries for reporting.
160  
161 =head1 ATTRIBUTES
162  
163 The attributes may be set in new() and accessed using get_ATTRIBUTE
164 methods:
165  
166 =head2 select
167  
168 An array of fields that are selected to be filtered on.
169  
170 =head2 from_report
171  
172 The name of the report queried.
173  
174 =head2 date_range_or_start_date
175  
176 The date range string in the during clause or the start date in the during
177 clause when the end_date is specified.
178  
179 =head2 end_date
180  
181 The end date of the date range in the during clause.
182  
183 =head1 METHODS
184  
185 =head2 new
186  
187 Constructor. The following data structure may be passed to new():
188  
189 { # Google::Ads::AdWords::Utilities::ReportQueryBuilder
190 client # Required: Google::Ads::AdWords::Client
191 query_builder # Optional: the Google::Ads::AdWords::Utilities::QueryBuilder
192 # to be copied
193 },
194  
195 =head1 METHODS
196  
197 =head2 select
198  
199 Adds the specified list of fields to be added to a SELECT clause.
200  
201 =head3 Parameters
202  
203 =over
204  
205 =item *
206  
207 A reference to an array of fields selected.
208  
209 =back
210  
211 =head3 Returns
212  
213 A query builder (this instance).
214  
215 =head2 from
216  
217 Set the report name to be queried.
218  
219 =head3 Parameters
220  
221 =over
222  
223 =item *
224  
225 A name of a report.
226  
227 =back
228  
229 =head3 Returns
230  
231 A query builder (this instance).
232  
233 =head2 during
234  
235 Sets the values of the DURING clause. If this subroutine is called, either
236 the first argument is undef or the second and third arguments are undef.
237  
238 =head3 Parameters
239  
240 =over
241  
242 =item *
243  
244 The specified date range string, e.g., YESTERDAY or when an end date
245 is specified, this is the start date string in 'YYYYMMDD' format.
246  
247 =item *
248  
249 The end date string in 'YYYYMMDD' format.
250  
251 =back
252  
253 =head3 Returns
254  
255 A query builder (this instance).
256  
257 =head2 build
258  
259 Builds the AWQL query.
260  
261 =head3 Returns
262  
263 A string that is the AWQL query.
264  
265 =cut
« lib/Google/Ads/Common/ErrorUtils.pm ↑ Summary blib/lib/Google/Ads/Common/OAuth2ServiceAccountsHandler.pm »