mysql_framework 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db86d4f46bfac72057d5b95d706229b5ce43824f2e585ce690d9e71d64564322
4
- data.tar.gz: ce5cf833871398172c4d401185671b94f5226f0384ada8aa956926e2d7024753
3
+ metadata.gz: 63af1c4a1fb7d7ae8d69681134a6e58ade4b2baa6070443bf8ecb2e8f5a5b94e
4
+ data.tar.gz: 89478ee0fec0d2e6fb9ac3dbac4efeed74e751dce998961f7cd547b6d90632d2
5
5
  SHA512:
6
- metadata.gz: 53b60798e62bc6d5febfe8a4f670c3bb448b43440811bc4b3f832098ba9aa40f68c8eefc4346b496acdbed551e57afa19efa37c933651ab19b4ce3faf40dbfb1
7
- data.tar.gz: c4518d98cbca222d2e0031d9559871633e650807b4c75c3d36012914738e08fb403401d01ecb7e118f237379b31f96b9f7aec6f4a2c0b193a874329e0afe31bd
6
+ metadata.gz: 479082218477782a3c435c31cdabab1285f48d50a210f03b94fd4ace9569e6cf0497612dc45e451558b8a5c4ad0c982c11a1905057bc120228bd81029d359b52
7
+ data.tar.gz: 31c848e20ee916e9ae61d1454b711df9dacf78e81f1265d798aafae061f5465af0b6e0066fdce8886c271be8510c14e415f83320d9b3c9b88264ce26b55464a2
@@ -18,119 +18,177 @@ module MysqlFramework
18
18
 
19
19
  # This method is called to start a select query
20
20
  def select(*columns)
21
- @sql = "select #{columns.join(',')}"
21
+ @sql = "SELECT #{columns.join(', ')}"
22
+
22
23
  self
23
24
  end
24
25
 
25
26
  # This method is called to start a delete query
26
27
  def delete
27
- @sql = 'delete'
28
+ @sql = 'DELETE'
29
+
28
30
  self
29
31
  end
30
32
 
31
33
  # This method is called to start an update query
32
34
  def update(table, partition = nil)
33
- @sql = "update #{table}"
34
- @sql += " partition(p#{partition})" unless partition.nil?
35
+ @sql = "UPDATE #{table}"
36
+ @sql += " PARTITION (p#{partition})" unless partition.nil?
37
+
35
38
  self
36
39
  end
37
40
 
38
41
  # This method is called to start an insert query
39
42
  def insert(table, partition = nil)
40
- @sql += "insert into #{table}"
41
- @sql += " partition(p#{partition})" unless partition.nil?
43
+ @sql += "INSERT INTO #{table}"
44
+ @sql += " PARTITION (p#{partition})" unless partition.nil?
45
+
42
46
  self
43
47
  end
44
48
 
45
49
  # This method is called to specify the columns to insert into.
46
50
  def into(*columns)
47
- @sql += " (#{columns.join(',')})"
51
+ @sql += " (#{columns.join(', ')})"
52
+
48
53
  self
49
54
  end
50
55
 
51
56
  # This method is called to specify the values to insert.
52
57
  def values(*values)
53
- @sql += " values (#{values.map { |_v| '?' }.join(',')})"
54
- values.each do |v|
55
- @params << v
56
- end
58
+ @sql += " VALUES (#{values.map { '?' }.join(', ')})"
59
+
60
+ values.each { |value| @params << value }
61
+
57
62
  self
58
63
  end
59
64
 
60
65
  # This method is called to specify the columns to update.
61
66
  def set(values)
62
- @sql += ' set '
63
- values.each do |k, p|
64
- @sql += "`#{k}` = ?, "
65
- @params << p
67
+ @sql += ' SET '
68
+
69
+ values.each do |key, param|
70
+ @sql += "`#{key}` = ?, "
71
+ @params << param
66
72
  end
