 
                 @index++
                 if rdb_query instanceof @TermBaseConstructor
-                    @skip_value = 0
                     @start_time = new Date()
                     @current_results = []
 
-                    @id_execution++ # Update the id_execution and use it to tag the callbacks
-                    rdb_global_callback = @generate_rdb_global_callback @id_execution
-                    # Date are displayed in their raw format for now.
-                    @state.last_query_has_profile = @state.options.profiler
+                    query_result = new DataExplorerView.QueryResult
+                        profile: @state.options.profiler
+                        events:
+                            sync: @query_result_callback
+                            error: (err) =>
+                                @toggle_executing false
+                                @results_view.render_error(@query, err)
+
                     @driver_handler.create_connection (error, connection) =>
                         if (error)
                             @error_on_connect error
                         else
                             rdb_query.private_run connection,
                                 {binaryFormat: "raw", timeFormat: "raw", profile: @state.options.profiler},
-                                rdb_global_callback # @rdb_global_callback can be fired more than once
-                    , @id_execution, @error_on_connect
+                                query_result.set
+                    , @error_on_connect
 
                     return true
                 else if rdb_query instanceof DataExplorerView.DriverHandler

                    flatten_attr: flatten_attr

        tag_record: (doc, i) =>
            doc.record = @metadata.skip_value + i

        render_result: (args) =>
            if args? and args.results isnt undefined
                @results = args.results
                @results_array = null # if @results is not an array (possible starting from 1.4), we will transform @results_array to [@results] for the table view

            profiler: false
        history: []
        focus_on_codemirror: true

    # This class represents the results of a query.
    #
    # If there is a profile, `profile` is set
    #
    # After a 'sync' event, one of `error`, `value` or `cursor` is always set
    #
    # It triggers the following events:
    #
    #  * sync: The first response has been received
    #
    #  * add: Another row has been received from a cursor
    #
    #  * error: An error has been received from a cursor
    #
    #  * end: There are no more documents to fetch
    #
    #  * discard: The results have been discarded
    class @QueryResult
        _.extend @::, Backbone.Events

        constructor: (options) ->
            @has_profile = options.profile
            @discard_results = options.discard_results ? false
            if options.events?
                for own event, handler of options.events
                    @on event, handler

        # Can be used as a callback to run
        set: (error, result) =>
            if error?
                @error = error
            else if not @discard_results
                if @has_profile
                    @profile = result.profile
                    value = result.value
                else
                    value = result
                if typeof value._next is 'function' # if it's a cursor
                    @results = []
                    @truncated = false
                    @cursor = value
                    @is_feed = @cursor.toString() == '[object Feed]'
                    @missing = 0
                    @ended = false
                    @start_index = 0
                else
                    @start_index = 0
                    @value = value
                    @ended = true
            @trigger 'sync', @

        # Remove n elements from the result set of a cursor Waits for
        # n+1 elements, or the end before returning
        #
        # Note: the callbacks are not ordered. In this code, `f` is
        # never called: `qr.shift 2 f; cb = -> qr.shift 1 cb`
        shift: (n, k) =>
            cb = => 
                if @results.length > n or @ended
                    @off 'end', cb
                    ret = @results[0...n]
                    @results = @results[n...]
                    @start_index += n
                    k @, ret
                else
                    @once 'add', cb
                    @more()
            @once 'end', cb
            cb()

        # Discard the results
        discard: =>
            @trigger 'discard', @
            @off()
            @discard_results = true
            delete @profile
            delete @value
            delete @results
            delete @truncated
            @cursor?.close()
            delete @cursor

        # Ask for more results from the cursor
        more: (n) =>
            prev_missing = @missing
            @missing = prev_missing + (n ? 1)
            if prev_missing <= 0
                @fetch_next()

        # Gets the next result from the cursor
        fetch_next: =>
            if not @ended
                @cursor.next (error, row) =>
                    if error?
                        if error.message is 'No more rows in the cursor.'
                            @ended = true
                            @trigger 'end', @
                        else
                            @error = error
                            @trigger 'error', @, error
                            @discard_results = true
                    else
                        if @discard_results
                            return
                        @results.push row
                        @trigger 'add', @, row
                        @missing--
                        if @missing >= 0
                            @fetch_next()

    class @Container extends Backbone.View
        id: 'dataexplorer'
