Method: DbMeta::Oracle::TableDataCollection#extract

Defined in:
lib/db_meta/oracle/types/table_data_collection.rb

#extract(args = {}) ⇒ Object



16
17
18
19
20
21
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
# File 'lib/db_meta/oracle/types/table_data_collection.rb', line 16

def extract(args = {})
  buffer = [block(@name)]
  buffer << "set define off;"
  buffer << "set sqlblanklines on;"
  buffer << nil

  connection = Connection.instance.get

  @tables.each do |table|
    Log.info("Extracting data from #{table.name}...")

    buffer << block(table.name, 40)

    name_type_map = {}
    table.columns.each do |column|
      name_type_map[column.name] = column.type
    end

    statement = "select * from #{table.name} #{table.get_core_data_where_clause}"
    cursor = connection.exec(statement)
    cursor.fetch_hash do |item|
      buffer << "insert into #{table.name}(#{item.keys.join(", ")}) values(#{format_values(name_type_map, item)});"
    end
    cursor.close
    buffer << nil
  end

  buffer << "commit;"
  buffer << nil

  buffer.join("\n")
ensure
  connection.logoff
end