<commit_before>require 'masamune/commands/shell'

module Masamune::Commands
  class Hive
    attr_accessor :file, :exec, :output, :quote, :block

    def initialize(opts = {})
      self.file       = opts[:file]
      self.quote      = opts.fetch(:quote, false)
      self.exec       = opts[:exec]
      self.output     = opts[:output]
      self.block      = opts[:block]
    end

    def exec=(sql)
      if sql
        if quote
          @exec = quote_sql(strip_sql(sql))
        else
          @exec = strip_sql(sql)
        end
      end
    end

    def interactive?
      !(exec || file)
    end

    def command_args
      args = []
      args << Masamune.configuration.hive[:path]
      args << Masamune.configuration.hive[:options].map(&:to_a)
      args << ['-e', @exec] if @exec
      args << ['-f', @file] if @file
      args.flatten
    end

    def before_execute
      if file
        Masamune.print("hive with file #{file}")
      end

      if exec
        Masamune.print("hive exec '#{exec}' #{'into ' + output if output}")
      end

      if output
        @tmpfile = Tempfile.new('masamune')
      end
    end

    def around_execute(&block)
      Dir.chdir(Masamune.filesystem.path(:var_dir)) do
        yield
      end
    end

    def after_execute
      if output
        @tmpfile.close
        Masamune.filesystem.move_file(@tmpfile.path, output)
        @tmpfile.unlink
      end
    end

    def handle_stdout(line, line_no)
      block.call(line) if block
      if @tmpfile
        @tmpfile.puts(line)
      else
        Masamune::logger.debug(line)
      end
    end

    private

    def strip_sql(sql)
      out = sql.dup
      out.gsub!(/\s\s+/, ' ')
      out.strip!
      out
    end

    # force SQL be enclosed in single quotes, terminated with semicolon
    def quote_sql(sql)
      out = sql.dup
      out.gsub!(/\A'|'\z/,'') if out =~ /\A'/
      out.gsub!(/;\z/,'')
      out.gsub!("'", %q("'"))
      %q{'} + out + %q{;'}
    end
  end
end<commit_msg>Allow user to define hive rollback function<commit_after>require 'masamune/commands/shell'

module Masamune::Commands
  class Hive
    attr_accessor :file, :exec, :output, :quote, :block, :rollback

    def initialize(opts = {})
      self.file       = opts[:file]
      self.quote      = opts.fetch(:quote, false)
      self.exec       = opts[:exec]
      self.output     = opts[:output]
      self.block      = opts[:block]
      self.rollback   = opts[:rollback]
    end

    def exec=(sql)
      if sql
        if quote
          @exec = quote_sql(strip_sql(sql))
        else
          @exec = strip_sql(sql)
        end
      end
    end

    def interactive?
      !(exec || file)
    end

    def command_args
      args = []
      args << Masamune.configuration.hive[:path]
      args << Masamune.configuration.hive[:options].map(&:to_a)
      args << ['-e', exec] if exec
      args << ['-f', file] if file
      args.flatten
    end

    def before_execute
      if file
        Masamune.print("hive with file #{file}")
      end

      if exec
        Masamune.print("hive exec '#{exec}' #{'into ' + output if output}")
      end

      if output
        @tmpfile = Tempfile.new('masamune')
      end
    end

    def around_execute(&block)
      Dir.chdir(Masamune.filesystem.path(:var_dir)) do
        yield
      end
    end

    def after_execute
      if output
        @tmpfile.close
        Masamune.filesystem.move_file(@tmpfile.path, output)
        @tmpfile.unlink
      end
    end

    def handle_stdout(line, line_no)
      block.call(line) if block
      if @tmpfile
        @tmpfile.puts(line)
      else
        Masamune::logger.debug(line)
      end
    end

    def handle_failure(status)
      if rollback
        Masamune::logger.error('rolling back')
        rollback.call
      end
    end

    private

    def strip_sql(sql)
      out = sql.dup
      out.gsub!(/\s\s+/, ' ')
      out.strip!
      out
    end

    # force SQL be enclosed in single quotes, terminated with semicolon
    def quote_sql(sql)
      out = sql.dup
      out.gsub!(/\A'|'\z/,'') if out =~ /\A'/
      out.gsub!(/;\z/,'')
      out.gsub!("'", %q("'"))
      %q{'} + out + %q{;'}
    end
  end
end
{'fraction_non_alphanumeric': 0.1273203236554022, 'fraction_numerical': 0.0, 'mean_word_length': 1.8398648648648648, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2619791', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1388, 'n_words': 327}