Class: Dbsketch::Model::Table
Instance Attribute Summary collapse
#comment, #dependencies, #meaning, #name, #order
Instance Method Summary
collapse
#add_dependencies, #class_name, #compute_order!, #inspect, #reset_order!
Constructor Details
#initialize(name, meaning: nil, comment: nil, dependencies: [], columns: [], p_key_columns: []) ⇒ Table
Returns a new instance of Table.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/dbsketch/model/table.rb', line 18
def initialize name, meaning: nil, comment: nil, dependencies: [], columns: [], p_key_columns: []
super name, :meaning => meaning, :comment => , :dependencies => dependencies
columns = columns.is_a?(Array) ? columns.flatten : [columns]
p_key_columns = p_key_columns.is_a?(Array) ? p_key_columns : [p_key_columns]
columns.each_with_index do |column, index|
raise ArgumentError, "columns[#{index}] is not a Dbsketch::Model::AbstractColumn" unless column.is_a? AbstractColumn
end
p_key_columns.each_with_index do |column, index|
raise ArgumentError, "p_key_columns[#{index}] is not a String nor a Dbsketch::Model::AbstractColumn" unless column.is_a? String or column.is_a? AbstractColumn
end
@columns = []
columns.each { |c| add_column c }
@primary_key = nil
primary_key_columns = p_key_columns.map { |c| (c.is_a? AbstractColumn) ? c : self[c] }
set_primary_key PrimaryKey.new "PK_#{@name}", primary_key_columns if not primary_key_columns.empty?
@check_constraints = []
@foreign_keys = []
@unique_constraints = []
end
|
Instance Attribute Details
#check_constraints ⇒ Object
Returns the value of attribute check_constraints.
40
41
42
|
# File 'lib/dbsketch/model/table.rb', line 40
def check_constraints
@check_constraints
end
|
#columns ⇒ Object
Returns the value of attribute columns.
40
41
42
|
# File 'lib/dbsketch/model/table.rb', line 40
def columns
@columns
end
|
#foreign_keys ⇒ Object
Returns the value of attribute foreign_keys.
40
41
42
|
# File 'lib/dbsketch/model/table.rb', line 40
def foreign_keys
@foreign_keys
end
|
#primary_key ⇒ Object
Returns the value of attribute primary_key.
40
41
42
|
# File 'lib/dbsketch/model/table.rb', line 40
def primary_key
@primary_key
end
|
#unique_constraints ⇒ Object
Returns the value of attribute unique_constraints.
40
41
42
|
# File 'lib/dbsketch/model/table.rb', line 40
def unique_constraints
@unique_constraints
end
|
Instance Method Details
#[](column_name) ⇒ Object
92
93
94
95
96
|
# File 'lib/dbsketch/model/table.rb', line 92
def [] column_name
column = @columns.find { |c| c.name.downcase == column_name.downcase }
raise ArgumentError, "column #{column_name} not found in table #{@name}" if nil == column
column
end
|
#add(object) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/dbsketch/model/table.rb', line 42
def add object
objects = object.is_a?(Array) ? object : [object]
objects.each_with_index do |o, index|
raise ArgumentError, "object n°#{index + 1} is not a valid Dbsketch::Model object" unless o.is_a? AbstractColumn or o.is_a? PrimaryKey or o.is_a? CheckConstraint or o.is_a? ForeignKey or o.is_a? UniqueConstraint
end
objects.each do |o|
if o.is_a? AbstractColumn
add_column o
elsif o.is_a? PrimaryKey
set_primary_key o
elsif o.is_a? CheckConstraint
add_check_constraint o
elsif o.is_a? ForeignKey
add_foreign_key o
elsif o.is_a? UniqueConstraint
add_unique_constraint o
end
end
end
|
#check_constraint(constraint_name) ⇒ Object
98
99
100
101
102
|
# File 'lib/dbsketch/model/table.rb', line 98
def check_constraint constraint_name
constraint = @check_constraints.find { |c| c.name.downcase == constraint_name.downcase }
raise ArgumentError, "check constraint #{constraint_name} not found in table #{@name}" if nil == constraint
constraint
end
|
#foreign_key(key_name) ⇒ Object
104
105
106
107
108
|
# File 'lib/dbsketch/model/table.rb', line 104
def foreign_key key_name
key = @foreign_keys.find { |c| c.name.downcase == key_name.downcase }
raise ArgumentError, "foreign key #{key_name} not found in table #{@name}" if nil == key
key
end
|
#has_check_constraint?(constraint_name) ⇒ Boolean
71
72
73
74
75
76
|
# File 'lib/dbsketch/model/table.rb', line 71
def has_check_constraint? constraint_name
raise ArgumentError, "constraint_name is not a String" unless constraint_name.is_a? String
nil != @check_constraints.find { |c| c.name.downcase == constraint_name.downcase }
end
|
#has_column?(column_name) ⇒ Boolean
64
65
66
67
68
69
|
# File 'lib/dbsketch/model/table.rb', line 64
def has_column? column_name
raise ArgumentError, "column_name is not a String" unless column_name.is_a? String
nil != @columns.find { |c| c.name.downcase == column_name.downcase }
end
|
#has_foreign_key?(key_name) ⇒ Boolean
78
79
80
81
82
83
|
# File 'lib/dbsketch/model/table.rb', line 78
def has_foreign_key? key_name
raise ArgumentError, "key_name is not a String" unless key_name.is_a? String
nil != @foreign_keys.find { |c| c.name.downcase == key_name.downcase }
end
|
#has_unique_constraint?(constraint_name) ⇒ Boolean
85
86
87
88
89
90
|
# File 'lib/dbsketch/model/table.rb', line 85
def has_unique_constraint? constraint_name
raise ArgumentError, "constraint_name is not a String" unless constraint_name.is_a? String
nil != @unique_constraints.find { |c| c.name.downcase == constraint_name.downcase }
end
|
#unique_constraint(constraint_name) ⇒ Object
110
111
112
113
114
|
# File 'lib/dbsketch/model/table.rb', line 110
def unique_constraint constraint_name
constraint = @unique_constraints.find { |c| c.name.downcase == constraint_name.downcase }
raise ArgumentError, "unique constraint #{constraint_name} not found in table #{@name}" if nil == constraint
constraint
end
|