class DataMapper::Query

Public Instance Methods

fmp_operator(operation) click to toggle source

Convert operation class to operator string

# File lib/dm-filemaker-adapter/core_patches.rb, line 109
def fmp_operator(operation)
  case
  when operation[/GreaterThanOrEqualTo/]; '>='
  when operation[/LessThanOrEqualTo/]; '<='
  when operation[/GreaterThan/]; '>'
  when operation[/LessThan/]; '<'
  when operation[/EqualTo/]; '=='
  when operation[/Like/];
  when operation[/Null/];
  else nil
  end
end
fmp_options(query=self) click to toggle source

Get fmp options hash from query

# File lib/dm-filemaker-adapter/core_patches.rb, line 123
def fmp_options(query=self)
  fm_options = {}
  fm_options[:skip_records] = query.offset if query.offset
  fm_options[:max_records] = query.limit if query.limit
  if query.order
    fm_options[:sort_field] = query.order.collect do |ord|
      ord.target.field
    end
    fm_options[:sort_order] = query.order.collect do |ord|
      ord.operator.to_s + 'end'
    end
  end
  fm_options
end
to_fmp_query(input=self.conditions) click to toggle source

Convert dm query conditions to fmp query params (hash)

# File lib/dm-filemaker-adapter/core_patches.rb, line 58
def to_fmp_query(input=self.conditions)
  #puts "FMP_QUERY input #{input.class.name}"
  rslt = if input.class.name[/OrOperation/]
     #puts "FMP_QUERY OrOperation #{input.class}"
    input.operands.collect do |o|
     r = to_fmp_query o
     #puts "FMP_QUERY or-operation operand #{r}"
     r
    end
  elsif input.class.name[/AndOperation/]
     #puts "FMP_QUERY AndOperation input class #{input.class}"
     #puts "FMP_QUERY AndOperation input value #{input.inspect}"
     out = {}
    input.operands.each do |k,v|
     #puts "FMP_QUERY and-operation pre-process operand key:val #{k}:#{v}"
      r = to_fmp_query(k).to_hash
      #puts "FMP_QUERY and-operation post-process operand #{r}"
      if r.is_a?(Hash)
             #puts "FMP_QUERY and-operation operand is a hash"
             # Filemaker can't have the same field twice in a single find request,
             # but we can mash the two conditions together in a way that FMP can use.
        out.merge!(r){|k, oldv, newv| "#{oldv} #{newv}"}
      else
             #puts "FMP_QUERY and-operation operand is NOT a hash"
        out = r
        break
      end
    end
    out
  elsif input.class.name[/NullOperation/] || input.nil?
    #puts "FMP_QUERY NullOperation #{input.class}"
    {}
  else
    #puts "FMP_QUERY else input class #{input.class}"
    #puts "FMP_QUERY else input value #{input.inspect}"
    #puts "FMP_QUERY else-options #{self.options.inspect}"
    #prepare_fmp_attributes({input.subject=>input.value}, :prepend=>fmp_operator(input.class.name))
    value = (
     self.options[input.keys[0]] ||
     self.options[input.subject.name] ||
     self.options.find{|o,v| o.respond_to?(:target) && o.target.to_s == input.subject.name.to_s}[1] ||
     input.value
    ) rescue input.value   #(puts "ERROR #{$!}"; input.value)
    #puts "FMP_QUERY else-value #{value}"
    repository.adapter.prepare_fmp_attributes({input.subject=>value}, :prepend=>fmp_operator(input.class.name))
  end
  #puts "FMP_QUERY output #{rslt.inspect}"
  rslt
end