Sha256: b16f22d725f7573b42a0918dd27db8deefff4e564d690086d76b95da843f7bd8

Contents?: true

Size: 918 Bytes

Versions: 2

Compression:

Stored size: 918 Bytes

Contents

module ActiveRecord
  module ConnectionAdapters
    class AbstractMysqlAdapter < AbstractAdapter
      # MySQL is too stupid to create a temporary table for use subquery, so we have
      # to give it some prompting in the form of a subsubquery. Ugh!
      def subquery_for(key, select)
        subsubselect = select.clone
        subsubselect.projections = [key]

        # Materialize subquery by adding distinct
        # to work with MySQL 5.7.6 which sets optimizer_switch='derived_merge=on'
        subsubselect.distinct unless select.limit || select.offset || select.orders.any?

        subselect = Arel::SelectManager.new(select.engine)

        # subselect.project Arel.sql(key.name)
        arel_table = select.engine.arel_table
        subselect.project *[key].map { |x| arel_table[x.name] }
        subselect.from subsubselect.as('__active_record_temp')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
composite_primary_keys-8.1.8 lib/composite_primary_keys/connection_adapters/abstract_mysql_adapter.rb
composite_primary_keys-8.1.7 lib/composite_primary_keys/connection_adapters/abstract_mysql_adapter.rb