Class: DbAgile::Command::SQL::Send

Inherits:
DbAgile::Command show all
Defined in:
lib/dbagile/command/sql/send.rb

Overview

Send SQL commands directly to the DBMS

Usage: dba #DbAgile::Command#command_name [–file=SCRIPT] [QUERY]

Constant Summary

Constants inherited from DbAgile::Command

CATEGORIES, CATEGORY_NAMES

Instance Attribute Summary collapse

Attributes inherited from DbAgile::Command

#environment

Attributes included from ClassMethods

#description, #summary, #usage

Instance Method Summary collapse

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

#fileObject

The file to execute



16
17
18
# File 'lib/dbagile/command/sql/send.rb', line 16

def file
  @file
end

#queryObject

Query to send



13
14
15
# File 'lib/dbagile/command/sql/send.rb', line 13

def query
  @query
end

Instance Method Details

#add_options(opt) ⇒ Object

Contribute to options



19
20
21
22
23
24
25
26
# File 'lib/dbagile/command/sql/send.rb', line 19

def add_options(opt)
  opt.separator nil
  opt.separator "Options:"
  opt.on("--file=SCRIPT", '-f',
         "Executes a whole SQL script file") do |value|
    self.file = valid_read_file!(value)
  end
end

#execute_commandObject

Executes the command.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dbagile/command/sql/send.rb', line 41

def execute_command
  result = nil
  with_current_connection do |connection|
    connection.transaction do |t|
      if self.file
        result = t.direct_sql(File.read(self.file))
      elsif self.query
        result = t.direct_sql(self.query)
      else
        script = environment.input_buffer.readlines.join("\n")
        result = t.direct_sql(script)
      end
    end
  end
  case result
    when DbAgile::Contract::Data::Dataset
      result.to_text(environment.output_buffer)
    else
      flush(result)
  end
  result
end

#normalize_pending_arguments(arguments) ⇒ Object

Normalizes the pending arguments



29
30
31
32
33
34
35
36
37
38
# File 'lib/dbagile/command/sql/send.rb', line 29

def normalize_pending_arguments(arguments)
  case arguments.size
    when 0
    when 1
      self.query = valid_argument_list!(arguments, String)
    else
      bad_argument_list!(arguments)
  end
  ambigous_argument_list! if self.query and self.file
end