1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2026-03-19 01:32:01 +01:00

Small bugfixes in sqlobject and sqlwrapper

This commit is contained in:
Yann 2015-06-03 09:30:12 +02:00
commit 9896c16e8e
3 changed files with 20 additions and 17 deletions

View file

@ -27,11 +27,17 @@ class SqlObject(object):
"""
def __init__(self, tname):
self.tname = tname
if not type(tname) is str:
logger.error("Unable to instanciate, bad argument...")
raise TypeError('Excepted a string not a '+str(type(tname)))
@property
def table(self):
return sql.Table(self.tname, sql.MetaData())
self.tname = tname
self.table = self.Table()
pass
def Table(self):
self.table = sql.Table(self.tname, sql.MetaData(), autoload_with=SqlWrapper.rengine, autoload=True)
return self.table
@property
def col(self):
@ -39,7 +45,7 @@ class SqlObject(object):
@property
def sel(self):
return sql.select(self.table)
return sql.select([self.table])
@property
def where(self):
@ -51,11 +57,11 @@ class SqlObject(object):
@property
def rconn(self):
return SqlWrapper.rconn()
return SqlWrapper.rc()
@property
def wconn(self):
return SqlWrapper.wconn()
return SqlWrapper.wc()
def sFetchAll(self, sel):
return self.rexec(sel).fetchall()

View file

@ -46,13 +46,14 @@ class SqlWrapper(object):
pass
@classmethod
def table(c, o):
def table(c, tname):
""" Return a SqlAlchemy Table object
@param o str: Table name
@return a SqlAlchemy Table instance
"""
if isinstance(o, str):
return sqla.Table(o, sqla.MetaData())
return None
if not isinstance(tname, str):
raise TypeError("Excepting a str but got a "+str(type(name)))
return sqla.Table(o, sqla.MetaData())
@classmethod
def connect(c,read = None):
@ -75,7 +76,7 @@ class SqlWrapper(object):
if res == None:
if not c.connect(read):
raise RuntimeError('Unable to connect to Db')
return self.conn(read)
return c.conn(read)
return c.rconn

View file

@ -37,18 +37,14 @@ class EmComponent(object):
def populate(self):
dbo = SqlObject(self.table)
t = dbo.table
req = dbo.sel
print(t.c.__dict__)
if self.id is None:
req.where(t.c.name == self.name)
req.where(dbo.col.name == self.name)
else:
req.where(dbo.col.id == self.id)
sqlresult = dbo.rexec(req)
print (sqlresult)
# Transformation du résultat en une liste de dictionnaires
records = sqlresult.fetchall()