server/sources/native.py
changeset 6957 ffda12be2e9f
parent 6945 28bf94d062a9
child 7118 e094b3d4eb95
equal deleted inserted replaced
6956:b172c383dbce 6957:ffda12be2e9f
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
   370             self.restore_from_file(backupfile, confirm, drop=drop)
   370             self.restore_from_file(backupfile, confirm, drop=drop)
   371         finally:
   371         finally:
   372             if self.repo.config.open_connections_pools:
   372             if self.repo.config.open_connections_pools:
   373                 self.open_pool_connections()
   373                 self.open_pool_connections()
   374 
   374 
   375     def init(self, activated, session=None):
   375     def init(self, activated, source_entity):
   376         self.init_creating(session and session.pool)
   376         self.init_creating(source_entity._cw.pool)
   377 
   377 
   378     def shutdown(self):
   378     def shutdown(self):
   379         if self._eid_creation_cnx:
   379         if self._eid_creation_cnx:
   380             self._eid_creation_cnx.close()
   380             self._eid_creation_cnx.close()
   381             self._eid_creation_cnx = None
   381             self._eid_creation_cnx = None
   801             if not isinstance(res, list):
   801             if not isinstance(res, list):
   802                 res = list(res)
   802                 res = list(res)
   803             res[-1] = b64decode(res[-1])
   803             res[-1] = b64decode(res[-1])
   804         return res
   804         return res
   805 
   805 
   806     def extid2eid(self, session, source, extid):
   806     def extid2eid(self, session, source_uri, extid):
   807         """get eid from an external id. Return None if no record found."""
   807         """get eid from an external id. Return None if no record found."""
   808         assert isinstance(extid, str)
   808         assert isinstance(extid, str)
   809         cursor = self.doexec(session,
   809         cursor = self.doexec(session,
   810                              'SELECT eid FROM entities '
   810                              'SELECT eid FROM entities '
   811                              'WHERE extid=%(x)s AND source=%(s)s',
   811                              'WHERE extid=%(x)s AND source=%(s)s',
   812                              {'x': b64encode(extid), 's': source.uri})
   812                              {'x': b64encode(extid), 's': source_uri})
   813         # XXX testing rowcount cause strange bug with sqlite, results are there
   813         # XXX testing rowcount cause strange bug with sqlite, results are there
   814         #     but rowcount is 0
   814         #     but rowcount is 0
   815         #if cursor.rowcount > 0:
   815         #if cursor.rowcount > 0:
   816         try:
   816         try:
   817             result = cursor.fetchone()
   817             result = cursor.fetchone()
   896         """add type and source info for an eid into the system table"""
   896         """add type and source info for an eid into the system table"""
   897         # begin by inserting eid/type/source/extid into the entities table
   897         # begin by inserting eid/type/source/extid into the entities table
   898         if extid is not None:
   898         if extid is not None:
   899             assert isinstance(extid, str)
   899             assert isinstance(extid, str)
   900             extid = b64encode(extid)
   900             extid = b64encode(extid)
       
   901         uri = 'system' if source.copy_based_source else source.uri
   901         attrs = {'type': entity.__regid__, 'eid': entity.eid, 'extid': extid,
   902         attrs = {'type': entity.__regid__, 'eid': entity.eid, 'extid': extid,
   902                  'source': source.uri, 'mtime': datetime.now()}
   903                  'source': uri, 'mtime': datetime.now()}
   903         self.doexec(session, self.sqlgen.insert('entities', attrs), attrs)
   904         self.doexec(session, self.sqlgen.insert('entities', attrs), attrs)
   904         # insert core relations: is, is_instance_of and cw_source
   905         # insert core relations: is, is_instance_of and cw_source
   905         try:
   906         try:
   906             self.doexec(session, 'INSERT INTO is_relation(eid_from,eid_to) VALUES (%s,%s)'
   907             self.doexec(session, 'INSERT INTO is_relation(eid_from,eid_to) VALUES (%s,%s)'
   907                         % (entity.eid, eschema_eid(session, entity.e_schema)))
   908                         % (entity.eid, eschema_eid(session, entity.e_schema)))