server/sources/extlite.py
branchstable
changeset 2306 95da5d9f0870
parent 2101 08003e0354a7
child 2493 9806571ea790
equal deleted inserted replaced
2305:8f6dbe884700 2306:95da5d9f0870
   172             return []
   172             return []
   173         sql, query_args = self.rqlsqlgen.generate(union, args)
   173         sql, query_args = self.rqlsqlgen.generate(union, args)
   174         if server.DEBUG:
   174         if server.DEBUG:
   175             print self.uri, 'SOURCE RQL', union.as_string()
   175             print self.uri, 'SOURCE RQL', union.as_string()
   176         args = self.sqladapter.merge_args(args, query_args)
   176         args = self.sqladapter.merge_args(args, query_args)
   177         cursor = session.pool[self.uri]
   177         res = self.sqladapter.process_result(self.doexec(session, sql, args))
   178         self.doexec(cursor, sql, args)
       
   179         res = self.sqladapter.process_result(cursor)
       
   180         if server.DEBUG:
   178         if server.DEBUG:
   181             print '------>', res
   179             print '------>', res
   182         return res
   180         return res
   183 
   181 
   184     def local_add_entity(self, session, entity):
   182     def local_add_entity(self, session, entity):
   188         don't want to simply do this, so let raise NotImplementedError and the
   186         don't want to simply do this, so let raise NotImplementedError and the
   189         source implementor may use this method if necessary
   187         source implementor may use this method if necessary
   190         """
   188         """
   191         attrs = self.sqladapter.preprocess_entity(entity)
   189         attrs = self.sqladapter.preprocess_entity(entity)
   192         sql = self.sqladapter.sqlgen.insert(SQL_PREFIX + str(entity.e_schema), attrs)
   190         sql = self.sqladapter.sqlgen.insert(SQL_PREFIX + str(entity.e_schema), attrs)
   193         self.doexec(session.pool[self.uri], sql, attrs)
   191         self.doexec(session, sql, attrs)
   194 
   192 
   195     def add_entity(self, session, entity):
   193     def add_entity(self, session, entity):
   196         """add a new entity to the source"""
   194         """add a new entity to the source"""
   197         raise NotImplementedError()
   195         raise NotImplementedError()
   198 
   196 
   205         """
   203         """
   206         if attrs is None:
   204         if attrs is None:
   207             attrs = self.sqladapter.preprocess_entity(entity)
   205             attrs = self.sqladapter.preprocess_entity(entity)
   208         sql = self.sqladapter.sqlgen.update(SQL_PREFIX + str(entity.e_schema),
   206         sql = self.sqladapter.sqlgen.update(SQL_PREFIX + str(entity.e_schema),
   209                                             attrs, [SQL_PREFIX + 'eid'])
   207                                             attrs, [SQL_PREFIX + 'eid'])
   210         self.doexec(session.pool[self.uri], sql, attrs)
   208         self.doexec(session, sql, attrs)
   211 
   209 
   212     def update_entity(self, session, entity):
   210     def update_entity(self, session, entity):
   213         """update an entity in the source"""
   211         """update an entity in the source"""
   214         raise NotImplementedError()
   212         raise NotImplementedError()
   215 
   213 
   220         source. Main usage is to delete repository content when a Repository
   218         source. Main usage is to delete repository content when a Repository
   221         entity is deleted.
   219         entity is deleted.
   222         """
   220         """
   223         attrs = {SQL_PREFIX + 'eid': eid}
   221         attrs = {SQL_PREFIX + 'eid': eid}
   224         sql = self.sqladapter.sqlgen.delete(SQL_PREFIX + etype, attrs)
   222         sql = self.sqladapter.sqlgen.delete(SQL_PREFIX + etype, attrs)
   225         self.doexec(session.pool[self.uri], sql, attrs)
   223         self.doexec(session, sql, attrs)
   226 
   224 
   227     def local_add_relation(self, session, subject, rtype, object):
   225     def local_add_relation(self, session, subject, rtype, object):
   228         """add a relation to the source
   226         """add a relation to the source
   229 
   227 
   230         This is not provided as add_relation implementation since usually
   228         This is not provided as add_relation implementation since usually
   231         source don't want to simply do this, so let raise NotImplementedError
   229         source don't want to simply do this, so let raise NotImplementedError
   232         and the source implementor may use this method if necessary
   230         and the source implementor may use this method if necessary
   233         """
   231         """
   234         attrs = {'eid_from': subject, 'eid_to': object}
   232         attrs = {'eid_from': subject, 'eid_to': object}
   235         sql = self.sqladapter.sqlgen.insert('%s_relation' % rtype, attrs)
   233         sql = self.sqladapter.sqlgen.insert('%s_relation' % rtype, attrs)
   236         self.doexec(session.pool[self.uri], sql, attrs)
   234         self.doexec(session, sql, attrs)
   237 
   235 
   238     def add_relation(self, session, subject, rtype, object):
   236     def add_relation(self, session, subject, rtype, object):
   239         """add a relation to the source"""
   237         """add a relation to the source"""
   240         raise NotImplementedError()
   238         raise NotImplementedError()
   241 
   239 
   250             sql = 'UPDATE %s SET %s=NULL WHERE %seid=%%(eid)s' % (table, column, SQL_PREFIX)
   248             sql = 'UPDATE %s SET %s=NULL WHERE %seid=%%(eid)s' % (table, column, SQL_PREFIX)
   251             attrs = {'eid' : subject}
   249             attrs = {'eid' : subject}
   252         else:
   250         else:
   253             attrs = {'eid_from': subject, 'eid_to': object}
   251             attrs = {'eid_from': subject, 'eid_to': object}
   254             sql = self.sqladapter.sqlgen.delete('%s_relation' % rtype, attrs)
   252             sql = self.sqladapter.sqlgen.delete('%s_relation' % rtype, attrs)
   255         self.doexec(session.pool[self.uri], sql, attrs)
   253         self.doexec(session, sql, attrs)
   256 
   254 
   257     def doexec(self, cursor, query, args=None):
   255     def doexec(self, session, query, args=None):
   258         """Execute a query.
   256         """Execute a query.
   259         it's a function just so that it shows up in profiling
   257         it's a function just so that it shows up in profiling
   260         """
   258         """
   261         #t1 = time()
       
   262         if server.DEBUG:
   259         if server.DEBUG:
   263             print 'exec', query, args
   260             print 'exec', query, args
   264         #import sys
   261         cursor = session.pool[self.uri]
   265         #sys.stdout.flush()
       
   266         # str(query) to avoid error if it's an unicode string
       
   267         try:
   262         try:
       
   263             # str(query) to avoid error if it's an unicode string
   268             cursor.execute(str(query), args)
   264             cursor.execute(str(query), args)
   269         except Exception, ex:
   265         except Exception, ex:
   270             self.critical("sql: %r\n args: %s\ndbms message: %r",
   266             self.critical("sql: %r\n args: %s\ndbms message: %r",
   271                           query, args, ex.args[0])
   267                           query, args, ex.args[0])
       
   268             try:
       
   269                 session.pool.connection(self.uri).rollback()
       
   270                 self.critical('transaction has been rollbacked')
       
   271             except:
       
   272                 pass
   272             raise
   273             raise
       
   274         return cursor