Commit 12d5900f authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Add functions to create and remove database triggers



Change-Id: Id4f87c1fef835a11bc7413df93d60a6fada59a6a
Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent 9f517cc4
......@@ -1708,3 +1708,62 @@ function postgres_create_language($language) {
execute_sql("CREATE LANGUAGE $language;");
return postgres_language_exists($language);
}
/**
* Creates a database row trigger
*
* @param string $name trigger name
* @param string $time trigger action time, e.g. AFTER
* @param string $event trigger event, one of (INSERT, UPDATE, DELETE)
* @param string $table table name
* @param string $body function body
*/
function db_create_trigger($name, $time, $event, $table, $body) {
if ($time != 'AFTER' || ($event != 'INSERT' && $event != 'UPDATE' && $event != 'DELETE')) {
throw new SQLException("db_create_trigger() not implemented for $time $event");
}
if (is_postgres()) {
$functionname = $name . '_function';
$triggername = $name . '_trigger';
execute_sql('
CREATE FUNCTION {' . $functionname . '}() RETURNS TRIGGER AS $$
BEGIN
' . $body . '
RETURN NULL;
END
$$ LANGUAGE plpgsql;
CREATE TRIGGER {' . $triggername . '} ' . $time . ' ' . $event . '
ON {' . $table . '} FOR EACH ROW
EXECUTE PROCEDURE {' . $functionname . '}();'
);
}
else if (is_mysql()) {
$triggername = $name . '_trigger';
execute_sql('
CREATE TRIGGER {' . $triggername . '} ' . $time . ' ' . $event . '
ON {' . $table . '} FOR EACH ROW
BEGIN
' . $body . '
END'
);
}
else {
throw new SQLException("db_create_trigger() is not implemented for your database engine");
}
}
function db_drop_trigger($name, $table) {
if (is_postgres()) {
$functionname = $name . '_function';
$triggername = $name . '_trigger';
execute_sql('DROP TRIGGER {' . $triggername . '} ON {' . $table . '}');
execute_sql('DROP FUNCTION {' . $functionname . '}()');
}
else if (is_mysql()) {
$triggername = $name . '_trigger';
execute_sql('DROP TRIGGER {' . $triggername . '}');
}
else {
throw new SQLException("db_drop_trigger() is not implemented for your database engine");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment