Class: ArJdbc::Tasks::DerbyDatabaseTasks

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

Constant Summary collapse

SIZEABLE =
%w( VARCHAR CLOB BLOB )

Instance Attribute Summary

Attributes inherited from JdbcDatabaseTasks

#configuration

Instance Method Summary collapse

Methods inherited from JdbcDatabaseTasks

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

Constructor Details

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

Instance Method Details

#createObject



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

def create
  establish_connection(config)
  ActiveRecord::Base.connection
end

#dropObject



12
13
14
15
16
17
18
# File 'lib/arjdbc/tasks/derby_database_tasks.rb', line 12

def drop
  db_dir = expand_path resolve_database(config, true)
  if File.exist?(db_dir)
    FileUtils.rm_r(db_dir)
    FileUtils.rmdir(db_dir) rescue nil
  end
end

#structure_dump(filename) ⇒ Object



22
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
# File 'lib/arjdbc/tasks/derby_database_tasks.rb', line 22

def structure_dump(filename)
  establish_connection(config)
  dump = File.open(filename, "w:utf-8")

   = connection.jdbc_connection.
  tables_rs = .getTables(nil, nil, nil, ["TABLE"].to_java(:String))

  while tables_rs.next
    table_name = tables_rs.getString('TABLE_NAME') # getString(3)
    dump << "CREATE TABLE #{connection.quote_table_name(table_name)} (\n"

    columns_rs = .getColumns(nil, nil, table_name, nil)
    first_col = true
    while columns_rs.next
      column_name = columns_rs.getString(4)
      default = columns_rs.getString(13)
      if default =~ /^GENERATED_/
        default = column_auto_increment_def(table_name, column_name)
      elsif default
        default = " DEFAULT #{default}"
      end
      type = columns_rs.getString(6)
      column_size = columns_rs.getString(7)
      nulling = ( columns_rs.getString(18) == 'NO' ? " NOT NULL" : nil )

      create_column = connection.quote_column_name(column_name)
      create_column << " #{type}"
      create_column << ( SIZEABLE.include?(type) ? "(#{column_size})" : "" )
      create_column << nulling.to_s
      create_column << default.to_s

      create_column = first_col ? " #{create_column}" : ",\n #{create_column}"
      dump << create_column

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

  dump.close
end

#structure_load(filename) ⇒ Object



64
65
66
67
# File 'lib/arjdbc/tasks/derby_database_tasks.rb', line 64

def structure_load(filename)
  establish_connection(config)
  IO.read(filename).split(/;\n*/m).each { |ddl| connection.execute(ddl) }
end