server/sources/native.py
changeset 2593 16d9419a4a79
parent 2589 92f2bc945261
child 2610 2933cc6bf9ad
equal deleted inserted replaced
2592:c97c4b56e6a0 2593:16d9419a4a79
    42 
    42 
    43     def execute(self, query, args=None):
    43     def execute(self, query, args=None):
    44         """Execute a query.
    44         """Execute a query.
    45         it's a function just so that it shows up in profiling
    45         it's a function just so that it shows up in profiling
    46         """
    46         """
    47         if server.DEBUG:
    47         if server.DEBUG & server.DBG_SQL:
    48             print 'exec', query, args
    48             print 'exec', query, args
    49         try:
    49         try:
    50             self.cu.execute(str(query), args)
    50             self.cu.execute(str(query), args)
    51         except Exception, ex:
    51         except Exception, ex:
    52             print "sql: %r\n args: %s\ndbms message: %r" % (
    52             print "sql: %r\n args: %s\ndbms message: %r" % (
   303         a rql syntax tree and a solution dictionary mapping each used
   303         a rql syntax tree and a solution dictionary mapping each used
   304         variable to a possible type). If cachekey is given, the query
   304         variable to a possible type). If cachekey is given, the query
   305         necessary to fetch the results (but not the results themselves)
   305         necessary to fetch the results (but not the results themselves)
   306         may be cached using this key.
   306         may be cached using this key.
   307         """
   307         """
   308         if server.DEBUG:
   308         if server.DEBUG & server.DBG_RQL:
   309             print 'RQL FOR NATIVE SOURCE', self.uri, cachekey
   309             print 'RQL FOR NATIVE SOURCE %s: %s' % (self.uri, union.as_string())
   310             if varmap:
   310             if varmap:
   311                 print 'USING VARMAP', varmap
   311                 print 'using varmap', varmap
   312             print union.as_string()
   312             if args:
   313             if args: print 'ARGS', args
   313                 print 'args', args
   314             print 'SOLUTIONS', ','.join(str(s.solutions) for s in union.children)
   314             if server.DEBUG & server.DBG_MORE:
       
   315                 print 'cache key', cachekey
       
   316                 print 'solutions', ','.join(str(s.solutions) for s in union.children)
   315         # remember number of actually selected term (sql generation may append some)
   317         # remember number of actually selected term (sql generation may append some)
   316         if cachekey is None:
   318         if cachekey is None:
   317             self.no_cache += 1
   319             self.no_cache += 1
   318             # generate sql query if we are able to do so (not supported types...)
   320             # generate sql query if we are able to do so (not supported types...)
   319             sql, query_args = self._rql_sqlgen.generate(union, args, varmap)
   321             sql, query_args = self._rql_sqlgen.generate(union, args, varmap)
   335             # FIXME: better detection of deconnection pb
   337             # FIXME: better detection of deconnection pb
   336             self.info("request failed '%s' ... retry with a new cursor", sql)
   338             self.info("request failed '%s' ... retry with a new cursor", sql)
   337             session.pool.reconnect(self)
   339             session.pool.reconnect(self)
   338             cursor = self.doexec(session, sql, args)
   340             cursor = self.doexec(session, sql, args)
   339         res = self.process_result(cursor)
   341         res = self.process_result(cursor)
   340         if server.DEBUG:
   342         if server.DEBUG & (server.DBG_SQL | server.DBG_RQL):
   341             print '------>', res
   343             print '------>', res
   342         return res
   344         return res
   343 
   345 
   344     def flying_insert(self, table, session, union, args=None, varmap=None):
   346     def flying_insert(self, table, session, union, args=None, varmap=None):
   345         """similar as .syntax_tree_search, but inserts data in the
   347         """similar as .syntax_tree_search, but inserts data in the
   346         temporary table (on-the-fly if possible, eg for the system
   348         temporary table (on-the-fly if possible, eg for the system
   347         source whose the given cursor come from). If not possible,
   349         source whose the given cursor come from). If not possible,
   348         inserts all data by calling .executemany().
   350         inserts all data by calling .executemany().
   349         """
   351         """
   350         if self.uri == 'system':
   352         if self.uri == 'system':
   351             if server.DEBUG:
   353             if server.DEBUG & server.DBG_RQL:
   352                 print 'FLYING RQL FOR SOURCE', self.uri
   354                 print 'FLYING RQL FOR SOURCE %s: %s', self.uri, union.as_string()
   353                 if varmap:
   355                 if varmap:
   354                     print 'USING VARMAP', varmap
   356                     print 'USING VARMAP', varmap
   355                 print union.as_string()
   357                 if server.DEBUG & server.DBG_MORE:
   356                 print 'SOLUTIONS', ','.join(str(s.solutions) for s in union.children)
   358                     print 'SOLUTIONS', ','.join(str(s.solutions) for s in union.children)
   357             # generate sql queries if we are able to do so
   359             # generate sql queries if we are able to do so
   358             sql, query_args = self._rql_sqlgen.generate(union, args, varmap)
   360             sql, query_args = self._rql_sqlgen.generate(union, args, varmap)
   359             query = 'INSERT INTO %s %s' % (table, sql.encode(self.encoding))
   361             query = 'INSERT INTO %s %s' % (table, sql.encode(self.encoding))
   360             self.doexec(session, query, self.merge_args(args, query_args))
   362             self.doexec(session, query, self.merge_args(args, query_args))
   361         else:
   363         else:
   435         it's a function just so that it shows up in profiling
   437         it's a function just so that it shows up in profiling
   436         """
   438         """
   437         if server.DEBUG:
   439         if server.DEBUG:
   438             print 'exec', query, args
   440             print 'exec', query, args
   439         cursor = session.pool[self.uri]
   441         cursor = session.pool[self.uri]
       
   442         if server.DEBUG & server.DBG_SQL:
       
   443             print 'exec', query, args, session.pool.connection(self.uri)._cnx
   440         try:
   444         try:
   441             # str(query) to avoid error if it's an unicode string
   445             # str(query) to avoid error if it's an unicode string
   442             cursor.execute(str(query), args)
   446             cursor.execute(str(query), args)
   443         except Exception, ex:
   447         except Exception, ex:
   444             self.critical("sql: %r\n args: %s\ndbms message: %r",
   448             self.critical("sql: %r\n args: %s\ndbms message: %r",
   453 
   457 
   454     def doexecmany(self, session, query, args):
   458     def doexecmany(self, session, query, args):
   455         """Execute a query.
   459         """Execute a query.
   456         it's a function just so that it shows up in profiling
   460         it's a function just so that it shows up in profiling
   457         """
   461         """
   458         if server.DEBUG:
   462         if server.DEBUG & server.DBG_SQL:
   459             print 'execmany', query, 'with', len(args), 'arguments'
   463             print 'execmany', query, 'with', len(args), 'arguments'
   460         cursor = session.pool[self.uri]
   464         cursor = session.pool[self.uri]
   461         try:
   465         try:
   462             # str(query) to avoid error if it's an unicode string
   466             # str(query) to avoid error if it's an unicode string
   463             cursor.executemany(str(query), args)
   467             cursor.executemany(str(query), args)