function getTriggerDef($trigger)
{
$this->fieldArrayClean($trigger);
// Constants to figure out tgtype
if (!defined('TRIGGER_TYPE_ROW')) {
define('TRIGGER_TYPE_ROW', 1 << 0);
}
if (!defined('TRIGGER_TYPE_BEFORE')) {
define('TRIGGER_TYPE_BEFORE', 1 << 1);
}
if (!defined('TRIGGER_TYPE_INSERT')) {
define('TRIGGER_TYPE_INSERT', 1 << 2);
}
if (!defined('TRIGGER_TYPE_DELETE')) {
define('TRIGGER_TYPE_DELETE', 1 << 3);
}
if (!defined('TRIGGER_TYPE_UPDATE')) {
define('TRIGGER_TYPE_UPDATE', 1 << 4);
}
$trigger['tgisconstraint'] = $this->phpBool($trigger['tgisconstraint']);
$trigger['tgdeferrable'] = $this->phpBool($trigger['tgdeferrable']);
$trigger['tginitdeferred'] = $this->phpBool($trigger['tginitdeferred']);
// Constraint trigger or normal trigger
if ($trigger['tgisconstraint']) {
$tgdef = 'CREATE CONSTRAINT TRIGGER ';
} else {
$tgdef = 'CREATE TRIGGER ';
}
$tgdef .= "\"{$trigger['tgname']}\" ";
// Trigger type
$findx = 0;
if (($trigger['tgtype'] & TRIGGER_TYPE_BEFORE) == TRIGGER_TYPE_BEFORE) {
$tgdef .= 'BEFORE';
} else {
$tgdef .= 'AFTER';
}
if (($trigger['tgtype'] & TRIGGER_TYPE_INSERT) == TRIGGER_TYPE_INSERT) {
$tgdef .= ' INSERT';
$findx++;
}
if (($trigger['tgtype'] & TRIGGER_TYPE_DELETE) == TRIGGER_TYPE_DELETE) {
if ($findx > 0) {
$tgdef .= ' OR DELETE';
} else {
$tgdef .= ' DELETE';
$findx++;
}
}
if (($trigger['tgtype'] & TRIGGER_TYPE_UPDATE) == TRIGGER_TYPE_UPDATE) {
if ($findx > 0) {
$tgdef .= ' OR UPDATE';
} else {
$tgdef .= ' UPDATE';
}
}
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
// Table name
$tgdef .= " ON \"{$f_schema}\".\"{$trigger['relname']}\" ";
// Deferrability
if ($trigger['tgisconstraint']) {
if ($trigger['tgconstrrelid'] != 0) {
// Assume constrelname is not null
$tgdef .= " FROM \"{$trigger['tgconstrrelname']}\" ";
}
if (!$trigger['tgdeferrable']) {
$tgdef .= 'NOT ';
}
$tgdef .= 'DEFERRABLE INITIALLY ';
if ($trigger['tginitdeferred']) {
$tgdef .= 'DEFERRED ';
} else {
$tgdef .= 'IMMEDIATE ';
}
}
// Row or statement
if ($trigger['tgtype'] & TRIGGER_TYPE_ROW == TRIGGER_TYPE_ROW) {
$tgdef .= 'FOR EACH ROW ';
} else {
$tgdef .= 'FOR EACH STATEMENT ';
}
// Execute procedure
$tgdef .= "EXECUTE PROCEDURE \"{$trigger['tgfname']}\"(";
// Parameters
// Escape null characters
$v = addCSlashes($trigger['tgargs'], "");
// Split on escaped null characters
$params = explode('\\000', $v);
for ($findx = 0; $findx < $trigger['tgnargs']; $findx++) {
$param = "'" . str_replace('\'', '\\\'', $params[$findx]) . "'";
$tgdef .= $param;
if ($findx < $trigger['tgnargs'] - 1) {
$tgdef .= ', ';
}
}
// Finish it off
$tgdef .= ')';
return $tgdef;
}