Class: DbMeta::Oracle::Objects

Inherits:
Object
  • Object
show all
Extended by:
Helper
Includes:
Helper
Defined in:
lib/db_meta/oracle/objects.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

block, create_folder, pluralize, remove_folder, type_sequence, write_buffer_to_file

Constructor Details

#initializeObjects

Returns a new instance of Objects.



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

def initialize
  @data = Hash.new{ |h, type|  h[type] = {} }
  @worker_queue = ::Queue.new
  @types_with_object_status_default = []

  @summary = Hash.new{ |h, type| h[type] = 0 }
  @summary_system_object = Hash.new{ |h, type| h[type] = 0 }
  @invalids = Hash.new{ |h, type| h[type] = [] }
end

Instance Attribute Details

#summary_system_objectObject (readonly)

Returns the value of attribute summary_system_object.



7
8
9
# File 'lib/db_meta/oracle/objects.rb', line 7

def summary_system_object
  @summary_system_object
end

Class Method Details

.allObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/db_meta/oracle/objects.rb', line 195

def self.all
  objects = []

  # get all objects as a hash containing name, type, status
  items = []
  types = []
  connection = Connection.instance.get
  cursor = connection.exec(OBJECT_QUERY)
  cursor.fetch_hash do |item|
    items << item
    types << item['OBJECT_TYPE']
  end
  cursor.close

  # sort items and make an object instance
  items.sort_by{ |i| [ type_sequence(i['OBJECT_TYPE']), i['OBJECT_NAME']]}.each do |item|
    objects << Base.from_type(item)
  end

  Log.info("Objects: #{items.size}, Object Types: #{types.uniq.size}")

  objects
ensure
  connection.logoff if connection # closes logical connection
end

Instance Method Details

#<<(object) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/db_meta/oracle/objects.rb', line 19

def <<(object)
  @data[object.type][object.name] = object
  @worker_queue << object

  @summary[object.type] += 1
  @summary_system_object[object.type] += 1 if object.system_object?
  @invalids[object.type] << object if [:invalid, :disabled].include?(object.status)
end

#default_eachObject



156
157
158
159
160
161
162
163
164
165
# File 'lib/db_meta/oracle/objects.rb', line 156

def default_each
  @data.keys.sort_by{ |type| type_sequence(type) }.each do |type|
    @data[type].keys.sort.each do |name|
      object = @data[type][name]
      next if object.system_object?
      next unless object.extract_type == :default
      yield(object)
    end
  end
end

#detect_system_objectsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/db_meta/oracle/objects.rb', line 45

def detect_system_objects
  Log.info("Detecting system objects...")

  # detect materialized view tables
  @data['MATERIALZIED VIEW'].values.each do |object|
    table = @data['TABLE'][object.name]
    next unless table
    table.system_object = true
  end

  @data['QUEUE'].values.each do |object|
    table = @data['TABLE'][object.queue_table]
    next unless table
    table.system_object = true
  end
end

#embed_constraintsObject



99
100
101
102
103
104
105
106
# File 'lib/db_meta/oracle/objects.rb', line 99

def embed_constraints
  Log.info("Embedding constraints...")

  @data["CONSTRAINT"].values.each do |constraint|
    next unless @data['TABLE'][constraint.table_name]
    @data['TABLE'][constraint.table_name].add_object(constraint)
  end
end

#embed_indexesObject



90
91
92
93
94
95
96
97
# File 'lib/db_meta/oracle/objects.rb', line 90

def embed_indexes
  Log.info("Embedding indexes...")

  @data['INDEX'].values.each do |object|
    next unless @data['TABLE'][object.table_name]
    @data['TABLE'][object.table_name].add_object(object)
  end
end

#embed_triggersObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/db_meta/oracle/objects.rb', line 108

def embed_triggers
  Log.info("Embedding triggers...")

  @data["TRIGGER"].values.each do |object|
    table_object = @data['TABLE'][object.table_name]

    if table_object
      table_object.add_object(object)
    else
      # if there is no table relation, just extract as default
      object.extract_type = :default
    end
  end
end

#fetch(args = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/db_meta/oracle/objects.rb', line 28

def fetch(args={})
  # fetch details in parallel
  # start as many worker threads as max physical connections
  worker = (1..Connection.instance.worker).map do
    Thread.new do
      begin
        while object = @worker_queue.pop(true)
          Log.info(" - #{object.type} - #{object.name}")
          object.fetch
        end
      rescue ThreadError
      end
    end
  end
  worker.map(&:join) # wait until all are done
end

#handle_table_data(args) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/db_meta/oracle/objects.rb', line 138

def handle_table_data(args)
  Log.info("Handling table data...")

  @exclude_data = args[:exclude_data] if args[:exclude_data]
  @include_data = args[:include_data] if args[:include_data]

  tables = []
  @data['TABLE'].values.each do |table|
    next if table.system_object?
    next if table.name =~ @exclude_data if @exclude_data
    next unless table.name =~ @include_data if @include_data
    tables << table
  end

  self << TableDataCollection.new(name: 'ALL CORE DATA', type: 'DATA', tables: tables)
  @summary['DATA'] -= 1 # no need to count DATA object
end

#invalid_eachObject



188
189
190
191
192
# File 'lib/db_meta/oracle/objects.rb', line 188

def invalid_each
  @invalids.each_pair do |type, objects|
    yield type, objects
  end
end

#invalids?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/db_meta/oracle/objects.rb', line 184

def invalids?
  @invalids.keys.size > 0
end

#merge_constraintsObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/db_meta/oracle/objects.rb', line 123

def merge_constraints
  Log.info("Merging constraints...")
  constraint_collection = ConstraintCollection.new(type: 'CONSTRAINT', name: 'ALL FOREIGN KEYS')

  @data['CONSTRAINT'].values.each do |object|
    next unless object.extract_type == :merged
    constraint_collection << object
  end

  return if constraint_collection.empty?

  self << constraint_collection
  @summary['CONSTRAINT'] -= 1    # no need to count collection object
end

#merge_grantsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/db_meta/oracle/objects.rb', line 76

def merge_grants
  Log.info("Merging grants...")
  grant_collection = GrantCollection.new(type: 'GRANT', name: 'ALL')

  @data['GRANT'].values.sort_by{ |o| o.sort_value }.each do |object|
    grant_collection << object
  end

  return if grant_collection.empty?

  self << grant_collection
  @summary['GRANT'] -= 1    # no need to count collection object
end

#merge_synonymsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/db_meta/oracle/objects.rb', line 62

def merge_synonyms
  Log.info("Merging synonyms...")
  synonym_collection = SynonymCollection.new(type: 'SYNONYM', name: 'ALL')

  @data['SYNONYM'].values.each do |object|
    synonym_collection << object
  end

  return if synonym_collection.empty?

  self << synonym_collection
  @summary['SYNONYM'] -= 1    # no need to count collection object
end

#reverse_default_eachObject



167
168
169
170
171
172
173
174
175
176
# File 'lib/db_meta/oracle/objects.rb', line 167

def reverse_default_each
  @data.keys.sort_by{ |type| type_sequence(type) }.reverse_each do |type|
    @data[type].keys.sort.each do |name|
      object = @data[type][name]
      next if object.system_object?
      next unless object.extract_type == :default
      yield object
    end
  end
end

#summary_eachObject



178
179
180
181
182
# File 'lib/db_meta/oracle/objects.rb', line 178

def summary_each
  @summary.each_pair do |type, count|
    yield type, count
  end
end