Class: MessageStore::Postgres::Get::Stream::Last

Inherits:
Object
  • Object
show all
Includes:
Get::Stream::Last
Defined in:
lib/message_store/postgres/get/stream/last.rb

Instance Method Summary collapse

Instance Method Details

#call(stream_name, type = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/message_store/postgres/get/stream/last.rb', line 14

def call(stream_name, type=nil)
  logger.trace(tag: :get) { "Getting last message data (Stream Name: #{stream_name})" }

  result = get_result(stream_name, type)

  return nil if result.nil?

  message_data = convert(result[0])

  logger.info(tag: :get) { "Finished getting message data (Stream Name: #{stream_name})" }
  logger.info(tags: [:data, :message_data]) { message_data.pretty_inspect }

  message_data
end

#configure(session: nil) ⇒ Object



10
11
12
# File 'lib/message_store/postgres/get/stream/last.rb', line 10

def configure(session: nil)
  Session.configure(self, session: session)
end

#convert(record) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/message_store/postgres/get/stream/last.rb', line 76

def convert(record)
  logger.trace(tag: :get) { "Converting record to message data" }

  message_data = Get.message_data(record)

  logger.debug(tag: :get) { "Converted record to message data" }

  message_data
end

#get_result(stream_name, type) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/message_store/postgres/get/stream/last.rb', line 29

def get_result(stream_name, type)
  logger.trace(tag: :get) { "Getting last record (Stream: #{stream_name})" }

  parameter_values = parameter_values(stream_name, type)
  sql_command = sql_command(type)

  result = session.execute(sql_command, parameter_values)

  logger.debug(tag: :get) { "Finished getting result (Count: #{result.ntuples}, Stream: #{stream_name}" }

  return nil if result.ntuples == 0

  result
end

#parameter_values(stream_name, type) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/message_store/postgres/get/stream/last.rb', line 62

def parameter_values(stream_name, type)
  parameter_values = [
    stream_name
  ]

  # Backwards compatibility with versions of message-db that do not
  # support the type parameter - Aaron, Scott, Tue Jul 12 2022
  if not type.nil?
    parameter_values << type
  end

  parameter_values
end

#parameters(type) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/message_store/postgres/get/stream/last.rb', line 50

def parameters(type)
  parameters = "$1::varchar"

  # Backwards compatibility with versions of message-db that do not
  # support the type parameter - Aaron, Scott, Tue Jul 12 2022
  if not type.nil?
    parameters << ", $2::varchar"
  end

  parameters
end

#sql_command(type) ⇒ Object



44
45
46
47
48
# File 'lib/message_store/postgres/get/stream/last.rb', line 44

def sql_command(type)
  parameters = parameters(type)

  "SELECT * FROM get_last_stream_message(#{parameters});"
end