Class: DbMeta::Oracle::Table

Inherits:
Base
  • Object
show all
Defined in:
lib/db_meta/oracle/types/table.rb

Constant Summary

Constants inherited from Base

Base::TYPES

Instance Attribute Summary collapse

Attributes inherited from Base

#extract_type, #name, #status, #system_object, #type

Instance Method Summary collapse

Methods inherited from Base

from_type, register_type, #system_object?

Methods included from Helper

#block, #create_folder, #pluralize, #remove_folder, #type_sequence, #write_buffer_to_file

Constructor Details

#initialize(args = {}) ⇒ Table

Returns a new instance of Table.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/db_meta/oracle/types/table.rb', line 8

def initialize(args = {})
  super

  @comment = nil # table level comment

  @comment = nil
  @columns = []
  @indexes = []
  @constraints = []
  @triggers = []
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



6
7
8
# File 'lib/db_meta/oracle/types/table.rb', line 6

def cache
  @cache
end

#columnsObject (readonly)

Returns the value of attribute columns.



6
7
8
# File 'lib/db_meta/oracle/types/table.rb', line 6

def columns
  @columns
end

#durationObject (readonly)

Returns the value of attribute duration.



6
7
8
# File 'lib/db_meta/oracle/types/table.rb', line 6

def duration
  @duration
end

#iot_typeObject (readonly)

Returns the value of attribute iot_type.



6
7
8
# File 'lib/db_meta/oracle/types/table.rb', line 6

def iot_type
  @iot_type
end

#temporaryObject (readonly)

Returns the value of attribute temporary.



6
7
8
# File 'lib/db_meta/oracle/types/table.rb', line 6

def temporary
  @temporary
end

Instance Method Details

#add_object(object) ⇒ Object



20
21
22
23
24
# File 'lib/db_meta/oracle/types/table.rb', line 20

def add_object(object)
  @indexes << object if object.type == "INDEX"
  @constraints << object if object.type == "CONSTRAINT"
  @triggers << object if object.type == "TRIGGER"
end

#ddl_dropObject



110
111
112
# File 'lib/db_meta/oracle/types/table.rb', line 110

def ddl_drop
  "DROP #{@type} #{@name} CASCADE CONSTRAINTS PURGE;"
end

#extract(args = {}) ⇒ Object



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/db_meta/oracle/types/table.rb', line 43

def extract(args = {})
  buffer = [block(@name)]
  buffer << "CREATE#{" #{@temporary}" if @temporary} TABLE #{@name}"
  buffer << "("

  # add columns
  @columns.each_with_index do |c, index|
    buffer << "  #{c.extract}#{"," if index + 1 < @columns.size}"
  end

  # Primary key definition must be here for IOT tables
  if @iot_type == "IOT"
    constraint = @constraints.find { |c| c.constraint_type == "PRIMARY KEY" }
    buffer[-1] += ","
    buffer << "  CONSTRAINT #{constraint.name}"
    buffer << "  PRIMARY KEY (#{constraint.columns.join(", ")})"
    buffer << "  ENABLE VALIDATE"
  end

  buffer << ")"
  buffer << translate_duration if @duration.size > 0
  buffer << @cache if @temporary
  buffer << "ORGANIZATION INDEX" if @iot_type == "IOT"
  buffer[-1] += ";"
  buffer << nil

  # table comments
  if @comment
    buffer << "COMMENT ON TABLE #{@name} IS '#{@comment.text("'", "''")}';"
  end

  # table column comments
  @columns.each do |column|
    next if column.comment.nil? || column.comment.size == 0
    buffer << "COMMENT ON COLUMN #{@name}.#{column.name} IS '#{column.comment.gsub("'", "''")}';"
  end

  # indexes
  if @indexes.size > 0
    buffer << block("Indexes", 40)
    @indexes.each do |index|
      line = ""
      line << "-- System Index: " if index.system_object? || @iot_type == "IOT"
      line << index.extract(args)
      buffer << line
    end
    buffer << nil
  end

  # constaints
  if @constraints.size > 0
    buffer << block("Constraints", 40)
    @constraints.sort_by { |c| [Constraint.sort_value(c.constraint_type), c.name] }.each do |constraint|
      buffer << constraint.extract(args.merge({comment: (constraint.constraint_type == "FOREIGN KEY")}))
    end
  end

  # triggers
  if @triggers.size > 0
    buffer << block("Triggers", 40)
    buffer << @triggers.map { |o| o.extract(args) }.join("\n")
    buffer << nil
  end

  buffer.join("\n")
end

#fetchObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/db_meta/oracle/types/table.rb', line 26

def fetch
  @comment = Comment.find(type: "TABLE", name: @name)
  @columns = Column.all(object_name: @name)

  connection = Connection.instance.get
  cursor = connection.exec("select temporary, cache, iot_type, duration from user_tables where table_name = '#{@name}'")
  while (row = cursor.fetch)
    @temporary = (row[0].to_s.strip == "Y") ? "GLOBAL TEMPORARY" : nil
    @cache = (row[1].to_s.strip == "Y") ? "CACHE" : "NOCACHE"
    @iot_type = row[2].to_s
    @duration = row[3].to_s
  end
  cursor.close
rescue
  connection.logoff
end

#get_core_data_where_clause(id = 1000000) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/db_meta/oracle/types/table.rb', line 114

def get_core_data_where_clause(id = 1000000)
  buffer = []
  @constraints.each do |constraint|
    if constraint.constraint_type == "PRIMARY KEY"
      constraint.columns.each do |column|
        buffer << "#{column} < #{id}"
      end
    end
  end

  return "" if buffer.size == 0

  "where " + buffer.join(" and ")
end