Method: #EachSQL

Defined in:
lib/each_sql.rb

#EachSQL(input, type = :default) ⇒ Object

Shortcut method for creating a Enumerable EachSQL object for the given input. @param input Input script. @param The type of the input SQL script. :default, :mysql, and :oracle (or :plsql) @yield Executable SQL statement or block. @return Enumerator of executable SQL statements and blocks.



13
14
15
16
17
18
19
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
50
51
52
# File 'lib/each_sql.rb', line 13

def EachSQL input, type = :default
  return enum_for(:EachSQL, input, type) unless block_given?

  esql   = EachSQL.new(type)
  result = {}

  process = lambda {
    return if esql.empty?
    result = esql.shift
    sqls   = result[:sqls]
    sqls.each do |sql|
      yield sql
    end
  }

  input.to_s.each_line do |line|
    case line
    when /^\s*delimiter\s+(\S+)/i
      process.call
      if esql.empty?
        esql.delimiter = $1
      else
        esql << line
      end
    when /#{Regexp.escape esql.delimiter}/
      esql << line
      process.call
    else
      esql << line
    end
  end

  if !esql.empty?
    process.call
  end

  if sql = result[:leftover]
    yield sql
  end
end