db.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter database
  5. name:db.py
  6. author:rabin
  7. """
  8. class Influxdb(object):
  9. instance = None
  10. def __new__(cls, *args, **kwd):
  11. if Influxdb.instance is None:
  12. Influxdb.instance = object.__new__(cls, *args, **kwd)
  13. return Influxdb.instance
  14. def __init__(self, config):
  15. influxdb = __import__('influxdb')
  16. InfluxDBClient = getattr(influxdb, 'InfluxDBClient')
  17. self.connect = InfluxDBClient(config['host'], config['port'], config['username'], config['password'], config['dbname'])
  18. self.create(config['dbname'])
  19. def get(self):
  20. return self.connect
  21. def create(self, name):
  22. database = self.connect.get_list_database()
  23. self.connect.create_database(name)
  24. class Postgresql(object):
  25. instance = None
  26. def __new__(cls, *args, **kwd):
  27. if Postgresql.instance is None:
  28. Postgresql.instance = object.__new__(cls, *args, **kwd)
  29. return Postgresql.instance
  30. def __init__(self, config):
  31. psycopg2 = __import__('psycopg2')
  32. self.connect = psycopg2.connect(host=config['host'], port=config['port'], user=config['username'], password=config['password'], database=config['dbname'])
  33. def get(self):
  34. return self.connect
  35. def create(self, name):
  36. 'psql -U postgres'
  37. sql = 'CREATE DATABASE '+name+' WITH OWNER = postgres ENCODING = "UTF8"'
  38. return sql
  39. class Mysql(object):
  40. instance = None
  41. def __new__(cls, *args, **kwd):
  42. if Mysql.instance is None:
  43. Mysql.instance = object.__new__(cls, *args, **kwd)
  44. return Mysql.instance
  45. def __init__(self, config):
  46. pymysql = __import__('pymysql')
  47. self.connect = pymysql.connect(host=config['host'], port=int(config['port']), user=config['username'], password=config['password'], database=config['dbname'])
  48. def get(self):
  49. return self.connect
  50. def create(self, name):
  51. sql = 'CREATE DATABASE '+name+' ENCODING = "UTF8"'
  52. return sql