function createFunction($funcname, $args, $returns, $definition, $language, $flags, $setof, $cost, $rows, $comment, $replace = false)
{
// Begin a transaction
$status = $this->beginTransaction();
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
$this->fieldClean($funcname);
$this->clean($args);
$this->fieldClean($language);
$this->arrayClean($flags);
$this->clean($cost);
$this->clean($rows);
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$sql = "CREATE";
if ($replace) {
$sql .= " OR REPLACE";
}
$sql .= " FUNCTION \"{$f_schema}\".\"{$funcname}\" (";
if ($args != '') {
$sql .= $args;
}
// For some reason, the returns field cannot have quotes...
$sql .= ") RETURNS ";
if ($setof) {
$sql .= "SETOF ";
}
$sql .= "{$returns} AS ";
if (is_array($definition)) {
$this->arrayClean($definition);
$sql .= "'" . $definition[0] . "'";
if ($definition[1]) {
$sql .= ",'" . $definition[1] . "'";
}
} else {
$this->clean($definition);
$sql .= "'" . $definition . "'";
}
$sql .= " LANGUAGE \"{$language}\"";
// Add costs
if (!empty($cost)) {
$sql .= " COST {$cost}";
}
if ($rows != 0) {
$sql .= " ROWS {$rows}";
}
// Add flags
foreach ($flags as $v) {
// Skip default flags
if ($v == '') {
continue;
} else {
$sql .= "\n{$v}";
}
}
$status = $this->execute($sql);
if ($status != 0) {
$this->rollbackTransaction();
return -3;
}
/* set the comment */
$status = $this->setComment('FUNCTION', "\"{$funcname}\"({$args})", null, $comment);
if ($status != 0) {
$this->rollbackTransaction();
return -4;
}
return $this->endTransaction();
}