mysql_framework 2.0.0.rc1 → 2.0.0

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: 79a94352053f85513e65fe768c6a2dbe420c14dd84e483e6f5ee276029b3c494
4
- data.tar.gz: 794c2b7cafbdfa11d9be23e924a71cec74140003396266c2cc2dec023f2c7c26
3
+ metadata.gz: 67610b6f4fad6d1ae4feca440f9952c02c92e56dc9add1f8ae65c5258cbb70e5
4
+ data.tar.gz: d27443bd3f0d37eecc86471f43a9a67047dea2a873b122851cb32b3f41062c47
5
5
  SHA512:
6
- metadata.gz: affb71d82cf2c8446d72b2a898abbade1d62a04dbebc97502ccef5a92c6c1e0377a9acc9dccf2a08ce6d7d9da03b64eb0b1d1a861715ca15977c2b53f6aa8c89
7
- data.tar.gz: 8f48457c4dbc815b62406ab8c64fa14a4cc572d0f30c683d66a22a0e9cc7f68112d364edc0bf24d6569fabfaef5869e0606282a9a998910cf1c78bf2f785f264
6
+ metadata.gz: def004956a96d594de0afe4e6b2f6033d539b0affc5ba08e9a83813f46dd8f0beaa9d0e6e65d45bb985cbd931c5d33c39a0b093412850fb8e7acd33130418893
7
+ data.tar.gz: bb88d080d02dbf476de3f7c94ff4982b93fb92ac63aaf7a472944a13859de7bf01458e1cb8b6e9af6c2b38e2b49e277c610abaed4d078f6a43d9df01ba2f6517
@@ -3,18 +3,54 @@
3
3
  module MysqlFramework
4
4
  # This class is used to represent a Sql Condition for a column.
5
5
  class SqlCondition
6
+ NIL_COMPARISONS = ['IS NULL', 'IS NOT NULL'].freeze
7
+
6
8
  # This method is called to get the value of this condition for prepared statements.
7
9
  attr_reader :value
8
10
 
9
- def initialize(column:, comparison:, value:)
11
+ # Creates a new SqlCondition using the given parameters.
12
+ #
13
+ # @raise ArgumentError if comparison is 'IS NULL' and value is not nil
14
+ # @raise ArgumentError if comparison is 'IS NOT NULL' and value is not nil
15
+ # @raise ArgumentError if comparison is neither 'IS NULL' or 'IS NOT NULL' and value is nil
16
+ #
17
+ # @param column [String] - the name of the column to use in the comparison
18
+ # @param comparison [String] - the MySQL comparison operator to use
19
+ # @param value [Object] - the value to use in the comparison (default nil)
20
+ def initialize(column:, comparison:, value: nil)
10
21
  @column = column
11
22
  @comparison = comparison
23
+
24
+ validate(value)
12
25
  @value = value
13
26
  end
14
27
 
15
28
  # This method is called to get the condition as a string for a sql prepared statement
29
+ #
30
+ # @return [String]
16
31
  def to_s
32
+ return "#{@column} #{@comparison.upcase}" if nil_comparison?
33
+
17
34
  "#{@column} #{@comparison} ?"
18
35
  end
36
+
37
+ private
38
+
39
+ def nil_comparison?
40
+ NIL_COMPARISONS.include?(@comparison.upcase)
41
+ end
42
+
43
+ def validate(value)
44
+ raise ArgumentError, "Cannot set value when comparison is #{@comparison}" if invalid_null_condition?(value)
45
+ raise ArgumentError, "Comparison of #{@comparison} requires value to be not nil" if invalid_nil_value?(value)
46
+ end
47
+
48
+ def invalid_null_condition?(value)
49
+ nil_comparison? && value != nil
50
+ end
51
+
52
+ def invalid_nil_value?(value)
53
+ nil_comparison? == false && value.nil?
54
+ end
19
55
  end
20
56
  end
@@ -234,7 +234,7 @@ module MysqlFramework
234
234
  # query.insert('users')
235
235
  # .into('id', first_name', 'login_count')
236
236
  # .values(1, 'Bob', 1)
237
- # .duplicate_update(
237
+ # .on_duplicate(
238
238
  # {
239
239
  # first_name: nil,
240
240
  # login_count: 'login_count + 5'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MysqlFramework
