module ToArel

Constants

VERSION

Public Class Methods

create_select_manager(ast) click to toggle source
# File lib/to_arel.rb, line 35
def self.create_select_manager(ast)
  table = ast.fetch('fromClause')
  raise 'dunno how to handle multiple tables' if table.length > 1

  table = table.first

  table = Arel::Table.new(table.fetch('RangeVar').fetch('relname'))

  Arel::SelectManager.new table
end
manager_from_statement(statement) click to toggle source
# File lib/to_arel.rb, line 23
def self.manager_from_statement(statement)
  type = statement.keys.first
  ast = statement[type]

  case type
  when 'SelectStmt'
    create_select_manager(ast)
  else
    raise "unknown statement type `#{type}`"
  end
end
parse(sql) click to toggle source
# File lib/to_arel.rb, line 9
def self.parse(sql)
  tree = PgQuery.parse(sql).tree

  raise 'cannot process more than 1 statement' if tree.length > 1

  statement = tree.first
    .fetch('RawStmt')
    .fetch('stmt')

  raise 'dunno how to handle more than 1 statement' if statement.keys.length > 1

  manager_from_statement statement
end