<commit_before>require "jdbc_helper/athena"
require_relative "commands"

module AmazonAthena
  class Client

    def initialize(key: nil, secret: nil, region: "us-east-1", s3_staging_dir: nil)
      @key = key || ENV["AWS_ACCESS_KEY"]
      @secret = secret || ENV['AWS_SECRET_KEY']
      @region = region
      @s3_staging_dir = s3_staging_dir || ENV["ATHENA_S3_STAGING_DIR"]
    end

    def databases
      cmd = AmazonAthena::Commands::ShowDatabases.new

      run(cmd)
    end

    def database_drop(database)
      cmd = AmazonAthena::Commands::DropDatabase.new(database)

      run(cmd)
    end

    def tables(database)
      cmd = AmazonAthena::Commands::ShowTables.new(database)

      run(cmd)
    end

    def table_drop(database_table)
      cmd = AmazonAthena::Commands::DropTable.new(database)

      run(cmd)
    end

    def table_columns(database_table)
      cmd = AmazonAthena::Commands::ShowColumns.new(database_table)

      run(cmd)
    end

    def table_show_create(database_table)
      cmd = AmazonAthena::Commands::ShowCreateTable.new(database_table)

      run(cmd)
    end

    def table_describe(database_table)
      cmd = AmazonAthena::Commands::DescribeTable.new(database_table)

      run(cmd)
    end

    def table_repair(database_table)
      cmd = AmazonAthena::Commands::RepairTable.new(database_table)
      run(cmd)

      partitions(database_table)
    end

    def table_properties(database_table)
      cmd = AmazonAthena::Commands::ShowTableProperties.new(database_table)

      run(cmd)
    end

    def partitions(table)
      cmd = AmazonAthena::Commands::ShowPartitions.new(database_table)

      run(cmd)
    end

    def partitions_drop(database_table, partitions_expression)
      cmd = AmazonAthena::Commands::DropPartition.new(database_table, partitions)

      run(cmd)
    end

    def connection
      return @connection if defined?(@connection) && !@connection.closed?

      @connection = JDBCHelper::Athena.connect(
        key: @key,
        secret: @secret,
        region: @region,
        s3_staging_dir: @s3_staging_dir
      )
    end

    def run(cmd, preview = false)
      output = if preview
                 cmd.preview
               else
                 cmd.run(connection)
               end

      output
    end

  end
end<commit_msg>Improve readability with guard clause<commit_after>require "jdbc_helper/athena"
require_relative "commands"

module AmazonAthena
  class Client

    def initialize(key: nil, secret: nil, region: "us-east-1", s3_staging_dir: nil)
      @key = key || ENV["AWS_ACCESS_KEY"]
      @secret = secret || ENV['AWS_SECRET_KEY']
      @region = region
      @s3_staging_dir = s3_staging_dir || ENV["ATHENA_S3_STAGING_DIR"]
    end

    def databases
      cmd = AmazonAthena::Commands::ShowDatabases.new

      run(cmd)
    end

    def database_drop(database)
      cmd = AmazonAthena::Commands::DropDatabase.new(database)

      run(cmd)
    end

    def tables(database)
      cmd = AmazonAthena::Commands::ShowTables.new(database)

      run(cmd)
    end

    def table_drop(database_table)
      cmd = AmazonAthena::Commands::DropTable.new(database)

      run(cmd)
    end

    def table_columns(database_table)
      cmd = AmazonAthena::Commands::ShowColumns.new(database_table)

      run(cmd)
    end

    def table_show_create(database_table)
      cmd = AmazonAthena::Commands::ShowCreateTable.new(database_table)

      run(cmd)
    end

    def table_describe(database_table)
      cmd = AmazonAthena::Commands::DescribeTable.new(database_table)

      run(cmd)
    end

    def table_repair(database_table)
      cmd = AmazonAthena::Commands::RepairTable.new(database_table)
      run(cmd)

      partitions(database_table)
    end

    def table_properties(database_table)
      cmd = AmazonAthena::Commands::ShowTableProperties.new(database_table)

      run(cmd)
    end

    def partitions(table)
      cmd = AmazonAthena::Commands::ShowPartitions.new(database_table)

      run(cmd)
    end

    def partitions_drop(database_table, partitions_expression)
      cmd = AmazonAthena::Commands::DropPartition.new(database_table, partitions)

      run(cmd)
    end

    def connection
      return @connection if defined?(@connection) && !@connection.closed?

      @connection = JDBCHelper::Athena.connect(
        key: @key,
        secret: @secret,
        region: @region,
        s3_staging_dir: @s3_staging_dir
      )
    end

    def run(cmd, preview = false)
      return cmd.preview if preview

      cmd.run(connection)
    end

  end
end
{'fraction_non_alphanumeric': 0.09483136224266316, 'fraction_numerical': 0.0030661410424879547, 'mean_word_length': 2.988646288209607, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7191678', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1446, 'n_words': 262}