Sha256: 68b0ecb3ac989ec1c557c1cee61ea83cc3d26f260698c49b7e23b8d8af2cce1b

Contents?: true

Size: 1.64 KB

Versions: 11

Compression:

Stored size: 1.64 KB

Contents

module Switchman
  module ActiveRecord
    module StatementCache
      # since the StatememtCache is only implemented
      # for basic relations in AR::Base#find, AR::Base#find_by and AR::Association#get_records,
      # we can make some assumptions about the shard source
      # (e.g. infer from the primary key or use the current shard)

      def execute(params, klass, connection)
        target_shard = nil
        if primary_index = bind_map.primary_value_index
          primary_value = params[primary_index]
          target_shard = Shard.local_id_for(primary_value)[1]
        end
        current_shard = Shard.current(klass.shard_category)
        target_shard ||= current_shard

        bind_values = bind_map.bind(params, current_shard, target_shard || current_shard)

        sql = query_builder.sql_for(bind_values, connection)
        target_shard.activate(klass.shard_category) do
          klass.find_by_sql(sql, bind_values)
        end
      end

      module BindMap
        # performs id transposition here instead of query_methods.rb
        def bind(values, current_shard, target_shard)
          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

        def primary_value_index
          if primary_bv_index = @bind_values.index{|col, sub| sub.primary}
            @indexes.index(primary_bv_index)
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
switchman-1.4.4 lib/switchman/active_record/statement_cache.rb
switchman-1.4.3 lib/switchman/active_record/statement_cache.rb
switchman-1.4.2 lib/switchman/active_record/statement_cache.rb
switchman-1.4.1 lib/switchman/active_record/statement_cache.rb
switchman-1.4.0 lib/switchman/active_record/statement_cache.rb
switchman-1.3.18 lib/switchman/active_record/statement_cache.rb
switchman-1.3.17 lib/switchman/active_record/statement_cache.rb
switchman-1.3.14.pre.1 lib/switchman/active_record/statement_cache.rb
switchman-1.3.16 lib/switchman/active_record/statement_cache.rb
switchman-1.3.15 lib/switchman/active_record/statement_cache.rb
switchman-1.3.14 lib/switchman/active_record/statement_cache.rb