Sha256: fc171ec1c7019773783ea11d116d0ea9ef2b0fe7d5eb6f4ad38f2b756f424cc1

Contents?: true

Size: 554 Bytes

Versions: 4

Compression:

Stored size: 554 Bytes

Contents

# frozen_string_literal: true

# Used to parse strings containing one or more SQL statements.
class SqlParser
  def self.find_statement_at_cursor(sql, cursor)
    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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sqlui-0.1.22 app/sql_parser.rb
sqlui-0.1.21 app/sql_parser.rb
sqlui-0.1.20 app/sql_parser.rb
sqlui-0.1.19 app/sql_parser.rb