Class: DbAgile::Command::Bulk::Import
- Inherits:
-
DbAgile::Command
- Object
- DbAgile::Command
- DbAgile::Command::Bulk::Import
- Includes:
- Commons, Tools::Tuple
- Defined in:
- lib/dbagile/command/bulk/import.rb
Overview
Import a table from (csv, json, yaml, ruby)
Usage: dba #DbAgile::Command#command_name [OPTIONS] TABLE [FILE]
Constant Summary
Constants inherited from DbAgile::Command
Instance Attribute Summary collapse
-
#create_table ⇒ Object
Create table?.
-
#drop_table ⇒ Object
Drop table?.
-
#input_file ⇒ Object
Input file to use.
-
#truncate_table ⇒ Object
Truncate table?.
-
#update_on_existing ⇒ Object
Update tuple when key already exists?.
Attributes included from Commons
#allbut, #conn_options, #dataset, #format, #io_options, #select, #type_system
Attributes inherited from DbAgile::Command
Attributes included from ClassMethods
#description, #summary, #usage
Instance Method Summary collapse
-
#add_options(opt) ⇒ Object
Contribute to options.
-
#execute_command ⇒ Object
Executes the command.
- #find_key(t, tuple) ⇒ Object
-
#handle_schema_modification(t, tuple) ⇒ Object
Handles the schema modifications.
-
#make_the_job(t, tuple, first = true) ⇒ Object
Makes the insert job.
-
#normalize_pending_arguments(arguments) ⇒ Object
Normalizes the pending arguments.
-
#with_emitter(&block) ⇒ Object
Yields the block with each emitted tuple.
-
#with_io(&block) ⇒ Object
Yields the block with the IO object to use for input.
Methods included from Tools::Tuple
#check_tuple_heading, #tuple_has_key?, #tuple_heading, #tuple_key, #tuple_project, #tuple_to_querystring
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
#create_table ⇒ Object
Create table?
21 22 23 |
# File 'lib/dbagile/command/bulk/import.rb', line 21 def create_table @create_table end |
#drop_table ⇒ Object
Drop table?
18 19 20 |
# File 'lib/dbagile/command/bulk/import.rb', line 18 def drop_table @drop_table end |
#input_file ⇒ Object
Input file to use
15 16 17 |
# File 'lib/dbagile/command/bulk/import.rb', line 15 def input_file @input_file end |
#truncate_table ⇒ Object
Truncate table?
24 25 26 |
# File 'lib/dbagile/command/bulk/import.rb', line 24 def truncate_table @truncate_table end |
#update_on_existing ⇒ Object
Update tuple when key already exists?
27 28 29 |
# File 'lib/dbagile/command/bulk/import.rb', line 27 def update_on_existing @update_on_existing end |
Instance Method Details
#add_options(opt) ⇒ Object
Contribute to options
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/dbagile/command/bulk/import.rb', line 30 def (opt) # Main output options opt.separator "\nInput options:" opt.on("--input=FILE", "-i", "Read input from FILE (stdin by default)") do |value| self.input_file = value end opt.separator "\nSQL options:" opt.on("--create-table", "-c", "Create table before inserting values") do |value| self.drop_table = true self.create_table = true self.truncate_table = false end opt.on("--drop-table", "-d", "Drop table before inserting values") do |value| self.drop_table = true self.create_table = true self.truncate_table = false end opt.on("--truncate", "-t", "Truncate table before inserting values") do |value| self.truncate_table = true end opt.on('--trace-sql', "Trace SQL statements on STDOUT (but executes them)") do |value| self.[:trace_sql] = true end opt.on("--update", "-u", "Update tuple instead of insert when key already exists") do |value| self.update_on_existing = true end opt.on('--dry-run', "Trace SQL statements on STDOUT only, do nothing on the database") do |value| self.[:trace_sql] = true self.[:trace_only] = true self.[:trace_buffer] = environment.output_buffer end opt.separator "\nRecognized format options:" (opt) (opt) # CSV output options opt.separator "\nCSV options:" (opt) end |
#execute_command ⇒ Object
Executes the command
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/dbagile/command/bulk/import.rb', line 114 def execute_command with_current_connection() do |connection| # Make the job now connection.transaction do |t| first = true with_emitter do |tuple| make_the_job(t, tuple, first) first = false end end end end |
#find_key(t, tuple) ⇒ Object
129 130 131 132 133 |
# File 'lib/dbagile/command/bulk/import.rb', line 129 def find_key(t, tuple) t.keys(self.dataset).find{|key| key.all?{|k| tuple.has_key?(k)} } end |
#handle_schema_modification(t, tuple) ⇒ Object
Handles the schema modifications
159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/dbagile/command/bulk/import.rb', line 159 def handle_schema_modification(t, tuple) # Handle table creation/deletion/truncation if drop_table t.drop_table(self.dataset) if t.has_table?(self.dataset) end if create_table heading = tuple_heading(tuple) heading = check_tuple_heading(heading, environment) t.create_table(self.dataset, heading) end if truncate_table t.delete(self.dataset, {}) end end |
#make_the_job(t, tuple, first = true) ⇒ Object
Makes the insert job
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/dbagile/command/bulk/import.rb', line 136 def make_the_job(t, tuple, first = true) handle_schema_modification(t, tuple) if first if self.update_on_existing # find a candidate key key = find_key(t, tuple) if key.nil? raise DbAgile::CandidateKeyNotFoundError, "Unable to find a candidate key for UPDATE" end # Make insert or update according to key key_value = tuple_project(tuple, key) if t.exists?(self.dataset, key_value) t.update(self.dataset, tuple, key_value) else t.insert(self.dataset, tuple) end else t.insert(self.dataset, tuple) end end |
#normalize_pending_arguments(arguments) ⇒ Object
Normalizes the pending arguments
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/dbagile/command/bulk/import.rb', line 73 def normalize_pending_arguments(arguments) case arguments.size when 1 self.dataset = valid_argument_list!(arguments, Symbol) when 2 raise OptionParser::AmbiguousArgument, '--input-file=#{self.input_file}' if self.input_file self.dataset, self.input_file = valid_argument_list!(arguments, Symbol, String) else bad_argument_list!(arguments) end end |
#with_emitter(&block) ⇒ Object
Yields the block with each emitted tuple
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/dbagile/command/bulk/import.rb', line 95 def with_emitter(&block) with_io{|io| = [self.format] [:type_system] = self.type_system if self.type_system [:input_file] = self.input_file case self.format when :csv DbAgile::IO::CSV::from_csv(io, , &block) when :json DbAgile::IO::JSON::from_json(io, , &block) when :yaml DbAgile::IO::YAML::from_yaml(io, , &block) when :ruby DbAgile::IO::Ruby::from_ruby(io, , &block) end } end |
#with_io(&block) ⇒ Object
Yields the block with the IO object to use for input
86 87 88 89 90 91 92 |
# File 'lib/dbagile/command/bulk/import.rb', line 86 def with_io(&block) if self.input_file File.open(self.input_file, "r", &block) else block.call(environment.input_buffer) end end |