Class: PgMorph::Polymorphic
- Inherits:
-
Object
- Object
- PgMorph::Polymorphic
show all
- Includes:
- Naming
- Defined in:
- lib/pg_morph/polymorphic.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Naming
#after_insert_fun_name, #after_insert_trigger_name, #before_insert_fun_name, #before_insert_trigger_name, #column_name_id, #column_name_type, #proxy_table, #type
Constructor Details
#initialize(parent_table, child_table, options) ⇒ Polymorphic
Returns a new instance of Polymorphic.
7
8
9
10
11
12
13
|
# File 'lib/pg_morph/polymorphic.rb', line 7
def initialize(parent_table, child_table, options)
@parent_table = parent_table
@child_table = child_table
@column_name = options[:column]
raise PgMorph::Exception.new("Column not specified") unless @column_name
end
|
Instance Attribute Details
#child_table ⇒ Object
Returns the value of attribute child_table.
5
6
7
|
# File 'lib/pg_morph/polymorphic.rb', line 5
def child_table
@child_table
end
|
#column_name ⇒ Object
Returns the value of attribute column_name.
5
6
7
|
# File 'lib/pg_morph/polymorphic.rb', line 5
def column_name
@column_name
end
|
#parent_table ⇒ Object
Returns the value of attribute parent_table.
5
6
7
|
# File 'lib/pg_morph/polymorphic.rb', line 5
def parent_table
@parent_table
end
|
Instance Method Details
#create_after_insert_trigger_fun_sql ⇒ Object
45
46
47
48
49
50
|
# File 'lib/pg_morph/polymorphic.rb', line 45
def create_after_insert_trigger_fun_sql
fun_name = after_insert_fun_name
create_trigger_fun(fun_name) do
%Q{DELETE FROM ONLY #{parent_table} WHERE id = NEW.id;}
end
end
|
#create_after_insert_trigger_sql ⇒ Object
38
39
40
41
42
43
|
# File 'lib/pg_morph/polymorphic.rb', line 38
def create_after_insert_trigger_sql
fun_name = after_insert_fun_name
trigger_name = after_insert_trigger_name
create_trigger_sql(parent_table, trigger_name, fun_name, 'AFTER INSERT')
end
|
#create_before_insert_trigger_fun_sql ⇒ Object
25
26
27
28
29
|
# File 'lib/pg_morph/polymorphic.rb', line 25
def create_before_insert_trigger_fun_sql
before_insert_trigger_content do
create_trigger_body.strip
end
end
|
#create_before_insert_trigger_sql ⇒ Object
31
32
33
34
35
36
|
# File 'lib/pg_morph/polymorphic.rb', line 31
def create_before_insert_trigger_sql
fun_name = before_insert_fun_name
trigger_name = before_insert_trigger_name
create_trigger_sql(parent_table, trigger_name, fun_name, 'BEFORE INSERT')
end
|
#create_proxy_table_sql ⇒ Object
15
16
17
18
19
20
21
22
23
|
# File 'lib/pg_morph/polymorphic.rb', line 15
def create_proxy_table_sql
%Q{
CREATE TABLE #{proxy_table} (
CHECK (#{column_name_type} = '#{type}'),
PRIMARY KEY (id),
FOREIGN KEY (#{column_name_id}) REFERENCES #{child_table}(id)
) INHERITS (#{parent_table});
}
end
|
#remove_after_insert_trigger_sql ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/pg_morph/polymorphic.rb', line 81
def remove_after_insert_trigger_sql
prosrc = get_function(before_insert_fun_name)
scan = prosrc.scan(/(( +(ELS)?IF.+\n)(\s+INSERT INTO.+;\n))/)
cleared = scan.reject { |x| x[0].match("#{proxy_table}") }
return '' if cleared.present?
fun_name = after_insert_fun_name
trigger_name = after_insert_trigger_name
drop_trigger_and_fun_sql(trigger_name, parent_table, fun_name)
end
|
#remove_before_insert_trigger_sql ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/pg_morph/polymorphic.rb', line 52
def remove_before_insert_trigger_sql
trigger_name = before_insert_trigger_name
fun_name = before_insert_fun_name
prosrc = get_function(fun_name)
raise PG::Error.new("There is no such function #{fun_name}()\n") unless prosrc
scan = prosrc.scan(/(( +(ELS)?IF.+\n)(\s+INSERT INTO.+;\n))/)
cleared = scan.reject { |x| x[0].match("#{proxy_table}") }
if cleared.present?
cleared[0][0].sub!('ELSIF', 'IF')
before_insert_trigger_content do
cleared.map { |m| m[0] }.join('').strip
end
else
drop_trigger_and_fun_sql(trigger_name, parent_table, fun_name)
end
end
|
#remove_partition_table ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/pg_morph/polymorphic.rb', line 72
def remove_partition_table
table_empty = ActiveRecord::Base.connection.select_value("SELECT COUNT(*) FROM #{parent_table}_#{child_table}").to_i.zero?
if table_empty
%Q{ DROP TABLE IF EXISTS #{proxy_table}; }
else
raise PG::Error.new("Partition table #{proxy_table} contains data.\nRemove them before if you want to drop that table.\n")
end
end
|