lib/ddr/index/csv_query_result.rb in ddr-models-2.4.0.rc5 vs lib/ddr/index/csv_query_result.rb in ddr-models-2.4.0.rc6

- old
+ new

@@ -2,60 +2,67 @@ module Ddr::Index class CSVQueryResult < AbstractQueryResult MAX_ROWS = 10**8 + MV_SEP = ";" - COL_SEP = CSV::DEFAULT_OPTIONS[:col_sep].freeze - QUOTE_CHAR = CSV::DEFAULT_OPTIONS[:quote_char].freeze + attr_reader :mv_sep - SOLR_CSV_OPTS = { - "csv.header" => "false", - "csv.mv.separator" => ";", - "wt" => "csv", - }.freeze + delegate :read, :each, to: :csv - CSV_OPTS = { - return_headers: true, - write_headers: true, - }.freeze - - attr_reader :csv_opts, :solr_csv_opts - - def initialize(query, **opts) + def initialize(query, mv_sep: MV_SEP) super(query) + @mv_sep = mv_sep + end - @solr_csv_opts = SOLR_CSV_OPTS.dup - @solr_csv_opts[:rows] ||= MAX_ROWS + def csv + CSV.new(data, csv_opts.to_h) + end - @csv_opts = CSV_OPTS.dup - @csv_opts[:headers] = query.fields.map(&:label) + def to_s + read.to_csv + end - # Set column separator and quote character consistently - @csv_opts[:col_sep] = @solr_csv_opts["csv.separator"] = opts.fetch(:col_sep, COL_SEP) - @csv_opts[:quote_char] = @solr_csv_opts["csv.encapsulator"] = opts.fetch(:quote_char, QUOTE_CHAR) + def rows + query.rows || MAX_ROWS end - def csv - CSV.new(data, csv_opts) + def csv_opts + @csv_opts ||= CSVOptions.new(headers: csv_headers) end - def each - csv.each + def solr_csv_opts + @solr_csv_opts ||= SolrCSVOptions.new(col_sep: csv_opts.col_sep, + quote_char: csv_opts.quote_char, + header: solr_csv_header, + mv_sep: mv_sep, + rows: rows) end - def to_s - csv.string + def headers + @headers ||= query.fields.map(&:heading) end - private + def csv_headers + if headers.empty? + :first_row + else + headers + end + end - def csv_params - params.merge(solr_csv_opts) + def solr_csv_header + csv_headers == :first_row end + def solr_csv_params + params.merge solr_csv_opts.params + end + def data - conn.get "select", params: csv_params + raw = conn.get("select", params: solr_csv_params) + raw.gsub(/\\#{mv_sep}/, mv_sep) end end end