class Zuora::Calls::Query

Public Class Methods

new(select, from = nil, where = nil) click to toggle source

@param [String|Array] select - query statement or field name sym array @param [Symbol|Nil] from - table name symbol @param [Symbol|Nil] where - hash of equalities for where clauses

Operations: only = is supported
Custom field names are supported: some_field__c => SomeField__c

@return [Zuora::Calls:Query]

# File lib/zuora/calls/query.rb, line 19
def initialize(select, from = nil, where = nil)
  @query_string = if select.is_a? Array
                    query_to_string(select, from, where)
                  else
                    select
                  end
end

Public Instance Methods

xml_builder() click to toggle source

@return [Callable]

# File lib/zuora/calls/query.rb, line 28
def xml_builder
  lambda do |builder|
    builder[:api].query { builder[:api].queryString(@query_string) }
  end
end

Private Instance Methods

query_to_string(fields, table, conditions) click to toggle source

@param [Array] fields @param [Symbol] table @param [Hash] conditions

# File lib/zuora/calls/query.rb, line 39
def query_to_string(fields, table, conditions)
  raise 'Fields must be an Array' unless fields.is_a?(Array)
  raise 'Table must respond to :to_sym' unless table.respond_to?(:to_sym)
  raise 'Conditions must be Array' if fields && !fields.is_a?(Array)

  key_fn = ->(key) { Zuora::Utils::Envelope.to_zuora_key(key) }

  select = fields.map { |field| key_fn[field] }.join(', ').to_s
  from = table.to_s
  if conditions
    where = 'WHERE ' + conditions.map do |key, value|
      "#{key_fn[key]} = '#{value}'"
    end.join(' AND ')
  end

  "SELECT #{select} FROM #{from} #{where}".strip.squeeze(' ')
end