Class: Exwiw::QueryAst::Select

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSelect

Returns a new instance of Select.



96
97
98
99
100
101
102
# File 'lib/exwiw/query_ast.rb', line 96

def initialize
  @from_table_name = nil
  @columns = []
  @where_clauses = []
  @join_clauses = []
  @select_all = false
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



94
95
96
# File 'lib/exwiw/query_ast.rb', line 94

def columns
  @columns
end

#from_table_nameObject (readonly)

Returns the value of attribute from_table_name.



94
95
96
# File 'lib/exwiw/query_ast.rb', line 94

def from_table_name
  @from_table_name
end

#join_clausesObject (readonly)

Returns the value of attribute join_clauses.



94
95
96
# File 'lib/exwiw/query_ast.rb', line 94

def join_clauses
  @join_clauses
end

#select_allObject (readonly)

Returns the value of attribute select_all.



94
95
96
# File 'lib/exwiw/query_ast.rb', line 94

def select_all
  @select_all
end

#where_clausesObject (readonly)

Returns the value of attribute where_clauses.



94
95
96
# File 'lib/exwiw/query_ast.rb', line 94

def where_clauses
  @where_clauses
end

Instance Method Details

#from(table) ⇒ Object



104
105
106
# File 'lib/exwiw/query_ast.rb', line 104

def from(table)
  @from_table_name = table
end

#join(join_clause) ⇒ Object



120
121
122
# File 'lib/exwiw/query_ast.rb', line 120

def join(join_clause)
  @join_clauses << join_clause
end

#select(columns) ⇒ Object



108
109
110
# File 'lib/exwiw/query_ast.rb', line 108

def select(columns)
  @columns = map_column_value(columns)
end

#select_all!Object



112
113
114
# File 'lib/exwiw/query_ast.rb', line 112

def select_all!
  @select_all = true
end

#to_hObject



124
125
126
127
128
129
130
131
# File 'lib/exwiw/query_ast.rb', line 124

def to_h
  {
    from: from_table_name,
    columns: select_all ? "*" : columns.map { |c| { name: c.name, value: c.value } },
    joins: join_clauses.map(&:to_h),
    where: where_clauses.map { |w| w.is_a?(String) ? w : w.to_h },
  }
end

#where(where_clause) ⇒ Object



116
117
118
# File 'lib/exwiw/query_ast.rb', line 116

def where(where_clause)
  @where_clauses << where_clause
end