Class: Itiel::Script::SQLScript

Inherits:
Object
  • Object
show all
Includes:
DB::SQLConnectable, ChainedStep
Defined in:
lib/itiel/script/sql_script.rb

Overview

Executes a SQL script or command on the specified connection

Instance Attribute Summary collapse

Attributes included from DB::SQLConnectable

#connection, #connection_file_path

Attributes included from ChainedStep

#next_step

Instance Method Summary collapse

Methods included from DB::SQLConnectable

included

Methods included from ChainedStep

#input=

Constructor Details

#initialize(sql = nil, &block) ⇒ SQLScript

Returns a new instance of SQLScript.



13
14
15
16
17
18
19
# File 'lib/itiel/script/sql_script.rb', line 13

def initialize(sql=nil, &block)
  if block_given?
    self.sql = block
  else
    self.sql = sql
  end
end

Instance Attribute Details

#sqlObject

Returns the value of attribute sql.



11
12
13
# File 'lib/itiel/script/sql_script.rb', line 11

def sql
  @sql
end

Instance Method Details

#execute(input_stream = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/itiel/script/sql_script.rb', line 21

def execute(input_stream=nil)
  db = self.class.sequel_connection(connection)
  if sql.respond_to?(:call)
    input_stream.each do |row|
      db << sql.call(row)
    end
  else
    db << sql
  end
end