Class: Babik::QuerySet::Join::AbstractJoin

Inherits:
Object
  • Object
show all
Defined in:
lib/babik/queryset/lib/join/join.rb

Overview

A table join

Direct Known Subclasses

LeftJoin

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_table, origin_table) ⇒ AbstractJoin

Construct a Join

Parameters:



32
33
34
35
36
# File 'lib/babik/queryset/lib/join/join.rb', line 32

def initialize(target_table, origin_table)
  @target_table = target_table
  @origin_table = origin_table
  _init_sql
end

Instance Attribute Details

#origin_tableObject (readonly)

Returns the value of attribute origin_table.



27
28
29
# File 'lib/babik/queryset/lib/join/join.rb', line 27

def origin_table
  @origin_table
end

#sqlObject (readonly)

Returns the value of attribute sql.



27
28
29
# File 'lib/babik/queryset/lib/join/join.rb', line 27

def sql
  @sql
end

#target_tableObject (readonly)

Returns the value of attribute target_table.



27
28
29
# File 'lib/babik/queryset/lib/join/join.rb', line 27

def target_table
  @target_table
end

Instance Method Details

#_init_sqlObject

Initialize SQL of the JOIN



39
40
41
42
43
44
45
# File 'lib/babik/queryset/lib/join/join.rb', line 39

def _init_sql
  # Create the SQL code of the join
  @sql = %(
  #{self.class::JOIN_TYPE} JOIN #{@target_table.name} #{@target_table.table_alias}
      ON #{@target_table.table_alias}.#{@target_table.key} = #{@origin_table.table_alias}.#{@origin_table.key}
  ).delete("\n").gsub(/\s{2,}/, ' ').strip
end

#target_aliasString

Return the target table alias.

Returns:

  • (String)

    Target table alias.



49
50
51
# File 'lib/babik/queryset/lib/join/join.rb', line 49

def target_alias
  @target_table.table_alias
end