server/sqlutils.py
branchstable
changeset 5013 ad91f93bbb93
parent 4965 04543ed0bbdc
child 5214 3285b6e3b930
equal deleted inserted replaced
5012:9c4ea944ecf9 5013:ad91f93bbb93
   186             # should not collide
   186             # should not collide
   187             newargs.update(query_args)
   187             newargs.update(query_args)
   188             return newargs
   188             return newargs
   189         return query_args
   189         return query_args
   190 
   190 
   191     def process_result(self, cursor):
   191     def process_result(self, cursor, column_callbacks=None):
   192         """return a list of CubicWeb compliant values from data in the given cursor
   192         """return a list of CubicWeb compliant values from data in the given cursor
   193         """
   193         """
       
   194         # use two different implementations to avoid paying the price of
       
   195         # callback lookup for each *cell* in results when there is nothing to
       
   196         # lookup
       
   197         if not column_callbacks:
       
   198             return self._process_result(cursor)
       
   199         return self._cb_process_result(cursor, column_callbacks)
       
   200 
       
   201     def _process_result(self, cursor, column_callbacks=None):
   194         # begin bind to locals for optimization
   202         # begin bind to locals for optimization
   195         descr = cursor.description
   203         descr = cursor.description
   196         encoding = self._dbencoding
   204         encoding = self._dbencoding
   197         process_value = self._process_value
   205         process_value = self._process_value
   198         binary = Binary
   206         binary = Binary
   203             for col, value in enumerate(line):
   211             for col, value in enumerate(line):
   204                 if value is None:
   212                 if value is None:
   205                     result.append(value)
   213                     result.append(value)
   206                     continue
   214                     continue
   207                 result.append(process_value(value, descr[col], encoding, binary))
   215                 result.append(process_value(value, descr[col], encoding, binary))
       
   216             results[i] = result
       
   217         return results
       
   218 
       
   219     def _cb_process_result(self, cursor, column_callbacks):
       
   220         # begin bind to locals for optimization
       
   221         descr = cursor.description
       
   222         encoding = self._dbencoding
       
   223         process_value = self._process_value
       
   224         binary = Binary
       
   225         # /end
       
   226         results = cursor.fetchall()
       
   227         for i, line in enumerate(results):
       
   228             result = []
       
   229             for col, value in enumerate(line):
       
   230                 if value is None:
       
   231                     result.append(value)
       
   232                     continue
       
   233                 cbstack = column_callbacks.get(col, None)
       
   234                 if cbstack is None:
       
   235                     value = process_value(value, descr[col], encoding, binary)
       
   236                 else:
       
   237                     for cb in cbstack:
       
   238                         value = cb(self, value)
       
   239                 result.append(value)
   208             results[i] = result
   240             results[i] = result
   209         return results
   241         return results
   210 
   242 
   211     def preprocess_entity(self, entity):
   243     def preprocess_entity(self, entity):
   212         """return a dictionary to use as extra argument to cursor.execute
   244         """return a dictionary to use as extra argument to cursor.execute
   275     cnx.create_function("TEXT_LIMIT_SIZE", 2, limit_size2)
   307     cnx.create_function("TEXT_LIMIT_SIZE", 2, limit_size2)
   276 
   308 
   277     import yams.constraints
   309     import yams.constraints
   278     yams.constraints.patch_sqlite_decimal()
   310     yams.constraints.patch_sqlite_decimal()
   279 
   311 
   280     def fspath(eid, etype, attr):
       
   281         try:
       
   282             cu = cnx.cursor()
       
   283             cu.execute('SELECT X.cw_%s FROM cw_%s as X '
       
   284                        'WHERE X.cw_eid=%%(eid)s' % (attr, etype),
       
   285                        {'eid': eid})
       
   286             return cu.fetchone()[0]
       
   287         except:
       
   288             import traceback
       
   289             traceback.print_exc()
       
   290             raise
       
   291     cnx.create_function('fspath', 3, fspath)
       
   292 
       
   293     def _fsopen(fspath):
       
   294         if fspath:
       
   295             try:
       
   296                 return buffer(file(fspath).read())
       
   297             except:
       
   298                 import traceback
       
   299                 traceback.print_exc()
       
   300                 raise
       
   301     cnx.create_function('_fsopen', 1, _fsopen)
       
   302 
       
   303 sqlite_hooks = SQL_CONNECT_HOOKS.setdefault('sqlite', [])
   312 sqlite_hooks = SQL_CONNECT_HOOKS.setdefault('sqlite', [])
   304 sqlite_hooks.append(init_sqlite_connexion)
   313 sqlite_hooks.append(init_sqlite_connexion)