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_selectorObject



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

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

.column_selectorObject



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

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

Instance Method Details

#add_limit_offset!(sql, options) ⇒ Object



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

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



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

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.



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

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



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

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



137
138
139
140
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 137

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

#modify_types(tp) ⇒ Object



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

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

#quote(value, column = nil) ⇒ Object

:nodoc:



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

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



70
71
72
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 70

def quote_column_name(column_name)
  column_name
end

#quote_string(string) ⇒ Object



92
93
94
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 92

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

#quoted_falseObject



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

def quoted_false
  '0'
end

#quoted_trueObject



96
97
98
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 96

def quoted_true
  '1'
end

#recreate_database(name) ⇒ Object



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

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



113
114
115
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 113

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

#strip_quotes(str) ⇒ Object



131
132
133
134
135
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 131

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

#structure_dumpObject

:nodoc:



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
178
179
# File 'lib/jdbc_adapter/jdbc_db2.rb', line 143

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