Class: PGExaminer::Result::Constraint
- Inherits:
-
Item
- Object
- Base
- Item
- PGExaminer::Result::Constraint
show all
- Defined in:
- lib/pg_examiner/result/constraint.rb
Constant Summary
collapse
- FOREIGN_KEY_ACTIONS =
{
"a" => "no action",
"r" => "restrict",
"c" => "cascade",
"n" => "set null",
"d" => "set default",
}.freeze
- FOREIGN_KEY_MATCH_TYPES =
{
"f" => "full",
"p" => "partial",
"s" => "simple",
}.freeze
Instance Attribute Summary
Attributes inherited from Item
#parent, #result, #row
Instance Method Summary
collapse
Methods inherited from Item
#initialize, #inspect, #name, #oid
Methods inherited from Base
#==, #diff, #diffable_lists
Instance Method Details
#constrained_columns ⇒ Object
66
67
68
|
# File 'lib/pg_examiner/result/constraint.rb', line 66
def constrained_columns
@constrained_columns ||= (row['conkey']).map{|n| parent.columns.find{|c| c.row['attnum'] == n}.name} if row['conkey']
end
|
#diffable_attrs ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/pg_examiner/result/constraint.rb', line 6
def diffable_attrs
{
"name" => "name",
"contype" => "constraint type",
"condeferrable" => "constraint is deferrable",
"condeferred" => "constraint is initially deferred",
"convalidated" => "constraint is validated",
"connoinherit" => "constraint is not inheritable",
"check_def" => "check constraint definition",
}
end
|
#diffable_methods ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/pg_examiner/result/constraint.rb', line 18
def diffable_methods
{
"type" => "type",
"index" => "index",
"foreign_table_name" => "table referenced by foreign key",
"constrained_columns" => "local constrained columns",
"foreign_constrained_columns" => "foreign constrained columns",
"foreign_key_update_action" => "foreign key on update action",
"foreign_key_delete_action" => "foreign key on delete action",
"foreign_key_match_type" => "foreign key match type",
}
end
|
#foreign_constrained_columns ⇒ Object
70
71
72
|
# File 'lib/pg_examiner/result/constraint.rb', line 70
def foreign_constrained_columns
@foreign_constrained_columns ||= (row['confkey']).map{|n| foreign_table.columns.find{|c| c.row['attnum'] == n}.name} if row['confkey']
end
|
#foreign_key_delete_action ⇒ Object
86
87
88
|
# File 'lib/pg_examiner/result/constraint.rb', line 86
def foreign_key_delete_action
FOREIGN_KEY_ACTIONS.fetch(row['confdeltype']) if row['confdeltype'] != ' '
end
|
#foreign_key_match_type ⇒ Object
96
97
98
|
# File 'lib/pg_examiner/result/constraint.rb', line 96
def foreign_key_match_type
FOREIGN_KEY_MATCH_TYPES.fetch(row['confmatchtype']) if row['confmatchtype'] != ' '
end
|
#foreign_key_update_action ⇒ Object
82
83
84
|
# File 'lib/pg_examiner/result/constraint.rb', line 82
def foreign_key_update_action
FOREIGN_KEY_ACTIONS.fetch(row['confupdtype']) if row['confupdtype'] != ' '
end
|
#foreign_table ⇒ Object
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/pg_examiner/result/constraint.rb', line 55
def foreign_table
if row['confrelid'] != '0'
@foreign_table ||= begin
table_row = result.pg_class.find { |c| c['relkind'] == 'r' && c['oid'] == row['confrelid'] }
schema = result.schemas.find { |s| s.oid == table_row['relnamespace'] }
schema.tables.find { |t| t.name == table_row['name'] }
end
end
end
|
#foreign_table_name ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/pg_examiner/result/constraint.rb', line 39
def foreign_table_name
if ft = foreign_table
this_schema_name = parent.parent.name
that_schema_name = ft.parent.name
relative_schema =
if this_schema_name == that_schema_name
"(same schema)"
else
"#{that_schema_name} schema"
end
[relative_schema, ft.name]
end
end
|
#index ⇒ Object
35
36
37
|
# File 'lib/pg_examiner/result/constraint.rb', line 35
def index
@index ||= result.pg_index.find{|i| i['indexrelid'] == row['conindid']}['name'] if row['conindid'] != '0'
end
|
#type ⇒ Object
31
32
33
|
# File 'lib/pg_examiner/result/constraint.rb', line 31
def type
@type ||= result.pg_type.find{|t| t['oid'] == row['contypid']}['name'] if row['contypid'] != '0'
end
|