Commit d4b6ab2a authored by Romain Bignon's avatar Romain Bignon

add reflect of database and create declarative classes

parent 77fc0b3a
import builtins
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.orm import sessionmaker, scoped_session, mapper
from sqlalchemy.schema import MetaData
#import pdb
class BaseTable(object):
pass
class Database(object):
......@@ -7,3 +15,18 @@ class Database(object):
self.url = url
self.engine = create_engine(url, pool_recycle=3600)
self.Session = scoped_session(sessionmaker(bind=self.engine))
builtins.Session = self.Session
self.reflect()
def reflect(self):
metadata = MetaData(self.engine)
metadata.reflect(self.engine)
for name, table in metadata.tables.items():
tableClass = type(str(table.fullname), (BaseTable,), {})
try:
mapper(tableClass, table)
except:
continue
else:
setattr(builtins, name, tableClass)
#pdb.set_trace()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment