Guest User

Untitled

a guest
Apr 16th, 2018
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. require 'rubygems'
  2. require 'mysql'
  3. require 'date'
  4.  
  5. origin = Date.parse('2006-01-01')
  6.  
  7. db = Mysql::new('localhost', 'rich')
  8.  
  9. begin
  10. db.query(%Q{select 1 from sites.dates})
  11. rescue Mysql::Error
  12. create_ddl = <<-SQL
  13. create table sites.dates (
  14. sql_date date not null,
  15. cal_year int not null,
  16. cal_month int not null,
  17. cal_week int not null,
  18. cal_day_in_month int not null,
  19. cal_day_in_year int not null,
  20. cal_day_in_week int not null,
  21. cal_year_month varchar(10) not null
  22. );
  23. SQL
  24. db.query(create_ddl)
  25. end
  26.  
  27. db.query('begin')
  28. 0.upto(5000) do |i|
  29. d = (origin + i).to_s
  30. sql = <<-SQL
  31. insert into sites.dates (sql_date, cal_year, cal_month, cal_week, cal_day_in_month, cal_day_in_year, cal_day_in_week, cal_year_month) values ('#{d}', extract(year from '#{d}'), extract(month from '#{d}'), extract(week from '#{d}'), extract(day from '#{d}'), dayofmonth('#{d}'), dayofweek('#{d}'), extract(year_month from '#{d}'));
  32. SQL
  33. db.query(sql)
  34. end
  35. db.query('commit')
Add Comment
Please, Sign In to add comment