Mysql.rb 551 B

12345678910111213141516171819202122232425262728293031323334
  1. require 'mysql2'
  2. require 'pathname'
  3. require Pathname.new(File.dirname(__FILE__)).realpath.to_s + '/Config'
  4. require 'singleton'
  5. class Mysql
  6. include Singleton
  7. @@client = nil
  8. def getClient()
  9. if !@@client
  10. @@client = Mysql2::Client.new(
  11. :host => Config::HOST,
  12. :username => Config::USER,
  13. :password => Config::PASSWORD,
  14. :database => Config::DBNAME,
  15. :encoding => Config::CHARSET
  16. )
  17. end
  18. return @@client
  19. end
  20. def close()
  21. if @@client
  22. @@client.close
  23. end
  24. @@client = nil
  25. end
  26. end
  27. END{
  28. Mysql.instance.close();
  29. }