Class: DbAgile::Command::Bulk::Export

Inherits:
DbAgile::Command show all
Includes:
Commons
Defined in:
lib/dbagile/command/bulk/export.rb

Overview

Export a table/view/query to (csv, json, yaml, ruby, xml)

Usage: dba #DbAgile::Command#command_name [OPTIONS] DATASET [FILE]

This command helps exporting data in a SQL database to common transfer formats.

Constant Summary

Constants inherited from DbAgile::Command

CATEGORIES, CATEGORY_NAMES

Instance Attribute Summary collapse

Attributes included from Commons

#allbut, #conn_options, #dataset, #format, #io_options, #select, #type_system

Attributes inherited from DbAgile::Command

#environment

Attributes included from ClassMethods

#description, #summary, #usage

Instance Method Summary collapse

Methods included from Commons

#add_csv_options, #add_html_output_options, #add_input_format_options, #add_json_output_options, #add_output_format_options, #add_select_options, #add_text_output_options, #add_typesafe_options, #set_default_options

Methods inherited from DbAgile::Command

#category, #check_command, #command_name, #description, #initialize, #options, #run, #set_default_options, #show_help, #summary, #unsecure_run, #usage

Methods included from ClassMethods

#build_command_options, #build_me, #category, #command_for, #command_name, #command_name_of, #each_subclass, #inherited, #ruby_method_for, #subclasses

Methods included from Robust

#ambigous_argument_list!, #assumption_error!, #bad_argument_list!, #has_command!, #is_in!, #valid_argument_list!, #valid_read_file!

Methods included from DbAgile::Core::IO::Robustness

#has_database!, #valid_database_name!, #valid_database_uri!, #valid_schema_files!

Constructor Details

This class inherits a constructor from DbAgile::Command

Instance Attribute Details

#output_fileObject

Output file to use



17
18
19
# File 'lib/dbagile/command/bulk/export.rb', line 17

def output_file
  @output_file
end

Instance Method Details

#add_options(opt) ⇒ Object

Contribute to options



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dbagile/command/bulk/export.rb', line 20

def add_options(opt)
  # Main output options
  opt.separator "\nOutput options:"
  opt.on("--output=FILE", "-o", "Flush output in FILE (stdout by default)") do |value|
    self.output_file = value
  end

  opt.separator "\nRelational options:"
  add_select_options(opt)

  opt.separator "\nRecognized format options:"
  add_output_format_options(opt)
  add_typesafe_options(opt)

  # CSV output options
  opt.separator "\nCSV options:"
  add_csv_output_options(opt)

  # CSV output options
  opt.separator "\nTEXT options:"
  add_text_output_options(opt)

  # HTML output options
  opt.separator "\nHTML options:"
  add_html_output_options(opt)

  # JSON output options
  opt.separator "\nJSON options:"
  add_json_output_options(opt)
end

#execute_commandObject

Executes the command



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dbagile/command/bulk/export.rb', line 79

def execute_command
  with_current_connection do |connection|
    # Prepare the dataset
    ds = connection.dataset(self.dataset)
    if self.select
      ds = ds.select(*self.select)
    elsif self.allbut
      ds = ds.allbut(*self.allbut)
    end

    # Export it
    with_io{|io| 
      method = "to_#{self.format}".to_sym
      options =  io_options[self.format]
      options[:type_system] = self.type_system if self.type_system
      ds.send(method, io, options)
    }
  end
end

#normalize_pending_arguments(arguments) ⇒ Object

Normalizes the pending arguments



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dbagile/command/bulk/export.rb', line 52

def normalize_pending_arguments(arguments)
  case arguments.size
    when 1
      self.dataset = valid_argument_list!(arguments, String)
    when 2
      raise OptionParser::AmbiguousArgument, '--output-file=#{self.output_file}' if self.output_file
      self.dataset, self.output_file = valid_argument_list!(arguments, String, String)
    else
      bad_argument_list!(arguments)
  end
  unless /select|SELECT/ =~ self.dataset
    self.dataset = self.dataset.to_sym
  end
end

#with_io(&block) ⇒ Object

Yields the block with the IO object to use for output



68
69
70
71
72
73
74
75
76
# File 'lib/dbagile/command/bulk/export.rb', line 68

def with_io(&block)
  if self.output_file
    require 'fileutils'
    FileUtils.mkdir_p(File.dirname(self.output_file))
    File.open(self.output_file, "w", &block)
  else
    block.call(environment.output_buffer)
  end
end