Hi,
Having seen Nil's elexir approach I had another go at this problem.
I wanted a solution which is usable from non Python tools accessing the
database, so I came up with:
e.g. for countries:
- Country/countries - class and table with the default language values
and everything else for countries
- Country_L/countries_l - class and table with the localized column
information
- countries_lp - a stored procedure which does the localization/default
value stuff (uses some Firebird SQL specific code, which could probably
be changed to be more generic)
- Country_LV/countries_lv - class and database view, the select of the
view is using the stored procedure
Using it looks like this:
# set localization to "DE_de"
session.execute("select rdb$set_context('USER_SESSION', 'LANG_CODE',
'DE_de')from rdb$database").fetchone()
session.commit()
print '---- base table ----'
result = session.query(db.Country)
for item in result:
print item.name
print '---- localize for "DE_de" ----'
result = session.query(db.Country_LV)
for item in result:
print item.name
# set localization to "FR_fr"
session.execute("select rdb$set_context('USER_SESSION', 'LANG_CODE',
'FR_fr')from rdb$database").fetchone()
session.commit()
print '---- localize for "FR_fr" - which is getting default value for
e.g. "France" and "Germany" ----'
result = session.query(db.Country_LV)
for item in result:
print item.name
which results in:
---- base table ----
France
Switzerland
Germany
---- localize for "DE_de" ----
Frankreich
Schweiz
Deutschland
---- localize for "FR_fr" - which is getting default value for e.g.
"France" and "Germany" ----
France
Suisse
Germany
The SA model for this looks like this:
class Country(Base, CreateUpdateMixin):
__tablename__ = u'countries'
id = sa.Column(sa.BigInteger(), sa.Sequence('countries_id'),
primary_key=True, nullable=False)
name = sa.Column(sa.String(length=30, convert_unicode=False))
iso2 = sa.Column(sa.String(length=2, convert_unicode=False))
iso3 = sa.Column(sa.String(length=3, convert_unicode=False))
telcode = sa.Column(sa.SmallInteger())
__localize_columns__ = ['name', ]
class Country_L(Base):
__table__ = sautils.make_localize_table(Country(), 'countries_l',
metadata)
class Country_LV(Base):
__table__ = sautils.make_localize_view(Country(), 'countries_lv',
metadata)
I am sure that this could be done even nicer/better and that it could be
done in a way that would be compatible with "meta.drop_all(engine) and
meta.create_all(engine)", currently I need to create the "_LV" view and
the stored procedure outside of sa.
The "make_localize_*" functions are inspired from code I have seen in
"wiki/UsageRecipes".
If there is interest to further enhance this and get it to "SQLAlchemy"
standard of code I would very much like to help but I am not good enough
a coder to actually do the work or I would definitely need a lot of hand
holding and coaching.
Werner
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.