1 """Adapters for native cubicweb sources. |
1 """Adapters for native cubicweb sources. |
2 |
2 |
3 :organization: Logilab |
3 :organization: Logilab |
4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 """ |
6 """ |
7 __docformat__ = "restructuredtext en" |
7 __docformat__ = "restructuredtext en" |
8 |
8 |
9 from threading import Lock |
9 from threading import Lock |
10 |
10 from datetime import datetime |
11 from mx.DateTime import now |
|
12 |
11 |
13 from logilab.common.cache import Cache |
12 from logilab.common.cache import Cache |
14 from logilab.common.configuration import REQUIRED |
13 from logilab.common.configuration import REQUIRED |
15 from logilab.common.adbh import get_adv_func_helper |
14 from logilab.common.adbh import get_adv_func_helper |
16 |
15 |
449 """return a tuple (type, source, extid) for the entity with id <eid>""" |
448 """return a tuple (type, source, extid) for the entity with id <eid>""" |
450 sql = 'SELECT type, source, extid FROM entities WHERE eid=%s' % eid |
449 sql = 'SELECT type, source, extid FROM entities WHERE eid=%s' % eid |
451 try: |
450 try: |
452 res = session.system_sql(sql).fetchone() |
451 res = session.system_sql(sql).fetchone() |
453 except: |
452 except: |
454 assert self.pool, 'session has no pool set' |
453 assert session.pool, 'session has no pool set' |
455 raise UnknownEid(eid) |
454 raise UnknownEid(eid) |
456 if res is None: |
455 if res is None: |
457 raise UnknownEid(eid) |
456 raise UnknownEid(eid) |
458 return res |
457 return res |
459 |
458 |
497 |
496 |
498 def add_info(self, session, entity, source, extid=None): |
497 def add_info(self, session, entity, source, extid=None): |
499 """add type and source info for an eid into the system table""" |
498 """add type and source info for an eid into the system table""" |
500 # begin by inserting eid/type/source/extid into the entities table |
499 # begin by inserting eid/type/source/extid into the entities table |
501 attrs = {'type': str(entity.e_schema), 'eid': entity.eid, |
500 attrs = {'type': str(entity.e_schema), 'eid': entity.eid, |
502 'extid': extid, 'source': source.uri, 'mtime': now()} |
501 'extid': extid, 'source': source.uri, 'mtime': datetime.now()} |
503 session.system_sql(self.sqlgen.insert('entities', attrs), attrs) |
502 session.system_sql(self.sqlgen.insert('entities', attrs), attrs) |
504 |
503 |
505 def delete_info(self, session, eid, etype, uri, extid): |
504 def delete_info(self, session, eid, etype, uri, extid): |
506 """delete system information on deletion of an entity by transfering |
505 """delete system information on deletion of an entity by transfering |
507 record from the entities table to the deleted_entities table |
506 record from the entities table to the deleted_entities table |
508 """ |
507 """ |
509 attrs = {'eid': eid} |
508 attrs = {'eid': eid} |
510 session.system_sql(self.sqlgen.delete('entities', attrs), attrs) |
509 session.system_sql(self.sqlgen.delete('entities', attrs), attrs) |
511 if self.has_deleted_entitites_table: |
510 if self.has_deleted_entitites_table: |
512 attrs = {'type': etype, 'eid': eid, 'extid': extid, |
511 attrs = {'type': etype, 'eid': eid, 'extid': extid, |
513 'source': uri, 'dtime': now()} |
512 'source': uri, 'dtime': datetime.now()} |
514 session.system_sql(self.sqlgen.insert('deleted_entities', attrs), attrs) |
513 session.system_sql(self.sqlgen.insert('deleted_entities', attrs), attrs) |
515 |
514 |
516 def fti_unindex_entity(self, session, eid): |
515 def fti_unindex_entity(self, session, eid): |
517 """remove text content for entity with the given eid from the full text |
516 """remove text content for entity with the given eid from the full text |
518 index |
517 index |
532 session.pool['system']) |
531 session.pool['system']) |
533 except: |
532 except: |
534 if self.indexer is not None: |
533 if self.indexer is not None: |
535 self.exception('error while reindexing %s', entity) |
534 self.exception('error while reindexing %s', entity) |
536 # update entities.mtime |
535 # update entities.mtime |
537 attrs = {'eid': entity.eid, 'mtime': now()} |
536 attrs = {'eid': entity.eid, 'mtime': datetime.now()} |
538 session.system_sql(self.sqlgen.update('entities', attrs, ['eid']), attrs) |
537 session.system_sql(self.sqlgen.update('entities', attrs, ['eid']), attrs) |
539 |
538 |
540 def modified_entities(self, session, etypes, mtime): |
539 def modified_entities(self, session, etypes, mtime): |
541 """return a 2-uple: |
540 """return a 2-uple: |
542 * list of (etype, eid) of entities of the given types which have been |
541 * list of (etype, eid) of entities of the given types which have been |