Module: Switchman::ActiveRecord::StatementCache::BindMap

Defined in:
lib/switchman/active_record/statement_cache.rb

Instance Method Summary collapse

Instance Method Details

#bind(values, current_shard, target_shard) ⇒ Object

performs id transposition here instead of query_methods.rb



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/switchman/active_record/statement_cache.rb', line 57

def bind(values, current_shard, target_shard)
  if ::Rails.version >= '5'
    bas = @bound_attributes.dup
    @indexes.each_with_index do |offset,i|
      ba = bas[offset]
      if ba.is_a?(::ActiveRecord::Relation::QueryAttribute) && ba.value.sharded
        new_value = Shard.relative_id_for(values[i], current_shard, target_shard || current_shard)
      else
        new_value = values[i]
      end
      bas[offset] = ba.with_cast_value(new_value)
    end
    bas
  else
    bvs = @bind_values.map { |pair| pair.dup }
    @indexes.each_with_index do |offset,i|
      bv = bvs[offset]
      if bv[1].sharded
        bv[1] = Shard.relative_id_for(values[i], current_shard, target_shard || current_shard)
      else
        bv[1] = values[i]
      end
    end
    bvs
  end
end

#primary_value_indexObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/switchman/active_record/statement_cache.rb', line 84

def primary_value_index
  if ::Rails.version >= '5'
    primary_ba_index = @bound_attributes.index do |ba|
      ba.is_a?(::ActiveRecord::Relation::QueryAttribute) && ba.value.primary
    end
    if primary_ba_index
      @indexes.index(primary_ba_index)
    end
  else
    if primary_bv_index = @bind_values.index{|col, sub| sub.primary}
      @indexes.index(primary_bv_index)
    end
  end
end