function addForeignKey($table, $targschema, $targtable, $sfields, $tfields, $upd_action, $del_action, $match, $deferrable, $initially, $name = '')
{
if (!is_array($sfields) || sizeof($sfields) == 0 || !is_array($tfields) || sizeof($tfields) == 0) {
return -1;
}
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$this->fieldClean($table);
$this->fieldClean($targschema);
$this->fieldClean($targtable);
$this->fieldArrayClean($sfields);
$this->fieldArrayClean($tfields);
$this->fieldClean($name);
$sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ADD ";
if ($name != '') {
$sql .= "CONSTRAINT \"{$name}\" ";
}
$sql .= "FOREIGN KEY (\"" . join('","', $sfields) . "\") ";
// Target table needs to be fully qualified
$sql .= "REFERENCES \"{$targschema}\".\"{$targtable}\"(\"" . join('","', $tfields) . "\") ";
if ($match != $this->fkmatches[0]) {
$sql .= " {$match}";
}
if ($upd_action != $this->fkactions[0]) {
$sql .= " ON UPDATE {$upd_action}";
}
if ($del_action != $this->fkactions[0]) {
$sql .= " ON DELETE {$del_action}";
}
if ($deferrable != $this->fkdeferrable[0]) {
$sql .= " {$deferrable}";
}
if ($initially != $this->fkinitial[0]) {
$sql .= " {$initially}";
}
return $this->execute($sql);
}