karideo/back/src/db.py

35 lines
827 B
Python
Raw Normal View History

2020-02-20 22:52:58 +01:00
from realog import debug
import psycopg2
2020-03-10 22:32:47 +01:00
import config
2020-02-20 22:52:58 +01:00
2020-02-24 07:50:04 +01:00
connection = None
connection_count = 0
2020-02-20 22:52:58 +01:00
def connect_bdd():
2020-02-24 07:50:04 +01:00
global connection
global connection_count
if connection == None:
debug.info("connect BDD: ")
2020-03-10 22:32:47 +01:00
conf = config.get_rest_config()
connection = psycopg2.connect(dbname=conf["db_name"], user=conf["db_user"], password=conf["db_password"], host=conf["db_host"], port=conf["db_port"])
2020-02-24 07:50:04 +01:00
connection_count += 1
return connection
2020-02-20 22:52:58 +01:00
2020-02-24 07:50:04 +01:00
def remove_connection():
global connection
global connection_count
connection_count -= 1
if connection_count < 0:
debug.warning("Request remove too much time the BDD connection");
connection_count = 0;
return;
if connection_count == 0:
debug.warning("dicconnect BDD");
connection.commit()
connection.close()
connection = None
return;
2020-02-20 22:52:58 +01:00
base_bdd_name = "karideo_"