Skip to content

Instantly share code, notes, and snippets.

@d0ivanov
Created April 1, 2014 17:26
Show Gist options
  • Save d0ivanov/9918871 to your computer and use it in GitHub Desktop.
Save d0ivanov/9918871 to your computer and use it in GitHub Desktop.
#!/home/dobromir/.rvm/rubies/ruby-2.1.1/bin/ruby
#====================================================================================
#==============================HERE BE DRAGONS=======================================
#
#
# __----~~~~~~~~~~~------___
# . . ~~//====...... __--~ ~~
# -. \_|// |||\\ ~~~~~~::::... /~
# ___-==_ _-~o~ \/ ||| \\ _/~~-
# __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~
# _-~~ .=~ | \\-_ '-~7 /- / || \ /
# .~ .~ | \\ -_ / /- / || \ /
# / ____ / | \\ ~-_/ /|- _/ .|| \ /
# |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\
# ' ~-| /| |-~\~~ __--~~
# |-~~-_/ | | ~\_ _-~ /\
# / \ \__ \/~ \__
# _--~ _/ | .-~~____--~-/ ~~==.
# ((->/~ '.|||' -_| ~~-/ , . _||
# -_ ~\ ~~---l__i__i__i--~~_/
# _-~-__ ~) \--______________--~~
# //.-~~~-~_--~- |-------~~~~~~~~
# //.-~~~--\
#
#
#
#====================================================================================
module Solution
USERNAME = ""
PASSWORD = ""
class Task
attr_accessor :tables
def initialize
@problem = ARGF.read
@tables = []
end
def parse
@problem.each_line do |line|
if (match = parse_table line)
@tables << Table.new(match["table"])
end
if (match = parse_column line)
@tables.last.fields[match["name"]] = match["type"]
end
if (conn = parse_connections line)
@tables.each do |table|
if table.name == conn["ltable"]
table.connections = conn
end
end
end
end
self
end
private
def parse_table string
/table\s(?<table>\w+)/.match(string)
end
def parse_column definition
/(?<name>\w+):(?<type>\w+)/.match(definition)
end
def parse_connections connections
pattern = /(?<ltable>\w+)\shas\sa\s(?<type>
(many\sto\smany|one\sto\sone|many\sto\sone|one\sto\smany))\s
connection\sto\s(?<rtable>\w+)
/x
pattern.match(connections)
end
end
class Database
attr_accessor :name
def initialize name
@name = name
end
def create
"CREATE DATABASE `#{@name}` DEFAULT CHARSET UTF8;\nUSE #{@name};"
end
def export
`mysqldump -u#{USERNAME} -p#{PASSWORD} #{@name} > export.sql`
end
end
class Table
attr_accessor :name, :fields, :connections
def initialize name
@name = name
@fields = {}
@connections = {}
end
def connections= conn
@connections[conn["type"]] = [] if [email protected]_key? conn["type"]
@connections[conn["type"]] << conn["rtable"]
end
def create
sql = "CREATE TABLE `#{name.downcase}` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,"
fields.each {|field, type| sql += "\n\t`#{field}` #{type.upcase},"}
sql += "\n\tPRIMARY KEY (`id`)"
sql += "\n);"
sql
end
def connect
sql = ""
@connections.each do |connection, tables|
tables.each do |table|
sql += send("#{connection.gsub(" ", "_")}", table)
end
end
sql
end
def insert rows = 2
sql = "INSERT INTO #{@name.downcase} (`id`"
@fields.keys.map do |field|
sql += ",`#{field}`"
end
sql += ") VALUES\n\t"
rows.times do |i|
sql += "(''"
@fields.keys.count.times do
sql += ",''"
end
sql += "),\n\t" if i != rows - 1
sql += ");" if i == rows - 1
end
sql
end
private
def many_to_many table
"CREATE TABLE `#{table.downcase}_#{@name.downcase}` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`#{@name.downcase}_id` INT UNSIGNED NOT NULL,
`#{table.downcase}_id` INT UNSIGNED NOT NULL,
PRIMARY KEY(`id`)
);"
end
def one_to_one table
"ALTER TABLE `#{@name.downcase}` ADD FOREIGN KEY (id) REFERENCES #{table.downcase}(id);"
end
def many_to_one table
"ALTER TABLE `#{@name.downcase}` ADD COLUMN #{table.downcase}_id INT UNSIGNED NOT NULL;"
end
def one_to_many table
"ALTER TABLE `#{table.downcase}` ADD COLUMN #{@name.downcase}_id INT UNSIGNED NOT NULL;"
end
end
end
#Parse our task
tables = Solution::Task.new.parse.tables
db = Solution::Database.new("exam")
#Create a database with which we will be working
puts db.create
#Create the tables
tables.each do |table|
puts table.create
end
#Crete table connections
tables.each do |table|
puts table.connect
end
#Insert some data
tables.each do |table|
puts table.insert
end
#Export all we have done
#db.export
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment