class SqlParser

Attributes

to_a[R]

Public Class Methods

new(raw_lines) click to toggle source
# File lib/sqlparser.rb, line 11
def initialize(raw_lines)

  patterns = [
    [:root, /SELECT \*/, :select_all],
    [:root, /SELECT (?<cols>.*)/, :select],
    [:root, /FROM (?<tables>.*)/, :from],
    [:root, /WHERE (?<condition>.*)/, :where],
      [:where, 'AND :and', :where_and ],
      [:where, 'OR :or', :where_or ],
    [:root, 'INSERT INTO *', :insert_into],
      [:root, 'VALUES :cols', :insert_values],
    [:all, /--/, :comment]
  ]

  lp = LineParser.new patterns

  lines = raw_lines\
       .sub(/\bselect\s+/,'SELECT ')\
       .sub(/\s+from\s+/,"\nFROM ")\
       .sub(/\s+where\s+/,"\nWHERE ")\
       .sub(/\s+and\s+/,"\n  AND ").sub(/\s+or\s+/,"\n  OR ")    
  
  @to_a = lp.parse lines
end