Class: AdManagerApi::StatementBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ad_manager_api/pql_statement_utils.rb

Overview

A utility class for building PQL statements, used by the PublisherQueryLanguageService and other services’ get*ByStatement and perform*Action requsts.

Example usage: sb = AdManagerApi::StatementBuilder.new do |b|

b.select = 'ChangeDateTime, EntityId, EntityType, Operation'
b.from = 'Change_History'
b.where = 'ChangeDateTime < :start_date_time ' +
          'AND ChangeDateTime > :end_date_time'
b.with_bind_variable('start_date_time',
                     AdManagerApi::AdManagerDate.today-1)
b.with_bind_variable('end_date_time', AdManagerApi::Date.today)
b.order_by = 'ChangeDateTime'
b.ascending = false

end sb.to_statement()

Constant Summary collapse

SUGGESTED_PAGE_LIMIT =
500
SELECT =
'SELECT %s'
FROM =
'FROM %s'
WHERE =
'WHERE %s'
ORDER =
'ORDER BY %s %s'
LIMIT =
'LIMIT %d'
OFFSET =
'OFFSET %d'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api) {|_self| ... } ⇒ StatementBuilder

Create a StatementBuilder object.

Yields:

  • (_self)

Yield Parameters:



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 135

def initialize(api, &block)
  @api = api
  @select = nil
  @from = nil
  @where = nil
  @order_by = nil
  @ascending = true
  @limit = SUGGESTED_PAGE_LIMIT
  @offset = 0
  @pql_values = PQLValues.new
  yield self if block_given?
end

Instance Attribute Details

#ascendingObject

Returns the value of attribute ascending.



132
133
134
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 132

def ascending
  @ascending
end

#fromObject

Returns the value of attribute from.



132
133
134
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 132

def from
  @from
end

#limitObject

Returns the value of attribute limit.



132
133
134
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 132

def limit
  @limit
end

#offsetObject

Returns the value of attribute offset.



132
133
134
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 132

def offset
  @offset
end

#order_byObject

Returns the value of attribute order_by.



132
133
134
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 132

def order_by
  @order_by
end

#selectObject

Returns the value of attribute select.



132
133
134
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 132

def select
  @select
end

#whereObject

Returns the value of attribute where.



132
133
134
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 132

def where
  @where
end

Instance Method Details

#configure {|_self| ... } ⇒ Object

Edit a StatmentBuilder object with a block.

Yields:

  • (_self)

Yield Parameters:



149
150
151
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 149

def configure(&block)
  yield self
end

#to_statementObject

Create a statement object that can be sent in a Ad Manager API request.



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 179

def to_statement()
  @api.utils_reporter.statement_builder_used()
  validate()
  ordering = @ascending ? 'ASC' : 'DESC'
  pql_query = PQLQuery.new
  pql_query << SELECT % @select unless @select.to_s.empty?
  pql_query << FROM % @from unless @from.to_s.empty?
  pql_query << WHERE % @where unless @where.to_s.empty?
  pql_query << ORDER % [@order_by, ordering] unless @order_by.to_s.empty?
  pql_query << LIMIT % @limit unless @limit.nil?
  pql_query << OFFSET % @offset unless @offset.nil?
  return {:query => pql_query.to_s(), :values => @pql_values.values}
end

#validateObject

Return a list of validation errors.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 160

def validate()
  if !@select.to_s.strip.empty? and @from.to_s.strip.empty?
    raise 'FROM clause is required with SELECT'
  end
  if !@from.to_s.strip.empty? and @select.to_s.strip.empty?
    raise 'SELECT clause is required with FROM'
  end
  if !@offset.nil? and @limit.nil?
    raise 'LIMIT clause is required with OFFSET'
  end
  unless @limit.is_a? Integer or @limit.nil?
    raise 'LIMIT must be an integer'
  end
  unless @offset.is_a? Integer or @offset.nil?
    raise 'OFFSET must be an integer'
  end
end

#with_bind_variable(key, value) ⇒ Object

Add a value to be matched to bind a variable in WHERE conditions.



154
155
156
157
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 154

def with_bind_variable(key, value)
  @pql_values.add_value(key, value)
  return self
end