67
- @sql = @sql[0...-2]
73
+
74
+ @sql = @sql.chomp(', ')
75
+
76
+ self
77
+ end
78
+
79
+ def increment(values)
80
+ @sql += @sql.include?('SET') ? ', ' : ' SET '
81
+
82
+ values.each { |key, by| @sql += "`#{key}` = `#{key}` + #{by}, " }
83
+
84
+ @sql = @sql.chomp(', ')
85
+
86
+ self
87
+ end
88
+
89
+ def decrement(values)
90
+ @sql += @sql.include?('SET') ? ', ' : ' SET '
91
+
92
+ values.each { |key, by| @sql += "`#{key}` = `#{key}` - #{by}, " }
93
+
94
+ @sql = @sql.chomp(', ')
95
+
68
96
  self
69
97
  end
70
98
 
71
99
  # This method is called to specify the table/partition a select/delete query is for.
72
100
  def from(table, partition = nil)
73
- @sql += " from #{table}"
74
- @sql += " partition(p#{partition})" unless partition.nil?
101
+ @sql += " FROM #{table}"
102
+ @sql += " PARTITION (p#{partition})" unless partition.nil?
103
+
75
104
  self
76
105
  end
77
106
 
78
107
  # This method is called to specify a where clause for a query.
79
108
  def where(*conditions)
80
- @sql += ' where' unless @sql.include?('where')
81
- @sql += " (#{conditions.join(' and ')}) "
82
- conditions.each do |c|
83
- @params << c.value
84
- end
109
+ @sql += ' WHERE' unless @sql.include?('WHERE')
110
+ @sql += " (#{conditions.join(' AND ')}) "
111
+
112
+ conditions.each { |condition| @params << condition.value }
113
+
85
114
  self
86
115
  end
87
116
 
88
117
  # This method is called to add an `and` keyword to a query to provide additional where clauses.
89
118
  def and
90
- @sql += 'and'
119
+ @sql += 'AND'
120
+
91
121
  self
92
122
  end
93
123
 
94
124
  # This method is called to add an `or` keyword to a query to provide alternate where clauses.
95
125
  def or
96
- @sql += 'or'
126
+ @sql += 'OR'
127
+
97
128
  self
98
129
  end
99
130
 
100
131
  # This method is called to add an `order by` statement to a query
101
132
  def order(*columns)
102
- @sql += " order by #{columns.join(',')}"
133
+ @sql += " ORDER BY #{columns.join(', ')}"
134
+
103
135
  self
104
136
  end
105
137
 
106
138
  # This method is called to add an `order by ... desc` statement to a query
107
139
  def order_desc(*columns)
108
140
  order(*columns)
109
- @sql += ' desc'
141
+
142
+ @sql += ' DESC'
143
+
110
144
  self
111
145
  end
112
146
 
113
147
  # This method is called to add a limit to a query
114
148
  def limit(count)
115
- @sql += " limit #{count}"
149
+ @sql += " LIMIT #{count}"
150
+
151
+ self
152
+ end
153
+
154
+ # This method is called to add an offset to a query
155
+ def offset(offset)
156
+ raise 'A limit clause must be supplied to use an offset' unless @sql.include?('LIMIT')
157
+
158
+ @sql += " OFFSET #{offset}"
159
+
116
160
  self
117
161
  end
118
162
 
119
163
  # This method is called to add a join statement to a query.
120
- def join(table)
121
- @sql += " join #{table}"
164
+ def join(table, type: nil)
165
+ @sql += " #{type.upcase}" unless type.nil?
166
+ @sql += " JOIN #{table}"
167
+
122
168
  self
123
169
  end
124
170
 
125
171
  # This method is called to add the `on` detail to a join statement.
126
172
  def on(column_1, column_2)
127
- @sql += " on #{column_1} = #{column_2}"
173
+ @sql += " ON #{column_1} = #{column_2}"
174
+
128
175
  self
129
176
  end
130
177
 
131
178
  # This method is called to add a `group by` statement to a query
132
179
  def group_by(*columns)
