Skip to content

Commit 8fcb726

Browse files
committed
Merge pull request #506 from pierrickrouxel/db2_is_null_fix
[DB2] fix db2 = null issue
2 parents 006a218 + 49ec4cf commit 8fcb726

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/arjdbc/db2/adapter.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,11 @@ def runstats_for_table(tablename, priority = 10)
428428
@connection.execute_update "call sysproc.admin_cmd('RUNSTATS ON TABLE #{tablename} WITH DISTRIBUTION AND DETAILED INDEXES ALL UTIL_IMPACT_PRIORITY #{priority}')"
429429
end
430430

431+
def select(sql, name, binds)
432+
# DB2 does not like "= NULL", "!= NULL", or "<> NULL".
433+
exec_query(to_sql(sql.gsub(/(!=|<>)\s*null/i, "IS NOT NULL").gsub(/=\s*null/i, "IS NULL"), binds), name, binds)
434+
end
435+
431436
def add_index(table_name, column_name, options = {})
432437
if ! zos? || ( table_name.to_s == ActiveRecord::Migrator.schema_migrations_table_name.to_s )
433438
column_name = column_name.to_s if column_name.is_a?(Symbol)

test/db/db2/simple_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,21 @@ def test_find_by_sql_WITH_statement
114114
assert_equal db.id, connection.last_insert_id
115115
#assert_equal e.id, connection.last_insert_id('entries')
116116
end
117+
118+
# DB2 does not like "= NULL".
119+
def test_equals_null
120+
Entry.create!(:title => "Foo")
121+
entry = Entry.find(:first, :conditions => ["content = NULL"])
122+
assert_equal "Foo", entry.title
123+
end
124+
125+
# DB2 does not like "!= NULL" or "<> NULL".
126+
def test_not_equals_null
127+
Entry.create!(:title => "Foo", :content => "Bar")
128+
entry = Entry.find_by_title("Foo", :conditions => ["content != NULL"])
129+
assert_equal "Foo", entry.title
130+
entry = Entry.find_by_title("Foo", :conditions => ["content <> NULL"])
131+
assert_equal "Foo", entry.title
132+
end
117133

118134
end

0 commit comments

Comments
 (0)