Permalink
Browse files

Deprecate SqlAnywhere convert_smallint_to_bool global and dataset mut…

…ation methods

These basically mirror the changes made to the ibmdb adapter.

While here, cleanup the specs and remove the connecting to a
separate database.
1 parent 5355ebb commit c32a26fadca82e01c632a4be955ab7fa5831c0f2 @jeremyevans committed Jun 27, 2017
Showing with 108 additions and 89 deletions.
  1. +4 −0 CHANGELOG
  2. +23 −10 lib/sequel/adapters/shared/sqlanywhere.rb
  3. +81 −79 spec/adapters/sqlanywhere_spec.rb
View
@@ -1,5 +1,9 @@
=== master
+* Deprecate Sequel::SqlAnywhere::Dataset#convert_smallint_to_bool= method (jeremyevans)
+
+* Deprecate Sequel::SqlAnywhere.convert_smallint_to_bool accessor (jeremyevans)
+
* Use savepoints around index creation if creating table inside transaction if ignore_index_errors is used (jeremyevans)
* Deprecate treating :natrual_inner join type on MySQL as NATURAL LEFT JOIN (jeremyevans)
@@ -4,12 +4,17 @@ module Sequel
module SqlAnywhere
Sequel::Database.set_shared_adapter_scheme(:sqlanywhere, self)
+ # SEQUEL5: Remove
@convert_smallint_to_bool = true
-
class << self
- # Whether to convert smallint values to bool, false by default.
- # Can also be overridden per dataset.
- attr_accessor :convert_smallint_to_bool
+ def convert_smallint_to_bool
+ Sequel::Deprecation.deprecate("Sequel::SqlAnywhere.convert_smallint_to_bool", "Call this method on the Database instance")
+ @convert_smallint_to_bool
+ end
+ def convert_smallint_to_bool=(v)
+ Sequel::Deprecation.deprecate("Sequel::SqlAnywhere.convert_smallint_to_bool=", "Call this method on the Database instance")
+ @convert_smallint_to_bool = v
+ end
end
module DatabaseMethods
@@ -36,7 +41,7 @@ module DatabaseMethods
# Whether to convert smallint to boolean arguments for this dataset.
# Defaults to the SqlAnywhere module setting.
def convert_smallint_to_bool
- defined?(@convert_smallint_to_bool) ? @convert_smallint_to_bool : (@convert_smallint_to_bool = ::Sequel::SqlAnywhere.convert_smallint_to_bool)
+ defined?(@convert_smallint_to_bool) ? @convert_smallint_to_bool : (@convert_smallint_to_bool = ::Sequel::SqlAnywhere.instance_variable_get(:@convert_smallint_to_bool)) # true) # SEQUEL5
end
# Sysbase Server uses the :sqlanywhere type.
@@ -319,14 +324,22 @@ module DatasetMethods
Dataset.def_sql_method(self, :insert, %w'with insert into columns values')
Dataset.def_sql_method(self, :select, %w'with select distinct limit columns into from join where group having compounds order lock')
+ # Override the default IBMDB.convert_smallint_to_bool setting for this dataset.
+ def convert_smallint_to_bool=(v)
+ Sequel::Deprecation.deprecate("Sequel::SqlAnywhere::Dataset#convert_smallint_to_bool=", "Call with_convert_smallint_to_bool instead, which returns a modified copy instead of modifying the object")
+ @opts[:convert_smallint_to_bool] = v
+ end
+
# Whether to convert smallint to boolean arguments for this dataset.
- # Defaults to the SqlAnywhere module setting.
+ # Defaults to the IBMDB module setting.
def convert_smallint_to_bool
- defined?(@convert_smallint_to_bool) ? @convert_smallint_to_bool : (@convert_smallint_to_bool = @db.convert_smallint_to_bool)
+ opts.has_key?(:convert_smallint_to_bool) ? opts[:convert_smallint_to_bool] : db.convert_smallint_to_bool
end
- # Override the default SqlAnywhere.convert_smallint_to_bool setting for this dataset.
- attr_writer :convert_smallint_to_bool
+ # Return a cloned dataset with the convert_smallint_to_bool option set.
+ def with_convert_smallint_to_bool(v)
+ clone(:convert_smallint_to_bool=>v)
+ end
def supports_cte?(type=:select)
type == :select || type == :insert
@@ -424,7 +437,7 @@ def escape_like(string)
string.gsub(/[\\%_\[]/){|m| "\\#{m}"}
end
- # Use Date() and Now() for CURRENT_DATE and CURRENT_TIMESTAMP
+ # Use today() and Now() for CURRENT_DATE and CURRENT_TIMESTAMP
def constant_sql_append(sql, constant)
case constant
when :CURRENT_DATE
@@ -6,73 +6,66 @@
DB.drop_table(:test)
end
-describe "Convert smallint to boolean" do
+describe "convert_smallint_to_bool" do
before do
@db = DB
+ @ds = @db[:booltest]
+ @db.send(:remove_instance_variable, :@convert_smallint_to_bool) if @db.instance_variable_defined?(:@convert_smallint_to_bool)
end
after do
- Sequel::SqlAnywhere.convert_smallint_to_bool = true
- @db.convert_smallint_to_bool = true
+ deprecated do
+ Sequel::SqlAnywhere.convert_smallint_to_bool = true
+ end
+ @db.send(:remove_instance_variable, :@convert_smallint_to_bool) if @db.instance_variable_defined?(:@convert_smallint_to_bool)
end
+ # SEQUEL5: Remove
describe "Sequel::SqlAnywhere.convert_smallint_to_bool" do
before do
@db.create_table!(:booltest){column :b, 'smallint'; column :i, 'integer'}
- @ds = @db[:booltest]
end
after do
@db.drop_table(:booltest)
end
- it "should consider smallint datatypes as boolean if set, but if not, as larger smallints" do
+ deprecated "should consider smallint datatypes as boolean if set, but if not, as larger smallints" do
@db.create_table!(:booltest){column :b, 'smallint'; column :i, 'integer'}
@db.schema(:booltest, :reload=>true).first.last[:type].must_equal :boolean
@db.schema(:booltest, :reload=>true).first.last[:db_type].must_match /smallint/i
Sequel::SqlAnywhere.convert_smallint_to_bool = false
- @db2 = Sequel.connect(DB.url)
- @db2.schema(:booltest, :reload=>true).first.last[:type].must_equal :integer
- @db2.schema(:booltest, :reload=>true).first.last[:db_type].must_match /smallint/i
-
- @db.schema(:booltest, :reload=>true).first.last[:type].must_equal :boolean
+ @db.send(:remove_instance_variable, :@convert_smallint_to_bool) if @db.instance_variable_defined?(:@convert_smallint_to_bool)
+ @db.schema(:booltest, :reload=>true).first.last[:type].must_equal :integer
@db.schema(:booltest, :reload=>true).first.last[:db_type].must_match /smallint/i
-
- @db2.disconnect
end
- describe "datasets" do
- it "should return smallints as bools and integers as integers when set" do
- @ds.delete
- @ds << {:b=>true, :i=>10}
- @ds.all.must_equal [{:b=>true, :i=>10}]
- @ds.delete
- @ds << {:b=>false, :i=>0}
- @ds.all.must_equal [{:b=>false, :i=>0}]
- @ds.delete
- @ds << {:b=>true, :i=>1}
- @ds.all.must_equal [{:b=>true, :i=>1}]
- end
-
- it "should return all smallints as integers when unset" do
- Sequel::SqlAnywhere.convert_smallint_to_bool = false
- @db2 = Sequel.connect(DB.url)
- @ds2 = @db2[:booltest]
- @ds2.delete
- @ds2 << {:b=>true, :i=>10}
- @ds2.all.must_equal [{:b=>1, :i=>10}]
- @ds2.delete
- @ds2 << {:b=>false, :i=>0}
- @ds2.all.must_equal [{:b=>0, :i=>0}]
-
- @ds2.delete
- @ds2 << {:b=>1, :i=>10}
- @ds2.all.must_equal [{:b=>1, :i=>10}]
- @ds2.delete
- @ds2 << {:b=>0, :i=>0}
- @ds2.all.must_equal [{:b=>0, :i=>0}]
+ it "should return smallints as bools and integers as integers when set" do
+ @ds.delete
+ @ds << {:b=>true, :i=>10}
+ @ds.all.must_equal [{:b=>true, :i=>10}]
+ @ds.delete
+ @ds << {:b=>false, :i=>0}
+ @ds.all.must_equal [{:b=>false, :i=>0}]
+ @ds.delete
+ @ds << {:b=>true, :i=>1}
+ @ds.all.must_equal [{:b=>true, :i=>1}]
+ end
- @db2.disconnect
- end
+ deprecated "should return all smallints as integers when unset" do
+ Sequel::SqlAnywhere.convert_smallint_to_bool = false
+ @ds.delete
+ @ds << {:b=>true, :i=>10}
+ @ds.all.must_equal [{:b=>1, :i=>10}]
+ @ds.delete
+ @ds << {:b=>false, :i=>0}
+ @ds.all.must_equal [{:b=>0, :i=>0}]
+
+ @ds.delete
+ @ds << {:b=>1, :i=>10}
+ @ds.all.must_equal [{:b=>1, :i=>10}]
+ @ds.delete
+ @ds << {:b=>0, :i=>0}
+ @ds.all.must_equal [{:b=>0, :i=>0}]
end
end
@@ -92,47 +85,39 @@
@db.schema(:booltest, :reload=>true).first.last[:db_type].must_match /smallint/i
end
- describe "datasets" do
- it "should return smallints as bools and integers as integers when set" do
- @ds = @db[:booltest]
- @ds.delete
- @ds << {:b=>true, :i=>10}
- @ds.all.must_equal [{:b=>true, :i=>10}]
- @ds.delete
- @ds << {:b=>false, :i=>0}
- @ds.all.must_equal [{:b=>false, :i=>0}]
- @ds.delete
- @ds << {:b=>true, :i=>1}
- @ds.all.must_equal [{:b=>true, :i=>1}]
- end
-
- it "should return all smallints as integers when unset" do
- @db2 = Sequel.connect(DB.url)
- @db2.convert_smallint_to_bool = false
- @ds2 = @db2[:booltest]
- @ds2.delete
- @ds2 << {:b=>true, :i=>10}
- @ds2.all.must_equal [{:b=>1, :i=>10}]
- @ds2.delete
- @ds2 << {:b=>false, :i=>0}
- @ds2.all.must_equal [{:b=>0, :i=>0}]
-
- @ds2.delete
- @ds2 << {:b=>1, :i=>10}
- @ds2.all.must_equal [{:b=>1, :i=>10}]
- @ds2.delete
- @ds2 << {:b=>0, :i=>0}
- @ds2.all.must_equal [{:b=>0, :i=>0}]
+ it "should return smallints as bools and integers as integers when set" do
+ @ds.delete
+ @ds << {:b=>true, :i=>10}
+ @ds.all.must_equal [{:b=>true, :i=>10}]
+ @ds.delete
+ @ds << {:b=>false, :i=>0}
+ @ds.all.must_equal [{:b=>false, :i=>0}]
+ @ds.delete
+ @ds << {:b=>true, :i=>1}
+ @ds.all.must_equal [{:b=>true, :i=>1}]
+ end
- @db2.disconnect
- end
+ it "should return all smallints as integers when unset" do
+ @db.convert_smallint_to_bool = false
+ @ds.delete
+ @ds << {:b=>true, :i=>10}
+ @ds.all.must_equal [{:b=>1, :i=>10}]
+ @ds.delete
+ @ds << {:b=>false, :i=>0}
+ @ds.all.must_equal [{:b=>0, :i=>0}]
+
+ @ds.delete
+ @ds << {:b=>1, :i=>10}
+ @ds.all.must_equal [{:b=>1, :i=>10}]
+ @ds.delete
+ @ds << {:b=>0, :i=>0}
+ @ds.all.must_equal [{:b=>0, :i=>0}]
end
end
describe "Dataset#convert_smallint_to_bool" do
before do
@db.create_table!(:booltest){column :b, 'smallint'; column :i, 'integer'}
- @ds = @db[:booltest]
end
after do
@db.drop_table(:booltest)
@@ -150,7 +135,7 @@
@ds.all.must_equal [{:b=>true, :i=>1}]
end
- it "should return all smallints as integers when unset" do
+ deprecated "should return all smallints as integers when unset" do
@ds.convert_smallint_to_bool = false
@ds.delete
@ds << {:b=>true, :i=>10}
@@ -166,5 +151,22 @@
@ds << {:b=>0, :i=>0}
@ds.all.must_equal [{:b=>0, :i=>0}]
end
+
+ it "should support with_convert_smallint_to_bool for returning modified dataset with setting changed" do
+ @ds = @ds.with_convert_smallint_to_bool(false)
+ @ds.delete
+ @ds << {:b=>true, :i=>10}
+ @ds.all.must_equal [{:b=>1, :i=>10}]
+ @ds.delete
+ @ds << {:b=>false, :i=>0}
+ @ds.all.must_equal [{:b=>0, :i=>0}]
+
+ @ds.delete
+ @ds << {:b=>1, :i=>10}
+ @ds.all.must_equal [{:b=>1, :i=>10}]
+ @ds.delete
+ @ds << {:b=>0, :i=>0}
+ @ds.all.must_equal [{:b=>0, :i=>0}]
+ end
end
end

0 comments on commit c32a26f

Please sign in to comment.