mysql_framework 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mysql_framework/connector.rb +11 -11
- data/lib/mysql_framework/version.rb +1 -1
- data/spec/lib/mysql_framework/connector_spec.rb +53 -25
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30e4dba5e1c3a4c988b3c1384bd9ef59b3a0efc0e562aa180aa8ee2bbcd31fff
|
4
|
+
data.tar.gz: c8a387e05c36ad6b900bcf3dc268c28222fee086c9eef9a56aab770cd4a04e25
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: daf10b0a2e39f8b8837f90c87a6585ba8bb786499e9ab64718e8b93370098fc3a5878b8196c586ed5b8231c3a8f2ae5cea259b9c66495ed01c03b35a872e432c
|
7
|
+
data.tar.gz: a4dee54ea3cd66f3b08a12865cdd3e78b053973555cacd1ebaf576d624371197b5de4683bb2e0eb07d7d99e5774fc4b4255da296eb28513a41c7c8bacb24b293
|
@@ -32,21 +32,21 @@ module MysqlFramework
|
|
32
32
|
end
|
33
33
|
|
34
34
|
# This method is called to execute a prepared statement
|
35
|
-
def execute(query)
|
36
|
-
with_client do |client|
|
35
|
+
def execute(query, provided_client = nil)
|
36
|
+
with_client(provided_client) do |client|
|
37
37
|
statement = client.prepare(query.sql)
|
38
38
|
statement.execute(*query.params)
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
42
|
# This method is called to execute a query
|
43
|
-
def query(query_string)
|
44
|
-
with_client { |client| client.query(query_string) }
|
43
|
+
def query(query_string, provided_client = nil)
|
44
|
+
with_client(provided_client) { |client| client.query(query_string) }
|
45
45
|
end
|
46
46
|
|
47
47
|
# This method is called to execute a query which will return multiple result sets in an array
|
48
|
-
def query_multiple_results(query_string)
|
49
|
-
with_client do |client|
|
48
|
+
def query_multiple_results(query_string, provided_client = nil)
|
49
|
+
with_client(provided_client) do |client|
|
50
50
|
result = []
|
51
51
|
result << client.query(query_string).to_a
|
52
52
|
result << client.store_result&.to_a while client.next_result
|
@@ -72,11 +72,11 @@ module MysqlFramework
|
|
72
72
|
|
73
73
|
def default_options
|
74
74
|
{
|
75
|
-
host:
|
76
|
-
port:
|
77
|
-
database:
|
78
|
-
username:
|
79
|
-
password:
|
75
|
+
host: ENV.fetch('MYSQL_HOST'),
|
76
|
+
port: ENV.fetch('MYSQL_PORT'),
|
77
|
+
database: ENV.fetch('MYSQL_DATABASE'),
|
78
|
+
username: ENV.fetch('MYSQL_USERNAME'),
|
79
|
+
password: ENV.fetch('MYSQL_PASSWORD'),
|
80
80
|
reconnect: true
|
81
81
|
}
|
82
82
|
end
|
@@ -5,18 +5,18 @@ require 'spec_helper'
|
|
5
5
|
describe MysqlFramework::Connector do
|
6
6
|
let(:default_options) do
|
7
7
|
{
|
8
|
-
host:
|
9
|
-
port:
|
10
|
-
database:
|
11
|
-
username:
|
12
|
-
password:
|
8
|
+
host: ENV.fetch('MYSQL_HOST'),
|
9
|
+
port: ENV.fetch('MYSQL_PORT'),
|
10
|
+
database: ENV.fetch('MYSQL_DATABASE'),
|
11
|
+
username: ENV.fetch('MYSQL_USERNAME'),
|
12
|
+
password: ENV.fetch('MYSQL_PASSWORD'),
|
13
13
|
reconnect: true
|
14
14
|
}
|
15
15
|
end
|
16
16
|
let(:options) do
|
17
17
|
{
|
18
|
-
host:
|
19
|
-
port:
|
18
|
+
host: 'host',
|
19
|
+
port: 'port',
|
20
20
|
database: 'database',
|
21
21
|
username: 'username',
|
22
22
|
password: 'password',
|
@@ -25,12 +25,14 @@ describe MysqlFramework::Connector do
|
|
25
25
|
end
|
26
26
|
let(:client) { double }
|
27
27
|
let(:gems) { MysqlFramework::SqlTable.new('gems') }
|
28
|
+
let(:existing_client) { Mysql2::Client.new(default_options) }
|
28
29
|
|
29
30
|
subject { described_class.new }
|
30
31
|
|
31
32
|
describe '#initialize' do
|
32
33
|
it 'sets default query options on the Mysql2 client' do
|
33
34
|
subject
|
35
|
+
|
34
36
|
expect(Mysql2::Client.default_query_options[:symbolize_keys]).to eq(true)
|
35
37
|
expect(Mysql2::Client.default_query_options[:cast_booleans]).to eq(true)
|
36
38
|
end
|
@@ -53,6 +55,7 @@ describe MysqlFramework::Connector do
|
|
53
55
|
context 'when the connection pool has a client available' do
|
54
56
|
it 'returns a client instance from the pool' do
|
55
57
|
subject.instance_variable_get(:@connection_pool).push(client)
|
58
|
+
|
56
59
|
expect(subject.check_out).to eq(client)
|
57
60
|
end
|
58
61
|
end
|
@@ -61,6 +64,7 @@ describe MysqlFramework::Connector do
|
|
61
64
|
describe '#check_in' do
|
62
65
|
it 'returns the provided client to the connection pool' do
|
63
66
|
expect(subject.instance_variable_get(:@connection_pool)).to receive(:push).with(client)
|
67
|
+
|
64
68
|
subject.check_in(client)
|
65
69
|
end
|
66
70
|
end
|
@@ -104,29 +108,46 @@ describe MysqlFramework::Connector do
|
|
104
108
|
expect(results.length).to eq(1)
|
105
109
|
expect(results[0][:id]).to eq(guid)
|
106
110
|
end
|
111
|
+
|
112
|
+
it 'does not check out a new client when one is provided' do
|
113
|
+
expect(subject).not_to receive(:check_out)
|
114
|
+
|
115
|
+
guid = insert_query.params[0]
|
116
|
+
subject.execute(insert_query, existing_client)
|
117
|
+
|
118
|
+
results = subject.query("SELECT * FROM `gems` WHERE id = '#{guid}';", existing_client).to_a
|
119
|
+
expect(results.length).to eq(1)
|
120
|
+
expect(results[0][:id]).to eq(guid)
|
121
|
+
end
|
107
122
|
end
|
108
123
|
|
109
124
|
describe '#query' do
|
110
|
-
before
|
111
|
-
allow(subject).to receive(:check_out).and_return(client)
|
112
|
-
end
|
125
|
+
before(:each) { allow(subject).to receive(:check_out).and_return(client) }
|
113
126
|
|
114
127
|
it 'retrieves a client and calls query' do
|
115
128
|
expect(client).to receive(:query).with('SELECT 1')
|
129
|
+
|
116
130
|
subject.query('SELECT 1')
|
117
131
|
end
|
132
|
+
|
133
|
+
it 'does not check out a new client when one is provided' do
|
134
|
+
expect(subject).not_to receive(:check_out)
|
135
|
+
expect(existing_client).to receive(:query).with('SELECT 1')
|
136
|
+
|
137
|
+
subject.query('SELECT 1', existing_client)
|
138
|
+
end
|
118
139
|
end
|
119
140
|
|
120
141
|
describe '#query_multiple_results' do
|
121
142
|
let(:test) { MysqlFramework::SqlTable.new('test') }
|
122
143
|
let(:manager) { MysqlFramework::Scripts::Manager.new }
|
123
144
|
let(:connector) { MysqlFramework::Connector.new }
|
124
|
-
let(:timestamp) { Time.at(
|
145
|
+
let(:timestamp) { Time.at(628_232_400) } # 1989-11-28 00:00:00 -0500
|
125
146
|
let(:guid) { 'a3ccb138-48ae-437a-be52-f673beb12b51' }
|
126
147
|
let(:insert) do
|
127
148
|
MysqlFramework::SqlQuery.new.insert(test)
|
128
|
-
.into(test[:id],test[:name],test[:action],test[:created_at],test[:updated_at])
|
129
|
-
.values(guid,'name','action',timestamp,timestamp)
|
149
|
+
.into(test[:id], test[:name], test[:action], test[:created_at], test[:updated_at])
|
150
|
+
.values(guid, 'name', 'action', timestamp, timestamp)
|
130
151
|
end
|
131
152
|
let(:obj) do
|
132
153
|
{
|
@@ -134,7 +155,7 @@ describe MysqlFramework::Connector do
|
|
134
155
|
name: 'name',
|
135
156
|
action: 'action',
|
136
157
|
created_at: timestamp,
|
137
|
-
updated_at: timestamp
|
158
|
+
updated_at: timestamp
|
138
159
|
}
|
139
160
|
end
|
140
161
|
|
@@ -145,13 +166,24 @@ describe MysqlFramework::Connector do
|
|
145
166
|
connector.execute(insert)
|
146
167
|
end
|
147
168
|
|
148
|
-
after
|
149
|
-
manager.drop_all_tables
|
150
|
-
end
|
169
|
+
after(:each) { manager.drop_all_tables }
|
151
170
|
|
152
171
|
it 'returns the results from the stored procedure' do
|
153
|
-
query =
|
172
|
+
query = 'call test_procedure'
|
154
173
|
result = subject.query_multiple_results(query)
|
174
|
+
|
175
|
+
expect(result).to be_a(Array)
|
176
|
+
expect(result.length).to eq(2)
|
177
|
+
expect(result[0]).to eq([])
|
178
|
+
expect(result[1]).to eq([obj])
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'does not check out a new client when one is provided' do
|
182
|
+
expect(subject).not_to receive(:check_out)
|
183
|
+
|
184
|
+
query = 'call test_procedure'
|
185
|
+
result = subject.query_multiple_results(query, existing_client)
|
186
|
+
|
155
187
|
expect(result).to be_a(Array)
|
156
188
|
expect(result.length).to eq(2)
|
157
189
|
expect(result[0]).to eq([])
|
@@ -160,18 +192,14 @@ describe MysqlFramework::Connector do
|
|
160
192
|
end
|
161
193
|
|
162
194
|
describe '#transaction' do
|
163
|
-
before
|
164
|
-
allow(subject).to receive(:check_out).and_return(client)
|
165
|
-
end
|
195
|
+
before(:each) { allow(subject).to receive(:check_out).and_return(client) }
|
166
196
|
|
167
197
|
it 'wraps the client call with BEGIN and COMMIT statements' do
|
168
198
|
expect(client).to receive(:query).with('BEGIN')
|
169
199
|
expect(client).to receive(:query).with('SELECT 1')
|
170
200
|
expect(client).to receive(:query).with('COMMIT')
|
171
201
|
|
172
|
-
subject.transaction
|
173
|
-
subject.query('SELECT 1')
|
174
|
-
end
|
202
|
+
subject.transaction { subject.query('SELECT 1') }
|
175
203
|
end
|
176
204
|
|
177
205
|
context 'when an exception occurs' do
|
@@ -183,7 +211,7 @@ describe MysqlFramework::Connector do
|
|
183
211
|
subject.transaction do
|
184
212
|
raise
|
185
213
|
end
|
186
|
-
rescue
|
214
|
+
rescue StandardError
|
187
215
|
end
|
188
216
|
end
|
189
217
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mysql_framework
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sage
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|