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
11
# File 'lib/mysql_framework/connector.rb', line 5

def initialize(options = {})
  @connection_pool = ::Queue.new

  @options = default_options.merge(options)

  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.



22
23
24
# File 'lib/mysql_framework/connector.rb', line 22

def check_in(client)
  @connection_pool.push(client)
end

#check_outObject

This method is called to fetch a client from the connection pool or create a new client if no idle clients are available.



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

def check_out
  @connection_pool.pop(true)
rescue StandardError
  Mysql2::Client.new(@options)
end

#default_optionsObject



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

def default_options
  {
    host: ENV.fetch('MYSQL_HOST'),
    port: ENV.fetch('MYSQL_PORT'),
    database: ENV.fetch('MYSQL_DATABASE'),
    username: ENV.fetch('MYSQL_USERNAME'),
    password: ENV.fetch('MYSQL_PASSWORD'),
    reconnect: true
  }
end

#execute(query, provided_client = nil) ⇒ Object

This method is called to execute a prepared statement



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

def execute(query, provided_client = nil)
  with_client(provided_client) do |client|
    statement = client.prepare(query.sql)
    statement.execute(*query.params)
  end
end

#query(query_string, provided_client = nil) ⇒ Object

This method is called to execute a query



43
44
45
# File 'lib/mysql_framework/connector.rb', line 43

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



48
49
50
51
52
53
54
55
# File 'lib/mysql_framework/connector.rb', line 48

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

#transactionObject

This method is called to use a client within a transaction

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mysql_framework/connector.rb', line 58

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.



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

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