Java Trigger - 2
CREATE TRIGGER tr_demo BEFORE INSERT, UPDATE ON test
REFERENCING NEW AS new_test FOR EACH ROW
BEGIN
DECLARE current_java_timestamp java.sql.Timestamp;
SET current_java_timestamp
= NEW java.sql.Timestamp
( java.lang.System.currentTimeMillis() );
CALL current_java_timestamp.setNanos
( java.lang.Math.min
( java.lang.Math.abs
( global_java_random.nextInt() / 10 ),
999999999 ) );
SET new_test.timestamp_string
= current_java_timestamp.toString()
END
Notes:
This trigger assigns a value to the timestamp_string column every time a row is inserted or updated. The global_java_random variable is a Java random number generator object that has been initialized earlier via CREATE VARIABLE and SET. The nextInt() method returns the next random integer value. The abs() method is used to strip the sign, and min() is called to make sure the value does not exceed 999,999,999. Finally, the setNanos() method is called to fill in the timestamp nanoseconds component, and toString() converts it to a readable string. All this serves to demonstrate that Java functionality is available from within ASA triggers.