Sha256: 79e2adf1978a7864a5aac7332dbf98ac61f7197bd5318e95e9966345e7b56e46
Contents?: true
Size: 595 Bytes
Versions: 25
Compression:
Stored size: 595 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) 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 end
Version data entries
25 entries across 25 versions & 1 rubygems