django-orchestra-test/orchestra/utils/db.py

46 lines
1.4 KiB
Python
Raw Normal View History

2015-05-03 17:44:46 +00:00
from django import db
def close_connection(execute):
""" Threads have their own connection pool, closing it when finishing """
def wrapper(*args, **kwargs):
try:
log = execute(*args, **kwargs)
except Exception as e:
pass
else:
wrapper.log = log
finally:
db.connection.close()
return wrapper
def get_settings(settings_file):
""" get db settings from settings.py file without importing """
2015-05-03 19:07:16 +00:00
settings = {'__file__': settings_file}
with open(settings_file) as f:
__file__ = 'rata'
exec(f.read(), settings)
settings = settings['DATABASES']['default']
if settings['ENGINE'] not in ('django.db.backends.sqlite3', 'django.db.backends.postgresql_psycopg2'):
raise ValueError("%s engine not supported." % settings['ENGINE'])
return settings
2015-05-03 17:44:46 +00:00
def get_connection(settings):
2015-05-03 19:07:16 +00:00
if settings['ENGINE'] == 'django.db.backends.sqlite3':
import sqlite3
return sqlite3.connect(settings['NAME'])
elif settings['ENGINE'] == 'django.db.backends.postgresql_psycopg2':
import psycopg2
return psycopg2.connect("dbname='{NAME}' user='{USER}' host='{HOST}' password='{PASSWORD}'".format(**settings))
2015-05-03 17:44:46 +00:00
return conn
def run_query(conn, query):
cur = conn.cursor()
cur.execute(query)
result = cur.fetchall()
cur.close()
return result