Class: SqlParser

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

Instance Method Summary collapse

Instance Method Details

#case_sensitive_parseObject



8
# File 'lib/sql_munger/sql_parser.rb', line 8

alias_method :case_sensitive_parse, :parse

#dump(sql) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sql_munger/sql_parser.rb', line 32

def dump( sql )
  root = parse( sql )
  root.statements.map do |table|
    if table.respond_to? :name
      puts table.name
      columns = %w{name sql_type length precision scale}
      table.fields.each do |field|
        puts "  " + columns.map{|x| field.send(x) }.join(', ')
      end
    else
      puts table.text_value
    end
  end
  puts
end

#parse(st, *args) ⇒ Object

Allow for case-insensitive parsing. Convert to downcase, then parse, then replace the string with the original version. The numeric indices will still point to the right places.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sql_munger/sql_parser.rb', line 15

def parse( st, *args )
  ds = st.downcase
  begin
    root = case_sensitive_parse( ds, *args )
  ensure
    ds.replace st
  end
  
  # raise exception on failure
  # or return the root of the parse tree on success
  if root.nil?
    raise "SQL error at #{failure_line}:#{failure_column}. #{failure_reason}"
  else
    root
  end
end