133
- @sql += " group by #{columns.join(',')}"
180
+ @sql += " GROUP BY #{columns.join(', ')}"
181
+
182
+ self
183
+ end
184
+
185
+ # This method is called to specify a having clause for a query.
186
+ def having(*conditions)
187
+ @sql += ' HAVING' unless @sql.include?('HAVING')
188
+ @sql += " (#{conditions.join(' AND ')}) "
189
+
190
+ conditions.each { |condition| @params << condition.value }
191
+
134
192
  self
135
193
  end
136
194
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MysqlFramework
4
- VERSION = '0.0.10'
4
+ VERSION = '0.0.11'
5
5
  end
@@ -20,7 +20,7 @@ describe MysqlFramework::SqlQuery do
20
20
  '2018-06-28 10:00:00'
21
21
  )
22
22
 
23
- expect(subject.sql).to eq('insert into `gems` partition(p15) (`gems`.`name`,`gems`.`author`,`gems`.`created_at`,`gems`.`updated_at`) values (?,?,?,?)')
23
+ expect(subject.sql).to eq('INSERT INTO `gems` PARTITION (p15) (`gems`.`name`, `gems`.`author`, `gems`.`created_at`, `gems`.`updated_at`) VALUES (?, ?, ?, ?)')
24
24
  expect(subject.params).to eq(['mysql_framework', 'sage', '2018-06-28 10:00:00', '2018-06-28 10:00:00'])
25
25
  end
26
26
 
@@ -30,32 +30,23 @@ describe MysqlFramework::SqlQuery do
30
30
  name: 'mysql_framework',
31
31
  updated_at: '2018-06-28 13:00:00'
32
32
  )
33
- .where(
34
- gems[:id].eq('12345')
35
- )
33
+ .where(gems[:id].eq('12345'))
36
34
 
37
- expect(subject.sql).to eq('update `gems` partition(p20) set `name` = ?, `updated_at` = ? where (`gems`.`id` = ?)')
35
+ expect(subject.sql).to eq('UPDATE `gems` PARTITION (p20) SET `name` = ?, `updated_at` = ? WHERE (`gems`.`id` = ?)')
38
36
  expect(subject.params).to eq(['mysql_framework', '2018-06-28 13:00:00', '12345'])
39
37
  end
40
38
 
41
39
  it 'builds the delete query as expected' do
42
- subject.delete.from(gems, 30)
43
- .where(
44
- gems[:id].eq('45678')
45
- )
40
+ subject.delete.from(gems, 30).where(gems[:id].eq('45678'))
46
41
 
47
- expect(subject.sql).to eq('delete from `gems` partition(p30) where (`gems`.`id` = ?)')
42
+ expect(subject.sql).to eq('DELETE FROM `gems` PARTITION (p30) WHERE (`gems`.`id` = ?)')
48
43
  expect(subject.params).to eq(['45678'])
49
44
  end
50
45
 
51
46
  it 'builds a basic select query as expected' do
52
- subject.select('*')
53
- .from(gems, 40)
54
- .where(
55
- gems[:id].eq('9876')
56
- )
47
+ subject.select('*').from(gems, 40).where(gems[:id].eq('9876'))
57
48
 
58
- expect(subject.sql).to eq('select * from `gems` partition(p40) where (`gems`.`id` = ?)')
49
+ expect(subject.sql).to eq('SELECT * FROM `gems` PARTITION (p40) WHERE (`gems`.`id` = ?)')
59
50
  expect(subject.params).to eq(['9876'])
60
51
  end
61
52
 
@@ -63,11 +54,9 @@ describe MysqlFramework::SqlQuery do
63
54
  subject.select('*')
64
55
  .from(gems, 40)
65
56
  .join(versions).on(versions[:gem_id], gems[:id])
66
- .where(
67
- gems[:id].eq('9876')
68
- )
57
+ .where(gems[:id].eq('9876'))
69
58
 
