class AdManagerApi::StatementBuilder

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()

Constants

FROM
LIMIT
OFFSET
ORDER
SELECT
SUGGESTED_PAGE_LIMIT
WHERE

Attributes

ascending[RW]
from[RW]
limit[RW]
offset[RW]
order_by[RW]
select[RW]
where[RW]

Public Class Methods

new(api) { |self| ... } click to toggle source

Create a StatementBuilder object.

# 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

Public Instance Methods

configure() { |self| ... } click to toggle source

Edit a StatmentBuilder object with a block.

# File lib/ad_manager_api/pql_statement_utils.rb, line 149
def configure(&block)
  yield self
end
to_statement() click to toggle source

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

# 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
validate() click to toggle source

Return a list of validation errors.

# 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) click to toggle source

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

# 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