Class: ActiveRecordDataLoader::ConnectionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_data_loader/connection_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection_factory:, statement_timeout:) ⇒ ConnectionHandler

Returns a new instance of ConnectionHandler.



5
6
7
8
9
# File 'lib/active_record_data_loader/connection_handler.rb', line 5

def initialize(connection_factory:, statement_timeout:)
  @connection_factory = connection_factory
  @statement_timeout = statement_timeout
  cache_facts
end

Instance Method Details

#reset_timeout_commandObject



36
37
38
# File 'lib/active_record_data_loader/connection_handler.rb', line 36

def reset_timeout_command
  "RESET statement_timeout"
end

#supports_copy?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/active_record_data_loader/connection_handler.rb', line 28

def supports_copy?
  @supports_copy
end

#supports_timeout?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/active_record_data_loader/connection_handler.rb', line 24

def supports_timeout?
  @supports_timeout
end

#timeout_set_commandObject



32
33
34
# File 'lib/active_record_data_loader/connection_handler.rb', line 32

def timeout_set_command
  "SET statement_timeout = \"#{statement_timeout}\""
end

#with_connectionObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_record_data_loader/connection_handler.rb', line 11

def with_connection
  connection = connection_factory.call
  if supports_timeout?
    connection.execute(timeout_set_command)
    yield connection
    connection.execute(reset_timeout_command)
  else
    yield connection
  end
ensure
  connection&.close
end