* A class that implements the DB interface for Postgres
* Note: This class uses ADODB and returns RecordSets.
*
- * $Id: Postgres73.php,v 1.175 2007/11/15 06:06:45 xzilla Exp $
+ * $Id: Postgres73.php,v 1.176 2007/11/16 18:34:24 ioguix Exp $
*/
// @@@ THOUGHT: What about inherits? ie. use of ONLY???
'schema' => array('CREATE', 'USAGE', 'ALL PRIVILEGES')
);
// Function properties
- var $funcprops = array( array('', 'VOLATILE', 'IMMUTABLE', 'STABLE'),
+ var $funcprops = array( array('', 'VOLATILE', 'IMMUTABLE', 'STABLE'),
array('', 'CALLED ON NULL INPUT', 'RETURNS NULL ON NULL INPUT'),
array('', 'SECURITY INVOKER', 'SECURITY DEFINER'));
var $defaultprops = array('', '', '');
// Select operators
- var $selectOps = array('=' => 'i', '!=' => 'i', '<' => 'i', '>' => 'i', '<=' => 'i', '>=' => 'i', '<<' => 'i', '>>' => 'i', '<<=' => 'i', '>>=' => 'i',
- 'LIKE' => 'i', 'NOT LIKE' => 'i', 'ILIKE' => 'i', 'NOT ILIKE' => 'i', 'SIMILAR TO' => 'i',
- 'NOT SIMILAR TO' => 'i', '~' => 'i', '!~' => 'i', '~*' => 'i', '!~*' => 'i',
+ var $selectOps = array('=' => 'i', '!=' => 'i', '<' => 'i', '>' => 'i', '<=' => 'i', '>=' => 'i', '<<' => 'i', '>>' => 'i', '<<=' => 'i', '>>=' => 'i',
+ 'LIKE' => 'i', 'NOT LIKE' => 'i', 'ILIKE' => 'i', 'NOT ILIKE' => 'i', 'SIMILAR TO' => 'i',
+ 'NOT SIMILAR TO' => 'i', '~' => 'i', '!~' => 'i', '~*' => 'i', '!~*' => 'i',
'IS NULL' => 'p', 'IS NOT NULL' => 'p', 'IN' => 'x', 'NOT IN' => 'x');
/**
}
// Help functions
-
+
function getHelpPages() {
include_once('./help/PostgresDoc73.php');
return $this->help_page;
}
// Schema functions
-
+
/**
* Sets the current working schema. Will also set class variable.
* @param $schema The the name of the schema to work in
}
else return $status;
}
-
+
/**
* Sets the current schema search path
* @param $paths An array of schemas in required search order
*/
function setSearchPath($paths) {
if (!is_array($paths)) return -1;
- elseif (sizeof($paths) == 0) return -2;
+ elseif (sizeof($paths) == 0) return -2;
elseif (sizeof($paths) == 1 && $paths[0] == '') {
// Need to handle empty paths in some cases
$paths[0] = 'pg_catalog';
}
-
+
// Loop over all the paths to check that none are empty
$temp = array();
foreach ($paths as $schema) {
$this->fieldArrayClean($temp);
$sql = 'SET SEARCH_PATH TO "' . implode('","', $temp) . '"';
-
+
return $this->execute($sql);
}
-
+
/**
* Return the current schema search path
* @return Array of schema names
*/
function getSearchPath() {
$sql = 'SELECT current_schemas(false) AS search_path';
-
+
return $this->phpArray($this->selectField($sql, 'search_path'));
}
$sql = "CREATE SCHEMA \"{$schemaname}\"";
if ($authorization != '') $sql .= " AUTHORIZATION \"{$authorization}\"";
-
+
if ($comment != '') {
$status = $this->beginTransaction();
if ($status != 0) return -1;
}
-
+
// Create the new schema
$status = $this->execute($sql);
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
-
+
return $this->endTransaction();
}
-
+
return 0;
}
-
+
/**
* Drops a schema.
* @param $schemaname The name of the schema to drop
*/
function dropSchema($schemaname, $cascade) {
$this->fieldClean($schemaname);
-
+
$sql = "DROP SCHEMA \"{$schemaname}\"";
if ($cascade) $sql .= " CASCADE";
-
+
return $this->execute($sql);
}
$this->fieldClean($schemaname);
$this->fieldClean($name);
$this->clean($comment);
-
+
$status = $this->beginTransaction();
if ($status != 0) {
$this->rollbackTransaction();
/**
* Returns the specified variable information.
- * @return the field
+ * @return the field
*/
function getVariable($setting) {
$sql = "SHOW $setting";
-
+
return $this->selectSet($sql);
}
-
+
/**
* Returns all available variable information.
* @return A recordset
*/
function getVariables() {
$sql = "SHOW ALL";
-
+
return $this->selectSet($sql);
}
$this->clean($database);
$sql = "SELECT * FROM pg_catalog.pg_stat_activity WHERE datname='{$database}' ORDER BY usename, procpid";
}
-
+
return $this->selectSet($sql);
}
*/
function getLocks() {
global $conf;
-
+
if (!$conf['show_system'])
$where = "AND pn.nspname NOT LIKE 'pg\\\\_%'";
- else
+ else
$where = "AND nspname !~ '^pg_t(emp_[0-9]+|oast)$'";
$sql = "SELECT pn.nspname, pc.relname AS tablename, pl.transaction, pl.pid, pl.mode, pl.granted
function hasObjectID($table) {
$this->clean($table);
- $sql = "SELECT relhasoids FROM pg_catalog.pg_class WHERE relname='{$table}'
+ $sql = "SELECT relhasoids FROM pg_catalog.pg_class WHERE relname='{$table}'
AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname='{$this->_schema}')";
$rs = $this->selectSet($sql);
if (sizeof($atts) == 0) return array();
- $sql = "SELECT attnum, attname FROM pg_catalog.pg_attribute WHERE
+ $sql = "SELECT attnum, attname FROM pg_catalog.pg_attribute WHERE
attrelid=(SELECT oid FROM pg_catalog.pg_class WHERE relname='{$table}' AND
- relnamespace=(SELECT oid FROM pg_catalog.pg_namespace WHERE nspname='{$this->_schema}'))
+ relnamespace=(SELECT oid FROM pg_catalog.pg_namespace WHERE nspname='{$this->_schema}'))
AND attnum IN ('" . join("','", $atts) . "')";
$rs = $this->selectSet($sql);
function getRowIdentifier($table) {
$oldtable = $table;
$this->clean($table);
-
+
$status = $this->beginTransaction();
if ($status != 0) return -1;
// Get the first primary or unique index (sorting primary keys first) that
// is NOT a partial index.
- $sql = "SELECT indrelid, indkey FROM pg_catalog.pg_index WHERE indisunique AND
+ $sql = "SELECT indrelid, indkey FROM pg_catalog.pg_index WHERE indisunique AND
indrelid=(SELECT oid FROM pg_catalog.pg_class WHERE relname='{$table}' AND
relnamespace=(SELECT oid FROM pg_catalog.pg_namespace WHERE nspname='{$this->_schema}'))
AND indpred='' AND indproc='-' ORDER BY indisprimary DESC LIMIT 1";
// If none, check for an OID column. Even though OIDs can be duplicated, the edit and delete row
// functions check that they're only modiying a single row. Otherwise, return empty array.
- if ($rs->recordCount() == 0) {
+ if ($rs->recordCount() == 0) {
// Check for OID column
$temp = array();
if ($this->hasObjectID($table)) {
$this->endTransaction();
return $attnames;
}
- }
+ }
}
/**
*/
function getTable($table) {
$this->clean($table);
-
+
$sql = "
SELECT
c.relname, u.usename AS relowner,
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
AND n.nspname = '{$this->_schema}'
- AND c.relname = '{$table}'";
-
+ AND c.relname = '{$table}'";
+
return $this->selectSet($sql);
}
-
+
/**
* Return all tables in current database (and schema)
* @param $all True to fetch all tables, false for just in current schema
- * @return All tables, sorted alphabetically
+ * @return All tables, sorted alphabetically
*/
function getTables($all = false) {
if ($all) {
// Exclude pg_catalog and information_schema tables
$sql = "SELECT schemaname AS nspname, tablename AS relname, tableowner AS relowner
- FROM pg_catalog.pg_tables
+ FROM pg_catalog.pg_tables
WHERE schemaname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
ORDER BY schemaname, tablename";
} else {
- $sql = "SELECT c.relname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner,
+ $sql = "SELECT c.relname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner,
pg_catalog.obj_description(c.oid, 'pg_class') AS relcomment,
reltuples::bigint
FROM pg_catalog.pg_class c
if ($field == '') {
// This query is made much more complex by the addition of the 'attisserial' field.
- // The subquery to get that field checks to see if there is an internally dependent
+ // The subquery to get that field checks to see if there is an internally dependent
// sequence on the field.
$sql = "
SELECT
a.attname,
- pg_catalog.format_type(a.atttypid, a.atttypmod) as type,
+ pg_catalog.format_type(a.atttypid, a.atttypmod) as type,
a.atttypmod,
a.attnotnull, a.atthasdef, adef.adsrc,
a.attstattarget, a.attstorage, t.typstorage,
(
SELECT 1 FROM pg_catalog.pg_depend pd, pg_catalog.pg_class pc
- WHERE pd.objid=pc.oid
- AND pd.classid=pc.tableoid
+ WHERE pd.objid=pc.oid
+ AND pd.classid=pc.tableoid
AND pd.refclassid=pc.tableoid
AND pd.refobjid=a.attrelid
AND pd.refobjsubid=a.attnum
AND pd.deptype='i'
AND pc.relkind='S'
) IS NOT NULL AS attisserial,
- pg_catalog.col_description(a.attrelid, a.attnum) AS comment
+ pg_catalog.col_description(a.attrelid, a.attnum) AS comment
FROM
pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_attrdef adef
ON a.attrelid=adef.adrelid
AND a.attnum=adef.adnum
LEFT JOIN pg_catalog.pg_type t ON a.atttypid=t.oid
- WHERE
+ WHERE
a.attrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname='{$table}'
AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE
nspname = '{$this->_schema}'))
$sql = "
SELECT
a.attname,
- pg_catalog.format_type(a.atttypid, a.atttypmod) as type,
+ pg_catalog.format_type(a.atttypid, a.atttypmod) as type,
pg_catalog.format_type(a.atttypid, NULL) as base_type,
a.atttypmod,
a.attnotnull, a.atthasdef, adef.adsrc,
$this->fieldClean($column);
$sql = "ALTER TABLE \"{$table}\" DROP COLUMN \"{$column}\"";
- if ($cascade) $sql .= " CASCADE";
+ if ($cascade) $sql .= " CASCADE";
return $this->execute($sql);
}
}
// Inheritance functions
-
+
/**
* Finds the names and schemas of parent tables (in order)
* @param $table The table to find the parents for
*/
function getTableParents($table) {
$this->clean($table);
-
+
$sql = "
- SELECT
+ SELECT
pn.nspname, relname
FROM
pg_catalog.pg_class pc, pg_catalog.pg_inherits pi, pg_catalog.pg_namespace pn
ORDER BY
pi.inhseqno
";
-
- return $this->selectSet($sql);
- }
+
+ return $this->selectSet($sql);
+ }
/**
*/
function getTableChildren($table) {
$this->clean($table);
-
+
$sql = "
- SELECT
+ SELECT
pn.nspname, relname
FROM
pg_catalog.pg_class pc, pg_catalog.pg_inherits pi, pg_catalog.pg_namespace pn
AND pi.inhparent = (SELECT oid from pg_catalog.pg_class WHERE relname='{$table}'
AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = '{$this->_schema}'))
";
-
- return $this->selectSet($sql);
- }
+
+ return $this->selectSet($sql);
+ }
// View functions
-
+
/**
* Returns a list of all views in the database
* @return All views
*/
function getViews() {
- $sql = "SELECT c.relname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner,
+ $sql = "SELECT c.relname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner,
pg_catalog.obj_description(c.oid, 'pg_class') AS relcomment
FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace)
WHERE (n.nspname='{$this->_schema}') AND (c.relkind = 'v'::\"char\") ORDER BY relname";
return $this->selectSet($sql);
}
-
+
/**
* Returns all details for a particular view
* @param $view The name of the view to retrieve
function getView($view) {
$this->clean($view);
- $sql = "SELECT c.relname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner,
+ $sql = "SELECT c.relname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner,
pg_catalog.pg_get_viewdef(c.oid) AS vwdefinition, pg_catalog.obj_description(c.oid, 'pg_class') AS relcomment
FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace)
WHERE (c.relname = '$view')
AND n.nspname='{$this->_schema}'";
-
+
return $this->selectSet($sql);
}
-
+
/**
* Updates a view.
* @param $viewname The name fo the view to update
// Sequence functions
- /**
+ /**
* Resets a given sequence to min value of sequence
* @param $sequence Sequence name
* @return 0 success
/* This double-cleaning is deliberate */
$this->fieldClean($sequence);
$this->clean($sequence);
-
+
$sql = "SELECT pg_catalog.SETVAL('\"{$sequence}\"', {$minvalue})";
-
+
return $this->execute($sql);
}
- /**
+ /**
* Execute nextval on a given sequence
* @param $sequence Sequence name
* @return 0 success
$this->clean($sequence);
$sql = "SELECT pg_catalog.NEXTVAL('\"{$sequence}\"')";
-
+
return $this->execute($sql);
}
- /**
+ /**
* Execute setval on a given sequence
* @param $sequence Sequence name
* @param $nextvalue The next value
$this->clean($nextvalue);
$sql = "SELECT pg_catalog.SETVAL('\"{$sequence}\"', '{$nextvalue}')";
-
+
return $this->execute($sql);
}
$sql = "SELECT n.nspname, c.relname AS seqname, u.usename AS seqowner
FROM pg_catalog.pg_class c, pg_catalog.pg_user u, pg_catalog.pg_namespace n
WHERE c.relowner=u.usesysid AND c.relnamespace=n.oid
- AND c.relkind = 'S'
- AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
+ AND c.relkind = 'S'
+ AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
ORDER BY nspname, seqname";
} else {
$sql = "SELECT c.relname AS seqname, u.usename AS seqowner, pg_catalog.obj_description(c.oid, 'pg_class') AS seqcomment
WHERE c.relowner=u.usesysid AND c.relnamespace=n.oid
AND c.relkind = 'S' AND n.nspname='{$this->_schema}' ORDER BY seqname";
}
-
+
return $this->selectSet( $sql );
}
*/
function getSequence($sequence) {
$this->fieldClean($sequence);
-
+
$sql = "SELECT c.relname AS seqname, s.*,
pg_catalog.obj_description(s.tableoid, 'pg_class') AS seqcomment,
u.usename AS seqowner
FROM \"{$sequence}\" AS s, pg_catalog.pg_class c, pg_catalog.pg_user u, pg_catalog.pg_namespace n
WHERE c.relowner=u.usesysid AND c.relnamespace=n.oid
AND c.relname = '{$sequence}' AND c.relkind = 'S' AND n.nspname='{$this->_schema}'";
-
+
return $this->selectSet( $sql );
}
*/
function getIndexes($table = '', $unique = false) {
$this->clean($table);
-
+
$sql = "SELECT c2.relname AS indname, i.indisprimary, i.indisunique, i.indisclustered,
pg_catalog.pg_get_indexdef(i.indexrelid) AS inddef,
pg_catalog.obj_description(c.oid, 'pg_index') AS idxcomment
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i
- WHERE c.relname = '{$table}' AND pg_catalog.pg_table_is_visible(c.oid)
+ WHERE c.relname = '{$table}' AND pg_catalog.pg_table_is_visible(c.oid)
AND c.oid = i.indrelid AND i.indexrelid = c2.oid
";
if ($unique) $sql .= " AND i.indisunique ";
function getTriggers($table = '') {
$this->clean($table);
- $sql = "SELECT t.tgname, t.tgisconstraint, t.tgdeferrable, t.tginitdeferred, t.tgtype,
- t.tgargs, t.tgnargs, t.tgconstrrelid,
+ $sql = "SELECT t.tgname, t.tgisconstraint, t.tgdeferrable, t.tginitdeferred, t.tgtype,
+ t.tgargs, t.tgnargs, t.tgconstrrelid,
(SELECT relname FROM pg_catalog.pg_class c2 WHERE c2.oid=t.tgconstrrelid) AS tgconstrrelname,
- p.proname AS tgfname, c.relname, NULL AS tgdef,
+ p.proname AS tgfname, c.relname, NULL AS tgdef,
p.proname || ' (' || pg_catalog.oidvectortypes(p.proargtypes) || ')' AS proproto,
ns.nspname AS pronamespace
FROM pg_catalog.pg_namespace ns,
return $this->selectSet($sql);
}
-
+
/**
* Alters a trigger
* @param $table The name of the table containing the trigger
$this->fieldClean($table);
$this->fieldClean($trigger);
$this->fieldClean($name);
-
+
$sql = "ALTER TRIGGER \"{$trigger}\" ON \"{$table}\" RENAME TO \"{$name}\"";
-
+
return $this->execute($sql);
- }
+ }
// Function functions
/**
* Returns a list of all functions in the database
* @param $all If true, will find all available functions, if false just those in search path
- * @param $type If not null, will find all functions with return value = type
+ * @param $type If not null, will find all functions with return value = type
*
* @return All functions
*/
if ($all) {
$where = 'pg_catalog.pg_function_is_visible(p.oid)';
$distinct = 'DISTINCT ON (p.proname)';
-
+
if ($type) {
$where .= " AND p.prorettype = (select oid from pg_catalog.pg_type p where p.typname = 'trigger') ";
}
*/
function getFunction($function_oid) {
$this->clean($function_oid);
-
- $sql = "SELECT
+
+ $sql = "SELECT
pc.oid AS prooid,
proname,
+ pg_catalog.pg_get_userbyid(proowner) AS proowner,
+ nspname as proschema,
lanname as prolanguage,
pg_catalog.format_type(prorettype, NULL) as proresult,
prosrc,
pg_catalog.oidvectortypes(pc.proargtypes) AS proarguments,
pg_catalog.obj_description(pc.oid, 'pg_proc') AS procomment
FROM
- pg_catalog.pg_proc pc, pg_catalog.pg_language pl
- WHERE
+ pg_catalog.pg_proc pc, pg_catalog.pg_language pl, pg_catalog.pg_namespace n
+ WHERE
pc.oid = '$function_oid'::oid
- AND pc.prolang = pl.oid
+ AND pc.prolang = pl.oid
+ AND n.oid = pc.pronamespace
";
-
+
return $this->selectSet($sql);
}
-
- /**
+
+ /**
* Returns an array containing a function's properties
* @param $f The array of data for the function
* @return An array containing the properties
*/
function getFunctionProperties($f) {
$temp = array();
-
+
// Volatility
if ($f['provolatile'] == 'v')
$temp[] = 'VOLATILE';
$temp[] = 'STABLE';
else
return -1;
-
+
// Null handling
$f['proisstrict'] = $this->phpBool($f['proisstrict']);
if ($f['proisstrict'])
$temp[] = 'RETURNS NULL ON NULL INPUT';
else
$temp[] = 'CALLED ON NULL INPUT';
-
+
// Security
$f['prosecdef'] = $this->phpBool($f['prosecdef']);
if ($f['prosecdef'])
$temp[] = 'SECURITY DEFINER';
else
$temp[] = 'SECURITY INVOKER';
-
+
return $temp;
- }
+ }
/**
* Returns a list of all functions that can be used in triggers
$sql = "CREATE";
if ($replace) $sql .= " OR REPLACE";
$sql .= " FUNCTION \"{$funcname}\" (";
-
+
if ($args != '')
$sql .= $args;
$sql .= ") RETURNS ";
if ($setof) $sql .= "SETOF ";
$sql .= "{$returns} AS ";
-
+
if (is_array($definition)) {
$this->arrayClean($definition);
$sql .= "'" . $definition[0] . "'";
$this->clean($definition);
$sql .= "'" . $definition . "'";
}
-
+
$sql .= " LANGUAGE \"{$language}\"";
-
+
// Add flags
foreach ($flags as $v) {
// Skip default flags
return $this->execute($sql);
}
-
+
// Type functions
/**
// Create domain filter
if (!$domains)
$where .= " AND t.typtype != 'd'";
-
+
$sql = "SELECT
t.typname AS basename,
pg_catalog.format_type(t.oid, NULL) AS typname,
FROM (pg_catalog.pg_type t
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace)
LEFT JOIN pg_catalog.pg_user pu ON t.typowner = pu.usesysid
- WHERE (t.typrelid = 0 OR (SELECT c.relkind IN ({$tqry}) FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid {$where2}))
+ WHERE (t.typrelid = 0 OR (SELECT c.relkind IN ({$tqry}) FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid {$where2}))
AND t.typname !~ '^_'
- AND {$where}
+ AND {$where}
ORDER BY typname
";
// If not the first column, add a comma
if (!$first) $sql .= ", ";
else $first = false;
-
+
switch ($type[$i]) {
// Have to account for weird placing of length for with/without
// time zone types
$found = true;
}
-
+
if (!$found) return -1;
-
+
$sql .= ")";
-
+
$status = $this->execute($sql);
if ($status) {
$this->rollbackTransaction();
}
}
return $this->endTransaction();
-
+
}
-
+
// Rule functions
-
+
/**
* Removes a rule from a table OR view
* @param $rule The rule to drop
function getRules($table) {
$this->clean($table);
- $sql = "SELECT
+ $sql = "SELECT
*
- FROM
+ FROM
pg_catalog.pg_rules
WHERE
schemaname='{$this->_schema}'
return $this->selectSet($sql);
}
-
+
/**
* Edits a rule on a table OR view
* @param $name The name of the new rule
global $data;
$data->clean($table);
-
+
// get the max number of col used in a constraint for the table
$sql = "SELECT DISTINCT
max(SUBSTRING(array_dims(c.conkey) FROM '^\\\[.*:(.*)\\\]$')) as nb
$sql = '
SELECT
- c.contype, c.conname, pg_catalog.pg_get_constraintdef(c.oid) AS consrc,
+ c.contype, c.conname, pg_catalog.pg_get_constraintdef(c.oid) AS consrc,
ns1.nspname as p_schema, r1.relname as p_table, ns2.nspname as f_schema,
r2.relname as f_table, f1.attname as p_field, f2.attname as f_field,
pg_catalog.obj_description(c.oid, \'pg_constraint\') AS constcomment
LEFT JOIN (
pg_catalog.pg_class AS r2 JOIN pg_catalog.pg_namespace AS ns2 ON (r2.relnamespace=ns2.oid)
) ON (c.confrelid=r2.oid)
- LEFT JOIN pg_catalog.pg_attribute AS f2 ON
+ LEFT JOIN pg_catalog.pg_attribute AS f2 ON
(f2.attrelid=r2.oid AND ((c.confkey[1]=f2.attnum AND c.conkey[1]=f1.attnum)';
for ($i = 2; $i <= $rs->fields['nb']; $i++)
$sql.= "OR (c.confkey[$i]=f2.attnum AND c.conkey[$i]=f1.attnum)";
$sql .= sprintf("))
- WHERE
+ WHERE
r1.relname = '%s' AND ns1.nspname='%s'
ORDER BY 1", $table, $this->_schema);
function getLinkingKeys($tables) {
if (!is_array($tables)) return -1;
-
+
$tables_list = "'{$tables[0]['tablename']}'";
$schema_list = "'{$tables[0]['schemaname']}'";
$schema_tables_list = "'{$tables[0]['schemaname']}.{$tables[0]['tablename']}'";
AND (pc.conrelid = pgc1.relfilenode OR pc.confrelid = pgc1.relfilenode)
AND pgc1.relname IN ($tables_list)
";
-
+
//parse our output to find the highest dimension of foreign keys since pc.conkey is stored in an array
$rs = $this->selectSet($sql);
while (!$rs->EOF) {
$maxDimension = $tmpDimension > $maxDimension ? $tmpDimension : $maxDimension;
$rs->MoveNext();
}
-
+
//we know the highest index for foreign keys that conkey goes up to, expand for us in an IN query
$cons_str = '( (pfield.attnum = conkey[1] AND cfield.attnum = confkey[1]) ';
for ($i = 2; $i <= $maxDimension; $i++) {
$cons_str .= "OR (pfield.attnum = conkey[{$i}] AND cfield.attnum = confkey[{$i}]) ";
}
$cons_str .= ') ';
-
+
$sql = "
SELECT
pgc1.relname AS p_table,
AND cfield.attrelid = pc.confrelid
AND $cons_str
AND pgns1.nspname || '.' || pgc1.relname IN ($schema_tables_list)
- AND pgns2.nspname || '.' || pgc2.relname IN ($schema_tables_list)
- ";
+ AND pgns2.nspname || '.' || pgc2.relname IN ($schema_tables_list)
+ ";
return $this->selectSet($sql);
}
elseif ($acl == '' || $acl == null) return array();
else return $this->_parseACL($acl);
}
-
+
// Domain functions
-
+
/**
* Gets all information for a single domain
* @param $domain The name of the domain to fetch
*/
function getDomain($domain) {
$this->clean($domain);
-
+
$sql = "
SELECT
- t.typname AS domname,
+ t.typname AS domname,
pg_catalog.format_type(t.typbasetype, t.typtypmod) AS domtype,
t.typnotnull AS domnotnull,
t.typdefault AS domdef,
pg_catalog.pg_get_userbyid(t.typowner) AS domowner,
pg_catalog.obj_description(t.oid, 'pg_type') AS domcomment
- FROM
+ FROM
pg_catalog.pg_type t
- WHERE
+ WHERE
t.typtype = 'd'
AND t.typname = '{$domain}'
AND t.typnamespace = (SELECT oid FROM pg_catalog.pg_namespace
WHERE nspname = '{$this->_schema}')";
- return $this->selectSet($sql);
+ return $this->selectSet($sql);
}
-
+
/**
* Return all domains in current schema. Excludes domain constraints.
- * @return All tables, sorted alphabetically
+ * @return All tables, sorted alphabetically
*/
function getDomains() {
- $sql = "
+ $sql = "
SELECT
- t.typname AS domname,
+ t.typname AS domname,
pg_catalog.format_type(t.typbasetype, t.typtypmod) AS domtype,
t.typnotnull AS domnotnull,
t.typdefault AS domdef,
pg_catalog.pg_get_userbyid(t.typowner) AS domowner,
pg_catalog.obj_description(t.oid, 'pg_type') AS domcomment
- FROM
+ FROM
pg_catalog.pg_type t
- WHERE
+ WHERE
t.typtype = 'd'
AND t.typnamespace = (SELECT oid FROM pg_catalog.pg_namespace
WHERE nspname='{$this->_schema}')
* @param $length Optional type length
* @param $array True for array type, false otherwise
* @param $notnull True for NOT NULL, false otherwise
- * @param $default Default value for domain
+ * @param $default Default value for domain
* @param $check A CHECK constraint if there is one
* @return 0 success
*/
function createDomain($domain, $type, $length, $array, $notnull, $default, $check) {
$this->fieldClean($domain);
-
+
$sql = "CREATE DOMAIN \"{$domain}\" AS ";
if ($length == '')
$sql .= "{$type}({$length})";
}
}
-
+
// Add array qualifier, if requested
if ($array) $sql .= '[]';
-
+
if ($notnull) $sql .= ' NOT NULL';
if ($default != '') $sql .= " DEFAULT {$default}";
if ($this->hasDomainConstraints() && $check != '') $sql .= " CHECK ({$check})";
return $this->execute($sql);
}
-
+
/**
* Drops a domain.
* @param $domain The name of the domain to drop
if ($cascade) $sql .= " CASCADE";
return $this->execute($sql);
- }
-
+ }
+
// Find object functions
-
+
/**
* Searches all system catalogs to find objects that match a certain name.
* @param $term The search term
$where = '';
$lan_where = '';
}
-
+
// Apply outer filter
$sql = '';
if ($filter != '') {
$sql = "SELECT * FROM (";
}
-
+
$sql .= "
- SELECT 'SCHEMA' AS type, oid, NULL AS schemaname, NULL AS relname, nspname AS name
+ SELECT 'SCHEMA' AS type, oid, NULL AS schemaname, NULL AS relname, nspname AS name
FROM pg_catalog.pg_namespace pn WHERE nspname ILIKE '%{$term}%' {$where}
UNION ALL
SELECT CASE WHEN relkind='r' THEN 'TABLE' WHEN relkind='v' THEN 'VIEW' WHEN relkind='S' THEN 'SEQUENCE' END, pc.oid,
- pn.nspname, NULL, pc.relname FROM pg_catalog.pg_class pc, pg_catalog.pg_namespace pn
+ pn.nspname, NULL, pc.relname FROM pg_catalog.pg_class pc, pg_catalog.pg_namespace pn
WHERE pc.relnamespace=pn.oid AND relkind IN ('r', 'v', 'S') AND relname ILIKE '%{$term}%' {$where}
UNION ALL
SELECT CASE WHEN pc.relkind='r' THEN 'COLUMNTABLE' ELSE 'COLUMNVIEW' END, NULL, pn.nspname, pc.relname, pa.attname FROM pg_catalog.pg_class pc, pg_catalog.pg_namespace pn,
- pg_catalog.pg_attribute pa WHERE pc.relnamespace=pn.oid AND pc.oid=pa.attrelid
+ pg_catalog.pg_attribute pa WHERE pc.relnamespace=pn.oid AND pc.oid=pa.attrelid
AND pa.attname ILIKE '%{$term}%' AND pa.attnum > 0 AND NOT pa.attisdropped AND pc.relkind IN ('r', 'v') {$where}
UNION ALL
- SELECT 'FUNCTION', pp.oid, pn.nspname, NULL, pp.proname || '(' || pg_catalog.oidvectortypes(pp.proargtypes) || ')' FROM pg_catalog.pg_proc pp, pg_catalog.pg_namespace pn
+ SELECT 'FUNCTION', pp.oid, pn.nspname, NULL, pp.proname || '(' || pg_catalog.oidvectortypes(pp.proargtypes) || ')' FROM pg_catalog.pg_proc pp, pg_catalog.pg_namespace pn
WHERE pp.pronamespace=pn.oid AND NOT pp.proisagg AND pp.proname ILIKE '%{$term}%' {$where}
UNION ALL
SELECT 'INDEX', NULL, pn.nspname, pc.relname, pc2.relname FROM pg_catalog.pg_class pc, pg_catalog.pg_namespace pn,
- pg_catalog.pg_index pi, pg_catalog.pg_class pc2 WHERE pc.relnamespace=pn.oid AND pc.oid=pi.indrelid
+ pg_catalog.pg_index pi, pg_catalog.pg_class pc2 WHERE pc.relnamespace=pn.oid AND pc.oid=pi.indrelid
AND pi.indexrelid=pc2.oid
AND NOT EXISTS (
SELECT 1 FROM pg_catalog.pg_depend d JOIN pg_catalog.pg_constraint c
if ($conf['show_advanced']) {
$sql .= "
UNION ALL
- SELECT CASE WHEN pt.typtype='d' THEN 'DOMAIN' ELSE 'TYPE' END, pt.oid, pn.nspname, NULL,
- pt.typname FROM pg_catalog.pg_type pt, pg_catalog.pg_namespace pn
+ SELECT CASE WHEN pt.typtype='d' THEN 'DOMAIN' ELSE 'TYPE' END, pt.oid, pn.nspname, NULL,
+ pt.typname FROM pg_catalog.pg_type pt, pg_catalog.pg_namespace pn
WHERE pt.typnamespace=pn.oid AND typname ILIKE '%{$term}%'
AND (pt.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = pt.typrelid))
{$where}
UNION ALL
- SELECT 'OPERATOR', po.oid, pn.nspname, NULL, po.oprname FROM pg_catalog.pg_operator po, pg_catalog.pg_namespace pn
+ SELECT 'OPERATOR', po.oid, pn.nspname, NULL, po.oprname FROM pg_catalog.pg_operator po, pg_catalog.pg_namespace pn
WHERE po.oprnamespace=pn.oid AND oprname ILIKE '%{$term}%' {$where}
UNION ALL
SELECT 'CONVERSION', pc.oid, pn.nspname, NULL, pc.conname FROM pg_catalog.pg_conversion pc,
else {
$sql .= "
UNION ALL
- SELECT 'DOMAIN', pt.oid, pn.nspname, NULL,
- pt.typname FROM pg_catalog.pg_type pt, pg_catalog.pg_namespace pn
+ SELECT 'DOMAIN', pt.oid, pn.nspname, NULL,
+ pt.typname FROM pg_catalog.pg_type pt, pg_catalog.pg_namespace pn
WHERE pt.typnamespace=pn.oid AND pt.typtype='d' AND typname ILIKE '%{$term}%'
AND (pt.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = pt.typrelid))
{$where}
$sql .= "ORDER BY type, schemaname, relname, name";
return $this->selectSet($sql);
- }
+ }
// Operator functions
-
+
/**
* Returns a list of all operators in the database
* @return All operators
- */
+ */
function getOperators() {
// We stick with the subselects here, as you cannot ORDER BY a regtype
$sql = "
";
return $this->selectSet($sql);
- }
+ }
/**
* Returns all details for a particular operator
WHERE
po.oid='{$operator_oid}'
";
-
+
return $this->selectSet($sql);
}
// Cast functions
-
+
/**
* Returns a list of all casts in the database
* @return All casts
- */
+ */
function getCasts() {
global $conf;
-
+
if ($conf['show_system'])
$where = '';
else
pg_catalog.pg_type t1,
pg_catalog.pg_type t2,
pg_catalog.pg_namespace n1,
- pg_catalog.pg_namespace n2
+ pg_catalog.pg_namespace n2
WHERE
c.castsource=t1.oid
AND c.casttarget=t2.oid
";
return $this->selectSet($sql);
- }
+ }
// Conversion functions
-
+
/**
* Returns a list of all conversions in the database
* @return All conversions
- */
+ */
function getConversions() {
$sql = "
SELECT
return $this->selectSet($sql);
}
-
+
// Language functions
-
+
/**
* Gets all languages
* @param $all True to get all languages, regardless of show_system
*/
function getLanguages($all = false) {
global $conf;
-
+
if ($conf['show_system'] || $all)
$where = '';
else
ORDER BY
lanname
";
-
+
return $this->selectSet($sql);
}
function getAggregate($name, $basetype) {
$this->fieldclean($name);
$this->fieldclean($basetype);
-
+
$sql = "SELECT p.proname, CASE p.proargtypes[0] WHEN 'pg_catalog.\"any\"'::pg_catalog.regtype THEN NULL ELSE
- pg_catalog.format_type(p.proargtypes[0], NULL) END AS proargtypes, a.aggtransfn,
- format_type(a.aggtranstype, NULL) AS aggstype, a.aggfinalfn, a.agginitval, a.aggsortop, u.usename,
+ pg_catalog.format_type(p.proargtypes[0], NULL) END AS proargtypes, a.aggtransfn,
+ format_type(a.aggtranstype, NULL) AS aggstype, a.aggfinalfn, a.agginitval, a.aggsortop, u.usename,
pg_catalog.obj_description(p.oid, 'pg_proc') AS aggrcomment
- FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n, pg_catalog.pg_user u, pg_catalog.pg_aggregate a
- WHERE n.oid = p.pronamespace AND p.proowner=u.usesysid AND p.oid=a.aggfnoid
- AND p.proisagg AND n.nspname='{$this->_schema}'
- AND p.proname='" . $name . "' AND CASE p.proargtypes[0] WHEN 'pg_catalog.\"any\"'::pg_catalog.regtype
+ FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n, pg_catalog.pg_user u, pg_catalog.pg_aggregate a
+ WHERE n.oid = p.pronamespace AND p.proowner=u.usesysid AND p.oid=a.aggfnoid
+ AND p.proisagg AND n.nspname='{$this->_schema}'
+ AND p.proname='" . $name . "' AND CASE p.proargtypes[0] WHEN 'pg_catalog.\"any\"'::pg_catalog.regtype
THEN NULL ELSE pg_catalog.format_type(p.proargtypes[0], NULL) END ='" . $basetype . "'";
return $this->selectSet($sql);
*/
function getAggregates() {
$sql = "SELECT p.proname, CASE p.proargtypes[0] WHEN 'pg_catalog.\"any\"'::pg_catalog.regtype THEN NULL ELSE
- pg_catalog.format_type(p.proargtypes[0], NULL) END AS proargtypes, a.aggtransfn, u.usename,
+ pg_catalog.format_type(p.proargtypes[0], NULL) END AS proargtypes, a.aggtransfn, u.usename,
pg_catalog.obj_description(p.oid, 'pg_proc') AS aggrcomment
- FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n, pg_catalog.pg_user u, pg_catalog.pg_aggregate a
- WHERE n.oid = p.pronamespace AND p.proowner=u.usesysid AND p.oid=a.aggfnoid
+ FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n, pg_catalog.pg_user u, pg_catalog.pg_aggregate a
+ WHERE n.oid = p.pronamespace AND p.proowner=u.usesysid AND p.oid=a.aggfnoid
AND p.proisagg AND n.nspname='{$this->_schema}' ORDER BY 1, 2";
return $this->selectSet($sql);
}
// Operator Class functions
-
+
/**
* Gets all opclasses
* @return A recordset
$temp .= $query;
return $temp;
}
-
+
// Capabilities
function hasSchemas() { return true; }
function hasConversions() { return true; }
function hasConstraintsInfo() { return true; }
function hasViewColumnRename() { return true; }
function hasUserAndDbVariables() { return true; }
- function hasCompositeTypes() { return true; }
+ function hasCompositeTypes() { return true; }
function hasFuncPrivs() { return true; }
function hasLocksView() { return true; }
-
+
}
?>
/**
* PostgreSQL 8.0 support
*
- * $Id: Postgres80.php,v 1.25 2007/11/15 23:09:21 xzilla Exp $
+ * $Id: Postgres80.php,v 1.26 2007/11/16 18:34:24 ioguix Exp $
*/
include_once('./classes/database/Postgres74.php');
}
// Help functions
-
+
function getHelpPages() {
include_once('./help/PostgresDoc80.php');
return $this->help_page;
*/
function getDatabases($currentdatabase = NULL) {
global $conf, $misc;
-
+
$server_info = $misc->getServerInfo();
-
+
if (isset($conf['owned_only']) && $conf['owned_only'] && !$this->isSuperUser($server_info['username'])) {
$username = $server_info['username'];
$this->clean($username);
$this->clean($newName);
$this->clean($newOwner);
//ignore $comment, not supported pre 8.2
-
+
$status = $this->beginTransaction();
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
-
+
if ($dbName != $newName) {
$status = $this->alterDatabaseRename($dbName, $newName);
if ($status != 0) {
return -3;
}
}
-
+
$status = $this->alterDatabaseOwner($newName, $newOwner);
if ($status != 0) {
$this->rollbackTransaction();
function alterDatabaseOwner($dbName, $newOwner) {
$this->clean($dbName);
$this->clean($newOwner);
-
+
$sql = "ALTER DATABASE \"{$dbName}\" OWNER TO \"{$newOwner}\"";
return $this->execute($sql);
}
$default = pg_parameter_status($this->conn->_connectionID, 'default_with_oids');
if ($default !== false) return $default;
}
-
+
$sql = "SHOW default_with_oids";
-
+
return $this->selectField($sql, 'default_with_oids');
}
-
+
// Table functions
-
+
/**
* Return all tables in current database (and schema)
* @param $all True to fetch all tables, false for just in current schema
- * @return All tables, sorted alphabetically
+ * @return All tables, sorted alphabetically
*/
function getTables($all = false) {
if ($all) {
// Exclude pg_catalog and information_schema tables
$sql = "SELECT schemaname AS nspname, tablename AS relname, tableowner AS relowner
- FROM pg_catalog.pg_tables
+ FROM pg_catalog.pg_tables
WHERE schemaname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
ORDER BY schemaname, tablename";
} else {
- $sql = "SELECT c.relname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner,
+ $sql = "SELECT c.relname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner,
pg_catalog.obj_description(c.oid, 'pg_class') AS relcomment,
reltuples::bigint,
(SELECT spcname FROM pg_catalog.pg_tablespace pt WHERE pt.oid=c.reltablespace) AS tablespace
WHERE c.relkind = 'r'
AND nspname='{$this->_schema}'
ORDER BY c.relname";
- }
+ }
return $this->selectSet($sql);
- }
+ }
/**
* Returns table information
*/
function getTable($table) {
$this->clean($table);
-
+
$sql = "
SELECT
c.relname, u.usename AS relowner,
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
AND n.nspname = '{$this->_schema}'
- AND c.relname = '{$table}'";
-
+ AND c.relname = '{$table}'";
+
return $this->selectSet($sql);
}
-
+
/**
* Alters a column in a table
* @param $table The table in which the column resides
* @return -4 comment error
* @return -6 transaction error
*/
- function alterColumn($table, $column, $name, $notnull, $oldnotnull, $default, $olddefault,
+ function alterColumn($table, $column, $name, $notnull, $oldnotnull, $default, $olddefault,
$type, $length, $array, $oldtype, $comment) {
$this->fieldClean($table);
$this->fieldClean($column);
if ($notnull != $oldnotnull) {
$sql .= "ALTER TABLE \"{$table}\" ALTER COLUMN \"{$column}\" " . (($notnull) ? 'SET' : 'DROP') . " NOT NULL";
}
-
+
// Add default, if it has changed
if ($default != $olddefault) {
if ($default == '') {
$sql .= "ALTER COLUMN \"{$column}\" SET DEFAULT {$default}";
}
}
-
+
// Add type, if it has changed
if ($length == '')
$ftype = $type;
$ftype = "{$type}({$length})";
}
}
-
+
// Add array qualifier, if requested
if ($array) $ftype .= '[]';
-
+
if ($ftype != $oldtype) {
if ($sql == '') $sql = "ALTER TABLE \"{$table}\" ";
else $sql .= ", ";
$this->rollbackTransaction();
return -6;
}
-
+
// Attempt to process the batch alteration, if anything has been changed
if ($sql != '') {
$status = $this->execute($sql);
return -1;
}
}
-
+
// Update the comment on the column
$status = $this->setComment('COLUMN', $column, $table, $comment);
if ($status != 0) {
$sql = "SELECT n.nspname, c.relname AS seqname, u.usename AS seqowner
FROM pg_catalog.pg_class c, pg_catalog.pg_user u, pg_catalog.pg_namespace n
WHERE c.relowner=u.usesysid AND c.relnamespace=n.oid
- AND c.relkind = 'S'
- AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
+ AND c.relkind = 'S'
+ AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
ORDER BY nspname, seqname";
} else {
$sql = "SELECT c.relname AS seqname, u.usename AS seqowner, pg_catalog.obj_description(c.oid, 'pg_class') AS seqcomment,
WHERE c.relowner=u.usesysid AND c.relnamespace=n.oid
AND c.relkind = 'S' AND n.nspname='{$this->_schema}' ORDER BY seqname";
}
-
+
return $this->selectSet( $sql );
}
-
+
// Tablespace functions
-
+
/**
* Retrieves information for all tablespaces
* @param $all Include all tablespaces (necessary when moving objects back to the default space)
*/
function getTablespaces($all = false) {
global $conf;
-
+
$sql = "SELECT spcname, pg_catalog.pg_get_userbyid(spcowner) AS spcowner, spclocation
FROM pg_catalog.pg_tablespace";
-
+
if (!$conf['show_system'] && !$all) {
$sql .= " WHERE spcname NOT LIKE 'pg\\\\_%'";
}
-
+
$sql .= " ORDER BY spcname";
-
+
return $this->selectSet($sql);
}
-
+
/**
* Retrieves a tablespace's information
* @return A recordset
*/
function getTablespace($spcname) {
$this->clean($spcname);
-
+
$sql = "SELECT spcname, pg_catalog.pg_get_userbyid(spcowner) AS spcowner, spclocation
FROM pg_catalog.pg_tablespace WHERE spcname='{$spcname}'";
-
+
return $this->selectSet($sql);
}
-
+
/**
* Creates a tablespace
* @param $spcname The name of the tablespace to create
$this->fieldClean($spcname);
$this->clean($spcloc);
$this->clean($comment);
-
+
$sql = "CREATE TABLESPACE \"{$spcname}\"";
-
+
if ($spcowner != '') {
$this->fieldClean($spcowner);
$sql .= " OWNER \"{$spcowner}\"";
}
-
+
$sql .= " LOCATION '{$spcloc}'";
$status = $this->execute($sql);
if ($status != 0) return -1;
- if ($comment != '' && $this->hasSharedComments()) {
+ if ($comment != '' && $this->hasSharedComments()) {
$status = $this->setComment('TABLESPACE',$spcname,'',$comment);
if ($status != 0) return -2;
}
return 0;
-
+
}
/**
}
// Set comment if it has changed
- if (trim($comment) != '' && $this->hasSharedComments()) {
+ if (trim($comment) != '' && $this->hasSharedComments()) {
$status = $this->setComment('TABLESPACE',$spcname,'',$comment);
if ($status != 0) return -4;
}
-
+
return $this->endTransaction();
}
-
+
// Backend process signalling functions
-
+
/**
* Sends a cancel or kill command to a process
* @param $pid The ID of the backend process
function sendSignal($pid, $signal) {
// Clean
$pid = (int)$pid;
-
+
if ($signal == 'CANCEL')
$sql = "SELECT pg_catalog.pg_cancel_backend({$pid}) AS val";
else
return -1;
-
+
// Execute the query
$val = $this->selectField($sql, 'val');
-
+
if ($val === -1) return -1;
elseif ($val == '1') return 0;
else return -1;
*/
function getFunction($function_oid) {
$this->clean($function_oid);
-
- $sql = "SELECT
+
+ $sql = "SELECT
pc.oid AS prooid,
proname,
- pg_catalog.pg_get_userbyid(proowner) AS proowner,
- nspname as proschema,
+ pg_catalog.pg_get_userbyid(proowner) AS proowner,
+ nspname as proschema,
lanname as prolanguage,
pg_catalog.format_type(prorettype, NULL) as proresult,
prosrc,
pg_catalog.obj_description(pc.oid, 'pg_proc') AS procomment
FROM
pg_catalog.pg_proc pc, pg_catalog.pg_language pl, pg_catalog.pg_namespace pn
- WHERE
+ WHERE
pc.oid = '{$function_oid}'::oid
- AND
- pc.prolang = pl.oid
- AND
- pc.pronamespace = pn.oid
+ AND pc.prolang = pl.oid
+ AND pc.pronamespace = pn.oid
";
-
+
return $this->selectSet($sql);
}
-
+
// Capabilities
function hasAlterDatabaseOwner() { return true; }
function hasAlterColumnType() { return true; }
function hasSignals() { return true; }
function hasNamedParams() { return true; }
function hasFunctionAlterOwner() { return true; }
-
+
}
?>
/**
* Manage functions in a database
*
- * $Id: functions.php,v 1.72 2007/11/15 23:09:21 xzilla Exp $
+ * $Id: functions.php,v 1.73 2007/11/16 18:34:24 ioguix Exp $
*/
// Include application functions
include_once('./libraries/lib.inc.php');
-
+
$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
if (!isset($msg)) $msg = '';
-
- /**
+
+ /**
* Function to save after editing a function
*/
function doSaveEdit() {
- global $data, $lang;
- global $misc, $_reload_browser;
-
+ global $data, $lang, $misc, $_reload_browser;
+
$fnlang = strtolower($_POST['original_lang']);
-
+
if ($fnlang == 'c') {
$def = array($_POST['formObjectFile'], $_POST['formLinkSymbol']);
} else if ($fnlang == 'internal'){
} else {
$def = $_POST['formDefinition'];
}
-
- $status = $data->setFunction($_POST['function_oid'], $_POST['original_function'], $_POST['formFunction'],
- $_POST['original_arguments'],
- $_POST['original_returns'], $def,
- $_POST['original_lang'], $_POST['formProperties'],
- isset($_POST['original_setof']),
- $_POST['original_owner'], $_POST['formFuncOwn'],
- $_POST['original_schema'], $_POST['formFuncSchema'],
- isset($_POST['formCost']) ? $_POST['formCost'] : null,
- isset($_POST['formRows']) ? $_POST['formRows'] : 0, $_POST['formComment']);
+ if(!$data->hasFunctionAlterSchema()) $_POST['formFuncSchema'] = '';
+
+ $status = $data->setFunction($_POST['function_oid'], $_POST['original_function'], $_POST['formFunction'],
+ $_POST['original_arguments'], $_POST['original_returns'], $def,
+ $_POST['original_lang'], $_POST['formProperties'], isset($_POST['original_setof']),
+ $_POST['original_owner'], $_POST['formFuncOwn'], $_POST['original_schema'],
+ $_POST['formFuncSchema'], isset($_POST['formCost']) ? $_POST['formCost'] : null,
+ isset($_POST['formRows']) ? $_POST['formRows'] : 0, $_POST['formComment']);
+
if ($status == 0) {
- // If function has had schema altered, need to change to the new schema
- // and reload the browser frame.
- if ($_POST['formFuncSchema'] != $_POST['original_schema']) {
- // Jump them to the new function schema
- $_REQUEST['schema'] = $_POST['formFuncSchema'];
- $misc->href = "server={$_REQUEST['server']}&database={$_REQUEST['database']}&schema={$_REQUEST['schema']}";
- // Force a browser reload
- $_reload_browser = true;
- }
+ // If function has had schema altered, need to change to the new schema
+ // and reload the browser frame.
+ if (!empty($_POST['formFuncSchema']) && ($_POST['formFuncSchema'] != $_POST['original_schema'])) {
+ // Jump them to the new function schema
+ $_REQUEST['schema'] = $_POST['formFuncSchema'];
+ $misc->href = "server={$_REQUEST['server']}&database={$_REQUEST['database']}&schema={$_REQUEST['schema']}";
+ // Force a browser reload
+ $_reload_browser = true;
+ }
doProperties($lang['strfunctionupdated']);
} else {
doEdit($lang['strfunctionupdatedbad']);
- }
+ }
}
-
+
/**
* Function to allow editing of a Function
*/
for ($i = 0; $i < sizeof($args_arr); $i++) {
if ($i != 0) $args .= ', ';
if (isset($names_arr[$i]) && $names_arr[$i] != '') {
- $data->fieldClean($names_arr[$i]);
+ $data->fieldClean($names_arr[$i]);
$args .= '"' . $names_arr[$i] . '" ';
}
$args .= $args_arr[$i];
echo "<tr>\n";
echo "<td class=\"data1\">";
- echo "<input type=\"hidden\" name=\"original_schema\" value=\"", htmlspecialchars($fndata->fields['proschema']),"\" />\n";
- if ($data->hasFunctionAlterSchema()) {
- $schemas = $data->getSchemas();
- echo "<select name=\"formFuncSchema\">";
+ echo "<input type=\"hidden\" name=\"original_schema\" value=\"", htmlspecialchars($fndata->fields['proschema']),"\" />\n";
+ if ($data->hasFunctionAlterSchema()) {
+ $schemas = $data->getSchemas();
+ echo "<select name=\"formFuncSchema\">";
while (!$schemas->EOF) {
$schema = $schemas->fields['nspname'];
echo "<option value=\"", htmlspecialchars($schema), "\"",
$schemas->moveNext();
}
echo "</select>\n";
- }
+ }
+ else echo $fndata->fields['proschema'];
echo "</td>\n";
echo "<td class=\"data1\">";
- echo "<input type=\"hidden\" name=\"original_function\" value=\"", htmlspecialchars($fndata->fields['proname']),"\" />\n";
+ echo "<input type=\"hidden\" name=\"original_function\" value=\"", htmlspecialchars($fndata->fields['proname']),"\" />\n";
echo "<input name=\"formFunction\" style=\"width: 100%\" maxlength=\"{$data->_maxNameLen}\" value=\"", htmlspecialchars($_POST['formFunction']), "\" />";
echo "</td>\n";
echo "<td class=\"data1\">", $misc->printVal($args), "\n";
- echo "<input type=\"hidden\" name=\"original_arguments\" value=\"",htmlspecialchars($args),"\" />\n";
+ echo "<input type=\"hidden\" name=\"original_arguments\" value=\"",htmlspecialchars($args),"\" />\n";
echo "</td>\n";
echo "<td class=\"data1\">";
if ($fndata->fields['proretset']) echo "setof ";
echo $misc->printVal($fndata->fields['proresult']), "\n";
- echo "<input type=\"hidden\" name=\"original_returns\" value=\"", htmlspecialchars($fndata->fields['proresult']), "\" />\n";
+ echo "<input type=\"hidden\" name=\"original_returns\" value=\"", htmlspecialchars($fndata->fields['proresult']), "\" />\n";
if ($fndata->fields['proretset'])
- echo "<input type=\"hidden\" name=\"original_setof\" value=\"yes\" />\n";
+ echo "<input type=\"hidden\" name=\"original_setof\" value=\"yes\" />\n";
echo "</td>\n";
echo "<td class=\"data1\">", $misc->printVal($fndata->fields['prolanguage']), "\n";
- echo "<input type=\"hidden\" name=\"original_lang\" value=\"", htmlspecialchars($fndata->fields['prolanguage']), "\" />\n";
+ echo "<input type=\"hidden\" name=\"original_lang\" value=\"", htmlspecialchars($fndata->fields['prolanguage']), "\" />\n";
echo "</td>\n";
-
+
$fnlang = strtolower($fndata->fields['prolanguage']);
if ($fnlang == 'c') {
echo "<tr><th class=\"data required\" colspan=\"2\">{$lang['strobjectfile']}</th>\n";
htmlspecialchars($_POST['formLinkSymbol']), "\" /></td></tr>\n";
} else {
echo "<tr><th class=\"data required\" colspan=\"5\">{$lang['strdefinition']}</th></tr>\n";
- echo "<tr><td class=\"data1\" colspan=\"5\"><textarea style=\"width:100%;\" rows=\"20\" cols=\"50\" name=\"formDefinition\">",
+ echo "<tr><td class=\"data1\" colspan=\"5\"><textarea style=\"width:100%;\" rows=\"20\" cols=\"50\" name=\"formDefinition\">",
htmlspecialchars($_POST['formDefinition']), "</textarea></td></tr>\n";
}
-
+
// Display function comment
echo "<tr><th class=\"data\" colspan=\"5\">{$lang['strcomment']}</th></tr>\n";
- echo "<tr><td class=\"data1\" colspan=\"5\"><textarea style=\"width:100%;\" name=\"formComment\" rows=\"3\" cols=\"50\">",
+ echo "<tr><td class=\"data1\" colspan=\"5\"><textarea style=\"width:100%;\" name=\"formComment\" rows=\"3\" cols=\"50\">",
htmlspecialchars($_POST['formComment']), "</textarea></td></tr>\n";
// Display function cost options
foreach ($data->funcprops as $k => $v) {
echo "<select name=\"formProperties[{$i}]\">\n";
foreach ($v as $p) {
- echo "<option value=\"", htmlspecialchars($p), "\"",
- ($p == $_POST['formProperties'][$i]) ? ' selected="selected"' : '',
+ echo "<option value=\"", htmlspecialchars($p), "\"",
+ ($p == $_POST['formProperties'][$i]) ? ' selected="selected"' : '',
">", $misc->printVal($p), "</option>\n";
}
echo "</select><br />\n";
$i++;
}
echo "</td></tr>\n";
- }
+ }
// function owner
if ($data->hasFunctionAlterOwner()) {
$users->moveNext();
}
echo "</select>\n";
- echo "<input type=\"hidden\" name=\"original_owner\" value=\"", htmlspecialchars($fndata->fields['proowner']),"\" />\n";
- echo "</td></tr>\n";
+ echo "<input type=\"hidden\" name=\"original_owner\" value=\"", htmlspecialchars($fndata->fields['proowner']),"\" />\n";
+ echo "</td></tr>\n";
}
echo "</table>\n";
echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
function doProperties($msg = '') {
global $data, $misc;
global $lang;
-
+
$misc->printTrail('function');
$misc->printTitle($lang['strproperties'],'pg.function');
$misc->printMsg($msg);
-
+
$funcdata = $data->getFunction($_REQUEST['function_oid']);
-
+
if ($funcdata->recordCount() > 0) {
// Deal with named parameters
if ($data->hasNamedParams()) {
for ($i = 0; $i < sizeof($args_arr); $i++) {
if ($i != 0) $args .= ', ';
if (isset($names_arr[$i]) && $names_arr[$i] != '') {
- $data->fieldClean($names_arr[$i]);
+ $data->fieldClean($names_arr[$i]);
$args .= '"' . $names_arr[$i] . '" ';
}
$args .= $args_arr[$i];
echo "<tr><td class=\"data1\">", $misc->printVal($funcdata->fields['proname']), "</td>\n";
echo "<td class=\"data1\">", $misc->printVal($args), "</td>\n";
echo "<td class=\"data1\">";
- if ($funcdata->fields['proretset']) echo "setof ";
+ if ($funcdata->fields['proretset']) echo "setof ";
echo $misc->printVal($funcdata->fields['proresult']), "</td>\n";
echo "<td class=\"data1\">", $misc->printVal($funcdata->fields['prolanguage']), "</td></tr>\n";
-
+
$fnlang = strtolower($funcdata->fields['prolanguage']);
if ($fnlang == 'c') {
echo "<tr><th class=\"data\" colspan=\"2\">{$lang['strobjectfile']}</th>\n";
echo "<tr><th class=\"data\" colspan=\"4\">{$lang['strlinksymbol']}</th></tr>\n";
echo "<tr><td class=\"data1\" colspan=\"4\">", $misc->printVal($funcdata->fields['prosrc']), "</td></tr>\n";
} else {
- include_once('./libraries/highlight.php');
+ include_once('./libraries/highlight.php');
echo "<tr><th class=\"data\" colspan=\"4\">{$lang['strdefinition']}</th></tr>\n";
// Check to see if we have syntax highlighting for this language
if (isset($data->langmap[$funcdata->fields['prolanguage']])) {
echo $misc->printVal($v), "<br />\n";
}
echo "</td></tr>\n";
- }
+ }
- echo "<td class=\"data1\" colspan=\"5\">{$lang['strowner']}: ", htmlspecialchars($funcdata->fields['proowner']),"\n";
- echo "</td></tr>\n";
+ echo "<td class=\"data1\" colspan=\"5\">{$lang['strowner']}: ", htmlspecialchars($funcdata->fields['proowner']),"\n";
+ echo "</td></tr>\n";
echo "</table>\n";
}
else echo "<p>{$lang['strnodata']}</p>\n";
-
+
echo "<ul class=\"navlink\">\n\t<li><a href=\"functions.php?{$misc->href}\">{$lang['strshowallfunctions']}</a></li>\n";
- echo "\t<li><a href=\"functions.php?action=edit&{$misc->href}&function=",
+ echo "\t<li><a href=\"functions.php?action=edit&{$misc->href}&function=",
urlencode($_REQUEST['function']), "&function_oid=", urlencode($_REQUEST['function_oid']), "\">{$lang['stralter']}</a></li>\n";
echo "\t<li><a href=\"functions.php?action=confirm_drop&{$misc->href}&function=",
urlencode($func_full), "&function_oid=", $_REQUEST['function_oid'], "\">{$lang['strdrop']}</a></li>\n</ul>";
}
-
+
/**
* Show confirmation of drop and perform actual drop
*/
if ($confirm) {
$misc->printTrail('schema');
$misc->printTitle($lang['strdrop'],'pg.function.drop');
-
- echo "<p>", sprintf($lang['strconfdropfunction'], $misc->printVal($_REQUEST['function'])), "</p>\n";
-
+
+ echo "<p>", sprintf($lang['strconfdropfunction'], $misc->printVal($_REQUEST['function'])), "</p>\n";
+
echo "<form action=\"functions.php\" method=\"post\">\n";
echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
echo "<input type=\"hidden\" name=\"function\" value=\"", htmlspecialchars($_REQUEST['function']), "\" />\n";
doDefault($lang['strfunctiondroppedbad']);
}
}
-
+
}
-
+
/**
* Displays a screen where they can enter a new function
*/
function doCreate($msg = '',$szJS="") {
global $data, $misc;
global $lang;
-
+
$misc->printTrail('schema');
if (!isset($_POST['formFunction'])) $_POST['formFunction'] = '';
if (!isset($_POST['formArguments'])) $_POST['formArguments'] = '';
break;
}
$misc->printMsg($msg);
-
- // Create string for return type list
+
+ // Create string for return type list
$szTypes = "";
while (!$types->EOF) {
$szSelected = "";
$szArguments = "<td class=\"data1\"><input name=\"formArguments\" style=\"width:100%;\" size=\"16\" value=\"".
htmlspecialchars($_POST['formArguments']) ."\" /></td>";
-
+
$szSetOfSelected = "";
$szNotSetOfSelected = "";
if($_POST['formSetOf'] == '') {
}
$szLanguage .= "</select>\n";
}
-
+
$szLanguage .= "</td>";
$szJSArguments = "<tr><th class=\"data\" colspan=\"7\">{$lang['strarguments']}</th></tr>";
$arrayModes = array("IN","OUT","INOUT");
htmlspecialchars($_POST['formLinkSymbol']), "\" /></td></tr>\n";
} else {
echo "<tr><th class=\"data required\" colspan=\"4\">{$lang['strdefinition']}</th></tr>\n";
- echo "<tr><td class=\"data1\" colspan=\"4\"><textarea style=\"width:100%;\" rows=\"20\" cols=\"50\" name=\"formDefinition\">",
+ echo "<tr><td class=\"data1\" colspan=\"4\"><textarea style=\"width:100%;\" rows=\"20\" cols=\"50\" name=\"formDefinition\">",
htmlspecialchars($_POST['formDefinition']), "</textarea></td></tr>\n";
}
echo "<td class=\"data1\" colspan=\"2\">{$lang['strresultrows']}: <input name=\"formRows\" size=\"16\" value=\"".
htmlspecialchars($_POST['formRows']) ."\" /></td>";
}
-
+
// Display function properties
if (is_array($data->funcprops) && sizeof($data->funcprops) > 0) {
echo "<tr><th class=\"data required\" colspan=\"4\">{$lang['strproperties']}</th></tr>\n";
foreach ($data->funcprops as $k => $v) {
echo "<select name=\"formProperties[{$i}]\">\n";
foreach ($v as $p) {
- echo "<option value=\"", htmlspecialchars($p), "\"",
- ($p == $_POST['formProperties'][$i]) ? ' selected="selected"' : '',
+ echo "<option value=\"", htmlspecialchars($p), "\"",
+ ($p == $_POST['formProperties'][$i]) ? ' selected="selected"' : '',
">", $misc->printVal($p), "</option>\n";
}
echo "</select><br />\n";
$i++;
}
echo "</td></tr>\n";
- }
+ }
echo "</tbody></table>\n";
echo $szJSTRArg;
echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
echo "</form>\n";
echo $szJS;
}
-
+
/**
* Actually creates the new function in the database
*/
global $data, $lang;
$fnlang = strtolower($_POST['formLanguage']);
-
+
if ($fnlang == 'c') {
$def = array($_POST['formObjectFile'], $_POST['formLinkSymbol']);
} else if ($fnlang == 'internal'){
$szJS = "<script type=\"text/javascript\" src=\"functions.js\">noArgsRebuild(addArg());</script>";
}
- $cost = (isset($_POST['formCost'])) ? $_POST['formCost'] : null;
+ $cost = (isset($_POST['formCost'])) ? $_POST['formCost'] : null;
if ($cost == '' || !is_numeric($cost) || $cost != (int)$cost || $cost < 0) {
$cost = null;
}
- $rows = (isset($_POST['formRows'])) ? $_POST['formRows'] : null;
+ $rows = (isset($_POST['formRows'])) ? $_POST['formRows'] : null;
if ($rows == '' || !is_numeric($rows) || $rows != (int)$rows ) {
$rows = null;
}
elseif ($fnlang != 'internal' && !$def) doCreate($lang['strfunctionneedsdef'],$szJS);
else {
// Append array symbol to type if chosen
- $status = $data->createFunction($_POST['formFunction'], empty($_POST["nojs"]) ? buildFunctionArguments($_POST) : $_POST["formArguments"],
+ $status = $data->createFunction($_POST['formFunction'], empty($_POST["nojs"]) ? buildFunctionArguments($_POST) : $_POST["formArguments"],
$_POST['formReturns'] . $_POST['formArray'] , $def , $_POST['formLanguage'],
- $_POST['formProperties'], $_POST['formSetOf'] == 'SETOF',
+ $_POST['formProperties'], $_POST['formSetOf'] == 'SETOF',
$cost, $rows, false);
if ($status == 0)
doDefault($lang['strfunctioncreated']);
doCreate($lang['strfunctioncreatedbad'],$szJS);
}
}
- }
+ }
/**
* Build out the function arguments string
}
return implode(",",$arrayArgs);
}
-
+
/**
* Build out JS to re-create table rows for arguments
*/
function doDefault($msg = '') {
global $data, $conf, $misc, $func;
global $lang;
-
+
$misc->printTrail('schema');
$misc->printTabs('schema','functions');
$misc->printMsg($msg);
-
+
$funcs = $data->getFunctions();
-
+
$columns = array(
'function' => array(
'title' => $lang['strfunction'],
'field' => field('procomment'),
),
);
-
+
$actions = array(
'alter' => array(
'title' => $lang['stralter'],
if ( !$data->hasFuncPrivs() ) {
unset($actions['privileges']);
}
-
+
$misc->printTable($funcs, $columns, $actions, $lang['strnofunctions']);
echo "<ul class=\"navlink\">\n\t<li><a href=\"functions.php?action=create&{$misc->href}\">{$lang['strcreateplfunction']}</a></li>\n";
echo "\t<li><a href=\"functions.php?action=create&language=internal&{$misc->href}\">{$lang['strcreateinternalfunction']}</a></li>\n";
echo "\t<li><a href=\"functions.php?action=create&language=C&{$misc->href}\">{$lang['strcreatecfunction']}</a></li>\n</ul>\n";
}
-
+
/**
* Generate XML for the browser tree.
*/
function doTree() {
global $misc, $data;
-
+
$funcs = $data->getFunctions();
-
+
$proto = concat(field('proname'),' (',field('proarguments'),')');
-
+
$reqvars = $misc->getRequestVars('function');
-
+
$attrs = array(
'text' => $proto,
'icon' => 'Function',
)
)
);
-
+
$misc->printTreeXML($funcs, $attrs);
exit;
}
-
+
if ($action == 'tree') doTree();
-
+
$misc->printHeader($lang['strfunctions']);
$misc->printBody();
break;
case 'confirm_drop':
doDrop(true);
- break;
+ break;
case 'save_edit':
if (isset($_POST['cancel'])) doDefault();
else doSaveEdit();
default:
doDefault();
break;
- }
+ }
$misc->printFooter();
-
+
?>