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.
57 58 59 60 61 |
# File 'lib/mysql_framework/connector.rb', line 57 def check_in(client) client = new_client if client.closed? @connection_pool.push(client) end |
#check_out ⇒ Object
This method is called to fetch a client from the connection pool.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mysql_framework/connector.rb', line 38 def check_out 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.
33 34 35 |
# File 'lib/mysql_framework/connector.rb', line 33 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.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/mysql_framework/connector.rb', line 21 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
This method is called to execute a prepared statement
72 73 74 75 76 77 |
# File 'lib/mysql_framework/connector.rb', line 72 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
80 81 82 |
# File 'lib/mysql_framework/connector.rb', line 80 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
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/mysql_framework/connector.rb', line 85 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 |
# File 'lib/mysql_framework/connector.rb', line 12 def setup @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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mysql_framework/connector.rb', line 97 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.
64 65 66 67 68 69 |
# File 'lib/mysql_framework/connector.rb', line 64 def with_client(provided = nil) client = provided || check_out yield client ensure check_in(client) unless provided end |