diff --git a/lib/mysql_framework/sql_query.rb b/lib/mysql_framework/sql_query.rb index e68c27c..5919cf8 100644 --- a/lib/mysql_framework/sql_query.rb +++ b/lib/mysql_framework/sql_query.rb @@ -127,7 +127,7 @@ def where(*conditions) @sql += " (#{conditions.join(' AND ')}) " conditions.each do |condition| - next if condition.value.nil? + next if condition.value.nil? && !skip_nil_validation? if condition.value.is_a?(Enumerable) @params.concat(condition.value) else @@ -263,5 +263,11 @@ def on_duplicate(update_values = {}) self end + + private + + def skip_nil_validation? + ENV.fetch('MYSQL_FRAMEWORK_SKIP_NIL_VALUE_VALIDATION', 'false').downcase == 'true' + end end end diff --git a/spec/lib/mysql_framework/sql_query_spec.rb b/spec/lib/mysql_framework/sql_query_spec.rb index 53c83a2..634a243 100644 --- a/spec/lib/mysql_framework/sql_query_spec.rb +++ b/spec/lib/mysql_framework/sql_query_spec.rb @@ -233,6 +233,33 @@ expect(subject.sql).to eq('WHERE (`gems`.`author` = ? AND `gems`.`created_at` > ?) AND (`gems`.`name` IN (?, ?))') end end + + context 'when condition is nil' do + context 'when MYSQL_FRAMEWORK_SKIP_NIL_VALUE_VALIDATION is not present' do + it 'concats the parameter collections' do + expect { subject.where(gems[:created_at].eq(nil)) }.to raise_error ArgumentError + end + end + + context 'when MYSQL_FRAMEWORK_SKIP_NIL_VALUE_VALIDATION is present' do + after(:each) { ENV.delete('MYSQL_FRAMEWORK_SKIP_NIL_VALUE_VALIDATION') } + + context 'when value is false' do + it 'concats the parameter collections' do + ENV['MYSQL_FRAMEWORK_SKIP_NIL_VALUE_VALIDATION'] = 'false' + expect { subject.where(gems[:created_at].eq(nil)) }.to raise_error ArgumentError + end + end + + context 'when value is true' do + it 'concats the parameter collections' do + ENV['MYSQL_FRAMEWORK_SKIP_NIL_VALUE_VALIDATION'] = 'true' + query = subject.where(gems[:updated_at].eq(nil)) + expect(query.params).to eq ['sage', '2018-01-01 00:00:00', nil] + end + end + end + end end describe '#and' do