class Object

Public Instance Methods

EachSQL(input, type = :default) { |sql| ... } click to toggle source

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.

# 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