Class: DbMeta::Oracle::Constraint

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

Constant Summary

Constants inherited from Base

Base::TYPES

Instance Attribute Summary collapse

Attributes inherited from Base

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#ddl_drop, 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 = {}) ⇒ Constraint

Returns a new instance of Constraint.



8
9
10
11
12
13
# File 'lib/db_meta/oracle/types/constraint.rb', line 8

def initialize(args = {})
  super

  @extract_type = :embedded
  @columns = []
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



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

def columns
  @columns
end

#constraint_typeObject (readonly)

Returns the value of attribute constraint_type.



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

def constraint_type
  @constraint_type
end

#delete_ruleObject (readonly)

Returns the value of attribute delete_rule.



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

def delete_rule
  @delete_rule
end

#referential_constraintObject (readonly)

Returns the value of attribute referential_constraint.



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

def referential_constraint
  @referential_constraint
end

#search_conditionObject (readonly)

Returns the value of attribute search_condition.



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

def search_condition
  @search_condition
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



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

def table_name
  @table_name
end

Class Method Details

.sort_value(type) ⇒ Object



68
69
70
# File 'lib/db_meta/oracle/types/constraint.rb', line 68

def self.sort_value(type)
  ["PRIMARY KEY", "FOREIGN KEY", "UNIQUE", "CHECK"].index(type)
end

Instance Method Details

#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
# File 'lib/db_meta/oracle/types/constraint.rb', line 43

def extract(args = {})
  buffer = []
  buffer << "ALTER TABLE #{@table_name} ADD ("
  buffer << "  CONSTRAINT #{@name}"

  case @constraint_type
  when "CHECK"
    buffer << "  #{@constraint_type} (#{@search_condition})"
  when "FOREIGN KEY"
    buffer << "  #{@constraint_type} (#{@columns.join(", ")})"
    buffer << "  REFERENCES #{@referential_constraint.table_name} (#{@referential_constraint.columns.join(", ")})"
  else
    buffer << "  #{@constraint_type} (#{@columns.join(", ")})"
  end

  buffer << "  ON DELETE CASCADE" if @delete_rule == "CASCADE"
  buffer << "  ENABLE VALIDATE"
  buffer << ");"

  (0..buffer.size - 1).each { |n| buffer[n] = ("-- " + buffer[n]) } if args[:comment] == true

  buffer << nil
  buffer.join("\n")
end

#fetch(args = {}) ⇒ Object



15
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
# File 'lib/db_meta/oracle/types/constraint.rb', line 15

def fetch(args = {})
  connection = Connection.instance.get
  cursor = connection.exec("select * from user_constraints where constraint_name = '#{@name}'")
  cursor.fetch_hash do |item|
    @constraint_type = translate_constraint_type(item["CONSTRAINT_TYPE"])
    @extract_type = :merged if @constraint_type == "FOREIGN KEY"
    @table_name = item["TABLE_NAME"]
    @search_condition = item["SEARCH_CONDITION"]
    @delete_rule = item["DELETE_RULE"]

    if @constraint_type == "FOREIGN KEY"
      constraint = Constraint.new("OBJECT_TYPE" => "CONSTRAINT", "OBJECT_NAME" => item["R_CONSTRAINT_NAME"])
      constraint.fetch
      @referential_constraint = constraint
    end
  end
  cursor.close

  # get affected columns
  cursor = connection.exec("select * from user_cons_columns where constraint_name = '#{@name}' order by position")
  cursor.fetch_hash do |item|
    @columns << item["COLUMN_NAME"]
  end
  cursor.close
ensure
  connection.logoff
end