70
- expect(subject.sql).to eq('select * from `gems` partition(p40) join `versions` on `versions`.`gem_id` = `gems`.`id` where (`gems`.`id` = ?)')
59
+ expect(subject.sql).to eq('SELECT * FROM `gems` PARTITION (p40) JOIN `versions` ON `versions`.`gem_id` = `gems`.`id` WHERE (`gems`.`id` = ?)')
71
60
  expect(subject.params).to eq(['9876'])
72
61
  end
73
62
  end
@@ -75,27 +64,31 @@ describe MysqlFramework::SqlQuery do
75
64
  describe '#select' do
76
65
  it 'sets the sql for a select statement' do
77
66
  subject.select(gems[:id], gems[:name])
78
- expect(subject.sql).to eq('select `gems`.`id`,`gems`.`name`')
67
+
68
+ expect(subject.sql).to eq('SELECT `gems`.`id`, `gems`.`name`')
79
69
  end
80
70
  end
81
71
 
82
72
  describe '#delete' do
83
73
  it 'sets the sql for a delete statement' do
84
74
  subject.delete
85
- expect(subject.sql).to eq('delete')
75
+
76
+ expect(subject.sql).to eq('DELETE')
86
77
  end
87
78
  end
88
79
 
89
80
  describe '#update' do
90
81
  it 'sets the sql for an update statement' do
91
82
  subject.update(gems)
92
- expect(subject.sql).to eq('update `gems`')
83
+
84
+ expect(subject.sql).to eq('UPDATE `gems`')
93
85
  end
94
86
 
95
87
  context 'when a partition is specified' do
96
88
  it 'sets the sql for an update statement' do
97
89
  subject.update(gems, 25)
98
- expect(subject.sql).to eq('update `gems` partition(p25)')
90
+
91
+ expect(subject.sql).to eq('UPDATE `gems` PARTITION (p25)')
99
92
  end
100
93
  end
101
94
  end
@@ -103,13 +96,15 @@ describe MysqlFramework::SqlQuery do
103
96
  describe '#insert' do
104
97
  it 'sets the sql for an insert statement' do
105
98
  subject.insert(gems)
106
- expect(subject.sql).to eq('insert into `gems`')
99
+
100
+ expect(subject.sql).to eq('INSERT INTO `gems`')
107
101
  end
108
102
 
109
103
  context 'when a partition is specified' do
110
104
  it 'sets the sql for an insert statement' do
111
105
  subject.insert(gems, 35)
112
- expect(subject.sql).to eq('insert into `gems` partition(p35)')
106
+
107
+ expect(subject.sql).to eq('INSERT INTO `gems` PARTITION (p35)')
113
108
  end
114
109
  end
115
110
  end
@@ -117,55 +112,91 @@ describe MysqlFramework::SqlQuery do
117
112
  describe '#into' do
118
113
  it 'sets the sql for an into statement' do
119
114
  subject.into(gems[:name], gems[:author], gems[:created_at])
120
- expect(subject.sql).to eq('(`gems`.`name`,`gems`.`author`,`gems`.`created_at`)')
115
+
116
+ expect(subject.sql).to eq('(`gems`.`name`, `gems`.`author`, `gems`.`created_at`)')
121
117
  end
122
118
  end
123
119
 
124
120
  describe '#values' do
125
121
  it 'sets the sql for the values statement' do
126
122
  subject.values('mysql_framework', 'sage', '2016-06-28 10:00:00')
127
- expect(subject.sql).to eq('values (?,?,?)')
123
+
124
+ expect(subject.sql).to eq('VALUES (?, ?, ?)')
128
125
  end
129
126
  end
130
127
 
131
128
  describe '#set' do
132
129
  it 'sets the sql for the set statement' do
133
130
  subject.set(name: 'mysql_framework', author: 'sage', created_at: '2016-06-28 10:00:00')