4
- VERSION = '2.0.0.rc1'
4
+ VERSION = '2.0.0'
5
5
  end
@@ -8,4 +8,82 @@ describe MysqlFramework::SqlCondition do
8
8
  expect(subject.to_s).to eq('version = ?')
9
9
  end
10
10
  end
11
+
12
+ context 'when comparison is neither IS NULL or IS NOT NULL' do
13
+ context 'when value is nil' do
14
+ subject { described_class.new(column: 'version', comparison: '=', value: nil) }
15
+
16
+ it 'does raises an ArgumentError' do
17
+ expect { subject }.to raise_error(ArgumentError, "Comparison of = requires value to be not nil")
18
+ end
19
+ end
20
+ end
21
+
22
+ context 'when comparison is IS NULL' do
23
+ subject { described_class.new(column: 'version', comparison: 'IS NULL') }
24
+
25
+ it 'has a nil value by default' do
26
+ expect(subject.value).to be_nil
27
+ end
28
+
29
+ context 'when a value is passed to the constructor' do
30
+ subject { described_class.new(column: 'version', comparison: 'IS NULL', value: 'foo') }
31
+
32
+ describe '#new' do
33
+ it 'raises an ArgumentError if value is set' do
34
+ expect { subject }.to raise_error(ArgumentError, 'Cannot set value when comparison is IS NULL')
35
+ end
36
+ end
37
+ end
38
+
39
+ describe '#to_s' do
40
+ it 'does not include a value placeholder' do
41
+ expect(subject.to_s).to eq('version IS NULL')
42
+ end
43
+ end
44
+ end
45
+
46
+ context 'when comparison is lowercase is null' do
47
+ subject { described_class.new(column: 'version', comparison: 'is null') }
48
+
49
+ describe '#to_s' do
50
+ it 'ignores case' do
51
+ expect(subject.to_s).to eq 'version IS NULL'
52
+ end
53
+ end
54
+ end
55
+
56
+ context 'when comparison is IS NOT NULL' do
57
+ subject { described_class.new(column: 'version', comparison: 'IS NOT NULL') }
58
+
59
+ it 'has a nil value by default' do
60
+ expect(subject.value).to be_nil
61
+ end
62
+
63
+ context 'when a value is passed to the constructor' do
64
+ subject { described_class.new(column: 'version', comparison: 'IS NOT NULL', value: 'foo') }
65
+
66
+ describe '#new' do
67
+ it 'raises an ArgumentError if value is set' do
68
+ expect { subject }.to raise_error(ArgumentError, 'Cannot set value when comparison is IS NOT NULL')
69
+ end
70
+ end
71
+ end
72
+
73
+ describe '#to_s' do
74
+ it 'does not include a value placeholder' do
75
+ expect(subject.to_s).to eq('version IS NOT NULL')
76
+ end
77
+ end
78
+ end
79
+
80
+ context 'when comparison is lowercase is not null' do
81
+ subject { described_class.new(column: 'version', comparison: 'is not null') }
82
+
83
+ describe '#to_s' do
84
+ it 'ignores case' do
85
+ expect(subject.to_s).to eq 'version IS NOT NULL'
86
+ end
87
+ end
88
+ end
11
89
  end
@@ -213,6 +213,13 @@ describe MysqlFramework::SqlQuery do
213
213
  expect(subject.sql).to eq('WHERE (`gems`.`author` = ? AND `gems`.`created_at` > ?) AND (`gems`.`name` = ?)')
214
214
  end
215
215
  end
216
+
217
+ context 'when the condition includes an array of parameters' do
218
+ it 'concats the parameter collections' do
219
+ subject.and.where(gems[:name].in('a','b'))
220
+ expect(subject.sql).to eq('WHERE (`gems`.`author` = ? AND `gems`.`created_at` > ?) AND (`gems`.`name` IN (?, ?))')
221
+ end
222
+ end
216
223
  end
217
224
 
218
225
  describe '#and' do
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: 2.0.0.rc1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sage
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-12 00:00:00.000000000 Z
11
+ date: 2020-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -133,9 +133,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
133
133
  version: '0'
134
134
  required_rubygems_version: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - ">"
136
+ - - ">="
137
137
  - !ruby/object:Gem::Version
138
- version: 1.3.1
138
+ version: '0'
139
139
  requirements: []
140
140
  rubyforge_project:
141
141
  rubygems_version: 2.7.7