Skip to content

Instantly share code, notes, and snippets.

@baroquebobcat
Last active December 23, 2015 15:39
Show Gist options
  • Select an option

  • Save baroquebobcat/6656558 to your computer and use it in GitHub Desktop.

Select an option

Save baroquebobcat/6656558 to your computer and use it in GitHub Desktop.
require 'csv'
module SQLAwesome
class RDBMS
def self.new_from_csv_dir directory
tables = {}
Dir["#{directory.chomp("/")}/*.csv"].each do |file|
table = []
CSV.foreach(file, headers: true) { |row| table << row }
tables[file.split("/").last.chomp(".csv")] = table
end
new tables
end
def initialize table_hash
@tables= table_hash
end
def eval sql_str
raw_ast = Parser.new.parse program
model = Transformer.new.apply raw_ast
model.eval self
end
def [](table)
@tables[table]
end
end
end
end
describe SQLAwesome do
  it "retrieves all columns for all rows with a wildcard" do
    db = SQLAwesome::RDBMS.new_from_csv_dir File.dirname(__FILE__)+"/../data/"
    result = db.eval "SELECT * FROM one_to_five"
    result.each {|row| row.keys.must_equal ["dec", "eng"]
  end
end

B)

describe SQLAwesome do
  it "retrieves all columns for all rows with a wildcard" do
    result = SQLAwesome.eval "SELECT * FROM one_to_five",
                             database: build_database(File.dirname(__FILE__)+"/../data/")
    
    result.each {|row| row.keys.must_equal ["dec", "eng"]
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment