Class: MysqlFramework::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_framework/connector.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connector

Returns a new instance of Connector.



5
6
7
8
9
10
# File 'lib/mysql_framework/connector.rb', line 5

def initialize(options = {})
  @options = default_options.merge(options)
  @mutex = Mutex.new

  Mysql2::Client.default_query_options.merge!(symbolize_keys: true, cast_booleans: true)
end

Instance Method Details

#check_in(client) ⇒ Object

This method is called to check a client back in to the connection when no longer needed.



66
67
68
69
70
71
72
73
# File 'lib/mysql_framework/connector.rb', line 66

def check_in(client)
  @mutex.synchronize do
    return client&.close unless connection_pool_enabled?

    client = new_client if client&.closed?
    @connection_pool.push(client)
  end
end

#check_outObject

This method is called to fetch a client from the connection pool.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mysql_framework/connector.rb', line 41

def check_out
  @mutex.synchronize do
    begin
      return new_client unless connection_pool_enabled?

      client = @connection_pool.pop(true)

      client.ping if @options[:reconnect]

      client
    rescue ThreadError
      if @created_connections < max_pool_size
        client = new_client
        @created_connections += 1
        return client
      end

      MysqlFramework.logger.error { "[#{self.class}] - Database connection pool depleted." }

      raise 'Database connection pool depleted.'
    end
  end
end

#connectionsObject

This method is called to get the idle connection queue for this connector.



36
37
38
# File 'lib/mysql_framework/connector.rb', line 36

def connections
  @connection_pool
end

#disposeObject

This method is called to close all MySQL connections in the pool and dispose of the pool itself.



24
25
26
27
28
29
30
31
32
33
# File 'lib/mysql_framework/connector.rb', line 24

def dispose
  return if @connection_pool.nil?

  until @connection_pool.empty?
    conn = @connection_pool.pop(true)
    conn&.close
  end

  @connection_pool = nil
end

#execute(query, provided_client = nil) ⇒ Object

Note:

Ensure we free any result and close each statement, otherwise we

This method is called to execute a prepared statement

can run into a ‘Commands out of sync’ error if multiple threads are running different queries at the same time.



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mysql_framework/connector.rb', line 88

def execute(query, provided_client = nil)
  with_client(provided_client) do |client|
    begin
      statement = client.prepare(query.sql)
      result = statement.execute(*query.params)
      result&.to_a
    ensure
      result&.free
      statement&.close
    end
  end
end

#query(query_string, provided_client = nil) ⇒ Object

This method is called to execute a query



102
103
104
# File 'lib/mysql_framework/connector.rb', line 102

def query(query_string, provided_client = nil)
  with_client(provided_client) { |client| client.query(query_string) }
end

#query_multiple_results(query_string, provided_client = nil) ⇒ Object

This method is called to execute a query which will return multiple result sets in an array



107
108
109
110
111
112
113
114
115
116
# File 'lib/mysql_framework/connector.rb', line 107

def query_multiple_results(query_string, provided_client = nil)
  results = with_client(provided_client) do |client|
    result = []
    result << client.query(query_string)
    result << client.store_result while client.next_result
    result.compact
  end

  results.map(&:to_a)
end

#setupObject

This method is called to setup a pool of MySQL connections.



13
14
15
16
17
18
19
20
21
# File 'lib/mysql_framework/connector.rb', line 13

def setup
  return unless connection_pool_enabled?

  @connection_pool = ::Queue.new

  start_pool_size.times { @connection_pool.push(new_client) }

  @created_connections = start_pool_size
end

#transactionObject

This method is called to use a client within a transaction

Raises:

  • (ArgumentError)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mysql_framework/connector.rb', line 119

def transaction
  raise ArgumentError, 'No block was given' unless block_given?

  with_client do |client|
    begin
      client.query('BEGIN')
      yield client
      client.query('COMMIT')
    rescue StandardError => e
      client.query('ROLLBACK')
      raise e
    end
  end
end

#with_client(provided = nil) ⇒ Object

This method is called to use a client from the connection pool.



76
77
78
79
80
81
# File 'lib/mysql_framework/connector.rb', line 76

def with_client(provided = nil)
  client = provided || check_out
  yield client
ensure
  check_in(client) unless provided
end