File: alias_tracker.rb

package info (click to toggle)
rails 2%3A7.2.2.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,352 kB
  • sloc: ruby: 349,799; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (90 lines) | stat: -rw-r--r-- 2,853 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
83
84
85
86
87
88
89
90
# frozen_string_literal: true

require "active_support/core_ext/string/conversions"

module ActiveRecord
  module Associations
    # Keeps track of table aliases for ActiveRecord::Associations::JoinDependency
    class AliasTracker # :nodoc:
      def self.create(pool, initial_table, joins, aliases = nil)
        pool.with_connection do |connection|
          if joins.empty?
            aliases ||= Hash.new(0)
          elsif aliases
            default_proc = aliases.default_proc || proc { 0 }
            aliases.default_proc = proc { |h, k|
              h[k] = initial_count_for(connection, k, joins) + default_proc.call(h, k)
            }
          else
            aliases = Hash.new { |h, k|
              h[k] = initial_count_for(connection, k, joins)
            }
          end
          aliases[initial_table] = 1
          new(connection.table_alias_length, aliases)
        end
      end

      def self.initial_count_for(connection, name, table_joins)
        quoted_name = nil

        counts = table_joins.map do |join|
          if join.is_a?(Arel::Nodes::StringJoin)
            # quoted_name should be case ignored as some database adapters (Oracle) return quoted name in uppercase
            quoted_name ||= connection.quote_table_name(name)

            # Table names + table aliases
            join.left.scan(
              /JOIN(?:\s+\w+)?\s+(?:\S+\s+)?(?:#{quoted_name}|#{name})\sON/i
            ).size
          elsif join.is_a?(Arel::Nodes::Join)
            join.left.name == name ? 1 : 0
          else
            raise ArgumentError, "joins list should be initialized by list of Arel::Nodes::Join"
          end
        end

        counts.sum
      end

      # table_joins is an array of arel joins which might conflict with the aliases we assign here
      def initialize(table_alias_length, aliases)
        @aliases = aliases
        @table_alias_length = table_alias_length
      end

      def aliased_table_for(arel_table, table_name = nil)
        table_name ||= arel_table.name

        if aliases[table_name] == 0
          # If it's zero, we can have our table_name
          aliases[table_name] = 1
          arel_table = arel_table.alias(table_name) if arel_table.name != table_name
        else
          # Otherwise, we need to use an alias
          aliased_name = table_alias_for(yield)

          # Update the count
          count = aliases[aliased_name] += 1

          aliased_name = "#{truncate(aliased_name)}_#{count}" if count > 1

          arel_table = arel_table.alias(aliased_name)
        end

        arel_table
      end

      attr_reader :aliases

      private
        def table_alias_for(table_name)
          table_name[0...@table_alias_length].tr(".", "_")
        end

        def truncate(name)
          name.slice(0, @table_alias_length - 2)
        end
    end
  end
end