409 self.open_source_connections() |
409 self.open_source_connections() |
410 |
410 |
411 |
411 |
412 def init(self, activated, source_entity): |
412 def init(self, activated, source_entity): |
413 self.init_creating(source_entity._cw.cnxset) |
413 self.init_creating(source_entity._cw.cnxset) |
|
414 try: |
|
415 source_entity._cw.system_sql('SELECT COUNT(asource) FROM entities') |
|
416 except Exception, ex: |
|
417 self.eid_type_source = self.eid_type_source_pre_131 |
414 |
418 |
415 def shutdown(self): |
419 def shutdown(self): |
416 if self._eid_creation_cnx: |
420 if self._eid_creation_cnx: |
417 self._eid_creation_cnx.close() |
421 self._eid_creation_cnx.close() |
418 self._eid_creation_cnx = None |
422 self._eid_creation_cnx = None |
675 self._add_relations(session, rtype, subj_obj_list, inlined) |
679 self._add_relations(session, rtype, subj_obj_list, inlined) |
676 if session.undoable_action('A', rtype): |
680 if session.undoable_action('A', rtype): |
677 for subject, object in subj_obj_list: |
681 for subject, object in subj_obj_list: |
678 self._record_tx_action(session, 'tx_relation_actions', 'A', |
682 self._record_tx_action(session, 'tx_relation_actions', 'A', |
679 eid_from=subject, rtype=rtype, eid_to=object) |
683 eid_from=subject, rtype=rtype, eid_to=object) |
680 |
684 |
681 def _add_relations(self, session, rtype, subj_obj_list, inlined=False): |
685 def _add_relations(self, session, rtype, subj_obj_list, inlined=False): |
682 """add a relation to the source""" |
686 """add a relation to the source""" |
683 sql = [] |
687 sql = [] |
684 if inlined is False: |
688 if inlined is False: |
685 attrs = [{'eid_from': subject, 'eid_to': object} |
689 attrs = [{'eid_from': subject, 'eid_to': object} |
844 |
848 |
845 # system source interface ################################################# |
849 # system source interface ################################################# |
846 |
850 |
847 def eid_type_source(self, session, eid): |
851 def eid_type_source(self, session, eid): |
848 """return a tuple (type, source, extid) for the entity with id <eid>""" |
852 """return a tuple (type, source, extid) for the entity with id <eid>""" |
849 sql = 'SELECT type, source, extid FROM entities WHERE eid=%s' % eid |
853 sql = 'SELECT type, source, extid, asource FROM entities WHERE eid=%s' % eid |
850 try: |
854 try: |
851 res = self.doexec(session, sql).fetchone() |
855 res = self.doexec(session, sql).fetchone() |
852 except: |
856 except: |
853 assert session.cnxset, 'session has no connections set' |
857 assert session.cnxset, 'session has no connections set' |
854 raise UnknownEid(eid) |
858 raise UnknownEid(eid) |
855 if res is None: |
859 if res is None: |
856 raise UnknownEid(eid) |
860 raise UnknownEid(eid) |
857 if res[-1] is not None: |
861 if res[-2] is not None: |
858 if not isinstance(res, list): |
862 if not isinstance(res, list): |
859 res = list(res) |
863 res = list(res) |
|
864 res[-2] = b64decode(res[-2]) |
|
865 return res |
|
866 |
|
867 def eid_type_source_pre_131(self, session, eid): |
|
868 """return a tuple (type, source, extid) for the entity with id <eid>""" |
|
869 sql = 'SELECT type, source, extid FROM entities WHERE eid=%s' % eid |
|
870 try: |
|
871 res = self.doexec(session, sql).fetchone() |
|
872 except: |
|
873 assert session.cnxset, 'session has no connections set' |
|
874 raise UnknownEid(eid) |
|
875 if res is None: |
|
876 raise UnknownEid(eid) |
|
877 if not isinstance(res, list): |
|
878 res = list(res) |
|
879 if res[-1] is not None: |
860 res[-1] = b64decode(res[-1]) |
880 res[-1] = b64decode(res[-1]) |
|
881 res.append(res[1]) |
861 return res |
882 return res |
862 |
883 |
863 def extid2eid(self, session, source_uri, extid): |
884 def extid2eid(self, session, source_uri, extid): |
864 """get eid from an external id. Return None if no record found.""" |
885 """get eid from an external id. Return None if no record found.""" |
865 assert isinstance(extid, str) |
886 assert isinstance(extid, str) |
944 if extid is not None: |
965 if extid is not None: |
945 assert isinstance(extid, str) |
966 assert isinstance(extid, str) |
946 extid = b64encode(extid) |
967 extid = b64encode(extid) |
947 uri = 'system' if source.copy_based_source else source.uri |
968 uri = 'system' if source.copy_based_source else source.uri |
948 attrs = {'type': entity.__regid__, 'eid': entity.eid, 'extid': extid, |
969 attrs = {'type': entity.__regid__, 'eid': entity.eid, 'extid': extid, |
949 'source': uri, 'mtime': datetime.now()} |
970 'source': uri, 'asource': source.uri, 'mtime': datetime.now()} |
950 self.doexec(session, self.sqlgen.insert('entities', attrs), attrs) |
971 self.doexec(session, self.sqlgen.insert('entities', attrs), attrs) |
951 # insert core relations: is, is_instance_of and cw_source |
972 # insert core relations: is, is_instance_of and cw_source |
952 try: |
973 try: |
953 self.doexec(session, 'INSERT INTO is_relation(eid_from,eid_to) VALUES (%s,%s)' |
974 self.doexec(session, 'INSERT INTO is_relation(eid_from,eid_to) VALUES (%s,%s)' |
954 % (entity.eid, eschema_eid(session, entity.e_schema))) |
975 % (entity.eid, eschema_eid(session, entity.e_schema))) |
1432 |
1453 |
1433 CREATE TABLE entities ( |
1454 CREATE TABLE entities ( |
1434 eid INTEGER PRIMARY KEY NOT NULL, |
1455 eid INTEGER PRIMARY KEY NOT NULL, |
1435 type VARCHAR(64) NOT NULL, |
1456 type VARCHAR(64) NOT NULL, |
1436 source VARCHAR(64) NOT NULL, |
1457 source VARCHAR(64) NOT NULL, |
|
1458 asource VARCHAR(64) NOT NULL, |
1437 mtime %s NOT NULL, |
1459 mtime %s NOT NULL, |
1438 extid VARCHAR(256) |
1460 extid VARCHAR(256) |
1439 );; |
1461 );; |
1440 CREATE INDEX entities_type_idx ON entities(type);; |
1462 CREATE INDEX entities_type_idx ON entities(type);; |
1441 CREATE INDEX entities_mtime_idx ON entities(mtime);; |
1463 CREATE INDEX entities_mtime_idx ON entities(mtime);; |