Skip to content

Instantly share code, notes, and snippets.

@charles-l
Created September 23, 2015 15:35
Show Gist options
  • Save charles-l/b83b0c965bcd0ca2fdbd to your computer and use it in GitHub Desktop.
Save charles-l/b83b0c965bcd0ca2fdbd to your computer and use it in GitHub Desktop.
SQL generation for school project
# Ruby, Generate sql PLZ
require 'faker' # faker gem used to generate names/addresses/phone numbers
def gen_sql(tname, v)
id = 1
10.times do
s = "INSERT INTO #{tname} (#{v.keys.join(",")}) VALUES ("
o = []
v.values.each do |a|
if a == :id
o << id
id += 1
elsif a == :date
a = Faker::Date.between(Date.new(1980,1,1), Date.new(1999,1,1)).to_s.gsub("-", "/")
o << a
elsif a == :phonenumber
a = Faker::PhoneNumber.phone_number.gsub(/\D/, "")[0..10].to_i
o << a
elsif a == :address
a = Faker::Address.street_address
o << a
elsif a == :city
a = Faker::Address.city
o << a
elsif a == :state
a = Faker::Address.state_abbr
o << a
elsif a == :zip
a = Faker::Address.zip
o << a
elsif a == :firstname
a = Faker::Name.first_name
o << a
elsif a == :lastname
a = Faker::Name.last_name
o << a
elsif a.include? "%"
stringify = false
if a.include? "s" then stringify = true end
a = a.split(//).map do |e|
if e == "#"
(rand(8)+1).to_s
elsif e == "z"
rand(9).to_s
elsif e == "%" || e == "s"
""
end
end.join
unless stringify then a = a.to_i end
o << a
elsif a.include? ","
a = a.split(",").sample.strip
o << a
end
end
s += o.map {|k| k.class == String ? k = "\"#{k}\"" : k.to_s}.join(",")
s+= ");"
puts s
end
end
gen_sql("loc",
loc_id: :id,
bldg_code: "CR,BUS,LIB",
room: "s%###",
capacity: "%zzz"
)
gen_sql("faculty",
f_id: :id,
f_pid: "s%#zzz",
f_last: :lastname,
f_first: :firstname,
f_mi: ('A'..'Z').to_a.join(","),
loc_id: "%zz",
f_phone: :phonenumber,
f_rank: "associate,full",
f_super: "%z",
)
gen_sql("student",
s_id: :id,
s_last: :lastname,
s_first: :firstname,
s_mi: ('A'..'Z').to_a.join(","),
s_address: :address,
s_city: :city,
s_state: :state,
s_zip: :zip,
)
gen_sql("term",
term_id: :id,
term_desc: "Fall 2005, Spring 2006, Summer 2006, Spring 2007, Summer 2007",
status: "CLOSED,OPEN",
start_date: :date
)
gen_sql("course_section",
c_sec_id: :id,
course_id: "%z",
term_id: "%z",
sec_num: "%z",
max_enrl: "%zz",
f_id: "%z",
c_sec_day: "MW,TR,F,MWF,MTWRF",
c_sec_time: "10:00 AM, 11:00 AM, 12:00 PM, 1:00 PM, 2:00 PM, 3:00 PM, 4:00 PM",
loc_id: "%z",
)
gen_sql("enrollment",
s_id: :id,
c_sec_id: "%z",
grade: ('A'..'F').to_a.join(','),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment