[source/native] session -> cnx
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Tue, 03 Jun 2014 18:03:43 +0200
changeset 10086 98bc2ca1a816
parent 10085 dc315002a2d4
child 10087 ed0b076c119b
[source/native] session -> cnx Also swap process_results arguments order to make cnx come earlier.
server/sources/__init__.py
server/sources/native.py
server/sqlutils.py
--- a/server/sources/__init__.py	Tue Jun 10 16:18:46 2014 +0200
+++ b/server/sources/__init__.py	Tue Jun 03 18:03:43 2014 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -19,10 +19,7 @@
 
 __docformat__ = "restructuredtext en"
 
-import itertools
-from os.path import join, splitext
 from time import time
-from datetime import datetime, timedelta
 from logging import getLogger
 
 from logilab.common import configuration
@@ -31,8 +28,7 @@
 from yams.schema import role_name
 
 from cubicweb import ValidationError, set_log_methods, server
-from cubicweb.schema import VIRTUAL_RTYPES
-from cubicweb.server.sqlutils import SQL_PREFIX
+from cubicweb.server import SOURCE_TYPES
 from cubicweb.server.edition import EditedEntity
 
 
@@ -311,23 +307,15 @@
         """
         pass
 
-    def _load_mapping(self, session=None, **kwargs):
+    def _load_mapping(self, cnx, **kwargs):
         if not 'CWSourceSchemaConfig' in self.schema:
             self.warning('instance is not mapping ready')
             return
-        if session is None:
-            _session = self.repo.internal_session()
-        else:
-            _session = session
-        try:
-            for schemacfg in _session.execute(
-                'Any CFG,CFGO,S WHERE '
-                'CFG options CFGO, CFG cw_schema S, '
-                'CFG cw_for_source X, X eid %(x)s', {'x': self.eid}).entities():
-                self.add_schema_config(schemacfg, **kwargs)
-        finally:
-            if session is None:
-                _session.close()
+        for schemacfg in cnx.execute(
+            'Any CFG,CFGO,S WHERE '
+            'CFG options CFGO, CFG cw_schema S, '
+            'CFG cw_for_source X, X eid %(x)s', {'x': self.eid}).entities():
+            self.add_schema_config(schemacfg, **kwargs)
 
     def add_schema_config(self, schemacfg, checkonly=False):
         """added CWSourceSchemaConfig, modify mapping accordingly"""
@@ -372,33 +360,33 @@
         """return the external id for the given newly inserted entity"""
         raise NotImplementedError(self)
 
-    def add_entity(self, session, entity):
+    def add_entity(self, cnx, entity):
         """add a new entity to the source"""
         raise NotImplementedError(self)
 
-    def update_entity(self, session, entity):
+    def update_entity(self, cnx, entity):
         """update an entity in the source"""
         raise NotImplementedError(self)
 
-    def delete_entities(self, session, entities):
+    def delete_entities(self, cnx, entities):
         """delete several entities from the source"""
         for entity in entities:
-            self.delete_entity(session, entity)
+            self.delete_entity(cnx, entity)
 
-    def delete_entity(self, session, entity):
+    def delete_entity(self, cnx, entity):
         """delete an entity from the source"""
         raise NotImplementedError(self)
 
-    def add_relation(self, session, subject, rtype, object):
+    def add_relation(self, cnx, subject, rtype, object):
         """add a relation to the source"""
         raise NotImplementedError(self)
 
-    def add_relations(self, session,  rtype, subj_obj_list):
+    def add_relations(self, cnx,  rtype, subj_obj_list):
         """add a relations to the source"""
         # override in derived classes if you feel you can
         # optimize
         for subject, object in subj_obj_list:
-            self.add_relation(session, subject, rtype, object)
+            self.add_relation(cnx, subject, rtype, object)
 
     def delete_relation(self, session, subject, rtype, object):
         """delete a relation from the source"""
@@ -406,57 +394,56 @@
 
     # system source interface #################################################
 
-    def eid_type_source(self, session, eid):
+    def eid_type_source(self, cnx, eid):
         """return a tuple (type, source, extid) for the entity with id <eid>"""
         raise NotImplementedError(self)
 
-    def create_eid(self, session):
+    def create_eid(self, cnx):
         raise NotImplementedError(self)
 
-    def add_info(self, session, entity, source, extid):
+    def add_info(self, cnx, entity, source, extid):
         """add type and source info for an eid into the system table"""
         raise NotImplementedError(self)
 
-    def update_info(self, session, entity, need_fti_update):
+    def update_info(self, cnx, entity, need_fti_update):
         """mark entity as being modified, fulltext reindex if needed"""
         raise NotImplementedError(self)
 
-    def index_entity(self, session, entity):
+    def index_entity(self, cnx, entity):
         """create an operation to [re]index textual content of the given entity
         on commit
         """
         raise NotImplementedError(self)
 
-    def fti_unindex_entities(self, session, entities):
+    def fti_unindex_entities(self, cnx, entities):
         """remove text content for entities from the full text index
         """
         raise NotImplementedError(self)
 
-    def fti_index_entities(self, session, entities):
+    def fti_index_entities(self, cnx, entities):
         """add text content of created/modified entities to the full text index
         """
         raise NotImplementedError(self)
 
     # sql system source interface #############################################
 
-    def sqlexec(self, session, sql, args=None):
+    def sqlexec(self, cnx, sql, args=None):
         """execute the query and return its result"""
         raise NotImplementedError(self)
 
-    def create_index(self, session, table, column, unique=False):
+    def create_index(self, cnx, table, column, unique=False):
         raise NotImplementedError(self)
 
-    def drop_index(self, session, table, column, unique=False):
+    def drop_index(self, cnx, table, column, unique=False):
         raise NotImplementedError(self)
 
 
-    @deprecated('[3.13] use extid2eid(source, value, etype, session, **kwargs)')
-    def extid2eid(self, value, etype, session, **kwargs):
-        return self.repo.extid2eid(self, value, etype, session, **kwargs)
+    @deprecated('[3.13] use extid2eid(source, value, etype, cnx, **kwargs)')
+    def extid2eid(self, value, etype, cnx, **kwargs):
+        return self.repo.extid2eid(self, value, etype, cnx, **kwargs)
 
 
 
-from cubicweb.server import SOURCE_TYPES
 
 def source_adapter(source_type):
     try:
--- a/server/sources/native.py	Tue Jun 10 16:18:46 2014 +0200
+++ b/server/sources/native.py	Tue Jun 03 18:03:43 2014 +0200
@@ -566,7 +566,7 @@
                 cursor = self.doexec(cnx, sql, args)
             else:
                 raise
-        results = self.process_result(cursor, cbs, session=cnx)
+        results = self.process_result(cursor, cnx, cbs)
         assert dbg_results(results)
         return results
 
--- a/server/sqlutils.py	Tue Jun 10 16:18:46 2014 +0200
+++ b/server/sqlutils.py	Tue Jun 03 18:03:43 2014 +0200
@@ -375,12 +375,12 @@
             return newargs
         return query_args
 
-    def process_result(self, cursor, column_callbacks=None, session=None):
+    def process_result(self, cursor, cnx=None, column_callbacks=None):
         """return a list of CubicWeb compliant values from data in the given cursor
         """
-        return list(self.iter_process_result(cursor, column_callbacks, session))
+        return list(self.iter_process_result(cursor, cnx, column_callbacks))
 
-    def iter_process_result(self, cursor, column_callbacks=None, session=None):
+    def iter_process_result(self, cursor, cnx, column_callbacks=None):
         """return a iterator on tuples of CubicWeb compliant values from data
         in the given cursor
         """
@@ -390,10 +390,10 @@
         if not column_callbacks:
             return self.dbhelper.dbapi_module.process_cursor(cursor, self._dbencoding,
                                                              Binary)
-        assert session
-        return self._cb_process_result(cursor, column_callbacks, session)
+        assert cnx
+        return self._cb_process_result(cursor, column_callbacks, cnx)
 
-    def _cb_process_result(self, cursor, column_callbacks, session):
+    def _cb_process_result(self, cursor, column_callbacks, cnx):
         # begin bind to locals for optimization
         descr = cursor.description
         encoding = self._dbencoding
@@ -416,7 +416,7 @@
                         value = process_value(value, descr[col], encoding, binary)
                     else:
                         for cb in cbstack:
-                            value = cb(self, session, value)
+                            value = cb(self, cnx, value)
                     result.append(value)
                 yield result