db.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter database
  5. name:db.py
  6. author:rabin
  7. """
  8. from influxdb import InfluxDBClient
  9. import psycopg2
  10. class Influxdb(object):
  11. instance = None
  12. def __new__(cls, *args, **kwd):
  13. if Influxdb.instance is None:
  14. Influxdb.instance = object.__new__(cls, *args, **kwd)
  15. return Influxdb.instance
  16. def __init__(self, config):
  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. self.connect = psycopg2.connect(host=config['host'], port=config['port'], user=config['username'], password=config['password'], database=config['dbname'])
  32. def get(self):
  33. return self.connect
  34. def create(self, name):
  35. 'psql -U postgres'
  36. sql = 'CREATE DATABASE '+name+' WITH OWNER = postgres ENCODING = "UTF8"'
  37. return sql