Class: SqlParser

Inherits:
Object
  • Object
show all
Defined in:
app/sql_parser.rb

Overview

Used to parse strings containing one or more SQL statements.

Class Method Summary collapse

Class Method Details

.find_statement_at_cursor(sql, cursor) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/sql_parser.rb', line 5

def self.find_statement_at_cursor(sql, cursor)
  return sql unless sql.include?(';')

  parts_with_ranges = []
  sql.scan(/[^;]*;[ \n]*/) { |part| parts_with_ranges << [part, 0, part.size] }
  parts_with_ranges.inject(0) do |pos, current|
    current[1] += pos
    current[2] += pos
  end
  part_with_range = parts_with_ranges.find do |current|
    cursor >= current[1] && cursor < current[2]
  end || parts_with_ranges[-1]
  part_with_range[0].strip
end