server/sources/native.py
changeset 10209 4c64a41c0a1d
parent 10208 249126034c0e
child 10221 17ad882a211f
equal deleted inserted replaced
10208:249126034c0e 10209:4c64a41c0a1d
   866         return res
   866         return res
   867 
   867 
   868     def extid2eid(self, cnx, extid):
   868     def extid2eid(self, cnx, extid):
   869         """get eid from an external id. Return None if no record found."""
   869         """get eid from an external id. Return None if no record found."""
   870         assert isinstance(extid, str)
   870         assert isinstance(extid, str)
       
   871         args = {'x': b64encode(extid)}
   871         cursor = self.doexec(cnx,
   872         cursor = self.doexec(cnx,
   872                              'SELECT eid FROM entities WHERE extid=%(x)s',
   873                              'SELECT eid FROM entities WHERE extid=%(x)s',
   873                              {'x': b64encode(extid)})
   874                              args)
   874         # XXX testing rowcount cause strange bug with sqlite, results are there
   875         # XXX testing rowcount cause strange bug with sqlite, results are there
   875         #     but rowcount is 0
   876         #     but rowcount is 0
   876         #if cursor.rowcount > 0:
   877         #if cursor.rowcount > 0:
   877         try:
   878         try:
   878             result = cursor.fetchone()
   879             result = cursor.fetchone()
   879             if result:
   880             if result:
   880                 return result[0]
   881                 return result[0]
       
   882         except Exception:
       
   883             pass
       
   884         cursor = self.doexec(cnx,
       
   885                              'SELECT eid FROM moved_entities WHERE extid=%(x)s',
       
   886                              args)
       
   887         try:
       
   888             result = cursor.fetchone()
       
   889             if result:
       
   890                 # entity was moved to the system source, return negative
       
   891                 # number to tell the external source to ignore it
       
   892                 return -result[0]
   881         except Exception:
   893         except Exception:
   882             pass
   894             pass
   883         return None
   895         return None
   884 
   896 
   885     def _handle_is_relation_sql(self, cnx, sql, attrs):
   897     def _handle_is_relation_sql(self, cnx, sql, attrs):
  1388   type VARCHAR(64) NOT NULL,
  1400   type VARCHAR(64) NOT NULL,
  1389   asource VARCHAR(128) NOT NULL,
  1401   asource VARCHAR(128) NOT NULL,
  1390   extid VARCHAR(256) UNIQUE
  1402   extid VARCHAR(256) UNIQUE
  1391 );;
  1403 );;
  1392 CREATE INDEX entities_type_idx ON entities(type);;
  1404 CREATE INDEX entities_type_idx ON entities(type);;
       
  1405 CREATE TABLE moved_entities (
       
  1406   eid INTEGER PRIMARY KEY NOT NULL,
       
  1407   extid VARCHAR(256) UNIQUE
       
  1408 );;
  1393 
  1409 
  1394 CREATE TABLE transactions (
  1410 CREATE TABLE transactions (
  1395   tx_uuid CHAR(32) PRIMARY KEY NOT NULL,
  1411   tx_uuid CHAR(32) PRIMARY KEY NOT NULL,
  1396   tx_user INTEGER NOT NULL,
  1412   tx_user INTEGER NOT NULL,
  1397   tx_time %s NOT NULL
  1413   tx_time %s NOT NULL