Sha256: c0bb2b0a62c97574067108e4274003f15a187ae6ecf88879d29de57a2c504400

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require_relative 'query_builder/row_transformer'
require_relative 'query_builder/null_strategy'
require_relative 'query_builder/value_by_type_strategy'

module RSpec
  module Hive
    class QueryBuilder
      def initialize(schema, connection)
        @schema = schema
        @connection = connection
        @partition_hash = {}
        @rows = []
        @stubbing = false
      end

      def partition(hash)
        spawn.partition!(hash)
      end

      def partition!(partition)
        partition_hash.merge!(partition)
        self
      end

      def insert(*new_rows)
        spawn.insert!(new_rows)
      end

      def insert!(new_rows)
        rows.concat(new_rows)
        self
      end

      def execute
        if partition_hash.empty?
          connection.load_into_table(schema, transformed_rows)
        else
          connection.load_into_table(schema, transformed_rows, partition_hash)
        end
      end

      def with_stubbing
        spawn.with_stubbing!
      end

      def with_stubbing!
        self.stubbing = true
        self
      end

      protected

      attr_accessor :partition_hash, :connection, :rows, :stubbing

      private

      attr_reader :schema

      def spawn
        clone
      end

      def stubbing?
        stubbing
      end

      def transformed_rows
        transformer = RowTransformer.new(schema, missing_column_strategy)
        rows.map do |row|
          transformer.transform(row)
        end
      end

      def missing_column_strategy
        if stubbing?
          ValueByTypeStrategy.new
        else
          NullStrategy.new
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rspec-hive-0.6.3 lib/rspec/hive/query_builder.rb
rspec-hive-0.6.2 lib/rspec/hive/query_builder.rb
rspec-hive-0.6.1 lib/rspec/hive/query_builder.rb
rspec-hive-0.6.0 lib/rspec/hive/query_builder.rb