-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Sage/initial_code
Initial code
- Loading branch information
Showing
31 changed files
with
1,789 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,7 @@ group :test, :development do | |
end | ||
|
||
group :test do | ||
gem 'guard' | ||
gem 'guard-rspec' | ||
gem 'simplecov', require: false | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
# More info at https://github.com/guard/guard#readme | ||
guard :rspec, cmd: 'rspec' do | ||
watch(%r{^spec/.+_spec\.rb$}) | ||
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } | ||
watch(%r{^lib/mysql_framework/(.+)\.rb$}) { |m| "spec/lib/mysql_framework/#{m[1]}_spec.rb" } | ||
watch('spec/spec_helper.rb') { "spec" } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# frozen_string_literal: true | ||
|
||
module MysqlFramework | ||
class Connector | ||
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 | ||
|
||
# This method is called to fetch a client from the connection pool or create a new client if no idle clients | ||
# are available. | ||
def check_out | ||
@connection_pool.pop(true) | ||
rescue StandardError | ||
Mysql2::Client.new(@options) | ||
end | ||
|
||
# This method is called to check a client back in to the connection when no longer needed. | ||
def check_in(client) | ||
@connection_pool.push(client) | ||
end | ||
|
||
# This method is called to use a client from the connection pool. | ||
def with_client | ||
client = check_out | ||
yield client | ||
ensure | ||
check_in(client) unless client.nil? | ||
end | ||
|
||
# This method is called to execute a prepared statement | ||
def execute(query) | ||
with_client do |client| | ||
statement = client.prepare(query.sql) | ||
statement.execute(*query.params) | ||
end | ||
end | ||
|
||
# This method is called to execute a query | ||
def query(query_string) | ||
with_client { |client| client.query(query_string) } | ||
end | ||
|
||
# This method is called to execute a query which will return multiple result sets in an array | ||
def query_multiple_results(query_string) | ||
with_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 | ||
|
||
# This method is called to use a client within a transaction | ||
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 | ||
|
||
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 | ||
end | ||
end |
Oops, something went wrong.