db.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: utf-8 -*-
  2. """
  3. demeter
  4. name:db.py
  5. author:rabin
  6. """
  7. from demeter.core import *
  8. class Influxdb(object):
  9. """
  10. instance = None
  11. def __new__(cls, *args, **kwd):
  12. if Influxdb.instance is None:
  13. Influxdb.instance = object.__new__(cls, *args, **kwd)
  14. return Influxdb.instance
  15. """
  16. def __init__(self, config):
  17. influxdb = __import__('influxdb')
  18. InfluxDBClient = getattr(influxdb, 'InfluxDBClient')
  19. self.connect = InfluxDBClient(config['host'], config['port'], config['username'], config['password'], config['dbname'])
  20. self.create(config['dbname'])
  21. def get(self):
  22. return self.connect
  23. def create(self, name):
  24. database = self.connect.get_list_database()
  25. self.connect.create_database(name)
  26. class Postgresql(object):
  27. """
  28. instance = None
  29. def __new__(cls, *args, **kwd):
  30. if Postgresql.instance is None:
  31. Postgresql.instance = object.__new__(cls, *args, **kwd)
  32. return Postgresql.instance
  33. """
  34. def __init__(self, config):
  35. psycopg2 = __import__('psycopg2')
  36. self.create(config['dbname'])
  37. self.connect = psycopg2.connect(host=config['host'], port=config['port'], user=config['username'], password=config['password'], database=config['dbname'])
  38. def get(self):
  39. return self.connect
  40. def create(self, name):
  41. 'psql -U postgres'
  42. sql = 'CREATE DATABASE '+name+' WITH OWNER = postgres ENCODING = "UTF8"'
  43. if Demeter.runtime('postgresql', name, sql):
  44. Shell.popen('createdb -h localhost -p 5432 -U postgres ' + name)
  45. return True
  46. class Mysql(object):
  47. """
  48. instance = None
  49. def __new__(cls, *args, **kwd):
  50. if Mysql.instance is None:
  51. Mysql.instance = object.__new__(cls, *args, **kwd)
  52. return Mysql.instance
  53. """
  54. def __init__(self, config):
  55. pymysql = __import__('pymysql')
  56. self.connect = pymysql.connect(host=config['host'], port=int(config['port']), user=config['username'], password=config['password'], database=config['dbname'], charset=config['charset'])
  57. def get(self):
  58. return self.connect
  59. def create(self, name):
  60. sql = 'CREATE DATABASE IF NOT EXISTS '+name+' DEFAULT CHARSET utf8 COLLATE utf8_general_ci'
  61. if not Demeter.runtime('mysql', name, sql):
  62. self.connect.cursor().execute(sql)
  63. return sql
  64. class Sqlite(object):
  65. """
  66. instance = None
  67. def __new__(cls, *args, **kwd):
  68. if Sqlite.instance is None:
  69. Sqlite.instance = object.__new__(cls, *args, **kwd)
  70. return Sqlite.instance
  71. """
  72. def __init__(self, config):
  73. sqlite3 = __import__('sqlite3')
  74. self.connect = sqlite3.connect(config['file'])
  75. # 插入中文问题
  76. self.connect.text_factory = str
  77. def get(self):
  78. return self.connect
  79. def create(self, name):
  80. sql = 'CREATE DATABASE IF NOT EXISTS '+name+' DEFAULT CHARSET utf8 COLLATE utf8_general_ci'
  81. if not Demeter.runtime('sqlite', name, sql):
  82. self.connect.cursor().execute(sql)
  83. return sql