From fa1e86c527b75600d52f1163280194f29daef5db Mon Sep 17 00:00:00 2001 From: chriskl Date: Sun, 11 May 2003 10:39:29 +0000 Subject: [PATCH] support function volatility for create function --- classes/database/Postgres.php | 57 +++++++++++++---------------- classes/database/Postgres73.php | 51 +++++++++++++++++++++++++- functions.php | 65 +++++++++++++++++++++++---------- 3 files changed, 120 insertions(+), 53 deletions(-) diff --git a/classes/database/Postgres.php b/classes/database/Postgres.php index da5846c4..86f33025 100755 --- a/classes/database/Postgres.php +++ b/classes/database/Postgres.php @@ -4,7 +4,7 @@ * A class that implements the DB interface for Postgres * Note: This class uses ADODB and returns RecordSets. * - * $Id: Postgres.php,v 1.99 2003/05/08 15:25:58 chriskl Exp $ + * $Id: Postgres.php,v 1.100 2003/05/11 10:39:29 chriskl Exp $ */ // @@@ THOUGHT: What about inherits? ie. use of ONLY??? @@ -49,6 +49,9 @@ class Postgres extends BaseDB { var $triggerExecTimes = array('BEFORE', 'AFTER'); // Foreign key actions var $fkactions = array('NO ACTION', 'RESTRICT', 'CASCADE', 'SET NULL', 'SET DEFAULT'); + // Function properties + var $funcprops = array(array('', 'ISSTRICT'), array('', 'ISCACHABLE')); + var $defaultprops = array('', ''); // Last oid assigned to a system object var $_lastSystemOID = 18539; @@ -2181,7 +2184,7 @@ class Postgres extends BaseDB { /** * Creates a new function. * @param $funcname The name of the function to create - * @param $args The array of argument types + * @param $args A comma separated string of types * @param $returns The return type * @param $definition The definition for the new function * @param $language The language the function is written for @@ -2190,37 +2193,18 @@ class Postgres extends BaseDB { * @return 0 success */ function createFunction($funcname, $args, $returns, $definition, $language, $flags, $replace = false) { - /* - RE: arguments implementation It seem to me that we should be getting passed a comma delimited string - and that we need a comma delimited string - So why go through the array end around - ADODB throws errors if you leave it blank, and join complaines as well - - - Also I'm dropping support for the WITH option for now - Given that there are only 3 options, this might best be implemented with hardcoding - */ - - $this->clean($funcname); -// if (is_array($args)) { -// $this->arrayClean($args); -// } + $this->fieldClean($funcname); $this->clean($args); - $this->clean($returns); + $this->fieldClean($returns); $this->clean($definition); $this->clean($language); -// if (is_array($flags)) { -// $this->arrayClean($flags); -// } + $this->arrayClean($flags); $sql = "CREATE"; if ($replace) $sql .= " OR REPLACE"; $sql .= " FUNCTION \"{$funcname}\" ("; -/* - if (sizeof($args) > 0) - $sql .= '"' . join('", "', $args) . '"'; -*/ - if ($args) + + if ($args != '') $sql .= $args; // For some reason, the returns field cannot have quotes... @@ -2228,11 +2212,22 @@ class Postgres extends BaseDB { $sql .= $definition; $sql .= "\n'"; $sql .= " LANGUAGE \"{$language}\""; -/* - if (sizeof($flags) > 0) - $sql .= ' WITH ("' . join('", "', $flags) . '")'; -*/ - + + // Add flags + $first = true; + foreach ($flags as $v) { + // Skip default flags + if ($v == '') continue; + elseif ($first) { + $sql .= " WITH ({$v}"; + $first = false; + } + else { + $sql .= ", {$v}"; + } + } + // Close off WITH clause if necessary + if (!$first) $sql .= ")"; return $this->execute($sql); } diff --git a/classes/database/Postgres73.php b/classes/database/Postgres73.php index 857c06b6..49db37b2 100644 --- a/classes/database/Postgres73.php +++ b/classes/database/Postgres73.php @@ -4,7 +4,7 @@ * A class that implements the DB interface for Postgres * Note: This class uses ADODB and returns RecordSets. * - * $Id: Postgres73.php,v 1.41 2003/05/07 06:29:54 chriskl Exp $ + * $Id: Postgres73.php,v 1.42 2003/05/11 10:39:29 chriskl Exp $ */ // @@@ THOUGHT: What about inherits? ie. use of ONLY??? @@ -40,7 +40,12 @@ class Postgres73 extends Postgres72 { 'language' => array('USAGE', 'ALL PRIVILEGES'), 'schema' => array('CREATE', 'USAGE', 'ALL PRIVILEGES') ); - + // Function properties + var $funcprops = array( array('', 'VOLATILE', 'IMMUTABLE', 'STABLE'), + array('', 'CALLED ON NULL INPUT', 'RETURNS NULL ON NULL INPUT', 'STRICT'), + array('', 'SECURITY INVOKER', 'SECURITY DEFINER')); + var $defaultprops = array('', '', ''); + /** * Constructor * @param $host The hostname to connect to @@ -532,6 +537,48 @@ class Postgres73 extends Postgres72 { return $this->getFunctions(true, 'trigger'); } + /** + * Creates a new function. + * @param $funcname The name of the function to create + * @param $args A comma separated string of types + * @param $returns The return type + * @param $definition The definition for the new function + * @param $language The language the function is written for + * @param $flags An array of optional flags + * @param $replace (optional) True if OR REPLACE, false for normal + * @return 0 success + */ + function createFunction($funcname, $args, $returns, $definition, $language, $flags, $replace = false) { + $this->fieldClean($funcname); + $this->clean($args); + $this->fieldClean($returns); + $this->clean($definition); + $this->clean($language); + $this->arrayClean($flags); + + $sql = "CREATE"; + if ($replace) $sql .= " OR REPLACE"; + $sql .= " FUNCTION \"{$funcname}\" ("; + + if ($args != '') + $sql .= $args; + + // For some reason, the returns field cannot have quotes... + $sql .= ") RETURNS {$returns} AS '\n"; + $sql .= $definition; + $sql .= "\n'"; + $sql .= " LANGUAGE \"{$language}\""; + + // Add flags + foreach ($flags as $v) { + // Skip default flags + if ($v == '') continue; + else $sql .= "\n{$v}"; + } + + return $this->execute($sql); + } + // Type functions /** diff --git a/functions.php b/functions.php index 8f26d228..9b809209 100644 --- a/functions.php +++ b/functions.php @@ -3,7 +3,7 @@ /** * Manage functions in a database * - * $Id: functions.php,v 1.10 2003/04/30 06:49:11 chriskl Exp $ + * $Id: functions.php,v 1.11 2003/05/11 10:39:29 chriskl Exp $ */ // Include application functions @@ -168,6 +168,7 @@ if (!isset($_POST['formReturns'])) $_POST['formReturns'] = ''; if (!isset($_POST['formLanguage'])) $_POST['formLanguage'] = ''; if (!isset($_POST['formDefinition'])) $_POST['formDefinition'] = ''; + if (!isset($_POST['formProperties'])) $_POST['formProperties'] = $data->defaultprops; $types = &$localData->getTypes(true); $langs = &$localData->getLanguages(); @@ -176,45 +177,64 @@ $misc->printMsg($msg); echo "
\n"; - echo "\n"; - echo "\n"; - echo "\n"; - echo "\n"; - echo "\n"; + echo "
{$lang['strname']}{$lang['strarguments']}{$lang['strreturns']}{$lang['strlanguage']}
\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; - echo "\n"; + echo "\n"; - echo "\n"; + echo "\n"; - echo "\n"; - echo "\n"; - echo "\n"; + if (is_array($data->funcprops) && sizeof($data->funcprops) > 0) { + echo "\n"; + echo "\n"; echo "
{$lang['strname']}{$lang['strarguments']}{$lang['strreturns']}{$lang['strlanguage']}
_maxNameLen} maxlength={$data->_maxNameLen} value=\"", - htmlspecialchars($_POST['formFunction']), "\">
_maxNameLen}\" value=\"", + htmlspecialchars($_POST['formFunction']), "\" />_maxNameLen} maxlength={$data->_maxNameLen} value=\"", - htmlspecialchars($_POST['formArguments']), "\">\n"; - echo "\n"; echo "
{$lang['strdefinition']}
{$lang['strproperties']}
\n"; + $i = 0; + foreach ($data->funcprops as $k => $v) { + echo "
\n"; + $i++; + } + } + echo "
\n"; - echo "\n"; + echo "\n"; echo $misc->form; - echo " \n"; + echo "\n"; + echo "\n"; echo "
\n"; - echo "

href}\">{$lang['strshowallfunctions']}

\n"; + echo "

href}\">{$lang['strshowallfunctions']}

\n"; } /** @@ -223,11 +243,16 @@ function doSaveCreate() { global $localData, $lang; + // Set properties to an empty array if it doesn't exist (for db's without properties) + if (!is_array($_POST['formProperties'])) $_POST['formProperties'] = array(); + // Check that they've given a name and a definition if ($_POST['formFunction'] == '') doCreate($lang['strfunctionneedsname']); elseif ($_POST['formDefinition'] == '') doCreate($lang['strfunctionneedsdef']); else { - $status = $localData->createFunction($_POST['formFunction'], $_POST['formArguments'] , $_POST['formReturns'] , $_POST['formDefinition'] , $_POST['formLanguage'],0); + $status = $localData->createFunction($_POST['formFunction'], $_POST['formArguments'] , + $_POST['formReturns'] , $_POST['formDefinition'] , $_POST['formLanguage'], + $_POST['formProperties'], false); if ($status == 0) doDefault($lang['strfunctioncreated']); else -- 2.39.5