Sha256: 392fb8725258926506a1261cd5922e583b4c7c81fa19298580066fafcc89f499

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module PgEventstore
  # @!visibility private
  class EventTypeQueries
    attr_reader :connection
    private :connection

    # @param connection [PgEventstore::Connection]
    def initialize(connection)
      @connection = connection
    end

    # @param type [String]
    # @return [Integer] event type's id
    def find_or_create_type(type)
      find_type(type) || create_type(type)
    end

    # @param type [String]
    # @return [Integer, nil] event type's id
    def find_type(type)
      connection.with do |conn|
        conn.exec_params('SELECT id FROM event_types WHERE type = $1', [type])
      end.to_a.dig(0, 'id')
    end

    # @param type [String]
    # @return [Integer] event type's id
    def create_type(type)
      connection.with do |conn|
        conn.exec_params('INSERT INTO event_types (type) VALUES ($1) RETURNING id', [type])
      end.to_a.dig(0, 'id')
    end

    # @param types [Array<String>]
    # @return [Array<Integer, nil>]
    def find_event_types(types)
      connection.with do |conn|
        conn.exec_params(<<~SQL, [types])
          SELECT event_types.id, types.type
            FROM event_types 
            RIGHT JOIN (
              SELECT unnest($1::varchar[]) type
            ) types ON types.type = event_types.type
        SQL
      end.to_a.map { |attrs| attrs['id'] }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pg_eventstore-0.3.0 lib/pg_eventstore/queries/event_type_queries.rb
pg_eventstore-0.2.6 lib/pg_eventstore/queries/event_type_queries.rb
pg_eventstore-0.2.5 lib/pg_eventstore/queries/event_type_queries.rb
pg_eventstore-0.2.4 lib/pg_eventstore/queries/event_type_queries.rb