Class: ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation

Inherits:
JoinBase
  • Object
show all
Defined in:
lib/active_record/connection_adapters/ibm_db_pstmt.rb

Instance Method Summary collapse

Instance Method Details

#association_joinObject



1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
# File 'lib/active_record/connection_adapters/ibm_db_pstmt.rb', line 1642

def association_join
  return_hash = {} # A Hash to conatin the parameterised sql and the parameters
  parameter_array = []

  connection = reflection.active_record.connection
  join = case reflection.macro
    when :has_and_belongs_to_many
      " #{join_type} %s ON %s.%s = %s.%s " % [
         table_alias_for(options[:join_table], aliased_join_table_name),
         connection.quote_table_name(aliased_join_table_name),
         options[:foreign_key] || reflection.active_record.to_s.foreign_key,
         connection.quote_table_name(parent.aliased_table_name),
         reflection.active_record.primary_key] +
      " #{join_type} %s ON %s.%s = %s.%s " % [
         table_name_and_alias,
         connection.quote_table_name(aliased_table_name),
         klass.primary_key,
         connection.quote_table_name(aliased_join_table_name),
         options[:association_foreign_key] || klass.to_s.foreign_key
         ]
    when :has_many, :has_one
      case
        when reflection.options[:through]
          through_conditions = through_reflection.options[:conditions] ? "AND #{interpolate_sql(sanitize_sql(through_reflection.options[:conditions]))}" : ''

          jt_foreign_key = jt_as_extra = jt_source_extra = jt_sti_extra = nil
          first_key = second_key = as_extra = nil

          if through_reflection.options[:as] # has_many :through against a polymorphic join
            jt_foreign_key = through_reflection.options[:as].to_s + '_id'
            jt_as_extra = " AND %s.%s = ?" % [
              connection.quote_table_name(aliased_join_table_name),
              connection.quote_column_name(through_reflection.options[:as].to_s + '_type')
            ]
            parameter_array = parameter_array + [klass.quote_value(parent.active_record.base_class.name)]
          else
            jt_foreign_key = through_reflection.primary_key_name
          end

          case source_reflection.macro
          when :has_many
            if source_reflection.options[:as]
              first_key   = "#{source_reflection.options[:as]}_id"
              second_key  = options[:foreign_key] || primary_key
              parameter_array = parameter_array + [klass.quote_value(source_reflection.active_record.base_class.name)]
              as_extra    = " AND %s.%s = ?" % [
                connection.quote_table_name(aliased_table_name),
                connection.quote_column_name("#{source_reflection.options[:as]}_type")
              ]
            else
              first_key   = through_reflection.klass.base_class.to_s.foreign_key
              second_key  = options[:foreign_key] || primary_key
            end

            unless through_reflection.klass.descends_from_active_record?
              parameter_array = parameter_array + [through_reflection.klass.quote_value(through_reflection.klass.sti_name)]
              jt_sti_extra = " AND %s.%s = ?" % [
                connection.quote_table_name(aliased_join_table_name),
                connection.quote_column_name(through_reflection.active_record.inheritance_column)
              ]
            end
          when :belongs_to
            first_key = primary_key
            if reflection.options[:source_type]
              second_key = source_reflection.association_foreign_key
              parameter_array = parameter_array + [klass.quote_value(reflection.options[:source_type])]
              jt_source_extra = " AND %s.%s = ?" % [
                connection.quote_table_name(aliased_join_table_name),
                connection.quote_column_name(reflection.source_reflection.options[:foreign_type])
              ]
            else
              second_key = source_reflection.primary_key_name
            end
          end

          " #{join_type} %s ON (%s.%s = %s.%s%s%s%s) " % [
            table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),
            connection.quote_table_name(parent.aliased_table_name),
            connection.quote_column_name(parent.primary_key),
            connection.quote_table_name(aliased_join_table_name),
            connection.quote_column_name(jt_foreign_key),
            jt_as_extra, jt_source_extra, jt_sti_extra
          ] +
          " #{join_type} %s ON (%s.%s = %s.%s%s) " % [
            table_name_and_alias,
            connection.quote_table_name(aliased_table_name),
            connection.quote_column_name(first_key),
            connection.quote_table_name(aliased_join_table_name),
            connection.quote_column_name(second_key),
            as_extra
          ]

        when reflection.options[:as] && [:has_many, :has_one].include?(reflection.macro)
          parameter_array = parameter_array + [klass.quote_value(parent.active_record.base_class.name)]
          " #{join_type} %s ON %s.%s = %s.%s AND %s.%s = ?" % [
            table_name_and_alias,
            connection.quote_table_name(aliased_table_name),
            "#{reflection.options[:as]}_id",
            connection.quote_table_name(parent.aliased_table_name),
            parent.primary_key,
            connection.quote_table_name(aliased_table_name),
            "#{reflection.options[:as]}_type"
          ]
        else
          foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
          " #{join_type} %s ON %s.%s = %s.%s " % [
            table_name_and_alias,
            aliased_table_name,
            foreign_key,
            parent.aliased_table_name,
            reflection.options[:primary_key] || parent.primary_key
          ]
      end
    when :belongs_to
      " #{join_type} %s ON %s.%s = %s.%s " % [
         table_name_and_alias,
         connection.quote_table_name(aliased_table_name),
         reflection.klass.primary_key,
         connection.quote_table_name(parent.aliased_table_name),
         options[:foreign_key] || reflection.primary_key_name
        ]
    else
      ""
  end || ''

  unless klass.descends_from_active_record?
    sql_segment, *param_array = klass.send(:type_condition, aliased_table_name)
    join << %(AND %s) % [sql_segment]
    parameter_array = parameter_array + param_array unless param_array.nil?
  end

  [through_reflection, reflection].each do |ref|
    if ref && ref.options[:conditions]
      sql_param_hash = sanitize_sql(ref.options[:conditions], aliased_table_name)
      parameter_array = parameter_array + sql_param_hash["paramArray"] unless sql_param_hash["paramArray"].nil?
      join << "AND #{interpolate_sql(sql_param_hash["sqlSegment"])} "
    end
  end

  return_hash["sqlSegment"] = join
  return_hash["paramArray"] = parameter_array
  return_hash
end