Re: SQLAlchemy - z3c.sqlalchemy - collective.lead
robert rottermann <[email protected]>
| Newsgroups | gmane.comp.web.zope.german |
|---|---|
| Organization | redCOR AG |
| Message-ID | <[email protected]> |
Am 04.08.2010 16:20, schrieb floseries:
> robert r [via Plone (Regional forums)] schrieb:
>> salu fritz,
>> also ich habe vor einiger zeit eine reihe von projekten mit
>> collective.lead aufgegleist.
>> ich habe es aber mitlerweile bei allen wieser ad acta gelegt.
>>
>> collective.lead war einer der verschiedenen versuche, das transaction
>> management von zope und einer externen db unter einen hut zu bringen.
>> nach meinem verständnis, ist es in
>> z3c.saconfig
>> zope.sqlalchemy
>> aufgegangen.
>> ich jedenfalls benutze nur noch diese.
>> gruss
>> robert
> Hallo Robert,
>
> hat sich hierdurch etwas an der Zope-Python-Schnittstelle geändert ?
> Mußtest Du Änderungen an den Python-Skripten vornehmen, oder lief es
> sofort mit Austausch der Packages ?
ich weiss es nicht genau.
wie gesagt, wir haben das gegen z3c.saconfig ausgewechselt.
ich vermute, das collective.lead nicht mehr wirklich gepflegt wird.
hier ist ein beispiel einer datenbank "definition" mit zwei tabellen und
einer relation, wie wir es mit oben genannten duo brauchen.
das beispiel braucht mysql.
wenn du postgres nutzen willst, muss du nur im dbinfo "mysql" gegen
"postgres" austauschen.
gruss robert
der code lädt eine datei "dbinfo.py" mit den angaben zum datenbank treiber
dbinfo.py:
-------------
username = 'root'
pw = ''
host = 'localhost'
drivername = 'mysql'
database = 'cis'
model.py:
------------
(die ganzen try excepts sind da, weil ich das zeugs manchmal auch ohne
zope brauche.)
# -*- coding: utf-8 -*-
# sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy as sa
import os, sys
#from sqlalchemy import orm
from sqlalchemy.orm import backref, relationship, column_property, mapper
from sqlalchemy import Table as BT
# not in the tests directory but still we can be called by zopes test
framework
try:
from energiecluster.addresses import dbinfo
if os.environ.get('ZOPETESTCASE'):
p = os.path.split(__file__)[0]
sys.path.insert(0, '%s/tests' % p)
database = 'test'
except ImportError:
# hoppla schorsch
# maybe we are just called from a utility like transfer
try:
import dbinfo
except:
# gosh!!
# there is definitely no dbinfo to be found
raise ImportError("you must add a dbinfo !!!!")
info = {
'username' : dbinfo.username,
'pw' : dbinfo.pw,
'host' : dbinfo.host,
'drivername' : dbinfo.drivername,
'database' : dbinfo.database,
}
if info.get('pw'):
DSN="%(drivername)s://%(username)s:%(pw)s@%(host)s/%(database)s?charset=utf8"
% info
else:
DSN="%(drivername)s://%(username)s@%(host)s/%(database)s?charset=utf8" %
info
# we now have a DSN. with which we can create a global utility
try:
from z3c.saconfig import EngineFactory
from zope import component
# register global utilities
engine_factory = EngineFactory(DSN, pool_recycle=7200)
# not strictly neccessary
from z3c.saconfig.interfaces import IEngineFactory
component.provideUtility(engine_factory, provides=IEngineFactory)
engine = engine_factory()
# create a global session
from z3c.saconfig import GloballyScopedSession
utility = GloballyScopedSession(bind=engine) # i think, without
engine, it will find above provided one...
from z3c.saconfig.interfaces import IScopedSession
component.provideUtility(utility, provides=IScopedSession)
from z3c.saconfig import Session
session = Session()
except ImportError:
engine = sa.create_engine(DSN, echo=False, convert_unicode=True,
encoding="utf8")
from sqlalchemy.orm import scoped_session, sessionmaker, relation
Session = scoped_session(sessionmaker(bind=engine))
session = Session()
try:
from zope.sqlalchemy import mark_changed
import transaction
except ImportError:
def mark_changed(session):
pass
class Transaction(object):
def commit(self):
pass
transaction = Transaction()
Base = declarative_base(engine)
Base.metadata.reflect()
tables = Base.metadata.tables
# -----------------------------------------------------
# -----------------------------------------------------
#
"""
select * from
cisdata c,
branchen b,
cisbr cb
where c.ID_cis = cb.ID_cis and cb.ID_br = b.ID_br and b.br =
'Fotografie'
"""
class Cisdata(Base):
"""
extra attributes assigned by relations:
--------------------------------------
"""
#__allow_access_to_unprotected_subobjects__ = 1
__table__ = tables['cisdata']
branche = relationship(
'Cisdata',
secondary = tables['cisbr'],
primaryjoin =
tables['cisdata'].c.ID_cis==tables['cisbr'].c.ID_cisbr,
secondaryjoin = tables['branchen'].c.ID_br==tables['cisbr'].c.ID_br,
foreign_keys =
[tables['cisbr'].c.ID_cisbr,tables['branchen'].c.ID_br],
backref="firmen",
)
class Branchen(Base):
"""
extra attributes assigned by relations:
--------------------------------------
"""
__allow_access_to_unprotected_subobjects__ = 1
__table__ = tables['branchen']
_______________________________________________
zope mailing list
[email protected]
https://mail.dzug.org/mailman/listinfo/zope