3 def self.column_selector
4 [/db2/i, lambda {|cfg,col|
5 if cfg[:url] =~ /^jdbc:derby:net:/
6 col.extend(::JdbcSpec::Derby::Column)
8 col.extend(::JdbcSpec::DB2::Column)
12 def self.adapter_selector
13 [/db2/i, lambda {|cfg,adapt|
14 if cfg[:url] =~ /^jdbc:derby:net:/
15 adapt.extend(::JdbcSpec::Derby)
17 adapt.extend(::JdbcSpec::DB2)
23 return nil if value.nil? || value =~ /^\s*null\s*$/i
25 when :string then value
26 when :integer then defined?(value.to_i) ? value.to_i : (value ? 1 : 0)
27 when :primary_key then defined?(value.to_i) ? value.to_i : (value ? 1 : 0)
28 when :float then value.to_f
29 when :datetime then cast_to_date_or_time(value)
30 when :timestamp then cast_to_time(value)
31 when :time then cast_to_time(value)
35 def cast_to_date_or_time(value)
36 return value if value.is_a? Date
37 return nil if value.blank?
38 guess_date_or_time((value.is_a? Time) ? value : cast_to_time(value))
41 def cast_to_time(value)
42 return value if value.is_a? Time
43 time_array = ParseDate.parsedate value
44 time_array[0] ||= 2000; time_array[1] ||= 1; time_array[2] ||= 1;
45 Time.send(ActiveRecord::Base.default_timezone, *time_array) rescue nil
48 def guess_date_or_time(value)
49 (value.hour == 0 and value.min == 0 and value.sec == 0) ?
50 Date.new(value.year, value.month, value.day) : value
55 tp[:primary_key] = 'int generated by default as identity (start with 42) primary key'
56 tp[:string][:limit] = 255
57 tp[:integer][:limit] = nil
58 tp[:boolean][:limit] = nil
62 def add_limit_offset!(sql, options)
63 if limit = options[:limit]
64 offset = options[:offset] || 0
65 sql.gsub!(/SELECT/i, 'SELECT B.* FROM (SELECT A.*, row_number() over () AS internal$rownum FROM (SELECT')
66 sql << ") A ) B WHERE B.internal$rownum > #{offset} AND B.internal$rownum <= #{limit + offset}"
70 def quote_column_name(column_name)
74 def quote(value, column = nil) # :nodoc:
75 if column && column.type == :primary_key
78 if column && (column.type == :decimal || column.type == :integer) && value
83 if column && column.type == :binary
84 "BLOB('#{quote_string(value)}')"
86 "'#{quote_string(value)}'"
92 def quote_string(string)
93 string.gsub(/'/, "''") # ' (for ruby-mode)
104 def recreate_database(name)
105 do_not_drop = ["stmg_dbsize_info","hmon_atm_info","hmon_collection","policy"]
106 tables.each do |table|
107 unless do_not_drop.include?(table)
113 def remove_index(table_name, options = { })
114 execute "DROP INDEX #{quote_column_name(index_name(table_name, options))}"
117 # This method makes tests pass without understanding why.
118 # Don't use this in production.
119 def columns(table_name, name = nil)
120 super.select do |col|
121 # strip out "magic" columns from DB2 (?)
122 !/rolename|roleid|create_time|auditpolicyname|auditpolicyid|remarks/.match(col.name)
127 return name unless name
131 def strip_quotes(str)
132 return str unless str
133 return str unless /^(["']).*\1$/ =~ str
137 def expand_double_quotes(name)
138 return name unless name && name['"']
143 def structure_dump #:nodoc:
145 rs = @connection.connection.meta_data.getTables(nil,nil,nil,["TABLE"].to_java(:string))
147 tname = rs.getString(3)
148 definition << "CREATE TABLE #{tname} (\n"
149 rs2 = @connection.connection.meta_data.getColumns(nil,nil,tname,nil)
152 col_name = add_quotes(rs2.getString(4));
154 d1 = rs2.getString(13)
155 default = d1 ? " DEFAULT #{d1}" : ""
157 type = rs2.getString(6)
158 col_size = rs2.getString(7)
159 nulling = (rs2.getString(18) == 'NO' ? " NOT NULL" : "")
160 create_col_string = add_quotes(expand_double_quotes(strip_quotes(col_name))) +
167 create_col_string = ",\n #{create_col_string}"
169 create_col_string = " #{create_col_string}"
172 definition << create_col_string
176 definition << ");\n\n"
181 def dump_schema_information
183 if (current_schema = ActiveRecord::Migrator.current_version) > 0
184 #TODO: Find a way to get the DB2 instace name to properly form the statement
185 return "INSERT INTO DB2INST2.SCHEMA_INFO (version) VALUES (#{current_schema})"
187 rescue ActiveRecord::StatementInvalid