class CSQL::SQL

Public Class Methods

new(filepath) click to toggle source
# File lib/csql.rb, line 8
def initialize(filepath)
  @parser = SQLParser::Parser.new
  @filepath = filepath
end

Public Instance Methods

execute(query) click to toggle source
# File lib/csql.rb, line 13
def execute query
  begin
    modified_query = query.gsub(/csv/, @filepath)
    result,err,process = Open3.capture3("q -H -d \',\' \'#{modified_query}\'")
    if err != ""
      raise CSQLException.new(err)
    end
  end
  ast = @parser.scan_str(query)
  column = ast.query_expression.list.to_sql
  columns = nil
  if column == "*"
    columns = File.open(@filepath,'r').gets.chomp.split(',').map{|c|c.strip}
  else
    columns = column.chomp.split(',').map{|c|c.gsub('`','').split(/as|AS/).last.strip}
  end
  return result.chomp.split("\n").map{|r|
    data = r.split(",")
    hash = Hash.new
    data.size.times do |i|
      hash[columns[i]] = data[i]
    end
    hash
  }
end