Class: Csvsql::CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/csvsql/command_runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run!(argv) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/csvsql/command_runner.rb', line 6

def self.run!(argv)
  options = self.new.parse!(argv)
  return unless options

  if options[:clear_cache]
    Csvsql.clear_cache!
    puts "Completed clear cache."
    return
  end

  if options[:debug]
    Csvsql::Tracker.tracker = Csvsql::Tracker.new(Logger.new($stdout))
  end

  Csvsql.execute(
    options[:sql], options[:csv_data],
    use_cache: options[:use_cache],
    batch_rows: options[:batch_rows],
    sql_error_action: 'exit',
    encoding: options[:encoding]
  )
end

Instance Method Details

#optionsObject



29
30
31
# File 'lib/csvsql/command_runner.rb', line 29

def options
  @options ||= { csv_paths: [] }
end

#parse!(argv) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/csvsql/command_runner.rb', line 33

def parse!(argv)
  parser.parse!(argv)
  options[:sql] = argv.last

  paths = options.delete(:csv_paths)
  options[:csv_data] = case paths.size
  when 0
    $stdin
  when 1
    paths.first
  else
    paths.each_with_object({}) do |path, r|
      p, n = path.split(':')
      if n.nil? || n.empty?
        puts "You should give #{p} a name, example: #{p}:a_name"
        return false
      end
      r[n] = p
    end
  end

  return options
end