Class: Lhm::Connection

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/lhm/connection.rb

Overview

Lhm::Connection inherits from SingleDelegator. It will forward any unknown method calls to the ActiveRecord connection.

Instance Method Summary collapse

Constructor Details

#initialize(connection:, options: {}) ⇒ Connection

Returns a new instance of Connection.



17
18
19
20
21
22
23
24
25
26
# File 'lib/lhm/connection.rb', line 17

def initialize(connection:, options: {})
  @sql_retry = Lhm::SqlRetry.new(
    connection,
    retry_options: options[:retriable] || {},
    reconnect_with_consistent_host: options[:reconnect_with_consistent_host] || false
  )

  # Creates delegation for the ActiveRecord Connection
  super(connection)
end

Instance Method Details

#ar_connection=(connection) ⇒ Object

Raises:



28
29
30
31
32
33
34
# File 'lib/lhm/connection.rb', line 28

def ar_connection=(connection)
  raise Lhm::Error.new("Lhm::Connection requires an active record connection to operate") if connection.nil?

  @sql_retry.connection = connection
  # Sets connection as the delegated object
  __setobj__(connection)
end

#execute(query, should_retry: false, log_prefix: nil) ⇒ Object

ActiveRecord::Base overridden methods to incorporate custom retry logic All other methods will be delegated



38
39
40
41
42
43
44
# File 'lib/lhm/connection.rb', line 38

def execute(query, should_retry: false, log_prefix: nil)
  if should_retry
    exec_with_retries(:execute, query, log_prefix)
  else
    exec(:execute, query)
  end
end

#select_all(query, should_retry: false, log_prefix: nil) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/lhm/connection.rb', line 78

def select_all(query, should_retry: false, log_prefix: nil)
  if should_retry
    exec_with_retries(:select_all, query, log_prefix)
  else
    exec(:select_all, query)
  end
end

#select_one(query, should_retry: false, log_prefix: nil) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/lhm/connection.rb', line 70

def select_one(query, should_retry: false, log_prefix: nil)
  if should_retry
    exec_with_retries(:select_one, query, log_prefix)
  else
    exec(:select_one, query)
  end
end

#select_value(query, should_retry: false, log_prefix: nil) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/lhm/connection.rb', line 54

def select_value(query, should_retry: false, log_prefix: nil)
  if should_retry
    exec_with_retries(:select_value, query, log_prefix)
  else
    exec(:select_value, query)
  end
end

#select_values(query, should_retry: false, log_prefix: nil) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/lhm/connection.rb', line 62

def select_values(query, should_retry: false, log_prefix: nil)
  if should_retry
    exec_with_retries(:select_values, query, log_prefix)
  else
    exec(:select_values, query)
  end
end

#update(query, should_retry: false, log_prefix: nil) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/lhm/connection.rb', line 46

def update(query, should_retry: false, log_prefix: nil)
  if should_retry
    exec_with_retries(:update, query, log_prefix)
  else
    exec(:update, query)
  end
end