Class: Exwiw::QueryAst::JoinClause

Inherits:
Object
  • Object
show all
Defined in:
lib/exwiw/query_ast.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_table_name:, foreign_key:, join_table_name:, primary_key:, where_clauses: [], base_where_clauses: []) ⇒ JoinClause

Returns a new instance of JoinClause.



37
38
39
40
41
42
43
44
# File 'lib/exwiw/query_ast.rb', line 37

def initialize(base_table_name:, foreign_key:, join_table_name:, primary_key:, where_clauses: [], base_where_clauses: [])
  @base_table_name = base_table_name
  @foreign_key = foreign_key
  @join_table_name = join_table_name
  @primary_key = primary_key
  @where_clauses = where_clauses
  @base_where_clauses = base_where_clauses
end

Instance Attribute Details

#base_table_nameObject (readonly)

where_clauses is compiled against this join's join_table_name (the joined-to table). base_where_clauses, on the other hand, is compiled against base_table_name (the joined-from table). The latter is used for the case where the source table polymorphically belongs_to the joined-to table and the type column (foreign_type) lives on the source table.



35
36
37
# File 'lib/exwiw/query_ast.rb', line 35

def base_table_name
  @base_table_name
end

#base_where_clausesObject (readonly)

where_clauses is compiled against this join's join_table_name (the joined-to table). base_where_clauses, on the other hand, is compiled against base_table_name (the joined-from table). The latter is used for the case where the source table polymorphically belongs_to the joined-to table and the type column (foreign_type) lives on the source table.



35
36
37
# File 'lib/exwiw/query_ast.rb', line 35

def base_where_clauses
  @base_where_clauses
end

#foreign_keyObject (readonly)

where_clauses is compiled against this join's join_table_name (the joined-to table). base_where_clauses, on the other hand, is compiled against base_table_name (the joined-from table). The latter is used for the case where the source table polymorphically belongs_to the joined-to table and the type column (foreign_type) lives on the source table.



35
36
37
# File 'lib/exwiw/query_ast.rb', line 35

def foreign_key
  @foreign_key
end

#join_table_nameObject (readonly)

where_clauses is compiled against this join's join_table_name (the joined-to table). base_where_clauses, on the other hand, is compiled against base_table_name (the joined-from table). The latter is used for the case where the source table polymorphically belongs_to the joined-to table and the type column (foreign_type) lives on the source table.



35
36
37
# File 'lib/exwiw/query_ast.rb', line 35

def join_table_name
  @join_table_name
end

#primary_keyObject (readonly)

where_clauses is compiled against this join's join_table_name (the joined-to table). base_where_clauses, on the other hand, is compiled against base_table_name (the joined-from table). The latter is used for the case where the source table polymorphically belongs_to the joined-to table and the type column (foreign_type) lives on the source table.



35
36
37
# File 'lib/exwiw/query_ast.rb', line 35

def primary_key
  @primary_key
end

#where_clausesObject (readonly)

where_clauses is compiled against this join's join_table_name (the joined-to table). base_where_clauses, on the other hand, is compiled against base_table_name (the joined-from table). The latter is used for the case where the source table polymorphically belongs_to the joined-to table and the type column (foreign_type) lives on the source table.



35
36
37
# File 'lib/exwiw/query_ast.rb', line 35

def where_clauses
  @where_clauses
end

Instance Method Details

#to_hObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/exwiw/query_ast.rb', line 46

def to_h
  hash = {
    base_table_name: base_table_name,
    foreign_key: foreign_key,
    join_table_name: join_table_name,
    primary_key: primary_key,
  }
  if where_clauses.size.positive?
    hash[:where_clauses] = where_clauses.map { |wc| wc.is_a?(String) ? wc : wc.to_h }
  end
  if base_where_clauses.size.positive?
    hash[:base_where_clauses] = base_where_clauses.map { |wc| wc.is_a?(String) ? wc : wc.to_h }
  end
  hash
end