Class: CalculableAttrs::Utils::SqlParser

Inherits:
Object
  • Object
show all
Defined in:
lib/calculable_attrs/utils/sql_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(sql) ⇒ SqlParser

Returns a new instance of SqlParser.



2
3
4
5
# File 'lib/calculable_attrs/utils/sql_parser.rb', line 2

def initialize(sql)
  @sql = sql
  @masked_sql = mask_sql(sql)
end

Instance Method Details

#first_select_snippetObject



7
8
9
10
11
# File 'lib/calculable_attrs/utils/sql_parser.rb', line 7

def first_select_snippet
  match = @masked_sql.match(/SELECT (?<select>.*?) FROM/)
  from, to = *match.offset(:select)
  @sql[from..to-1]
end

#last_where_snippetObject



13
14
15
16
17
18
19
20
# File 'lib/calculable_attrs/utils/sql_parser.rb', line 13

def last_where_snippet
  last_where_start_at = @masked_sql.rindex('WHERE')
  return unless last_where_start_at
  masted_sql_starting_with_last_where = @masked_sql[last_where_start_at..-1]
  match = masted_sql_starting_with_last_where.match(/WHERE (?<where>.*?)( GROUP| ORDER| LIMIT| OFFSET|$)/)
  from, to = *match.offset(:where)
  @sql[last_where_start_at + from..last_where_start_at+to-1]
end