Skip to content

Commit

Permalink
Handle sending booleans appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jan 26, 2017
1 parent 0d8eefe commit 933fc34
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 19 deletions.
1 change: 1 addition & 0 deletions lib/active_record/connection_adapters/odbc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class ODBCAdapter < AbstractAdapter
include ::ODBCAdapter::SchemaStatements

ADAPTER_NAME = 'ODBC'.freeze
BOOLEAN_TYPE = 'BOOLEAN'.freeze
ERR_DUPLICATE_KEY_VALUE = 23505

attr_reader :dbms
Expand Down
16 changes: 16 additions & 0 deletions lib/odbc_adapter/adapters/mysql_odbc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ def quote_string(string)
string.gsub(/\\/, '\&\&').gsub(/'/, "''")
end

def quoted_true
'1'
end

def unquoted_true
1
end

def quoted_false
'0'
end

def unquoted_false
0
end

def disable_referential_integrity(&block) #:nodoc:
old = select_value("SELECT @@FOREIGN_KEY_CHECKS")

Expand Down
10 changes: 1 addition & 9 deletions lib/odbc_adapter/adapters/postgresql_odbc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def type_cast(value, column)

case value
when String
return super unless 'bytea' == column.sql_type
return super unless 'bytea' == column.native_type
{ value: value, format: 1 }
else
super
Expand All @@ -75,14 +75,6 @@ def quote_string(string)
string.gsub(/\\/, '\&\&').gsub(/'/, "''")
end

def quoted_true
"'t'"
end

def quoted_false
"'f'"
end

def disable_referential_integrity #:nodoc:
execute(tables.map { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(';'))
yield
Expand Down
4 changes: 0 additions & 4 deletions lib/odbc_adapter/quoting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ def quote_column_name(name)
"#{quote_char.chr}#{name}#{quote_char.chr}"
end

def quoted_true
'1'
end

# Ideally, we'd return an ODBC date or timestamp literal escape
# sequence, but not all ODBC drivers support them.
def quoted_date(value)
Expand Down
7 changes: 6 additions & 1 deletion lib/odbc_adapter/schema_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ def columns(table_name, name = nil)
# SQLColumns: IS_NULLABLE, SQLColumns: NULLABLE
col_nullable = nullability(col_name, col[17], col[10])

cast_type = lookup_cast_type(col_sql_type)
cast_type =
if col_native_type == self.class::BOOLEAN_TYPE
ActiveRecord::Type::Boolean.new
else
lookup_cast_type(col_sql_type)
end
cols << new_column(format_case(col_name), col_default, cast_type, col_sql_type, col_nullable, col_native_type, col_scale, col_limit)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/odbc_adapter/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ODBCAdapter
VERSION = '4.2.0'
VERSION = '4.2.1'
end
23 changes: 23 additions & 0 deletions test/attributes_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'test_helper'

class AttributesTest < Minitest::Test
def test_booleans?
skip 'Querying booleans not functioning properly yet'
assert_equal true, Todo.first.published?
assert_equal false, Todo.last.published?
end

def test_integers
assert_kind_of Fixnum, User.first.letters
end

def test_strings
assert_kind_of String, User.first.first_name
assert_kind_of String, Todo.first.body
end

def test_attributes
assert_kind_of Hash, User.first.attributes
assert_kind_of Hash, Todo.first.attributes
end
end
4 changes: 4 additions & 0 deletions test/selection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ def test_find
def test_arel_conditions
assert_equal 2, User.lots_of_letters.count
end

def test_where_boolean
assert_equal 4, Todo.where(published: true).count
end
end
9 changes: 5 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
create_table :todos, force: true do |t|
t.integer :user_id
t.text :body
t.boolean :published, null: false, default: false
t.timestamps null: false
end
end
Expand All @@ -46,20 +47,20 @@ class Todo < ActiveRecord::Base
end

User.find(1).todos.create([
{ body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.' },
{ body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', published: true },
{ body: 'Praesent ut dolor nec eros euismod hendrerit.' },
{ body: 'Curabitur lacinia metus eget interdum volutpat.' }
])

User.find(2).todos.create([
{ body: 'Nulla sollicitudin venenatis turpis vitae finibus.' },
{ body: 'Proin consectetur id lacus vel feugiat.' },
{ body: 'Nulla sollicitudin venenatis turpis vitae finibus.', published: true },
{ body: 'Proin consectetur id lacus vel feugiat.', published: true },
{ body: 'Pellentesque augue orci, aliquet nec ipsum ultrices, cursus blandit metus.' },
{ body: 'Nulla posuere nisl risus, eget scelerisque leo congue non.' },
{ body: 'Curabitur eget massa mollis, iaculis risus in, tristique metus.' }
])

User.find(4).todos.create([
{ body: 'In hac habitasse platea dictumst.' },
{ body: 'In hac habitasse platea dictumst.', published: true },
{ body: 'Integer molestie ornare velit, eu interdum felis euismod vitae.' }
])

0 comments on commit 933fc34

Please sign in to comment.