Class: AceMQ::AMQP::Patterns::SQL::PGConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/acemq/amqp/patterns/sql.rb

Overview

A connection over the pg gem.

PostgreSQL hands every column back as text, which is why the stores coerce what they read rather than trusting a driver to have guessed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ PGConnection

Returns a new instance of PGConnection.

Parameters:

  • connection (PG::Connection)


269
270
271
# File 'lib/acemq/amqp/patterns/sql.rb', line 269

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connection ⇒ Object (readonly)

The wrapped PG::Connection.



274
275
276
# File 'lib/acemq/amqp/patterns/sql.rb', line 274

def connection
  @connection
end

Instance Method Details

#constraint_violation?(error) ⇒ Boolean

Returns:

  • (Boolean)


286
287
288
289
290
291
292
293
294
# File 'lib/acemq/amqp/patterns/sql.rb', line 286

def constraint_violation?(error)
  # By SQLSTATE rather than by class name, and 23505 exactly rather
  # than the 23 class: NOT NULL, foreign key, CHECK and a trigger's
  # RAISE all share the class, and none of them means the key is
  # already taken.
  return false unless error.respond_to?(:result) && error.result

  error.result.error_field(PG::PG_DIAG_SQLSTATE) == "23505"
end

#placeholder(index) ⇒ Object

PostgreSQL numbers its parameters, and repeats matter: $1 twice is one parameter used twice, which is not what these statements mean, so every one of them is numbered in order.



284
# File 'lib/acemq/amqp/patterns/sql.rb', line 284

def placeholder(index) = "$#{index}"

#run(sql, params = []) ⇒ Object



276
277
278
279
# File 'lib/acemq/amqp/patterns/sql.rb', line 276

def run(sql, params = [])
  result = @connection.exec_params(sql, params)
  Result.new(result.values, result.cmd_tuples)
end

#transaction ⇒ Object

pg yields the connection to the block and this seam does not, so the yielded one is dropped rather than passed on: a caller writing transaction { } against this seam gets the same shape whichever driver is underneath.



300
# File 'lib/acemq/amqp/patterns/sql.rb', line 300

def transaction = @connection.transaction { |_| yield }