Class: JoinByPathsHelper

Inherits:
Set
  • Object
show all
Defined in:
lib/sequel/plugins/join_by_paths.rb

Constant Summary collapse

EMPTY_ARRAY =
[].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(smodel) ⇒ JoinByPathsHelper

Returns a new instance of JoinByPathsHelper.



84
85
86
87
# File 'lib/sequel/plugins/join_by_paths.rb', line 84

def initialize(smodel)
  super()
  @start_model = smodel
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



79
80
81
# File 'lib/sequel/plugins/join_by_paths.rb', line 79

def result
  @result
end

#start_modelObject (readonly)

Returns the value of attribute start_model.



80
81
82
# File 'lib/sequel/plugins/join_by_paths.rb', line 80

def start_model
  @start_model
end

Instance Method Details

#add(path_str) ⇒ Object



190
191
192
# File 'lib/sequel/plugins/join_by_paths.rb', line 190

def add(path_str)
  super(QPath.new(@start_model, path_str))
end

#build_unique_join_segmentsObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/sequel/plugins/join_by_paths.rb', line 89

def build_unique_join_segments
  return (@result = EMPTY_ARRAY) if empty?

  maxlen = map(&:size).max
  iset = nil
  @result = []

  (0...maxlen).each do |i|
    iset = Set.new
    each { |qp| (iset << qp.segments[i]) if qp.segments[i] }
    @result << iset
  end

  many_to_many_count = 0 # counter for differing multiple
  # many_to_many through alias
  @result.map! do |jseg|
    jseg.map  do |seg|
      leftm = seg.first.model_class
      assoc = leftm.association_reflection(seg.last.to_sym)

      rightm = seg.last.model_class
      # cf.  documentation in sequel/model/associations.rb
      case assoc[:type]
      # :many_to_one :: Foreign key in current model's table points to
      #                 associated model's primary key.
      when :many_to_one
        lks = [assoc[:key]].flatten
        rks = [rightm.primary_key].flatten

      when :one_to_many, :one_to_one
        # :one_to_many :: Foreign key in associated model's table points to this
        #                 model's primary key.
        # :one_to_one :: Similar to one_to_many in terms of foreign keys, but
        #                only one object is associated to the current object through the
        #                association.
        lks = [leftm.primary_key].flatten
        rks = [assoc[:key]].flatten

      when :many_to_many, :one_through_one
        #  :many_to_many :: A join table is used that has a foreign key that points
        #                  to this model's primary key and a foreign key that points to the
        #                  associated model's primary key.  Each current model object can be
        #                  associated with many associated model objects, and each associated
        #                  model object can be associated with many current model objects.
        # TODO: testcase for  :one_through_one
        # when    # :one_through_one :: Similar to many_to_many in terms of foreign keys, but only one object
        #                     is associated to the current object through the association.
        #                     Provides only getter methods, no setter or modification methods.

        result_ = []

        # in case of multiple many_to_many rels, we need differing through aliases
        many_to_many_count = many_to_many_count + 1
        through_alias = "t#{many_to_many_count}".to_sym

        # For many_to_many first we add the join with assigment table
        lks_ = [assoc[:left_primary_key]].flatten
        rks_ = [assoc[:left_key]].flatten

        lks_.map! { |k| Sequel[seg.first.alias_sym][k] } unless seg.first.empty?
        rks_.map! { |k| Sequel[through_alias][k] }

        result_ << {
          type: assoc[:type],
          left: leftm.table_name,
          right: assoc[:join_table],
          alias: through_alias,
          cond: rks_.zip(lks_).to_h
        }

        # then we add the join with with target table (right)
        lks = [assoc[:right_key]].flatten
        rks = [assoc.right_primary_key].flatten

        lks.map! { |k| Sequel[through_alias][k] }
        rks.map! { |k| Sequel[seg.last.alias_sym][k] }

        result_ << {
          type: assoc[:type],
          left: assoc[:join_table],
          right: rightm.table_name,
          alias: seg.last.alias_sym,
          cond: rks.zip(lks).to_h
        }

        next result_

      end

      lks.map! { |k| Sequel[seg.first.alias_sym][k] } unless seg.first.empty?
      rks.map! { |k| Sequel[seg.last.alias_sym][k] }

      { type: assoc[:type],
        left: leftm.table_name,
        right: rightm.table_name,
        alias: seg.last.alias_sym,
        cond: rks.zip(lks).to_h }
    end
  end
end

#dataset(start_dtset = nil) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/sequel/plugins/join_by_paths.rb', line 194

def dataset(start_dtset = nil)
  start_dataset = (start_dtset || @start_model.dataset)
  return start_dataset if empty?

  build_unique_join_segments
  need_distinct = false
  ret = @result.flatten.inject(start_dataset) do |dt, jo|
    need_distinct = true if jo[:type] == :many_to_many
    dt.left_join(Sequel[jo[:right]].as(jo[:alias]),
                 jo[:cond],
                 implicit_qualifier: jo[:left])
  end
  need_distinct ? ret.distinct : ret
end

#join_by_paths_helper(*pathlist) ⇒ Object



209
210
211
212
# File 'lib/sequel/plugins/join_by_paths.rb', line 209

def join_by_paths_helper(*pathlist)
  pathlist.each { |path_str| add path_str }
  self
end