lib/ddr/index/csv_query_result.rb in ddr-models-2.11.0 vs lib/ddr/index/csv_query_result.rb in ddr-models-3.0.0.alpha.1
- old
+ new
@@ -1,76 +1,61 @@
require "csv"
module Ddr::Index
class CSVQueryResult < AbstractQueryResult
- MAX_ROWS = 10**8 # Just set to a really high number :)
- CSV_MV_SEPARATOR = ";"
+ MAX_ROWS = 10**8
- delegate :headers, :to_s, :to_csv, to: :table
+ COL_SEP = CSV::DEFAULT_OPTIONS[:col_sep].freeze
+ QUOTE_CHAR = CSV::DEFAULT_OPTIONS[:quote_char].freeze
- def delete_empty_columns!
- table.by_col!.delete_if { |c, vals| vals.all?(&:nil?) }
- end
+ SOLR_CSV_OPTS = {
+ "csv.header" => "false",
+ "csv.mv.separator" => "|",
+ "wt" => "csv",
+ }.freeze
- def each(&block)
- table.by_row!.each(&block)
- end
+ CSV_OPTS = {
+ return_headers: true,
+ write_headers: true,
+ }.freeze
- def [](index_or_header)
- table.by_col_or_row![index_or_header]
- end
+ attr_reader :csv_opts, :solr_csv_opts
- def table
- @table ||= CSV.parse(data, csv_opts)
- end
+ def initialize(query, **opts)
+ super(query)
- def csv_opts
- { headers: csv_headers,
- converters: [convert_semicolons, convert_escaped_newlines],
- return_headers: false,
- write_headers: true,
- }
- end
+ @solr_csv_opts = SOLR_CSV_OPTS.dup
+ @solr_csv_opts[:rows] ||= MAX_ROWS
- def solr_csv_opts
- { "csv.mv.separator" => CSV_MV_SEPARATOR,
- "csv.header" => solr_csv_header?,
- "rows" => solr_csv_rows,
- "wt" => "csv",
- }
- end
+ @csv_opts = CSV_OPTS.dup
+ @csv_opts[:headers] = query.fields.map(&:label)
- def query_field_headings
- query.fields.map { |f| f.respond_to?(:heading) ? f.heading : f.to_s }
+ # 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)
end
- def csv_headers
- query.fields.empty? ? :first_row : query_field_headings
+ def csv
+ CSV.new(data, csv_opts)
end
- def solr_csv_header?
- csv_headers == :first_row
+ def each
+ csv.each
end
- def solr_csv_rows
- query.rows || MAX_ROWS
+ def to_s
+ csv.string
end
- def solr_csv_params
+ private
+
+ def csv_params
params.merge(solr_csv_opts)
end
def data
- Connection.get("select", params: solr_csv_params)
- end
-
- def convert_semicolons
- lambda { |f| f.gsub(/\\#{CSV_MV_SEPARATOR}/, CSV_MV_SEPARATOR) rescue f }
- end
-
- def convert_escaped_newlines
- lambda { |f| f.gsub(/\\r/, "\r").gsub(/\\n/, "\n") rescue f }
+ conn.get "select", params: csv_params
end
end
end