Class: DbMeta::Oracle::Constraint
- Defined in:
- lib/db_meta/oracle/types/constraint.rb
Constant Summary
Constants inherited from Base
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#constraint_type ⇒ Object
readonly
Returns the value of attribute constraint_type.
-
#delete_rule ⇒ Object
readonly
Returns the value of attribute delete_rule.
-
#referential_constraint ⇒ Object
readonly
Returns the value of attribute referential_constraint.
-
#search_condition ⇒ Object
readonly
Returns the value of attribute search_condition.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Attributes inherited from Base
#extract_type, #name, #status, #system_object, #type
Class Method Summary collapse
Instance Method Summary collapse
- #extract(args = {}) ⇒ Object
- #fetch(args = {}) ⇒ Object
-
#initialize(args = {}) ⇒ Constraint
constructor
A new instance of Constraint.
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(args) @extract_type = :embedded @columns = [] end |
Instance Attribute Details
#columns ⇒ Object (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_type ⇒ Object (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_rule ⇒ Object (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_constraint ⇒ Object (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_condition ⇒ Object (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_name ⇒ Object (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
70 71 72 |
# File 'lib/db_meta/oracle/types/constraint.rb', line 70 def self.sort_value(type) ['PRIMARY KEY', 'FOREIGN KEY', 'UNIQUE', 'CHECK'].index(type) end |
Instance Method Details
#extract(args = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/db_meta/oracle/types/constraint.rb', line 45 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 42 |
# 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 |