class Csv2Psql::Processor
Csv2Psql
processor class
Constants
- DEFAULT_OPTIONS
Attributes
analyzer[R]
generator[R]
output[R]
path[R]
Public Class Methods
new()
click to toggle source
# File lib/csv2psql/processor/processor.rb, line 25 def initialize @output = Output.new @generator = Generator.new(@output) @cache = Cache.new @analyzer = Analyzer.new(@cache) @frontend = Frontend::Csv.new end
Public Instance Methods
analyze(paths, opts = {})
click to toggle source
# File lib/csv2psql/processor/processor.rb, line 33 def analyze(paths, opts = {}) with_files(paths, opts) do |data| analyzer.analyze(data[:path], data[:row], opts) end analyzer end
convert(paths, opts = {})
click to toggle source
# File lib/csv2psql/processor/processor.rb, line 40 def convert(paths, opts = {}) details = {} with_files(paths, opts) do |data| create_converted_header(details, data, opts) output.write generator.format_row(data[:row], opts) end end
create_converted_header(details, data, opts = {})
click to toggle source
# File lib/csv2psql/processor/processor.rb, line 48 def create_converted_header(details, data, opts = {}) detail = get_file_details(details, data[:path]) unless detail[:header] # rubocop:disable Style/GuardClause generator.create_sql_script(data[:path], data[:row], opts) detail[:header] = true end end
create_file_details(files, path)
click to toggle source
# File lib/csv2psql/processor/processor.rb, line 56 def create_file_details(files, path) files[path] = { header: false, lines: 0, line: 0 } files[path] end
generate_schema(paths, opts = {})
click to toggle source
# File lib/csv2psql/processor/processor.rb, line 65 def generate_schema(paths, opts = {}) res = {} paths.each do |path| with_file(path, opts) do |data| path = data[:path] analyzer.analyze(path, data[:row], opts) end analysis = analyzer.files[path] if analysis res[path] = SchemaGenerator.generate(analysis) end end res end
get_file_details(files, path)
click to toggle source
# File lib/csv2psql/processor/processor.rb, line 82 def get_file_details(files, path) if files.key?(path) files[path] else create_file_details(files, path) end end
merge_csv_options(opts = {})
click to toggle source
# File lib/csv2psql/processor/processor.rb, line 90 def merge_csv_options(opts = {}) header = !opts['header'].nil? ? opts['header'] : DEFAULT_OPTIONS['header'] res = { headers: header, quote_char: opts['quote'] || DEFAULT_OPTIONS['quote'] } res[:col_sep] = opts['delimiter'] if opts['delimiter'] res[:col_sep] = opts[:delimiter] if opts[:delimiter] res[:row_sep] = opts['separator'] if opts['separator'] res[:row_sep] = opts[:separator] if opts[:separator] res end
process_file(path, csv, opts, &block)
click to toggle source
# File lib/csv2psql/processor/processor.rb, line 103 def process_file(path, csv, opts, &block) lines = 0 limit = opts[:l] skip = opts[:skip] csv.each do |row| lines += 1 next if skip > 0 && lines <= skip with_row(path, row, opts, &block) return if limit > 0 && lines >= limit end end
with_file(path, opts = {}, &block)
click to toggle source
# File lib/csv2psql/processor/processor.rb, line 117 def with_file(path, opts = {}, &block) output.write 'BEGIN;' if opts[:transaction] csv_opts = merge_csv_options(opts) @first_row = true @frontend.open(path, 'rt', csv_opts) do |csv| process_file(path, csv, opts, &block) end output.write 'COMMIT;' if opts[:transaction] end
with_files(paths, opts = {}, &block)
click to toggle source
# File lib/csv2psql/processor/processor.rb, line 127 def with_files(paths, opts = {}, &block) paths = [paths] unless paths.is_a?(Array) paths.each do |path| with_file(path, opts, &block) end end
with_row(path, row, _opts = {}, &_block)
click to toggle source
# File lib/csv2psql/processor/processor.rb, line 134 def with_row(path, row, _opts = {}, &_block) args = { path: path, row: row } Proc.new.call(args) end