server/sources/extlite.py
changeset 1251 af40e615dc89
parent 1137 9ce0ac82f94f
child 1263 01152fffd593
equal deleted inserted replaced
1250:5c20a7f13c84 1251:af40e615dc89
     1 """provide an abstract class for external sources using a sqlite database helper
     1 """provide an abstract class for external sources using a sqlite database helper
     2 
     2 
     3 :organization: Logilab
     3 :organization: Logilab
     4 :copyright: 2007-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     4 :copyright: 2007-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 
     9 
    10 import time
    10 import time
    11 import threading
    11 import threading
    12 from os.path import join, exists
    12 from os.path import join, exists
    13 
    13 
    14 from cubicweb import server
    14 from cubicweb import server
    15 from cubicweb.server.sqlutils import sqlexec, SQLAdapterMixIn
    15 from cubicweb.server.sqlutils import SQL_PREFIX, sqlexec, SQLAdapterMixIn
    16 from cubicweb.server.sources import AbstractSource, native
    16 from cubicweb.server.sources import AbstractSource, native
    17 from cubicweb.server.sources.rql2sql import SQLGenerator
    17 from cubicweb.server.sources.rql2sql import SQLGenerator
    18 
    18 
    19 def timeout_acquire(lock, timeout):
    19 def timeout_acquire(lock, timeout):
    20     while not lock.acquire(False):
    20     while not lock.acquire(False):
   120         cu = cnx.cursor()
   120         cu = cnx.cursor()
   121         schema = self.schema
   121         schema = self.schema
   122         for etype in self.support_entities:
   122         for etype in self.support_entities:
   123             eschema = schema.eschema(etype)
   123             eschema = schema.eschema(etype)
   124             createsqls = eschema2sql(self.sqladapter.dbhelper, eschema,
   124             createsqls = eschema2sql(self.sqladapter.dbhelper, eschema,
   125                                      skip_relations=('data',))
   125                                      skip_relations=('data',), prefix=SQL_PREFIX)
   126             sqlexec(createsqls, cu, withpb=False)
   126             sqlexec(createsqls, cu, withpb=False)
   127         for rtype in self.support_relations:
   127         for rtype in self.support_relations:
   128             rschema = schema.rschema(rtype)
   128             rschema = schema.rschema(rtype)
   129             if not rschema.inlined:
   129             if not rschema.inlined:
   130                 sqlexec(rschema2sql(rschema), cu, withpb=False)
   130                 sqlexec(rschema2sql(rschema), cu, withpb=False)
   194         don't want to simply do this, so let raise NotImplementedError and the
   194         don't want to simply do this, so let raise NotImplementedError and the
   195         source implementor may use this method if necessary
   195         source implementor may use this method if necessary
   196         """
   196         """
   197         cu = session.pool[self.uri]
   197         cu = session.pool[self.uri]
   198         attrs = self.sqladapter.preprocess_entity(entity)
   198         attrs = self.sqladapter.preprocess_entity(entity)
   199         sql = self.sqladapter.sqlgen.insert(str(entity.e_schema), attrs)
   199         sql = self.sqladapter.sqlgen.insert(SQL_PREFIX + str(entity.e_schema), attrs)
   200         cu.execute(sql, attrs)
   200         cu.execute(sql, attrs)
   201         
   201         
   202     def add_entity(self, session, entity):
   202     def add_entity(self, session, entity):
   203         """add a new entity to the source"""
   203         """add a new entity to the source"""
   204         raise NotImplementedError()
   204         raise NotImplementedError()
   210         source don't want to simply do this, so let raise NotImplementedError
   210         source don't want to simply do this, so let raise NotImplementedError
   211         and the source implementor may use this method if necessary
   211         and the source implementor may use this method if necessary
   212         """
   212         """
   213         cu = session.pool[self.uri]
   213         cu = session.pool[self.uri]
   214         attrs = self.sqladapter.preprocess_entity(entity)
   214         attrs = self.sqladapter.preprocess_entity(entity)
   215         sql = self.sqladapter.sqlgen.update(str(entity.e_schema), attrs, ['eid'])
   215         sql = self.sqladapter.sqlgen.update(SQL_PREFIX + str(entity.e_schema), attrs, ['eid'])
   216         cu.execute(sql, attrs)
   216         cu.execute(sql, attrs)
   217         
   217         
   218     def update_entity(self, session, entity):
   218     def update_entity(self, session, entity):
   219         """update an entity in the source"""
   219         """update an entity in the source"""
   220         raise NotImplementedError()
   220         raise NotImplementedError()
   225         this is not deleting a file in the svn but deleting entities from the
   225         this is not deleting a file in the svn but deleting entities from the
   226         source. Main usage is to delete repository content when a Repository
   226         source. Main usage is to delete repository content when a Repository
   227         entity is deleted.
   227         entity is deleted.
   228         """
   228         """
   229         sqlcursor = session.pool[self.uri]        
   229         sqlcursor = session.pool[self.uri]        
   230         attrs = {'eid': eid}
   230         attrs = {SQL_PREFIX + 'eid': eid}
   231         sql = self.sqladapter.sqlgen.delete(etype, attrs)
   231         sql = self.sqladapter.sqlgen.delete(SQL_PREFIX + etype, attrs)
   232         sqlcursor.execute(sql, attrs)
   232         sqlcursor.execute(sql, attrs)
   233     
   233     
   234     def delete_relation(self, session, subject, rtype, object):
   234     def delete_relation(self, session, subject, rtype, object):
   235         """delete a relation from the source"""
   235         """delete a relation from the source"""
   236         rschema = self.schema.rschema(rtype)
   236         rschema = self.schema.rschema(rtype)
   237         if rschema.inlined:
   237         if rschema.inlined:
   238             if subject in session.query_data('pendingeids', ()):
   238             if subject in session.query_data('pendingeids', ()):
   239                 return
   239                 return
   240             etype = session.describe(subject)[0]
   240             table = SQL_PREFIX + session.describe(subject)[0]
   241             sql = 'UPDATE %s SET %s=NULL WHERE eid=%%(eid)s' % (etype, rtype)
   241             column = SQL_PREFIX + rtype
       
   242             sql = 'UPDATE %s SET %s=NULL WHERE %seid=%%(eid)s' % (table, column, SQL_PREFIX)
   242             attrs = {'eid' : subject}
   243             attrs = {'eid' : subject}
   243         else:
   244         else:
   244             attrs = {'eid_from': subject, 'eid_to': object}
   245             attrs = {'eid_from': subject, 'eid_to': object}
   245             sql = self.sqladapter.sqlgen.delete('%s_relation' % rtype, attrs)
   246             sql = self.sqladapter.sqlgen.delete('%s_relation' % rtype, attrs)
   246         sqlcursor = session.pool[self.uri]        
   247         sqlcursor = session.pool[self.uri]