Class: ArJdbc::Tasks::DB2DatabaseTasks

Inherits:
JdbcDatabaseTasks show all
Defined in:
lib/arjdbc/tasks/db2_database_tasks.rb

Instance Attribute Summary

Attributes inherited from JdbcDatabaseTasks

#configuration

Instance Method Summary collapse

Methods inherited from JdbcDatabaseTasks

#charset, #collation, #drop, #expand_path, #initialize, #resolve_database, #resolve_database_from_url

Constructor Details

This class inherits a constructor from ArJdbc::Tasks::JdbcDatabaseTasks

Instance Method Details

#createObject



7
8
9
# File 'lib/arjdbc/tasks/db2_database_tasks.rb', line 7

def create
  raise "AR-JDBC adapter 'DB2' does not support create_database"
end

#purgeObject



11
12
13
14
# File 'lib/arjdbc/tasks/db2_database_tasks.rb', line 11

def purge
  establish_connection(config)
  connection.recreate_database
end

#structure_dump(filename) ⇒ Object

NOTE: does not work correctly (on non AS400) due driver meta data issue

also try db2move e.g. db2move SAMPLE EXPORT -sn db2inst

  • where SAMPLE is the database name
  • and -sn specified schema name


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/arjdbc/tasks/db2_database_tasks.rb', line 23

def structure_dump(filename)
  establish_connection(config)
  dump = File.open(filename, "w:utf-8")
  
  schema_name = connection.schema.upcase if connection.schema
   = connection.jdbc_connection.
  tables_rs = .getTables(nil, schema_name, nil, ["TABLE"].to_java(:String))
  
  have_scale = ArJdbc::DB2::HAVE_SCALE
  have_precision = ArJdbc::DB2::HAVE_LIMIT + ArJdbc::DB2::HAVE_LIMIT
  
  while tables_rs.next
    table_name = tables_rs.getString('TABLE_NAME')
    dump << "CREATE TABLE #{connection.quote_table_name(table_name)} (\n"
    
    cols_rs = .getColumns(nil, schema_name, table_name, nil)
    begin
      first_col = true
      while cols_rs.next
        column_name = cols_rs.getString(4)
        default = cols_rs.getString(13)
        default = default.empty? ? "" : " DEFAULT #{default}" if default
        type = cols_rs.getString(6)
        precision, scale = cols_rs.getString(7), cols_rs.getString(9)
        column_size = ""
        if scale && have_scale.include?(type)
          column_size = "(#{precision},#{scale})"
        elsif precision && have_precision.include?(type)
          column_size = "(#{precision})"
        end
        nulling = ( cols_rs.getString(18) == 'NO' ? " NOT NULL" : nil )
        autoinc = ( cols_rs.getString(23) == 'YES' ? " GENERATED ALWAYS AS IDENTITY" : nil )
        
        create_column = connection.quote_column_name(column_name)
        create_column << " #{type}"
        create_column << column_size
        create_column << nulling.to_s
        create_column << default.to_s
        create_column << autoinc.to_s
        
        create_column = first_col ? "  #{create_column}" : ",\n  #{create_column}"
        dump << create_column

        first_col = false
      end
    ensure
      cols_rs.close
    end

    dump << "\n);\n\n"

    pk_rs = .getPrimaryKeys(nil, schema_name, table_name)
    primary_keys = {}
    begin
      while pk_rs.next
        name = pk_rs.getString(6)
        primary_keys[name] ||= []
        primary_keys[name] << pk_rs.getString(4)
      end
    ensure
      pk_rs.close
    end
    primary_keys.each do |constraint_name, cols|
      dump << "ALTER TABLE #{connection.quote_table_name(table_name)}\n"
      dump << "  ADD CONSTRAINT #{constraint_name}\n"
      dump << "    PRIMARY KEY (#{cols.join(', ')});\n\n"
    end
  end
  
  dump.close
end

#structure_load(filename) ⇒ Object



95
96
97
98
99
100
# File 'lib/arjdbc/tasks/db2_database_tasks.rb', line 95

def structure_load(filename)
  establish_connection(config)
  IO.read(filename).split(/;\n*/m).each do |ddl| 
    connection.execute ddl.sub(/;$/, '')
  end
end