Class: QueryHelper::SqlManipulator

Inherits:
Object
  • Object
show all
Defined in:
lib/query_helper/sql_manipulator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sql:, where_clauses: nil, having_clauses: nil, qualify_clauses: nil, order_by_clauses: nil, include_limit_clause: false, additional_select_clauses: []) ⇒ SqlManipulator

Returns a new instance of SqlManipulator.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/query_helper/sql_manipulator.rb', line 8

def initialize(
  sql:,
  where_clauses: nil,
  having_clauses: nil,
  qualify_clauses: nil,
  order_by_clauses: nil,
  include_limit_clause: false,
  additional_select_clauses: []
)
  sql = remove_qualified_count(sql)
  @parser = SqlParser.new(sql)
  @sql = @parser.sql.dup
  @where_clauses = where_clauses
  @having_clauses = having_clauses
  @qualify_clauses = qualify_clauses
  @order_by_clauses = order_by_clauses
  @include_limit_clause = include_limit_clause
  @additional_select_clauses = additional_select_clauses
end

Instance Attribute Details

#sqlObject

Returns the value of attribute sql.



6
7
8
# File 'lib/query_helper/sql_manipulator.rb', line 6

def sql
  @sql
end

Instance Method Details

#buildObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/query_helper/sql_manipulator.rb', line 28

def build
  insert_qualify_clauses()
  insert_having_clauses()
  insert_where_clauses()
  insert_select_clauses()
  insert_order_by_and_limit_clause()
  insert_qualified_count_clauses if (qualify_clause_applicable? || @qualify_present) && @include_limit_clause

  @sql.squish
end