Skip to content
Snippets Groups Projects
Commit 9c1f3cc0 authored by Simon Künzel's avatar Simon Künzel
Browse files

Add postgres adapter

parent a24b07f0
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,13 @@ DB_DATA = 'db_example.sql' ...@@ -16,6 +16,13 @@ DB_DATA = 'db_example.sql'
#MYSQL_PASSWD = 'somuchsecret' #MYSQL_PASSWD = 'somuchsecret'
#MYSQL_DB = 'videos' #MYSQL_DB = 'videos'
#DB_ENGINE = 'postgres'
POSTGRES_HOST = '10.0.0.101'
POSTGRES_PORT = 5432
POSTGRES_USER = 'videoag'
POSTGRES_PASSWORD = ''
POSTGRES_DATABASE = 'videoag'
DB_ENGINE = 'sqlite' DB_ENGINE = 'sqlite'
SQLITE_DB = 'db.sqlite' SQLITE_DB = 'db.sqlite'
SQLITE_INIT_SCHEMA = True SQLITE_INIT_SCHEMA = True
......
...@@ -90,6 +90,30 @@ elif config['DB_ENGINE'] == 'mysql': ...@@ -90,6 +90,30 @@ elif config['DB_ENGINE'] == 'mysql':
cur.close() cur.close()
db.close() db.close()
return res return res
elif config['DB_ENGINE'] == 'postgres':
import psycopg
def get_dbcursor():
if 'db' not in g or g.db.broken:
g.db = psycopg.Connection.connect(
host=config["POSTGRES_HOST"],
port=config["POSTGRES_PORT"],
user=config["POSTGRES_USER"],
password=config["POSTGRES_PASSWORD"],
dbname=config["POSTGRES_DATABASE"]
# TODO autocommit?
)
if not hasattr(request, 'db'):
request.db = g.db.cursor()
return request.db
def fix_query(operation, params):
operation = operation.replace('?', '%s')
params = [(p.replace(microsecond=0) if isinstance(p, datetime) else p) for p in params]
return operation, params
def show(operation, host=None): #pylint: disable=unused-argument
return {}
def query(operation, *params, delim="sep", nlfix=True): def query(operation, *params, delim="sep", nlfix=True):
operation, params = fix_query(operation, params) operation, params = fix_query(operation, params)
...@@ -131,11 +155,18 @@ def query(operation, *params, delim="sep", nlfix=True): ...@@ -131,11 +155,18 @@ def query(operation, *params, delim="sep", nlfix=True):
def modify(operation, *params, get_id=False): def modify(operation, *params, get_id=False):
operation, params = fix_query(operation, params) operation, params = fix_query(operation, params)
if get_id and config["DB_ENGINE"] == "postgres":
operation += " RETURNING id" # Not nice, but works for now
cur = get_dbcursor() cur = get_dbcursor()
cur.execute(operation, params) cur.execute(operation, params)
if not get_id: if not get_id:
return None return None
if config["DB_ENGINE"] != "postgres":
return cur.lastrowid return cur.lastrowid
all_res = cur.fetchall()
if len(all_res) <= 0:
raise ValueError("Got no id")
return int(all_res[0][0])
@app.teardown_request @app.teardown_request
def commit_db(*args): #pylint: disable=unused-argument def commit_db(*args): #pylint: disable=unused-argument
......
...@@ -9,3 +9,4 @@ ldap3 ...@@ -9,3 +9,4 @@ ldap3
icalendar icalendar
mysql-connector-python mysql-connector-python
coverage coverage
psycopg[c]
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment