Module: Semian::PG

Includes:
Adapter
Defined in:
lib/semian/pg.rb,
lib/semian/pg/version.rb

Overview

The adapter wraps the PG::Connection class and implements the methods for resource acquisition.

Defined Under Namespace

Modules: ClassMethods Classes: ConfigurationChangedError

Constant Summary collapse

VERSION =
'0.1.5'
ResourceBusyError =
::PG::ResourceBusyError
CircuitOpenError =
::PG::CircuitOpenError
QUERY_WHITELIST =
Regexp.union(
  %r{\A(?:/\*.*?\*/)?\s*ROLLBACK}i,
  %r{\A(?:/\*.*?\*/)?\s*COMMIT}i,
  %r{\A(?:/\*.*?\*/)?\s*RELEASE\s+SAVEPOINT}i
)
QUERY_METHODS =
%i[query exec exec_params].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.exceptionsObject

Returns the value of attribute exceptions.



44
45
46
# File 'lib/semian/pg.rb', line 44

def exceptions
  @exceptions
end

.semian_configurationObject

Returns the value of attribute semian_configuration.



45
46
47
# File 'lib/semian/pg.rb', line 45

def semian_configuration
  @semian_configuration
end

Class Method Details

.retrieve_semian_configuration(host, port) ⇒ Object



53
54
55
# File 'lib/semian/pg.rb', line 53

def retrieve_semian_configuration(host, port)
  @semian_configuration.call(host, port) if @semian_configuration.respond_to?(:call)
end

Instance Method Details

#async_connect_or_reset(*args) ⇒ Object



84
85
86
87
88
# File 'lib/semian/pg.rb', line 84

def async_connect_or_reset(*args)
  acquire_semian_resource(adapter: :pg, scope: :connection) do
    super
  end
end

#conninfo_hashObject



58
59
60
61
62
# File 'lib/semian/pg.rb', line 58

def conninfo_hash
  h = super
  h.merge!(connect_timeout: @connect_timeout) if @connect_timeout
  h
end

#disabled?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/semian/pg.rb', line 76

def disabled?
  raw_semian_options.nil?
end

#query_whitelisted?(sql) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
# File 'lib/semian/pg.rb', line 98

def query_whitelisted?(sql)
  QUERY_WHITELIST =~ sql
rescue ArgumentError
  return false unless sql.valid_encoding?

  raise
end

#raw_semian_optionsObject



69
70
71
72
73
74
# File 'lib/semian/pg.rb', line 69

def raw_semian_options
  @raw_semian_options ||= begin
    @raw_semian_options = Semian::PG.retrieve_semian_configuration(@iopts[:host], @iopts[:port])
    @raw_semian_options = @raw_semian_options.dup unless @raw_semian_options.nil?
  end
end

#resource_exceptionsObject



80
81
82
# File 'lib/semian/pg.rb', line 80

def resource_exceptions
  [::PG::ConnectionBad, ::PG::QueryCanceled].freeze
end

#semian_identifierObject



64
65
66
67
# File 'lib/semian/pg.rb', line 64

def semian_identifier
  @semian_identifier ||= "pg_#{raw_semian_options[:name]}" if raw_semian_options && raw_semian_options[:name]
  @semian_identifier ||= "pg_#{@iopts[:host]}:#{@iopts[:port]}"
end

#with_resource_timeout(temp_timeout) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/semian/pg.rb', line 116

def with_resource_timeout(temp_timeout)
  prev_conn_timeout = conninfo_hash[:connect_timeout]
  begin
    @connect_timeout = temp_timeout
    yield
  ensure
    @connect_timeout = prev_conn_timeout
  end
end