Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix problem with AR 2.1 and Derby with 'DEFAULT NULL NULL' in column …

…defs

git-svn-id: svn+ssh://rubyforge.org/var/svn/jruby-extras/trunk/activerecord-jdbc@1015 8ba958d5-0c1a-0410-94a6-a65dfc1b28a6
  • Loading branch information...
commit 16793d09b3144c50769a98ace01397188b9080f3 1 parent 121d240
@nicksieger nicksieger authored
Showing with 38 additions and 29 deletions.
  1. +35 −28 lib/jdbc_adapter/jdbc_derby.rb
  2. +3 −1 test/jdbc_common.rb
View
63 lib/jdbc_adapter/jdbc_derby.rb
@@ -7,7 +7,7 @@ def derby_connection(config)
config[:driver] ||= "org.apache.derby.jdbc.EmbeddedDriver"
embedded_driver(config)
end
- end
+ end
module Derby
def self.column_selector
@@ -17,10 +17,10 @@ def self.column_selector
def self.adapter_selector
[/derby/i, lambda {|cfg,adapt| adapt.extend(::JdbcSpec::Derby)}]
end
-
+
def self.monkey_rails
unless @already_monkeyd
- # Needed because Rails is broken wrt to quoting of
+ # Needed because Rails is broken wrt to quoting of
# some values. Most databases are nice about it,
# but not Derby. The real issue is that you can't
# compare a CHAR value to a NUMBER column.
@@ -32,7 +32,7 @@ def select_limited_ids_list(options, join_dependency)
construct_finder_sql_for_association_limiting(options, join_dependency),
"#{name} Load IDs For Limited Eager Loading"
).collect { |row| connection.quote(row[primary_key], columns_hash[primary_key]) }.join(", ")
- end
+ end
end
@already_monkeyd = true
@@ -46,12 +46,12 @@ def self.extended(*args)
def self.included(*args)
monkey_rails
end
-
+
module Column
def value_to_binary(value)
value.scan(/[0-9A-Fa-f]{2}/).collect {|v| v.to_i(16)}.pack("C*")
end
-
+
def cast_to_date_or_time(value)
return value if value.is_a? Date
return nil if value.blank?
@@ -71,14 +71,14 @@ def guess_date_or_time(value)
end
def simplified_type(field_type)
- return :boolean if field_type =~ /smallint/i
+ return :boolean if field_type =~ /smallint/i
return :float if field_type =~ /real/i
super
end
end
include JdbcSpec::MissingFunctionalityHelper
-
+
def modify_types(tp)
tp[:primary_key] = "int generated by default as identity NOT NULL PRIMARY KEY"
tp[:integer][:limit] = nil
@@ -86,7 +86,14 @@ def modify_types(tp)
tp[:boolean] = {:name => "smallint"}
tp
end
-
+
+ # Override default -- fix case where ActiveRecord passes :default => nil, :null => true
+ def add_column_options!(sql, options)
+ options.delete(:default) if options.has_key?(:default) && options[:default].nil?
+ options.delete(:null) if options.has_key?(:null) && (options[:null].nil? || options[:null].true?)
+ super
+ end
+
def classes_for_table_name(table)
ActiveRecord::Base.send(:subclasses).select {|klass| klass.table_name == table}
end
@@ -94,9 +101,9 @@ def classes_for_table_name(table)
# Set the sequence to the max value of the table's column.
def reset_sequence!(table, column, sequence = nil)
mpk = select_value("SELECT MAX(#{quote_column_name column}) FROM #{table}")
- execute("ALTER TABLE #{table} ALTER COLUMN #{quote_column_name column} RESTART WITH #{mpk.to_i + 1}")
+ execute("ALTER TABLE #{table} ALTER COLUMN #{quote_column_name column} RESTART WITH #{mpk.to_i + 1}")
end
-
+
def reset_pk_sequence!(table, pk = nil, sequence = nil)
klasses = classes_for_table_name(table)
klass = klasses.nil? ? nil : klasses.first
@@ -105,7 +112,7 @@ def reset_pk_sequence!(table, pk = nil, sequence = nil)
reset_sequence!(klass.table_name, pk)
end
end
-
+
def primary_key(table_name) #:nodoc:
primary_keys(table_name).first
end
@@ -117,7 +124,7 @@ def remove_index(table_name, options) #:nodoc:
def rename_table(name, new_name)
execute "RENAME TABLE #{name} TO #{new_name}"
end
-
+
COLUMN_INFO_STMT = "SELECT C.COLUMNNAME, C.REFERENCEID, C.COLUMNNUMBER FROM SYS.SYSCOLUMNS C, SYS.SYSTABLES T WHERE T.TABLEID = '%s' AND T.TABLEID = C.REFERENCEID ORDER BY C.COLUMNNUMBER"
COLUMN_TYPE_STMT = "SELECT COLUMNDATATYPE, COLUMNDEFAULT FROM SYS.SYSCOLUMNS WHERE REFERENCEID = '%s' AND COLUMNNAME = '%s'"
@@ -129,7 +136,7 @@ def add_quotes(name)
return name unless name
%Q{"#{name}"}
end
-
+
def strip_quotes(str)
return str unless str
return str unless /^(["']).*\1$/ =~ str
@@ -140,7 +147,7 @@ def expand_double_quotes(name)
return name unless name && name['"']
name.gsub(/"/,'""')
end
-
+
def reinstate_auto_increment(name, refid, coldef)
stmt = AUTO_INC_STMT % [refid, strip_quotes(name)]
data = execute(stmt).first
@@ -224,9 +231,9 @@ def create_column(name, refid, colno)
end
coldef
end
-
+
SIZEABLE = %w(VARCHAR CLOB BLOB)
-
+
def structure_dump #:nodoc:
definition=""
rs = @connection.connection.meta_data.getTables(nil,nil,nil,["TABLE"].to_java(:string))
@@ -244,14 +251,14 @@ def structure_dump #:nodoc:
elsif d1
default = " DEFAULT #{d1}"
end
-
+
type = rs2.getString(6)
col_size = rs2.getString(7)
nulling = (rs2.getString(18) == 'NO' ? " NOT NULL" : "")
- create_col_string = add_quotes(expand_double_quotes(strip_quotes(col_name))) +
- " " +
+ create_col_string = add_quotes(expand_double_quotes(strip_quotes(col_name))) +
+ " " +
type +
- (SIZEABLE.include?(type) ? "(#{col_size})" : "") +
+ (SIZEABLE.include?(type) ? "(#{col_size})" : "") +
nulling +
default
if !first_col
@@ -259,23 +266,23 @@ def structure_dump #:nodoc:
else
create_col_string = " #{create_col_string}"
end
-
+
definition << create_col_string
-
+
first_col = false
end
definition << ");\n\n"
end
definition
end
-
+
# Support for removing columns added via derby bug issue:
# https://issues.apache.org/jira/browse/DERBY-1489
#
# This feature has not made it into a formal release and is not in Java 6.
# If the normal strategy fails we fall back on a strategy by creating a new
# table without the new column and there after moving the data to the new
- #
+ #
def remove_column(table_name, column_name)
begin
execute "ALTER TABLE #{table_name} DROP COLUMN #{column_name} RESTRICT"
@@ -285,7 +292,7 @@ def remove_column(table_name, column_name)
end
end
end
-
+
# Notes about changing in Derby:
# http://db.apache.org/derby/docs/10.2/ref/rrefsqlj81859.html#rrefsqlj81859__rrefsqlj37860)
#
@@ -338,7 +345,7 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
alter_table(table_name, :rename => {column_name => new_column_name})
end
end
-
+
def primary_keys(table_name)
@connection.primary_keys table_name.to_s.upcase
end
@@ -364,7 +371,7 @@ def quote_column_name(name) #:nodoc:
name
end
end
-
+
def quoted_true
'1'
end
View
4 test/jdbc_common.rb
@@ -1,5 +1,7 @@
-require 'jdbc_adapter'
require 'rubygems'
+# Specify version of activerecord here if desired
+# gem 'activerecord', '=2.1'
+require 'jdbc_adapter'
require 'models/auto_id'
require 'models/entry'
require 'models/add_not_null_column_to_table'
Please sign in to comment.
Something went wrong with that request. Please try again.