From 933fc3432e05cab7710edaa94f268118b8808393 Mon Sep 17 00:00:00 2001 From: Kevin Deisz Date: Thu, 26 Jan 2017 10:25:47 -0500 Subject: [PATCH 1/3] Handle sending booleans appropriately --- .../connection_adapters/odbc_adapter.rb | 1 + .../adapters/mysql_odbc_adapter.rb | 16 +++++++++++++ .../adapters/postgresql_odbc_adapter.rb | 10 +------- lib/odbc_adapter/quoting.rb | 4 ---- lib/odbc_adapter/schema_statements.rb | 7 +++++- lib/odbc_adapter/version.rb | 2 +- test/attributes_test.rb | 23 +++++++++++++++++++ test/selection_test.rb | 4 ++++ test/test_helper.rb | 9 ++++---- 9 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 test/attributes_test.rb diff --git a/lib/active_record/connection_adapters/odbc_adapter.rb b/lib/active_record/connection_adapters/odbc_adapter.rb index 567ff408..c1163245 100644 --- a/lib/active_record/connection_adapters/odbc_adapter.rb +++ b/lib/active_record/connection_adapters/odbc_adapter.rb @@ -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 diff --git a/lib/odbc_adapter/adapters/mysql_odbc_adapter.rb b/lib/odbc_adapter/adapters/mysql_odbc_adapter.rb index be144227..f98dfe11 100644 --- a/lib/odbc_adapter/adapters/mysql_odbc_adapter.rb +++ b/lib/odbc_adapter/adapters/mysql_odbc_adapter.rb @@ -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") diff --git a/lib/odbc_adapter/adapters/postgresql_odbc_adapter.rb b/lib/odbc_adapter/adapters/postgresql_odbc_adapter.rb index daca1a86..eb24a18d 100644 --- a/lib/odbc_adapter/adapters/postgresql_odbc_adapter.rb +++ b/lib/odbc_adapter/adapters/postgresql_odbc_adapter.rb @@ -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 @@ -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 diff --git a/lib/odbc_adapter/quoting.rb b/lib/odbc_adapter/quoting.rb index 21d00772..57ebc5b8 100644 --- a/lib/odbc_adapter/quoting.rb +++ b/lib/odbc_adapter/quoting.rb @@ -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) diff --git a/lib/odbc_adapter/schema_statements.rb b/lib/odbc_adapter/schema_statements.rb index 83c7f410..871ce7e0 100644 --- a/lib/odbc_adapter/schema_statements.rb +++ b/lib/odbc_adapter/schema_statements.rb @@ -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 diff --git a/lib/odbc_adapter/version.rb b/lib/odbc_adapter/version.rb index feb54b09..61615909 100644 --- a/lib/odbc_adapter/version.rb +++ b/lib/odbc_adapter/version.rb @@ -1,3 +1,3 @@ module ODBCAdapter - VERSION = '4.2.0' + VERSION = '4.2.1' end diff --git a/test/attributes_test.rb b/test/attributes_test.rb new file mode 100644 index 00000000..010da5f8 --- /dev/null +++ b/test/attributes_test.rb @@ -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 diff --git a/test/selection_test.rb b/test/selection_test.rb index 58969a9b..667dbbf6 100644 --- a/test/selection_test.rb +++ b/test/selection_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index d105bb8c..e937e615 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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 @@ -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.' } ]) From da9b2a4b33fbf2310e270bf996616c3bbaf834e6 Mon Sep 17 00:00:00 2001 From: Kevin Deisz Date: Thu, 26 Jan 2017 11:40:57 -0500 Subject: [PATCH 2/3] Handle booleans appropriately --- lib/odbc_adapter/adapters/mysql_odbc_adapter.rb | 2 +- lib/odbc_adapter/adapters/postgresql_odbc_adapter.rb | 8 +++++++- test/attributes_test.rb | 1 - 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/odbc_adapter/adapters/mysql_odbc_adapter.rb b/lib/odbc_adapter/adapters/mysql_odbc_adapter.rb index f98dfe11..a9377d4f 100644 --- a/lib/odbc_adapter/adapters/mysql_odbc_adapter.rb +++ b/lib/odbc_adapter/adapters/mysql_odbc_adapter.rb @@ -7,7 +7,7 @@ class BindSubstitution < Arel::Visitors::MySQL include Arel::Visitors::BindVisitor end - PRIMARY_KEY = 'INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY' + PRIMARY_KEY = 'INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY'.freeze def truncate(table_name, name = nil) execute("TRUNCATE TABLE #{quote_table_name(table_name)}", name) diff --git a/lib/odbc_adapter/adapters/postgresql_odbc_adapter.rb b/lib/odbc_adapter/adapters/postgresql_odbc_adapter.rb index eb24a18d..8fe3bb04 100644 --- a/lib/odbc_adapter/adapters/postgresql_odbc_adapter.rb +++ b/lib/odbc_adapter/adapters/postgresql_odbc_adapter.rb @@ -7,7 +7,13 @@ class BindSubstitution < Arel::Visitors::PostgreSQL include Arel::Visitors::BindVisitor end - PRIMARY_KEY = 'SERIAL PRIMARY KEY' + BOOLEAN_TYPE = 'bool'.freeze + PRIMARY_KEY = 'SERIAL PRIMARY KEY'.freeze + + # Override to handle booleans appropriately + def native_database_types + @native_database_types ||= super.merge(boolean: { name: 'bool' }) + end # Filter for ODBCAdapter#tables # Omits table from #tables if table_filter returns true diff --git a/test/attributes_test.rb b/test/attributes_test.rb index 010da5f8..ea24e19c 100644 --- a/test/attributes_test.rb +++ b/test/attributes_test.rb @@ -2,7 +2,6 @@ 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 From 9959e211ca1b582a504356a397154894555fb52d Mon Sep 17 00:00:00 2001 From: Kevin Deisz Date: Thu, 26 Jan 2017 11:48:24 -0500 Subject: [PATCH 3/3] [ci skip] bump version --- lib/odbc_adapter/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/odbc_adapter/version.rb b/lib/odbc_adapter/version.rb index 61615909..d8f4212b 100644 --- a/lib/odbc_adapter/version.rb +++ b/lib/odbc_adapter/version.rb @@ -1,3 +1,3 @@ module ODBCAdapter - VERSION = '4.2.1' + VERSION = '4.2.2' end