Module: DbAgile::Command::Bulk::Commons

Included in:
Export, Import
Defined in:
lib/dbagile/command/bulk/commons.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#allbutObject

Columns to allbut



25
26
27
# File 'lib/dbagile/command/bulk/commons.rb', line 25

def allbut
  @allbut
end

#conn_optionsObject

Connection options



10
11
12
# File 'lib/dbagile/command/bulk/commons.rb', line 10

def conn_options
  @conn_options
end

#datasetObject

Dataset whose contents must be shown



19
20
21
# File 'lib/dbagile/command/bulk/commons.rb', line 19

def dataset
  @dataset
end

#formatObject

Output/input format [ruby, csv, json]



7
8
9
# File 'lib/dbagile/command/bulk/commons.rb', line 7

def format
  @format
end

#io_optionsObject

Input/output options



13
14
15
# File 'lib/dbagile/command/bulk/commons.rb', line 13

def io_options
  @io_options
end

#selectObject

Columns to select



22
23
24
# File 'lib/dbagile/command/bulk/commons.rb', line 22

def select
  @select
end

#type_systemObject

Type system to use



16
17
18
# File 'lib/dbagile/command/bulk/commons.rb', line 16

def type_system
  @type_system
end

Instance Method Details

#add_csv_options(opt) ⇒ Object Also known as: add_csv_input_options, add_csv_output_options

Adds the CSV options



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dbagile/command/bulk/commons.rb', line 88

def add_csv_options(opt)
  opt.on("--headers", "-h", "Read/Write column names on first line") do
    io_options[:csv][:headers] = true
  end
  opt.on("--separator=C", "Use C as column separator character") do |value|
    io_options[:csv][:col_sep] = value
  end
  opt.on("--quote=C", "Use C as quoting character") do |value|
    io_options[:csv][:quote_char] = value
  end
  opt.on("--force-quotes", "Force quoting?") do 
    io_options[:csv][:force_quotes] = true
  end 
  opt.on("--skip-blanks", "Skip blank lines?") do 
    io_options[:csv][:skip_blanks] = true
  end 
end

#add_html_output_options(opt) ⇒ Object

Adds output HTML options



129
130
# File 'lib/dbagile/command/bulk/commons.rb', line 129

def add_html_output_options(opt)
end

#add_input_format_options(opt) ⇒ Object

Adds the format options for input



76
77
78
79
80
81
82
83
84
85
# File 'lib/dbagile/command/bulk/commons.rb', line 76

def add_input_format_options(opt)
  opt.on('--format=X', [:csv, :json, :yaml, :ruby],
         "Import dataset from (csv, json, yaml, ruby)") do |value|
    self.format = value
  end
  opt.on("--csv",  "Import dataset from csv (default)"){ self.format = :csv }
  opt.on("--json", "Import dataset from json"){ self.format = :json }
  opt.on("--ruby", "Import dataset from ruby code"){ self.format = :ruby }
  opt.on("--yaml", "Import dataset from yaml"){ self.format = :yaml }
end

#add_json_output_options(opt) ⇒ Object

Adds output JSON options



109
110
111
112
113
# File 'lib/dbagile/command/bulk/commons.rb', line 109

def add_json_output_options(opt)
  opt.on("--[no-]pretty", "Generate a pretty JSON document") do |value|
    io_options[:json][:pretty] = value
  end 
end

#add_output_format_options(opt) ⇒ Object

Adds the format options for output



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dbagile/command/bulk/commons.rb', line 61

def add_output_format_options(opt)
  opt.on('--format=X', [:csv, :text, :json, :yaml, :ruby, :xml, :html],
         "Export dataset in (csv, text, json, yaml, ruby, xml, html)") do |value|
    self.format = value
  end
  opt.on("--csv",  "Export dataset in csv (default)"){ self.format = :csv }
  opt.on("--text", "Export dataset as a plain text table"){ self.format = :text }
  opt.on("--json", "Export dataset in json"){ self.format = :json }
  opt.on("--yaml", "Export dataset in yaml"){ self.format = :yaml }
  opt.on("--xml",  "Export dataset in xml"){ self.format = :xml }
  opt.on("--html",  "Export dataset in html"){ self.format = :html }
  opt.on("--ruby", "Export dataset as ruby code"){ self.format = :ruby }
end

#add_select_options(opt) ⇒ Object

Adds the select/allbut options



36
37
38
39
40
41
42
43
44
45
# File 'lib/dbagile/command/bulk/commons.rb', line 36

def add_select_options(opt)
  opt.on('--select x,y,z', Array,
         "Select x, y, z columns only") do |value|
    self.select = value.collect{|c| c.to_sym}
  end
  opt.on('--allbut x,y,z', Array,
         "Select all but x, y, z columns") do |value|
    self.allbut = value.collect{|c| c.to_sym}
  end
end

#add_text_output_options(opt) ⇒ Object

Adds output TEXT options



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/dbagile/command/bulk/commons.rb', line 116

def add_text_output_options(opt)
  opt.on("--wrap-at=X", Integer,
         "Wraps table after X's character (no wrap by default)") do |x|
    io_options[:text][:wrap_at] = x
  end
  opt.on("--truncate-at=X", Integer,
         "Truncate row lines at X character") do |x|
    io_options[:text][:truncate_at] = x
    io_options[:text][:append_with] = '...'
  end
end

#add_typesafe_options(opt) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dbagile/command/bulk/commons.rb', line 47

def add_typesafe_options(opt)
  opt.on("--type-safe=[X]", [:ruby], 
         "Read/Write type-safe values for (ruby)") do |value|
    case value
      when :ruby, NilClass
        require 'sbyc/type_system/ruby'
        self.type_system = SByC::TypeSystem::Ruby
      else
        raise ArgumentError, "Unknown type system #{value}"
    end
  end
end

#set_default_optionsObject

Builds default io options



28
29
30
31
32
33
# File 'lib/dbagile/command/bulk/commons.rb', line 28

def set_default_options
  self.format = :csv
  self.type_system = nil
  self.conn_options = {}
  self.io_options = Hash.new{|h,k| h[k] = {}}
end