1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2026-11-04 02:21:33 +01:00

Nettoyage et mise en conformité PEP 8 de sqlutils.py

This commit is contained in:
Roland Haroutiounian 2015-07-02 15:58:41 +02:00
commit b774d33fae

View file

@ -9,21 +9,22 @@ from django.conf import settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
ENGINES = {'mysql': {
'driver': 'pymysql',
'encoding': 'utf8'
},
'postgresql': {
'driver': 'psycopg2',
'encoding': 'utf8',
},
'sqlite': {
'driver': 'pysqlite',
'encoding': 'utf8',
},
ENGINES = {
'mysql': {
'driver': 'pymysql',
'encoding': 'utf8'
},
'postgresql': {
'driver': 'psycopg2',
'encoding': 'utf8',
},
'sqlite': {
'driver': 'pysqlite',
'encoding': 'utf8',
},
}
sqlcfg = settings.LODEL2SQLWRAPPER
sql_config = settings.LODEL2SQLWRAPPER
## Returns an engine given dbconf name
@ -31,20 +32,20 @@ sqlcfg = settings.LODEL2SQLWRAPPER
# @param ename str: name of an item in django.conf.settings.LODEL2SQLWRAPPER['db']
# @param sqlaloggin None|bool: If None leave default value, if true activate sqlalchemy logging
# @return SqlAlchemy engine
def get_engine(ename='default',sqlalogging=None):
def get_engine(ename='default', sqlalogging=None):
# Loading confs
cfg = sqlcfg['db'][ename]
cfg = sql_config['db'][ename]
edata = ENGINES[cfg['ENGINE']] # engine infos
edata = ENGINES[cfg['ENGINE']] # engine infos
conn_str = ""
if cfg['ENGINE'] =='sqlite':
if cfg['ENGINE'] == 'sqlite':
#SQLite connection string
conn_str = '%s+%s:///%s' % (cfg['ENGINE'], edata['driver'], cfg['NAME'])
else:
#MySQL and PostgreSQL connection string
user = cfg['USER']
user += (':'+cfg['PASSWORD'] if 'PASSWORD' in cfg else '')
user += (':' + cfg['PASSWORD'] if 'PASSWORD' in cfg else '')
if 'HOST' not in cfg:
logger.info('Not HOST in configuration, using localhost')
@ -52,12 +53,12 @@ def get_engine(ename='default',sqlalogging=None):
else:
host = cfg['HOST']
host += (':'+cfg['PORT'] if 'PORT' in cfg else '')
host += (':' + cfg['PORT'] if 'PORT' in cfg else '')
conn_str = '%s+%s://' % (cfg['ENGINE'], edata['driver'])
conn_str += '%s@%s/%s' % (user, host, cfg['NAME'])
ret = sqla.create_engine(conn_str, encoding=edata['encoding'], echo=sqlalogging)
logger.debug("Getting engine :"+str(ret))
logger.debug("Getting engine :" + str(ret))
return ret
@ -65,6 +66,7 @@ def get_engine(ename='default',sqlalogging=None):
def getEngine(ename='default', sqlalogging=None):
return get_engine(ename=ename, sqlalogging=sqlalogging)
## Return a sqlalchemy.MetaData object
# @param engine sqlalchemy.engine : A sqlalchemy engine
# @return an sql alechemy MetaData instance bind to engine
@ -73,18 +75,23 @@ def meta(engine):
res.reflect(bind=engine)
return res
## Return an sqlalchemy table given an EmComponent child class
# @warning Except a class type not an instance
# @param cls : An EmComponent child class
# @return An sqlalchemy table
# @throw TypeError if em_instance is an EmComponent or not an EmComponent child class (or an instance)
def get_table(cls):
from EditorialModel.components import EmComponent #dirty circula inclusion hack
if not issubclass(cls, EmComponent) or cls.table == None:
raise TypeError("Excepting an EmComponent child class not an "+str(cls))
from EditorialModel.components import EmComponent # dirty circula inclusion hack
if not issubclass(cls, EmComponent) or cls.table is None:
raise TypeError("Excepting an EmComponent child class not an " + str(cls))
engine = cls.db_engine()
return sqla.Table(cls.table, meta(engine))
def getTable(cls): return get_table(cls)
def getTable(cls):
return get_table(cls)
## This function is intended to execute ddl defined in sqlalter
# @warning There is a dirty workaround here, DDL should returns only one query, but DropColumn for sqlite has to return 4 queries (rename, create, insert, drop). There is a split on the compiled SQL to extract and execute one query at a time
@ -96,10 +103,9 @@ def ddl_execute(ddl, db_engine):
req = str(ddl.compile(dialect=db_engine.dialect))
queries = req.split(';')
for query in queries:
logger.debug("Executing custom raw SQL query : '"+query+"'")
logger.debug("Executing custom raw SQL query : '" + query + "'")
ret = conn.execute(query)
if not bool(ret):
return False
conn.close()
return True