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

Instance Method Details

#add_limit_offset!(sql, options) ⇒ Object



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

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



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

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.



117
118
119
120
121
122
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 117

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



179
180
181
182
183
184
185
186
187
188
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 179

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



135
136
137
138
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 135

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

#modify_types(tp) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 52

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

#quote(value, column = nil) ⇒ Object

:nodoc:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 72

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



68
69
70
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 68

def quote_column_name(column_name)
  column_name
end

#quote_string(string) ⇒ Object



90
91
92
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 90

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

#quoted_falseObject



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

def quoted_false
  '0'
end

#quoted_trueObject



94
95
96
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 94

def quoted_true
  '1'
end

#recreate_database(name) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 102

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



111
112
113
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 111

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

#strip_quotes(str) ⇒ Object



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

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

#structure_dumpObject

:nodoc:



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 141

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