Module: JdbcSpec::DB2

Defined in:
lib/jdbc_adapter/jdbc_db2.rb

Defined Under Namespace

Modules: Column

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.adapter_matcher(name, config) ⇒ Object



3
4
5
6
7
8
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 3

def self.adapter_matcher(name, config)
  if name =~ /db2/i
     return config[:url] =~ /^jdbc:derby:net:/ ? ::JdbcSpec::Derby : self
  end
  false
end

.adapter_selectorObject



10
11
12
13
14
15
16
17
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 10

def self.adapter_selector
  [/db2/i, lambda {|cfg,adapt|
     if cfg[:url] =~ /^jdbc:derby:net:/
       adapt.extend(::JdbcSpec::Derby)
     else
       adapt.extend(::JdbcSpec::DB2)
     end }]
end

.extended(obj) ⇒ Object



19
20
21
22
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 19

def self.extended(obj)
  # Ignore these 4 system tables
  ActiveRecord::SchemaDumper.ignore_tables |= %w{hmon_atm_info hmon_collection policy stmg_dbsize_info}
end

Instance Method Details

#adapter_nameObject



77
78
79
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 77

def adapter_name
  'DB2'
end

#add_limit_offset!(sql, options) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 81

def add_limit_offset!(sql, options)
  if limit = options[:limit]
    offset = options[:offset] || 0
    sql.gsub!(/SELECT/i, 'SELECT B.* FROM (SELECT A.*, row_number() over () AS internal$rownum FROM (SELECT')
    sql << ") A ) B WHERE B.internal$rownum > #{offset} AND B.internal$rownum <= #{limit + offset}"
  end
end

#add_quotes(name) ⇒ Object



155
156
157
158
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 155

def add_quotes(name)
  return name unless name
  %Q{"#{name}"}
end

#columns(table_name, name = nil) ⇒ Object

This method makes tests pass without understanding why. Don’t use this in production.



148
149
150
151
152
153
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 148

def columns(table_name, name = nil)
  super.select do |col|
    # strip out "magic" columns from DB2 (?)
    !/rolename|roleid|create_time|auditpolicyname|auditpolicyid|remarks/.match(col.name)
  end
end

#dump_schema_informationObject



210
211
212
213
214
215
216
217
218
219
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 210

def dump_schema_information
  begin
    if (current_schema = ActiveRecord::Migrator.current_version) > 0
      #TODO: Find a way to get the DB2 instace name to properly form the statement
      return "INSERT INTO DB2INST2.SCHEMA_INFO (version) VALUES (#{current_schema})"
    end
  rescue ActiveRecord::StatementInvalid
    # No Schema Info
  end
end

#expand_double_quotes(name) ⇒ Object



166
167
168
169
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 166

def expand_double_quotes(name)
  return name unless name && name['"']
  name.gsub(/"/,'""')
end

#insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 57

def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
  id = super
  unless id
    table_name = sql.split(/\s/)[2]
    result = select(ActiveRecord::Base.send(:sanitize_sql,
        %[select IDENTITY_VAL_LOCAL() as last_insert_id from #{table_name}],
        nil))
    id = result.last['last_insert_id']
  end
  id
end

#modify_types(tp) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 69

def modify_types(tp)
  tp[:primary_key] = 'int not null generated by default as identity (start with 1) primary key'
  tp[:string][:limit] = 255
  tp[:integer][:limit] = nil
  tp[:boolean][:limit] = nil
  tp
end

#pk_and_sequence_for(table) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 89

def pk_and_sequence_for(table)
  # In JDBC/DB2 side, only upcase names of table and column are handled.
  keys = super(table.upcase)
  if keys[0]
    # In ActiveRecord side, only downcase names of table and column are handled.
    keys[0] = keys[0].downcase
  end
  keys
end

#quote(value, column = nil) ⇒ Object

:nodoc:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 103

def quote(value, column = nil) # :nodoc:
  if column && column.type == :primary_key
    return value.to_s
  end
  if column && (column.type == :decimal || column.type == :integer) && value
    return value.to_s
  end
  case value
  when String
    if column && column.type == :binary
      "BLOB('#{quote_string(value)}')"
    else
      "'#{quote_string(value)}'"
    end
  else super
  end
end

#quote_column_name(column_name) ⇒ Object



99
100
101
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 99

def quote_column_name(column_name)
  column_name
end

#quote_string(string) ⇒ Object



121
122
123
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 121

def quote_string(string)
  string.gsub(/'/, "''") # ' (for ruby-mode)
end

#quoted_falseObject



129
130
131
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 129

def quoted_false
  '0'
end

#quoted_trueObject



125
126
127
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 125

def quoted_true
  '1'
end

#recreate_database(name) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 133

def recreate_database(name)
  do_not_drop = ["stmg_dbsize_info","hmon_atm_info","hmon_collection","policy"]
  tables.each do |table|
    unless do_not_drop.include?(table)
      drop_table(table)
    end
  end
end

#remove_index(table_name, options = { }) ⇒ Object



142
143
144
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 142

def remove_index(table_name, options = { })
  execute "DROP INDEX #{quote_column_name(index_name(table_name, options))}"
end

#strip_quotes(str) ⇒ Object



160
161
162
163
164
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 160

def strip_quotes(str)
  return str unless str
  return str unless /^(["']).*\1$/ =~ str
  str[1..-2]
end

#structure_dumpObject

:nodoc:



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 172

def structure_dump #:nodoc:
  definition=""
  rs = @connection.connection..getTables(nil,nil,nil,["TABLE"].to_java(:string))
  while rs.next
    tname = rs.getString(3)
    definition << "CREATE TABLE #{tname} (\n"
    rs2 = @connection.connection..getColumns(nil,nil,tname,nil)
    first_col = true
    while rs2.next
      col_name = add_quotes(rs2.getString(4));
      default = ""
      d1 = rs2.getString(13)
      default = d1 ? " DEFAULT #{d1}" : ""

      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))) +
        " " +
        type +
        "" +
        nulling +
        default
      if !first_col
        create_col_string = ",\n #{create_col_string}"
      else
        create_col_string = " #{create_col_string}"
      end

      definition << create_col_string

      first_col = false
    end
    definition << ");\n\n"
  end
  definition
end