Sha256: 1fc19c42fd79ecdf6c5752ca3832ab6c5cd20c6e6d341678f04963e6fe94223b

Contents?: true

Size: 857 Bytes

Versions: 1

Compression:

Stored size: 857 Bytes

Contents

module TestSupport

  class TemporaryTable

    def self.create(*args)
      table = new(*args)
      table.drop if table.exists?
      table.create
      begin
        yield
      ensure
        table.drop
      end
    end

    def initialize(args)
      @connection = args[:connection]
      @table_name = args[:table_name]
      @columns = args[:columns]
    end

    def exists?
      sql = SqlPostgres::Select.new(@connection)
      sql.select_literal(1)
      sql.from('pg_class')
      sql.where(['relname = %s', @table_name])
      !sql.exec.empty?
    end

    def create
      statement = [
        'create temporary table', @table_name,
        "(#{@columns.join(', ')})",
      ].join(' ')
      @connection.exec statement
    end

    def drop
      statement = "drop table #{@table_name}"
      @connection.exec statement
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sqlpostgres-1.3.0 spec/lib/temporary_table.rb