Module: Postqueue::Item::PreparedRawInserter

Defined in:
lib/postqueue/item/inserter.rb

Instance Method Summary collapse

Instance Method Details

#create_prepared_inserter_statement(raw_connection) ⇒ Object

prepares the INSERT statement, and returns its name



41
42
43
44
45
# File 'lib/postqueue/item/inserter.rb', line 41

def create_prepared_inserter_statement(raw_connection)
  name = "postqueue-insert-#{table_name}-#{raw_connection.object_id}"
  raw_connection.prepare(name, insert_sql)
  name
end

#insert_item(op:, entity_id:) ⇒ Object



47
48
49
50
51
# File 'lib/postqueue/item/inserter.rb', line 47

def insert_item(op:, entity_id:)
  raw_connection = connection.raw_connection
  statement_name = prepared_inserter_statement(raw_connection)
  raw_connection.exec_prepared(statement_name, [op, entity_id])
end

#insert_sqlObject



27
28
29
# File 'lib/postqueue/item/inserter.rb', line 27

def insert_sql
  "INSERT INTO #{table_name}(op, entity_id) VALUES($1, $2)"
end

#prepared_inserter_statement(raw_connection) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/postqueue/item/inserter.rb', line 31

def prepared_inserter_statement(raw_connection)
  @prepared_inserter_statements ||= {}

  # a prepared connection is PER DATABASE CONNECTION. It is not shared across
  # connections, and it is not per thread, since a Thread might use different
  # connections during its lifetime.
  @prepared_inserter_statements[raw_connection.object_id] ||= create_prepared_inserter_statement(raw_connection)
end