« blib/lib/Google/Ads/Common/HTTPTransport.pm
↑ Summary
blib/lib/Google/Ads/Common/OAuth2BaseHandler.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 | 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 ($start_index xor $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 | ($self->get_start_index() and $self->get_page_size()) | ||
| 134 | ? sprintf(' LIMIT %s, %s', $self->get_start_index(), $self->get_page_size()) | ||
| 135 | : ''; | ||
| 136 | return $awql; | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; | ||
| 140 | |||
| 141 | =pod | ||
| 142 | |||
| 143 | =head1 NAME | ||
| 144 | |||
| 145 | Google::Ads::AdWords::Utilities::ServiceQueryBuilder | ||
| 146 | |||
| 147 | =head1 DESCRIPTION | ||
| 148 | |||
| 149 | A query builder for building AWQL queries for services. | ||
| 150 | |||
| 151 | =head1 ATTRIBUTES | ||
| 152 | |||
| 153 | The attributes properties may be set in new() and accessed using get_ATTRIBUTE | ||
| 154 | methods: | ||
| 155 | |||
| 156 | =head2 select | ||
| 157 | |||
| 158 | An array of fields that are selected to be filtered on. | ||
| 159 | |||
| 160 | =head2 order_by | ||
| 161 | |||
| 162 | A hash of field names to ASC or DESC by which results will be ordered. | ||
| 163 | |||
| 164 | =head2 start_index | ||
| 165 | |||
| 166 | The start index of the results to be retunred. | ||
| 167 | |||
| 168 | =head2 page_size | ||
| 169 | |||
| 170 | The size of each page that to be returned. | ||
| 171 | |||
| 172 | =head1 METHODS | ||
| 173 | |||
| 174 | =head2 new | ||
| 175 | |||
| 176 | Constructor. The following data structure may be passed to new(): | ||
| 177 | |||
| 178 | { # Google::Ads::AdWords::Utilities::ReportQueryBuilder | ||
| 179 | client # Required: Google::Ads::AdWords::Client | ||
| 180 | query_builder # Optional: the Google::Ads::AdWords::Utilities::QueryBuilder | ||
| 181 | to be copied | ||
| 182 | }, | ||
| 183 | |||
| 184 | =head1 METHODS | ||
| 185 | |||
| 186 | =head2 select | ||
| 187 | |||
| 188 | Sets the specified list of fields to be added to a SELECT clause. | ||
| 189 | |||
| 190 | =head3 Parameters | ||
| 191 | |||
| 192 | =over | ||
| 193 | |||
| 194 | =item * | ||
| 195 | |||
| 196 | A reference to an array of fields selected. | ||
| 197 | |||
| 198 | =back | ||
| 199 | |||
| 200 | =head3 Returns | ||
| 201 | |||
| 202 | A query builder (this instance). | ||
| 203 | |||
| 204 | =head2 order_by | ||
| 205 | |||
| 206 | Adds the provided field to the order-by list with the provided direction. | ||
| 207 | |||
| 208 | =head3 Parameters | ||
| 209 | |||
| 210 | =over | ||
| 211 | |||
| 212 | =item * | ||
| 213 | |||
| 214 | The specified field to be added to the order-by list. | ||
| 215 | |||
| 216 | =item * | ||
| 217 | |||
| 218 | If true, the newly created order-by clause will be in ascending order. | ||
| 219 | Otherwise, it will be in descending order. | ||
| 220 | |||
| 221 | =back | ||
| 222 | |||
| 223 | =head3 Returns | ||
| 224 | |||
| 225 | A query builder (this instance). | ||
| 226 | |||
| 227 | =head2 limit | ||
| 228 | |||
| 229 | Sets the LIMIT clause using the provided start index and page size. | ||
| 230 | |||
| 231 | =head3 Parameters | ||
| 232 | |||
| 233 | =over | ||
| 234 | |||
| 235 | =item * | ||
| 236 | |||
| 237 | The specified start index for the LIMIT clause. | ||
| 238 | |||
| 239 | =item * | ||
| 240 | |||
| 241 | The page size for the LIMIT clause. | ||
| 242 | |||
| 243 | =back | ||
| 244 | |||
| 245 | =head3 Returns | ||
| 246 | |||
| 247 | A query builder (this instance). | ||
| 248 | |||
| 249 | =head2 build | ||
| 250 | |||
| 251 | Builds the AWQL query. | ||
| 252 | |||
| 253 | =head3 Returns | ||
| 254 | |||
| 255 | A string that is the AWQL query. | ||
| 256 | |||
| 257 | =cut |