590 for subject, object in subj_obj_list] |
590 for subject, object in subj_obj_list] |
591 sql.append((self.sqlgen.insert('%s_relation' % rtype, attrs[0]), attrs)) |
591 sql.append((self.sqlgen.insert('%s_relation' % rtype, attrs[0]), attrs)) |
592 else: # used by data import |
592 else: # used by data import |
593 etypes = {} |
593 etypes = {} |
594 for subject, object in subj_obj_list: |
594 for subject, object in subj_obj_list: |
595 etype = session.describe(subject)[0] |
595 etype = session.entity_metas(subject)['type'] |
596 if etype in etypes: |
596 if etype in etypes: |
597 etypes[etype].append((subject, object)) |
597 etypes[etype].append((subject, object)) |
598 else: |
598 else: |
599 etypes[etype] = [(subject, object)] |
599 etypes[etype] = [(subject, object)] |
600 for subj_etype, subj_obj_list in etypes.iteritems(): |
600 for subj_etype, subj_obj_list in etypes.iteritems(): |
615 eid_from=subject, rtype=rtype, eid_to=object) |
615 eid_from=subject, rtype=rtype, eid_to=object) |
616 |
616 |
617 def _delete_relation(self, session, subject, rtype, object, inlined=False): |
617 def _delete_relation(self, session, subject, rtype, object, inlined=False): |
618 """delete a relation from the source""" |
618 """delete a relation from the source""" |
619 if inlined: |
619 if inlined: |
620 table = SQL_PREFIX + session.describe(subject)[0] |
620 table = SQL_PREFIX + session.entity_metas(subject)['type'] |
621 column = SQL_PREFIX + rtype |
621 column = SQL_PREFIX + rtype |
622 sql = 'UPDATE %s SET %s=NULL WHERE %seid=%%(eid)s' % (table, column, |
622 sql = 'UPDATE %s SET %s=NULL WHERE %seid=%%(eid)s' % (table, column, |
623 SQL_PREFIX) |
623 SQL_PREFIX) |
624 attrs = {'eid' : subject} |
624 attrs = {'eid' : subject} |
625 else: |
625 else: |
762 self.exception('failed to query entities table for eid %s', eid) |
762 self.exception('failed to query entities table for eid %s', eid) |
763 raise UnknownEid(eid) |
763 raise UnknownEid(eid) |
764 |
764 |
765 def eid_type_source(self, session, eid): # pylint: disable=E0202 |
765 def eid_type_source(self, session, eid): # pylint: disable=E0202 |
766 """return a tuple (type, source, extid) for the entity with id <eid>""" |
766 """return a tuple (type, source, extid) for the entity with id <eid>""" |
767 sql = 'SELECT type, source, extid, asource FROM entities WHERE eid=%s' % eid |
767 sql = 'SELECT type, extid, asource FROM entities WHERE eid=%s' % eid |
768 res = self._eid_type_source(session, eid, sql) |
768 res = self._eid_type_source(session, eid, sql) |
769 if res[-2] is not None: |
769 if res[-2] is not None: |
770 if not isinstance(res, list): |
770 if not isinstance(res, list): |
771 res = list(res) |
771 res = list(res) |
772 res[-2] = b64decode(res[-2]) |
772 res[-2] = b64decode(res[-2]) |
773 return res |
773 return res |
774 |
774 |
775 def eid_type_source_pre_131(self, session, eid): |
775 def eid_type_source_pre_131(self, session, eid): |
776 """return a tuple (type, source, extid) for the entity with id <eid>""" |
776 """return a tuple (type, source, extid) for the entity with id <eid>""" |
777 sql = 'SELECT type, source, extid FROM entities WHERE eid=%s' % eid |
777 sql = 'SELECT type, extid FROM entities WHERE eid=%s' % eid |
778 res = self._eid_type_source(session, eid, sql) |
778 res = self._eid_type_source(session, eid, sql) |
779 if not isinstance(res, list): |
779 if not isinstance(res, list): |
780 res = list(res) |
780 res = list(res) |
781 if res[-1] is not None: |
781 if res[-1] is not None: |
782 res[-1] = b64decode(res[-1]) |
782 res[-1] = b64decode(res[-1]) |
783 res.append(res[1]) |
783 res.append(res[1]) |
784 return res |
784 return res |
785 |
785 |
786 def extid2eid(self, session, source_uri, extid): |
786 def extid2eid(self, session, extid): |
787 """get eid from an external id. Return None if no record found.""" |
787 """get eid from an external id. Return None if no record found.""" |
788 assert isinstance(extid, str) |
788 assert isinstance(extid, str) |
789 cursor = self.doexec(session, |
789 cursor = self.doexec(session, |
790 'SELECT eid FROM entities ' |
790 'SELECT eid FROM entities WHERE extid=%(x)s', |
791 'WHERE extid=%(x)s AND source=%(s)s', |
791 {'x': b64encode(extid)}) |
792 {'x': b64encode(extid), 's': source_uri}) |
|
793 # XXX testing rowcount cause strange bug with sqlite, results are there |
792 # XXX testing rowcount cause strange bug with sqlite, results are there |
794 # but rowcount is 0 |
793 # but rowcount is 0 |
795 #if cursor.rowcount > 0: |
794 #if cursor.rowcount > 0: |
796 try: |
795 try: |
797 result = cursor.fetchone() |
796 result = cursor.fetchone() |
861 # begin by inserting eid/type/source/extid into the entities table |
860 # begin by inserting eid/type/source/extid into the entities table |
862 if extid is not None: |
861 if extid is not None: |
863 assert isinstance(extid, str) |
862 assert isinstance(extid, str) |
864 extid = b64encode(extid) |
863 extid = b64encode(extid) |
865 attrs = {'type': entity.cw_etype, 'eid': entity.eid, 'extid': extid, |
864 attrs = {'type': entity.cw_etype, 'eid': entity.eid, 'extid': extid, |
866 'source': 'system', 'asource': source.uri} |
865 'asource': source.uri} |
867 self._handle_insert_entity_sql(session, self.sqlgen.insert('entities', attrs), attrs) |
866 self._handle_insert_entity_sql(session, self.sqlgen.insert('entities', attrs), attrs) |
868 # insert core relations: is, is_instance_of and cw_source |
867 # insert core relations: is, is_instance_of and cw_source |
869 try: |
868 try: |
870 self._handle_is_relation_sql(session, 'INSERT INTO is_relation(eid_from,eid_to) VALUES (%s,%s)', |
869 self._handle_is_relation_sql(session, 'INSERT INTO is_relation(eid_from,eid_to) VALUES (%s,%s)', |
871 (entity.eid, eschema_eid(session, entity.e_schema))) |
870 (entity.eid, eschema_eid(session, entity.e_schema))) |
1347 %s |
1346 %s |
1348 |
1347 |
1349 CREATE TABLE entities ( |
1348 CREATE TABLE entities ( |
1350 eid INTEGER PRIMARY KEY NOT NULL, |
1349 eid INTEGER PRIMARY KEY NOT NULL, |
1351 type VARCHAR(64) NOT NULL, |
1350 type VARCHAR(64) NOT NULL, |
1352 source VARCHAR(128) NOT NULL, |
|
1353 asource VARCHAR(128) NOT NULL, |
1351 asource VARCHAR(128) NOT NULL, |
1354 extid VARCHAR(256) |
1352 extid VARCHAR(256) |
1355 );; |
1353 );; |
1356 CREATE INDEX entities_type_idx ON entities(type);; |
1354 CREATE INDEX entities_type_idx ON entities(type);; |
1357 CREATE INDEX entities_extid_idx ON entities(extid);; |
1355 CREATE INDEX entities_extid_idx ON entities(extid);; |