Sha256: 8db1e8b49d70130a970cd2957de616411f46c412fa94ebeb875478ca9025b925
Contents?: true
Size: 1.93 KB
Versions: 1
Compression:
Stored size: 1.93 KB
Contents
# frozen_string_literal: true module Zuora module Calls class Query < Hashie::Dash # This constructor has two arities. # Arity 1: provide a raw ZOQL query string # .new 'SELECT Id FROM Account WHERE Id = '1') # Arity 2: provide simple query components # .new [:id], :account, { :id => 1 }) will be transformed into query above # @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] def initialize(select, from = nil, where = nil) @query_string = if select.is_a? Array query_to_string(select, from, where) else select end end # @return [Callable] def xml_builder lambda do |builder| builder[:api].query { builder[:api].queryString(@query_string) } end end private # @param [Array] fields # @param [Symbol] table # @param [Hash] conditions 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 end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
zuora-ruby-0.7.0 | lib/zuora/calls/query.rb |