cache in sql works

This commit is contained in:
2014-02-01 16:26:23 +01:00
parent 02a987b39d
commit 313b03b246
5 changed files with 143 additions and 114 deletions

View File

@ -18,7 +18,9 @@
#
# ____________________________________________________________
from tiramisu.i18n import _
from tiramisu.setting import groups
from sqlalchemy import not_, or_
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy import create_engine, Column, Integer, String, Boolean, \
@ -40,9 +42,6 @@ SqlAlchemyBase = declarative_base()
# require
#_Base : object dans la base de donnée
# => _PropertyOption => liste des propriétés
# => _CallbackParam avec des Options
def load_requires(collection_type, proxy):
def getter(obj):
if obj is None:
@ -274,8 +273,10 @@ class _Base(SqlAlchemyBase):
'polymorphic_identity': 'option',
'polymorphic_on': _type
}
_extra = Column(PickleType)
#FIXME devrait etre une table
_group_type = Column(String)
_is_build_cache = Column(Boolean, default=False)
def __init__(self):
self.commit()
@ -324,21 +325,95 @@ class _Base(SqlAlchemyBase):
key))
class Cache(SqlAlchemyBase):
__tablename__ = 'cache'
id = Column(Integer, primary_key=True)
#FIXME indexer ... les 3
path = Column(String, nullable=False)
descr = Column(Integer, nullable=False)
option = Column(Integer, nullable=False)
opt_type = Column(String, nullable=False)
def __init__(self, descr, option, path):
self.descr = descr.id
self.option = option.id
self.path = path
self.opt_type = option.__class__.__name__
class StorageOptionDescription(object):
def impl_already_build_caches(self):
return self._is_build_cache
def impl_get_opt_by_path(self, path):
try:
#FIXME
idx = self._cache_paths[1].index(path)
opt_id = self._cache_paths[0][idx]
return session.query(_Base).filter_by(id=opt_id).first()
except ValueError:
ret = session.query(Cache).filter_by(descr=self.id, path=path).first()
if ret is None:
raise AttributeError(_('no option for path {0}').format(path))
return session.query(_Base).filter_by(id=ret.option).first()
def impl_get_path_by_opt(self, opt):
try:
return self._cache_paths[1][self._cache_paths[0].index(opt.id)]
except ValueError:
ret = session.query(Cache).filter_by(descr=self.id,
option=opt.id).first()
if ret is None:
raise AttributeError(_('no option {0} found').format(opt))
return ret.path
def impl_get_group_type(self):
return getattr(groups, self._group_type)
def impl_build_cache_option(self, descr=None, _currpath=None):
if descr is None:
save = True
descr = self
_currpath = []
else:
save = False
for option in self.impl_getchildren():
attr = option.impl_getname()
session.add(Cache(descr, option,
str('.'.join(_currpath + [attr]))))
if isinstance(option, StorageOptionDescription):
_currpath.append(attr)
option.impl_build_cache_option(descr,
_currpath)
_currpath.pop()
if save:
self._is_build_cache = True
session.commit()
def impl_get_options_paths(self, bytype, byname, _subpath, only_first):
#FIXME tester si 1er est un descr ...
#FAIRE UN JOIN pour only_first
sqlquery = session.query(Cache).filter_by(descr=self.id)
if bytype is None:
sqlquery = sqlquery.filter(not_(Cache.opt_type == 'OptionDescription'))
else:
sqlquery = sqlquery.filter_by(opt_type=bytype.__name__)
query = ''
or_query = ''
if _subpath is not None:
query += _subpath + '.'
if byname is not None:
or_query = query + byname
query += '%.' + byname
if query != '':
filter_query = Cache.path.like(query)
if or_query != '':
filter_query = or_(Cache.path == or_query, filter_query)
sqlquery = sqlquery.filter(filter_query)
if only_first:
opt = sqlquery.first()
if opt is None:
return tuple()
option = session.query(_Base).filter_by(id=opt.option).first()
return ((opt.path, option),)
else:
ret = []
for opt in sqlquery.all():
option = session.query(_Base).filter_by(id=opt.option).first()
ret.append((opt.path, option))
return ret
class StorageBase(_Base):
@ -347,7 +422,7 @@ class StorageBase(_Base):
return {'polymorphic_identity': self.__name__.lower()}
#FIXME
#engine.echo = True
SqlAlchemyBase.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()