class Mv::Postgresql::Constraint::Builder::Trigger
Public Instance Methods
create()
click to toggle source
# File lib/mv/postgresql/constraint/builder/trigger.rb, line 6 def create validation_builders.group_by(&:table_name).each do |table_name, validations| db.execute(drop_trigger_statement(table_name)) db.execute(drop_function_statement()) db.execute(create_function_statement(table_name)) db.execute(create_trigger_statement(table_name)) end end
delete()
click to toggle source
# File lib/mv/postgresql/constraint/builder/trigger.rb, line 15 def delete validation_builders.group_by(&:table_name).each do |table_name, validations| if db.data_source_exists?(table_name) db.execute(drop_trigger_statement(table_name)) db.execute(drop_function_statement()) end end end
update(new_constraint_builder)
click to toggle source
# File lib/mv/postgresql/constraint/builder/trigger.rb, line 24 def update new_constraint_builder delete new_constraint_builder.create end
Private Instance Methods
create_function_statement(table_name)
click to toggle source
# File lib/mv/postgresql/constraint/builder/trigger.rb, line 56 def create_function_statement table_name "CREATE FUNCTION #{func_name}() RETURNS TRIGGER AS $#{func_name}$ BEGIN #{function_body(table_name)}; RETURN NEW; END; $#{func_name}$ LANGUAGE plpgsql;" end
create_trigger_statement(table_name)
click to toggle source
# File lib/mv/postgresql/constraint/builder/trigger.rb, line 39 def create_trigger_statement table_name "CREATE TRIGGER #{name} BEFORE #{update? ? 'UPDATE' : 'INSERT'} ON #{table_name} FOR EACH ROW EXECUTE PROCEDURE #{func_name}();".squish end
drop_function_statement()
click to toggle source
# File lib/mv/postgresql/constraint/builder/trigger.rb, line 52 def drop_function_statement "DROP FUNCTION IF EXISTS #{func_name}();" end
drop_trigger_statement(table_name)
click to toggle source
# File lib/mv/postgresql/constraint/builder/trigger.rb, line 35 def drop_trigger_statement table_name "DROP TRIGGER IF EXISTS #{name} ON #{table_name};" end
func_name()
click to toggle source
# File lib/mv/postgresql/constraint/builder/trigger.rb, line 31 def func_name "#{name}_func" end
function_body(table_name)
click to toggle source
# File lib/mv/postgresql/constraint/builder/trigger.rb, line 44 def function_body(table_name) validation_builders.select{|b| b.table_name == table_name }.collect(&:conditions).flatten.collect do |condition| "IF NOT(#{condition[:statement]}) THEN RAISE EXCEPTION '#{condition[:message]}'; END IF".squish end.join("; \n") end