Class: MysqlFramework::Connector
- Inherits:
-
Object
- Object
- MysqlFramework::Connector
- Defined in:
- lib/mysql_framework/connector.rb
Instance Method Summary collapse
-
#check_in(client) ⇒ Object
This method is called to check a client back in to the connection when no longer needed.
-
#check_out ⇒ Object
This method is called to fetch a client from the connection pool.
-
#connections ⇒ Object
This method is called to get the idle connection queue for this connector.
-
#dispose ⇒ Object
This method is called to close all MySQL connections in the pool and dispose of the pool itself.
-
#execute(query, provided_client = nil) ⇒ Object
This method is called to execute a prepared statement.
-
#initialize(options = {}) ⇒ Connector
constructor
A new instance of Connector.
-
#query(query_string, provided_client = nil) ⇒ Object
This method is called to execute a query.
-
#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.
-
#setup ⇒ Object
This method is called to setup a pool of MySQL connections.
-
#transaction ⇒ Object
This method is called to use a client within a transaction.
-
#with_client(provided = nil) ⇒ Object
This method is called to use a client from the connection pool.
Constructor Details
#initialize(options = {}) ⇒ Connector
Returns a new instance of Connector.
5 6 7 8 9 |
# File 'lib/mysql_framework/connector.rb', line 5 def initialize( = {}) @options = .merge() Mysql2::Client..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.
61 62 63 64 65 66 |
# File 'lib/mysql_framework/connector.rb', line 61 def check_in(client) return client&.close unless connection_pool_enabled? client = new_client if client.nil? || client.closed? @connection_pool.push(client) end |
#check_out ⇒ Object
This method is called to fetch a client from the connection pool.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mysql_framework/connector.rb', line 40 def check_out 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 |
#connections ⇒ Object
This method is called to get the idle connection queue for this connector.
35 36 37 |
# File 'lib/mysql_framework/connector.rb', line 35 def connections @connection_pool end |
#dispose ⇒ Object
This method is called to close all MySQL connections in the pool and dispose of the pool itself.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/mysql_framework/connector.rb', line 23 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
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.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mysql_framework/connector.rb', line 81 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
95 96 97 |
# File 'lib/mysql_framework/connector.rb', line 95 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
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/mysql_framework/connector.rb', line 100 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 |
#setup ⇒ Object
This method is called to setup a pool of MySQL connections.
12 13 14 15 16 17 18 19 20 |
# File 'lib/mysql_framework/connector.rb', line 12 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 |
#transaction ⇒ Object
This method is called to use a client within a transaction
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/mysql_framework/connector.rb', line 112 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.
69 70 71 72 73 74 |
# File 'lib/mysql_framework/connector.rb', line 69 def with_client(provided = nil) client = provided || check_out yield client ensure check_in(client) unless provided end |