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

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 123 CC 10 SCAR 28.1
CC
10
Cov
0%
build
42.0
during
20.0
START
6.0
CRAP 16.5
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 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 %from_report_of : ATTR(:name<from_report> :default<>);
34 my %date_range_of : ATTR(:name<date_range> :default<>);
35 my %start_date_of : ATTR(:name<start_date> :default<>);
36 my %end_date_of : ATTR(:name<end_date> :default<>);
37  
38 sub START {
39 my ($self) = @_;
40  
41 Google::Ads::Common::Utilities::AdsUtilityRegistry->add_ads_utilities(
42 "ReportQueryBuilder");
43  
44 # If a query builder was specified, then make a copy.
45 if ($self->get_query_builder()) {
46 push(@{$self->get_select()}, @{$self->get_query_builder()->get_select()});
47 $self->set_from_report($self->get_query_builder()->get_from_report());
48 $self->set_date_range($self->get_query_builder()->get_date_range());
49 $self->set_start_date($self->get_query_builder()->get_start_date());
50 $self->set_end_date($self->get_query_builder()->get_end_date());
51 }
52  
53 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 sub select {
62 my ($self, $fields) = @_;
63 @{$self->get_select()} = ();
64 push(@{$self->get_select()}, @$fields);
65 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 sub from {
75 my ($self, $report_name) = @_;
76 $self->set_from_report($report_name);
77 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 sub during {
92 my ($self, $date_range_or_start_date, $end_date) = @_;
93  
94 if (!$date_range_or_start_date) {
95 $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 if ($date_range_or_start_date =~ /^\d+$/) {
103 if (!$end_date or !($end_date =~ /^\d+$/)) {
104 $self->_log_error("Pass in 2 arguments with a start date and an " .
105 "end date with the format 'YYYYMMDD'");
106 }
107 $self->set_start_date($date_range_or_start_date);
108 $self->set_end_date($end_date);
109 }
110 else {
111 $self->set_date_range($date_range_or_start_date);
112 }
113 return $self;
114 }
115  
116 # Builds an AWQL query.
117 # Returns: The AWQL string.
118 sub build {
119 my ($self) = @_;
120 if (!$self->get_select() or (scalar @{$self->get_select()} eq 0)) {
121 $self->_log_error(
122 "Must use select() to specify SELECT clause for valid AWQL first.");
123 }
124 if (!$self->get_from_report()) {
125 $self->_log_error(
126 "Must use from() to specify FROM clause for valid AWQL first.");
127 }
128 my $awql .= sprintf('SELECT %s', join(", ", @{$self->get_select()}));
129 $awql .= sprintf(' FROM %s', $self->get_from_report());
130 my @where_builder_strings = ();
131 foreach my $where_builder (@{$self->get_where_builders()}) {
132 push(@where_builder_strings, ($where_builder->build()));
133 }
134 $awql .=
135 (scalar @where_builder_strings > 0)
136 ? sprintf(' WHERE %s', join(" AND ", @where_builder_strings))
137 : '';
138 $awql .=
139 ($self->get_date_range())
140 ? sprintf(' DURING %s', $self->get_date_range())
141 : '';
142 $awql .=
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 return $awql;
147 }
148  
149 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