Seva Software

 

What is Aruna DB?

Last Updated: 9/25/2001

A_Triggers

Purpose

License

Description

Dependencies

Limitations

Performance Considerations

To Do

Usage

Methods

Testing

A_Debug Usage

 

Purpose:

This Ruby module contains methods for providing trigger support the A_Table class. Triggers are methods that you create and attach to one of the trigger events associated with each table. Triggers allow you to check data, update other pieces of data, and make sure the data is consistent before and after insert, updates and deletes. There can be many triggers installed on each table. When there are multiple triggers, they are called in the order they were added. The A_Trigger module is an extension to the A_Table_Shared class (this is the shared portion of the A_Table class). Triggers are an extension of the A_Table_Shared class. Triggers are used by the A_Table class.

 

Description:

Triggers are Ruby code (methods that you create) and to tables at run time. As a result, triggers are not stored in the system catalog.  Triggers can be disabled at in each transaction. The trigger events for every table are:

The following new error has been created in this module for triggers:

Here are some general guidelines you should use when creating trigger methods:

 

Dependencies:

 

Limitations:

  • None

 

Performance Considerations:

 

To Do:

 

Usage:

# create a table

cols = []

cols.push(A_Column.new('member_id', nil, true, nil, '%d > 0', nil, '%3d'))

cols.push(A_Column.new('last_name', nil, true, nil, "'%s' != ''", "'%s'.capitalize", '%-12s'))

cols.push(A_Column.new('first_name', nil, nil, '', nil, "'%s'.capitalize", '%-12s'))

m = A_Table.new('Membership', cols, 'member_id')

 

# create a method for the trigger event

def membership_before_update(old_array, data_array)

print "calling membership_before_update(#{old_array}, #{data_array})\n"

end

# create and install the trigger using the method above, the trigger name is 'tst_after_insert'

m.create_trigger('tst_after_insert', TRIGGER_AFTER_INSERT, 'membership_after_insert')

m.close

 

def membership_before_insert(data_array)

if (data_array.first_name.downcase == 'josh')

raise TriggerError, "membership_before_insert() trigger error on before insert, name should never = 'Josh'"

end

# this actually changes the data being inserted

data_array.status = 19 if (data_array.status == nil)

end

def membership_before_update(old_array, data_array)

data_array.status = 19 if (data_array.status == nil)

if (data_array.status_id != old_array.status_id)

raise TriggerError, "membership_before_update() trigger error, you should never update the status_id"

end

end

 

def membership_before_delete(old_array)

if (old_array.status_id == 19)

raise TriggerError, "membership_before_delete() trigger error, you should never delete row where the status_id == 19"

end

end 

 

Methods:

 

create_trigger(trigger_name, trigger_type, trigger_method_name)

Added a trigger method to this table. A table can have many triggers methods installed on it. When a single event has multiple methods installs, the methods are called in the order they were installed.

 

drop_trigger(trigger_name, trigger_type)

Drop a trigger.

show_triggers(prefix='')

Prints all triggers installed on this table. Prefix can be used to indent the output for nicer formatting.

triggers_exist()

Returns true if any triggers are installed on this table. Returns false if no triggers are installed on this table.

 

Testing: 

tst_a_triggers.rb - this script show how to create and install triggers on tables. To run this tests type:

ruby -I.. tst_a_ triggers.rb

 

A_Debug Usage:

10 - prints information about creating and dropping triggers.