134
- expect(subject.sql).to eq('set `name` = ?, `author` = ?, `created_at` = ?')
131
+
132
+ expect(subject.sql).to eq('SET `name` = ?, `author` = ?, `created_at` = ?')
133
+ end
134
+ end
135
+
136
+ describe '#increment' do
137
+ it 'appends the sql for the increment statement' do
138
+ subject.set(updated_at: '2016-01-15 19:00:00').increment(count: 1)
139
+
140
+ expect(subject.sql).to eq('SET `updated_at` = ?, `count` = `count` + 1')
141
+ end
142
+
143
+ context 'when a set statement has not been issued' do
144
+ it 'appends the sql for the increment statement' do
145
+ subject.increment(count: 1)
146
+
147
+ expect(subject.sql).to eq('SET `count` = `count` + 1')
148
+ end
149
+ end
150
+ end
151
+
152
+ describe '#decrement' do
153
+ it 'appends the sql for the decrement statement' do
154
+ subject.set(updated_at: '2016-01-15 19:00:00').decrement(count: 1)
155
+
156
+ expect(subject.sql).to eq('SET `updated_at` = ?, `count` = `count` - 1')
157
+ end
158
+
159
+ context 'when a set statement has not been issued' do
160
+ it 'appends the sql for the decrement statement' do
161
+ subject.decrement(count: 1)
162
+
163
+ expect(subject.sql).to eq('SET `count` = `count` - 1')
164
+ end
135
165
  end
136
166
  end
137
167
 
138
168
  describe '#from' do
139
169
  it 'sets the sql for a from statement' do
140
170
  subject.from(gems)
141
- expect(subject.sql).to eq('from `gems`')
171
+
172
+ expect(subject.sql).to eq('FROM `gems`')
142
173
  end
143
174
 
144
175
  context 'when a partition is specified' do
145
176
  it 'sets the sql for a from statement' do
146
177
  subject.from(gems, 45)
147
- expect(subject.sql).to eq('from `gems` partition(p45)')
178
+
179
+ expect(subject.sql).to eq('FROM `gems` PARTITION (p45)')
148
180
  end
149
181
  end
150
182
  end
151
183
 
152
184
  describe '#where' do
153
- before :each do
154
- subject.where(gems['author'].eq('sage'), gems['created_at'].gt('2018-01-01 00:00:00'))
155
- end
185
+ before(:each) { subject.where(gems[:author].eq('sage'), gems[:created_at].gt('2018-01-01 00:00:00')) }
156
186
 
157
187
  it 'appends where to the sql' do
158
- expect(subject.sql).to include('where')
188
+ expect(subject.sql).to include('WHERE')
159
189
  end
160
190
 
161
191
  it 'sets the sql for the where statement' do
162
- expect(subject.sql).to eq('where (`gems`.`author` = ? and `gems`.`created_at` > ?)')
192
+ expect(subject.sql).to eq('WHERE (`gems`.`author` = ? AND `gems`.`created_at` > ?)')
163
193
  end
164
194
 
165
195
  context 'when the sql already contains a where' do
166
196
  it 'does not append an extra where' do
167
- subject.and.where(gems['name'].eq('mysql_framework'))
168
- expect(subject.sql).to eq('where (`gems`.`author` = ? and `gems`.`created_at` > ?) and (`gems`.`name` = ?)')
197
+ subject.and.where(gems[:name].eq('mysql_framework'))
198
+
199
+ expect(subject.sql).to eq('WHERE (`gems`.`author` = ? AND `gems`.`created_at` > ?) AND (`gems`.`name` = ?)')
169
200
  end
170
201
  end
171
202
  end
@@ -173,56 +204,106 @@ describe MysqlFramework::SqlQuery do
173
204
  describe '#and' do
174
205
  it 'appends the sql for an and statement' do
175
206
  subject.and
176
- expect(subject.sql).to eq('and')
207
+
208
+ expect(subject.sql).to eq('AND')
177
209
  end
178
210
  end
179
211
 
180
212
  describe '#or' do
181
213
  it 'appends the sql for an or statement' do
182
214
  subject.or
183
- expect(subject.sql).to eq('or')
215
+
216
+ expect(subject.sql).to eq('OR')
184
217
  end
185
218
  end
186
219
 
187
220
  describe '#order' do
188
221
  it 'appends the sql for an order statement' do
