app/controllers/data_explorer/query_controller.rb
# Return value:
# success - true/false. if false, inspect the errors value.
# errors - array of strings.
# params - hash. Echo of the query parameters as executed.
# duration - float. Time to execute the query, in milliseconds, to 1 decimal place.
# columns - array of strings. Titles of the returned columns, in order.
# explain - string. (Optional - pass explain=true in the request) Postgres query plan, UNIX newlines.
# rows - array of array of strings. Results of the query. In the same order as 'columns'.
Return value:
success - true/false. if false, inspect the errors value.
errors - array of strings.
params - hash. Echo of the query parameters as executed.
duration - float. Time to execute the query, in milliseconds, to 1 decimal place.
columns - array of strings. Titles of the returned columns, in order.
explain - string. (Optional - pass explain=true in the request) Postgres query plan, UNIX newlines.
rows - array of array of strings. Results of the query. In the same order as 'columns'.
Return value : success - true / false . if false inspect the errors value . errors - array of strings . params - hash . Echo of the query parameters as executed . duration - float . Time to execute the query in milliseconds to 1 decimal place . columns - array of strings . Titles of the returned columns in order . explain - string . ( Optional - pass explain = true in the request ) Postgres query plan UNIX newlines . rows - array of array of strings . Results of the query . In the same order as ' columns ' .
def run
    check_xhr unless params[:download]
    query = DataExplorer::Query.find(params[:id].to_i)
    query.update!(last_run_at: Time.now)
    if params[:download]
      response.sending_file = true
    end
    params[:params] = params[:_params] if params[:_params] 
    query_params = {}
    query_params = MultiJson.load(params[:params]) if params[:params]
    opts = { current_user: current_user.username }
    opts[:explain] = true if params[:explain] == "true"
    opts[:limit] =
      if params[:format] == "csv"
        if params[:limit].present?
          limit = params[:limit].to_i
          limit = DataExplorer::QUERY_RESULT_MAX_LIMIT if limit > DataExplorer::QUERY_RESULT_MAX_LIMIT
          limit
        else
          DataExplorer::QUERY_RESULT_MAX_LIMIT
        end
      elsif params[:limit].present?
        params[:limit] == "ALL" ? "ALL" : params[:limit].to_i
      end
    result = DataExplorer.run_query(query, query_params, opts)
    if result[:error]
      err = result[:error]
      err_class = err.class
      err_msg = err.message
      if err.is_a? ActiveRecord::StatementInvalid
        err_class = err.original_exception.class
        err_msg.gsub!("#{err_class}:", '')
      else
        err_msg = "#{err_class}: #{err_msg}"
      end
      render json: {
        success: false,
        errors: [err_msg]
      }, status: 422
    else
      pg_result = result[:pg_result]
      cols = pg_result.fields
      respond_to do |format|
        format.json do
          if params[:download]
            response.headers['Content-Disposition'] =
              "attachment; filename=#{query.slug}@#{Slug.for(Discourse.current_hostname, 'discourse')}-#{Date.today}.dcqresult.json"
          end
          json = {
            success: true,
            errors: [],
            duration: (result[:duration_secs].to_f * 1000).round(1),
            result_count: pg_result.values.length || 0,
            params: query_params,
            columns: cols,
            default_limit: DataExplorer::QUERY_RESULT_DEFAULT_LIMIT
          }
          json[:explain] = result[:explain] if opts[:explain]
          if !params[:download]
            relations, colrender = DataExplorer.add_extra_data(pg_result)
            json[:relations] = relations
            json[:colrender] = colrender
          end
          json[:rows] = pg_result.values
          render json: json
        end
        format.csv do
          response.headers['Content-Disposition'] =
            "attachment; filename=#{query.slug}@#{Slug.for(Discourse.current_hostname, 'discourse')}-#{Date.today}.dcqresult.csv"
          require 'csv'
          text = CSV.generate do |csv|
            csv << cols
            pg_result.values.each do |row|
              csv << row
            end
          end
          render plain: text
        end
      end
    end
  end
def run check_xhr unless params [ :download ] query = DataExplorer :: Query . find ( params [ :id ] . to_i ) query . update! ( last_run_at : Time . now ) if params [ :download ] response . sending_file = true end params [ :params ] = params [ :_params ] if params [ :_params ] query_params = { } query_params = MultiJson . load ( params [ :params ] ) if params [ :params ] opts = { current_user : current_user . username } opts [ :explain ] = true if params [ :explain ] == "true" opts [ :limit ] = if params [ :format ] == "csv" if params [ :limit ] . present? limit = params [ :limit ] . to_i limit = DataExplorer :: QUERY_RESULT_MAX_LIMIT if limit > DataExplorer :: QUERY_RESULT_MAX_LIMIT limit else DataExplorer :: QUERY_RESULT_MAX_LIMIT end elsif params [ :limit ] . present? params [ :limit ] == "ALL" ? "ALL" : params [ :limit ] . to_i end result = DataExplorer . run_query ( query , query_params , opts ) if result [ :error ] err = result [ :error ] err_class = err . class err_msg = err . message if err . is_a? ActiveRecord :: StatementInvalid err_class = err . original_exception . class err_msg . gsub! ( "#{err_class}:" , '' ) else err_msg = "#{err_class}: #{err_msg}" end render json : { success : false , errors : [ err_msg ] } , status : 422 else pg_result = result [ :pg_result ] cols = pg_result . fields respond_to do | format | format . json do if params [ :download ] response . headers [ 'Content-Disposition' ] = "attachment; filename=#{query.slug}@#{Slug.for(Discourse.current_hostname, 'discourse')}-#{Date.today}.dcqresult.json" end json = { success : true , errors : [ ] , duration : ( result [ :duration_secs ] . to_f * 1000 ) . round ( 1 ) , result_count : pg_result . values . length || 0 , params : query_params , columns : cols , default_limit : DataExplorer :: QUERY_RESULT_DEFAULT_LIMIT } json [ :explain ] = result [ :explain ] if opts [ :explain ] if ! params [ :download ] relations , colrender = DataExplorer . add_extra_data ( pg_result ) json [ :relations ] = relations json [ :colrender ] = colrender end json [ :rows ] = pg_result . values render json : json end format . csv do response . headers [ 'Content-Disposition' ] = "attachment; filename=#{query.slug}@#{Slug.for(Discourse.current_hostname, 'discourse')}-#{Date.today}.dcqresult.csv" require 'csv' text = CSV . generate do | csv | csv << cols pg_result . values . each do | row | csv << row end end render plain : text end end end end
Return value:
success - true/false.
Return value : success - true / false .
# testing workaround # Pretty printing logic