class EventSource::Postgres::Get::SelectStatement

Public Class Methods

build(stream_name, position: nil, batch_size: nil) click to toggle source
# File lib/event_source/postgres/get/select_statement.rb, line 25
def self.build(stream_name, position: nil, batch_size: nil)
  new(stream_name, position, batch_size)
end

Public Instance Methods

batch_size() click to toggle source
# File lib/event_source/postgres/get/select_statement.rb, line 13
def batch_size
  @batch_size ||= Defaults.batch_size
end
category_stream?() click to toggle source
# File lib/event_source/postgres/get/select_statement.rb, line 21
def category_stream?
  is_category_stream ||= StreamName.category?(stream_name)
end
position() click to toggle source
# File lib/event_source/postgres/get/select_statement.rb, line 9
def position
  @position ||= Defaults.position
end
position_field() click to toggle source
# File lib/event_source/postgres/get/select_statement.rb, line 68
def position_field
  unless category_stream?
    'position'
  else
    'global_position'
  end
end
sql() click to toggle source
# File lib/event_source/postgres/get/select_statement.rb, line 29
        def sql
          logger.trace(tag: :sql) { "Composing select statement (Stream: #{stream_name}, Category: #{category_stream?}, Types: #{stream_type_list.inspect}, Position: #{position}, Batch Size: #{batch_size})" }

          statement = <<-SQL
            SELECT
              id::varchar,
              stream_name::varchar,
              position::int,
              type::varchar,
              global_position::bigint,
              data::varchar,
              metadata::varchar,
              time::timestamp
            FROM
              events
            WHERE
              #{where_clause_field} = '#{stream_name}' AND
              #{position_field} >= #{position}
            ORDER BY
              #{position_field} ASC
            LIMIT
              #{batch_size}
            ;
          SQL

          logger.debug(tag: :sql) { "Composed select statement (Stream: #{stream_name}, Category: #{category_stream?}, Types: #{stream_type_list.inspect}, Position: #{position}, Batch Size: #{batch_size})" }
          logger.debug(tags: [:data, :sql]) { "Statement: #{statement}" }

          statement
        end
stream_type_list() click to toggle source
# File lib/event_source/postgres/get/select_statement.rb, line 17
def stream_type_list
  @stream_type ||= StreamName.get_type_list(stream_name)
end
where_clause_field() click to toggle source
# File lib/event_source/postgres/get/select_statement.rb, line 60
def where_clause_field
  unless category_stream?
    'stream_name'
  else
    'category(stream_name)'
  end
end