| | |
| | | // | Author: Paul Cooper <pgc@ucecom.com> | |
| | | // +----------------------------------------------------------------------+ |
| | | // |
| | | // $Id: pgsql.php,v 1.82 2008/03/05 12:55:57 afz Exp $ |
| | | // $Id: pgsql.php 292715 2009-12-28 14:06:34Z quipo $ |
| | | |
| | | require_once 'MDB2/Driver/Manager/Common.php'; |
| | | |
| | |
| | | return MDB2_OK; |
| | | } |
| | | |
| | | $name = $db->quoteIdentifier($name, true); |
| | | |
| | | if (!empty($changes['remove']) && is_array($changes['remove'])) { |
| | | foreach ($changes['remove'] as $field_name => $field) { |
| | | $field_name = $db->quoteIdentifier($field_name, true); |
| | |
| | | 'changing column type for "'.$change_name.'\" requires PostgreSQL 8.0 or above', __FUNCTION__); |
| | | } |
| | | $db->loadModule('Datatype', null, true); |
| | | $query = "ALTER $field_name TYPE ".$db->datatype->getTypeDeclaration($field['definition']); |
| | | $type = $db->datatype->getTypeDeclaration($field['definition']); |
| | | $query = "ALTER $field_name TYPE $type USING CAST($field_name AS $type)"; |
| | | $result = $db->exec("ALTER TABLE $name $query"); |
| | | if (PEAR::isError($result)) { |
| | | return $result; |
| | |
| | | return $result; |
| | | } |
| | | } |
| | | if (!empty($field['definition']['notnull'])) { |
| | | if (array_key_exists('notnull', $field['definition'])) { |
| | | $query = "ALTER $field_name ".($field['definition']['notnull'] ? 'SET' : 'DROP').' NOT NULL'; |
| | | $result = $db->exec("ALTER TABLE $name $query"); |
| | | if (PEAR::isError($result)) { |
| | |
| | | } |
| | | } |
| | | |
| | | $name = $db->quoteIdentifier($name, true); |
| | | if (!empty($changes['name'])) { |
| | | $change_name = $db->quoteIdentifier($changes['name'], true); |
| | | $result = $db->exec("ALTER TABLE $name RENAME TO ".$change_name); |
| | |
| | | FROM pg_trigger trg, |
| | | pg_class tbl |
| | | WHERE trg.tgrelid = tbl.oid'; |
| | | if (!is_null($table)) { |
| | | if (null !== $table) { |
| | | $table = $db->quote(strtoupper($table), 'text'); |
| | | $query .= " AND tbl.relname = $table"; |
| | | $query .= " AND UPPER(tbl.relname) = $table"; |
| | | } |
| | | $result = $db->queryCol($query); |
| | | if (PEAR::isError($result)) { |
| | |
| | | return $db; |
| | | } |
| | | |
| | | list($schema, $table) = $this->splitTableSchema($table); |
| | | |
| | | $table = $db->quoteIdentifier($table, true); |
| | | if (!empty($schema)) { |
| | | $table = $db->quoteIdentifier($schema, true) . '.' .$table; |
| | | } |
| | | $db->setLimit(1); |
| | | $result2 = $db->query("SELECT * FROM $table"); |
| | | if (PEAR::isError($result2)) { |
| | |
| | | return $db; |
| | | } |
| | | |
| | | list($schema, $table) = $this->splitTableSchema($table); |
| | | |
| | | $table = $db->quote($table, 'text'); |
| | | $subquery = "SELECT indexrelid FROM pg_index, pg_class"; |
| | | $subquery.= " WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND indisunique != 't' AND indisprimary != 't'"; |
| | | $subquery = "SELECT indexrelid |
| | | FROM pg_index |
| | | LEFT JOIN pg_class ON pg_class.oid = pg_index.indrelid |
| | | LEFT JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid |
| | | WHERE pg_class.relname = $table |
| | | AND indisunique != 't' |
| | | AND indisprimary != 't'"; |
| | | if (!empty($schema)) { |
| | | $subquery .= ' AND pg_namespace.nspname = '.$db->quote($schema, 'text'); |
| | | } |
| | | $query = "SELECT relname FROM pg_class WHERE oid IN ($subquery)"; |
| | | $indexes = $db->queryCol($query, 'text'); |
| | | if (PEAR::isError($indexes)) { |
| | |
| | | } |
| | | |
| | | // }}} |
| | | // {{{ dropConstraint() |
| | | |
| | | /** |
| | | * drop existing constraint |
| | | * |
| | | * @param string $table name of table that should be used in method |
| | | * @param string $name name of the constraint to be dropped |
| | | * @param string $primary hint if the constraint is primary |
| | | * |
| | | * @return mixed MDB2_OK on success, a MDB2 error on failure |
| | | * @access public |
| | | */ |
| | | function dropConstraint($table, $name, $primary = false) |
| | | { |
| | | $db =& $this->getDBInstance(); |
| | | if (PEAR::isError($db)) { |
| | | return $db; |
| | | } |
| | | |
| | | // is it an UNIQUE index? |
| | | $query = 'SELECT relname |
| | | FROM pg_class |
| | | WHERE oid IN ( |
| | | SELECT indexrelid |
| | | FROM pg_index, pg_class |
| | | WHERE pg_class.relname = '.$db->quote($table, 'text').' |
| | | AND pg_class.oid = pg_index.indrelid |
| | | AND indisunique = \'t\') |
| | | EXCEPT |
| | | SELECT conname |
| | | FROM pg_constraint, pg_class |
| | | WHERE pg_constraint.conrelid = pg_class.oid |
| | | AND relname = '. $db->quote($table, 'text'); |
| | | $unique = $db->queryCol($query, 'text'); |
| | | if (PEAR::isError($unique) || empty($unique)) { |
| | | // not an UNIQUE index, maybe a CONSTRAINT |
| | | return parent::dropConstraint($table, $name, $primary); |
| | | } |
| | | |
| | | if (in_array($name, $unique)) { |
| | | return $db->exec('DROP INDEX '.$db->quoteIdentifier($name, true)); |
| | | } |
| | | $idxname = $db->getIndexName($name); |
| | | if (in_array($idxname, $unique)) { |
| | | return $db->exec('DROP INDEX '.$db->quoteIdentifier($idxname, true)); |
| | | } |
| | | return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
| | | $name . ' is not an existing constraint for table ' . $table, __FUNCTION__); |
| | | } |
| | | |
| | | // }}} |
| | | // {{{ listTableConstraints() |
| | | |
| | | /** |
| | |
| | | return $db; |
| | | } |
| | | |
| | | list($schema, $table) = $this->splitTableSchema($table); |
| | | |
| | | $table = $db->quote($table, 'text'); |
| | | $query = 'SELECT conname |
| | | FROM pg_constraint, pg_class |
| | | WHERE pg_constraint.conrelid = pg_class.oid |
| | | AND relname = ' .$table; |
| | | FROM pg_constraint |
| | | LEFT JOIN pg_class ON pg_constraint.conrelid = pg_class.oid |
| | | LEFT JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid |
| | | WHERE relname = ' .$table; |
| | | if (!empty($schema)) { |
| | | $query .= ' AND pg_namespace.nspname = ' . $db->quote($schema, 'text'); |
| | | } |
| | | $query .= ' |
| | | UNION DISTINCT |
| | | SELECT relname |
| | | FROM pg_class |
| | | WHERE oid IN ( |
| | | SELECT indexrelid |
| | | FROM pg_index |
| | | LEFT JOIN pg_class ON pg_class.oid = pg_index.indrelid |
| | | LEFT JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid |
| | | WHERE pg_class.relname = '.$table.' |
| | | AND indisunique = \'t\''; |
| | | if (!empty($schema)) { |
| | | $query .= ' AND pg_namespace.nspname = ' . $db->quote($schema, 'text'); |
| | | } |
| | | $query .= ')'; |
| | | $constraints = $db->queryCol($query); |
| | | if (PEAR::isError($constraints)) { |
| | | return $constraints; |