<commit_before>#
#  Copyright (c) 2011, SoundCloud Ltd., <NAME>, <NAME>, Tobias
#  Schmidt
#
#  Apply a change to the database.
#

module Lhm
  module Command
    def self.included(base)
      base.send :attr_reader, :connection
    end

    #
    # Command Interface
    #

    def validate; end

    def revert; end

    def run(&block)
      validate

      if(block_given?)
        before
        block.call(self)
        after
      else
        execute
      end
    end

  private

    def execute
      raise NotImplementedError.new(self.class.name)
    end

    def before
      raise NotImplementedError.new(self.class.name)
    end

    def after
      raise NotImplementedError.new(self.class.name)
    end

    def table?(table_name)
      @connection.table_exists?(table_name)
    end

    def error(msg)
      raise Exception.new("#{ self.class }: #{ msg }")
    end

    def sql(statements)
      [statements].flatten.each { |statement| @connection.execute(statement) }
    rescue ActiveRecord::StatementInvalid, Mysql::Error => e
      revert
      error e.message
    end

    def update(statements)
      [statements].flatten.inject(0) do |memo, statement|
        memo += @connection.update(statement)
      end
    rescue ActiveRecord::StatementInvalid, Mysql::Error => e
      revert
      error e.message
    end
  end
end
<commit_msg>Call revert in case of error<commit_after>#
#  Copyright (c) 2011, SoundCloud Ltd., <NAME>, <NAME>, Tobias
#  Schmidt
#
#  Apply a change to the database.
#

module Lhm
  class Error < StandardError
  end

  module Command
    def self.included(base)
      base.send :attr_reader, :connection
    end

    def run(&block)
      validate

      if(block_given?)
        before
        block.call(self)
        after
      else
        execute
      end
    rescue
      revert
      raise
    end

  private

    def validate
    end

    def revert
    end

    def execute
      raise NotImplementedError.new(self.class.name)
    end

    def before
    end

    def after
    end

    def table?(table_name)
      @connection.table_exists?(table_name)
    end

    def error(msg)
      raise Error.new(msg)
    end

    def sql(statements)
      [statements].flatten.each { |statement| @connection.execute(statement) }
    rescue ActiveRecord::StatementInvalid, Mysql::Error => e
      error e.message
    end

    def update(statements)
      [statements].flatten.inject(0) do |memo, statement|
        memo += @connection.update(statement)
      end
    rescue ActiveRecord::StatementInvalid, Mysql::Error => e
      error e.message
    end
  end
end

{'fraction_non_alphanumeric': 0.0880214313050134, 'fraction_numerical': 0.003827018752391887, 'mean_word_length': 2.2552926525529267, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 11, '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': '7442507', 'n_tokens_mistral': 829, 'n_tokens_neox': 785, 'n_words': 210}