189
222
  subject.order(gems[:created_at], gems[:updated_at])
190
- expect(subject.sql).to eq('order by `gems`.`created_at`,`gems`.`updated_at`')
223
+
224
+ expect(subject.sql).to eq('ORDER BY `gems`.`created_at`, `gems`.`updated_at`')
191
225
  end
192
226
  end
193
227
 
194
228
  describe '#order_desc' do
195
229
  it 'appends the sql for an order descending statement' do
196
230
  subject.order_desc(gems[:created_at], gems[:updated_at])
197
- expect(subject.sql).to eq('order by `gems`.`created_at`,`gems`.`updated_at` desc')
231
+
232
+ expect(subject.sql).to eq('ORDER BY `gems`.`created_at`, `gems`.`updated_at` DESC')
198
233
  end
199
234
  end
200
235
 
201
236
  describe '#limit' do
202
237
  it 'appends the sql for a limit statement' do
203
238
  subject.limit(10)
204
- expect(subject.sql).to eq('limit 10')
239
+
240
+ expect(subject.sql).to eq('LIMIT 10')
241
+ end
242
+ end
243
+
244
+ describe '#offset' do
245
+ it 'appends the sql for an offset statement' do
246
+ subject.limit(10).offset(5)
247
+
248
+ expect(subject.sql).to eq('LIMIT 10 OFFSET 5')
249
+ end
250
+
251
+ context 'when a limit statement is not found' do
252
+ it 'raises an error' do
253
+ expect { subject.offset(5) }.to raise_error(RuntimeError)
254
+ end
205
255
  end
206
256
  end
207
257
 
208
258
  describe '#join' do
209
259
  it 'appends the sql for a join statement' do
210
260
  subject.join(versions)
211
- expect(subject.sql).to eq('join `versions`')
261
+
262
+ expect(subject.sql).to eq('JOIN `versions`')
263
+ end
264
+
265
+ context 'when a type is supplied' do
266
+ it 'appends the sql for a join statement' do
267
+ subject.join(versions, type: 'LEFT OUTER')
268
+
269
+ expect(subject.sql).to eq('LEFT OUTER JOIN `versions`')
270
+ end
212
271
  end
213
272
  end
214
273
 
215
274
  describe '#on' do
216
275
  it 'appends the sql for the on statement' do
217
276
  subject.on(gems[:id], versions[:gem_id])
218
- expect(subject.sql).to eq('on `gems`.`id` = `versions`.`gem_id`')
277
+
278
+ expect(subject.sql).to eq('ON `gems`.`id` = `versions`.`gem_id`')
219
279
  end
220
280
  end
221
281
 
222
282
  describe '#group_by' do
223
283
  it 'appends the sql for a group by statement' do
224
284
  subject.group_by(gems[:created_at], gems[:updated_at])
225
- expect(subject.sql).to eq('group by `gems`.`created_at`,`gems`.`updated_at`')
285
+
286
+ expect(subject.sql).to eq('GROUP BY `gems`.`created_at`, `gems`.`updated_at`')
287
+ end
288
+ end
289
+
290
+ describe '#having' do
291
+ before(:each) { subject.having(gems[:count].gt(1), gems[:count].lt(10)) }
292
+
293
+ it 'appends having to the sql' do
294
+ expect(subject.sql).to include('HAVING')
295
+ end
296
+
297
+ it 'sets the sql for the having statement' do
298
+ expect(subject.sql).to eq('HAVING (`gems`.`count` > ? AND `gems`.`count` < ?)')
299
+ end
300
+
301
+ context 'when the sql already contains a having' do
302
+ it 'does not append an extra having' do
303
+ subject.or.having(gems[:count].gt(20))
304
+
305
+ expect(subject.sql).to eq('HAVING (`gems`.`count` > ? AND `gems`.`count` < ?) OR (`gems`.`count` > ?)')
306
+ end
226
307
  end
227
308
  end
228
309
  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.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sage
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-07 00:00:00.000000000 Z
11
+ date: 2018-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler