Class: Csv2Psql::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/csv2psql/processor/processor.rb

Overview

Csv2Psql processor class

Constant Summary collapse

DEFAULT_OPTIONS =
ConfigHelper.config['processor']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessor

Returns a new instance of Processor.



25
26
27
28
29
30
31
# 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

Instance Attribute Details

#analyzerObject (readonly)

Returns the value of attribute analyzer.



21
22
23
# File 'lib/csv2psql/processor/processor.rb', line 21

def analyzer
  @analyzer
end

#generatorObject (readonly)

Returns the value of attribute generator.



21
22
23
# File 'lib/csv2psql/processor/processor.rb', line 21

def generator
  @generator
end

#outputObject (readonly)

Returns the value of attribute output.



21
22
23
# File 'lib/csv2psql/processor/processor.rb', line 21

def output
  @output
end

#pathObject (readonly)

Returns the value of attribute path.



21
22
23
# File 'lib/csv2psql/processor/processor.rb', line 21

def path
  @path
end

Instance Method Details

#analyze(paths, opts = {}) ⇒ Object



33
34
35
36
37
38
# 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 = {}) ⇒ Object



40
41
42
43
44
45
46
# 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 = {}) ⇒ Object



48
49
50
51
52
53
54
# 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) ⇒ Object



56
57
58
59
60
61
62
63
# 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 = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object



82
83
84
85
86
87
88
# 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 = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# 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) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# 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) ⇒ Object



117
118
119
120
121
122
123
124
125
# 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) ⇒ Object



127
128
129
130
131
132
# 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) ⇒ Object



134
135
136
137
# 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