Rails raw query for csv format, to be returned via controller

前端 未结 2 522
闹比i
闹比i 2021-02-02 13:21

I was using active record to get my stories and then generate a CSV, the standard way as done in the rails cast. But I have a lot of rows and it takes minutes. I think if I coul

2条回答
  •  眼角桃花
    2021-02-02 13:59

    AFAIK you need to use the copy_data method on the underlying PostgreSQL database connection for this:

    - (Object) copy_data(sql)

    call-seq:

    conn.copy_data( sql ) {|sql_result| ... } -> PG::Result
    

    Execute a copy process for transferring [sic] data to or from the server.

    This issues the SQL COPY command via #exec. The response to this (if there is no error in the command) is a PG::Result object that is passed to the block, bearing a status code of PGRES_COPY_OUT or PGRES_COPY_IN (depending on the specified copy direction). The application should then use #put_copy_data or #get_copy_data to receive or transmit data rows and should return from the block when finished.

    And there's even an example:

    conn.copy_data "COPY my_table TO STDOUT CSV" do
      while row=conn.get_copy_data
        p row
      end
    end
    

    ActiveRecord's wrapper for the raw database connection doesn't know what copy_data is but you can use raw_connection to unwrap it:

    conn = ActiveRecord::Base.connection.raw_connection
    csv  = [ ]
    conn.copy_data('copy stories to stdout with csv header') do
      while row = conn.get_copy_data
        csv.push(row)
      end
    end
    

    That would leave you with an array of CSV strings in csv (one CSV row per array entry) and you could csv.join("\r\n") to get the final CSV data.

提交回复
热议问题