Hi Richard,
It's been a while since I tried to wrap my head around the MVC pattern,
but I think that this -- or a recommended better alternative -- is needed
for an application I'm developing. I would like clarification of my
understanding of MVC and advice on how to achieve what I need.
There will be two versions of the application: a single-user version with
a SQLite3 back end and a multi-user version with a PostgreSQL back end; the
'Model' in the MVC pattern. Both versions use the same wxPython UI code, the
'View' in the MVC. The Python middleware (the 'Controller') requires two
versions with the same basic structure but one using embedded SQLite3 syntax
and pysqlite, the other using SQLAlchemy and psycopg.
Why not use SQLAlchemy for both and just change the engine accordingly? I am in the progress of changing my application to use either Firebird SQL or PostgresSQL and have a config method which looks like this:
def setupDBURL(self):
"""Setup the SQLAlchemy URL to be used.
supported ones are:
'firebird+fdb'
'postgresql+psycopg2'
"""
self.dbEngineType = None
if "+fdb" in self.saEngine:
# TODO: at some point following needs to be stored externaly
self.dbRuser = "SYSDBA"
self.dbRpw = "somepassword"
self.dbEngineType = "fdb"
self.dburl = saURL(self.saEngine,
username=self.dbRuser,
password=self.dbRpw,
host=self.dataHost,
port=self.dataPort,
database=self.databaseName)
self.dbEngine = sa.create_engine(self.dburl, encoding='utf8',
echo=self.salog,
connect_args={'charset': 'utf8'})
elif "+psycopg2":
self.dbRuser = "postgres"
self.dbRpw = "somepassword"
self.dbEngineType = "psycopg2"
self.dburl = saURL(self.saEngine,
username=self.dbRuser,
password=self.dbRpw,
host=self.dataHost,
port=self.dataPort,
database=self.databaseName)
self.dbEngine = sa.create_engine(self.dburl, encoding='utf8',
echo=self.salog)
If the database is created with SQLAlchemy and you use such things as triggers and stored procedures you need to create two versions where needed and create them along these lines, here a simple DOMAIN for a memo field.
# meta data
meta_memo = DDL("""CREATE DOMAIN MEMO AS BLOB SUB_TYPE 1 SEGMENT SIZE 80 CHARACTER SET UTF8""")
meta_memo_PG = DDL("""CREATE DOMAIN MEMO AS TEXT""")
event.listen(dbCurrent.metadata, 'before_create',
meta_memo.execute_if(dialect='firebird'))
event.listen(dbCurrent.metadata, 'before_create',
meta_memo_PG.execute_if(dialect='postgresql'))
# similar for triggers and sp's
and then you call:
db.metadata.create_all(engine)
Werner
···
On 2/15/2015 1:46, Rich Shepard wrote: