2006-10-21 01:11:50 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
|
|
|
Doctrine::autoload('Doctrine_Export');
|
|
|
|
/**
|
|
|
|
* Doctrine_Export_Pgsql
|
|
|
|
*
|
2006-11-15 23:36:58 +03:00
|
|
|
* @package Doctrine
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
2006-10-21 01:11:50 +04:00
|
|
|
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
|
2006-11-15 23:36:58 +03:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @category Object Relational Mapping
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
class Doctrine_Export_Pgsql extends Doctrine_Export
|
|
|
|
{
|
2006-10-21 01:11:50 +04:00
|
|
|
/**
|
|
|
|
* create a new database
|
|
|
|
*
|
|
|
|
* @param string $name name of the database that should be created
|
|
|
|
* @throws PDOException
|
|
|
|
* @return void
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function createDatabase($name)
|
|
|
|
{
|
2006-10-21 01:11:50 +04:00
|
|
|
$query = 'CREATE DATABASE ' . $this->conn->quoteIdentifier($name);
|
2006-12-22 01:06:08 +03:00
|
|
|
$this->conn->exec($query);
|
2006-10-21 01:11:50 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* drop an existing database
|
|
|
|
*
|
|
|
|
* @param string $name name of the database that should be dropped
|
|
|
|
* @throws PDOException
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function dropDatabase($name)
|
|
|
|
{
|
2006-10-21 01:11:50 +04:00
|
|
|
$query = 'DROP DATABASE ' . $this->conn->quoteIdentifier($name);
|
2006-12-22 01:06:08 +03:00
|
|
|
$this->conn->exec($query);
|
2006-10-21 01:11:50 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* alter an existing table
|
|
|
|
*
|
|
|
|
* @param string $name name of the table that is intended to be changed.
|
|
|
|
* @param array $changes associative array that contains the details of each type
|
|
|
|
* of change that is intended to be performed. The types of
|
|
|
|
* changes that are currently supported are defined as follows:
|
|
|
|
*
|
|
|
|
* name
|
|
|
|
*
|
|
|
|
* New name for the table.
|
|
|
|
*
|
|
|
|
* add
|
|
|
|
*
|
|
|
|
* Associative array with the names of fields to be added as
|
|
|
|
* indexes of the array. The value of each entry of the array
|
|
|
|
* should be set to another associative array with the properties
|
|
|
|
* of the fields to be added. The properties of the fields should
|
|
|
|
* be the same as defined by the Metabase parser.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* remove
|
|
|
|
*
|
|
|
|
* Associative array with the names of fields to be removed as indexes
|
|
|
|
* of the array. Currently the values assigned to each entry are ignored.
|
|
|
|
* An empty array should be used for future compatibility.
|
|
|
|
*
|
|
|
|
* rename
|
|
|
|
*
|
|
|
|
* Associative array with the names of fields to be renamed as indexes
|
|
|
|
* of the array. The value of each entry of the array should be set to
|
|
|
|
* another associative array with the entry named name with the new
|
|
|
|
* field name and the entry named Declaration that is expected to contain
|
|
|
|
* the portion of the field declaration already in DBMS specific SQL code
|
|
|
|
* as it is used in the CREATE TABLE statement.
|
|
|
|
*
|
|
|
|
* change
|
|
|
|
*
|
|
|
|
* Associative array with the names of the fields to be changed as indexes
|
|
|
|
* of the array. Keep in mind that if it is intended to change either the
|
|
|
|
* name of a field and any other properties, the change array entries
|
|
|
|
* should have the new names of the fields as array indexes.
|
|
|
|
*
|
|
|
|
* The value of each entry of the array should be set to another associative
|
|
|
|
* array with the properties of the fields to that are meant to be changed as
|
|
|
|
* array entries. These entries should be assigned to the new values of the
|
|
|
|
* respective properties. The properties of the fields should be the same
|
|
|
|
* as defined by the Metabase parser.
|
|
|
|
*
|
|
|
|
* Example
|
|
|
|
* array(
|
|
|
|
* 'name' => 'userlist',
|
|
|
|
* 'add' => array(
|
|
|
|
* 'quota' => array(
|
|
|
|
* 'type' => 'integer',
|
|
|
|
* 'unsigned' => 1
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* 'remove' => array(
|
|
|
|
* 'file_limit' => array(),
|
|
|
|
* 'time_limit' => array()
|
|
|
|
* ),
|
|
|
|
* 'change' => array(
|
|
|
|
* 'name' => array(
|
|
|
|
* 'length' => '20',
|
|
|
|
* 'definition' => array(
|
|
|
|
* 'type' => 'text',
|
|
|
|
* 'length' => 20,
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* 'rename' => array(
|
|
|
|
* 'sex' => array(
|
|
|
|
* 'name' => 'gender',
|
|
|
|
* 'definition' => array(
|
|
|
|
* 'type' => 'text',
|
|
|
|
* 'length' => 1,
|
|
|
|
* 'default' => 'M',
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @param boolean $check indicates whether the function should just check if the DBMS driver
|
|
|
|
* can perform the requested table alterations if the value is true or
|
|
|
|
* actually perform them otherwise.
|
|
|
|
* @throws PDOException
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function alterTable($name, $changes, $check)
|
|
|
|
{
|
2006-10-21 01:11:50 +04:00
|
|
|
foreach ($changes as $change_name => $change) {
|
|
|
|
switch ($change_name) {
|
|
|
|
case 'add':
|
|
|
|
case 'remove':
|
|
|
|
case 'change':
|
|
|
|
case 'name':
|
|
|
|
case 'rename':
|
2006-12-30 00:30:37 +03:00
|
|
|
break;
|
2006-10-21 01:11:50 +04:00
|
|
|
default:
|
2007-01-26 01:36:43 +03:00
|
|
|
throw new Doctrine_Export_Exception('change type "'.$change_name.'\" not yet supported');
|
2006-10-21 01:11:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($check) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($changes['add']) && is_array($changes['add'])) {
|
|
|
|
foreach ($changes['add'] as $field_name => $field) {
|
2007-01-06 01:08:05 +03:00
|
|
|
$query = 'ADD ' . $this->conn->getDeclaration($field['type'], $field_name, $field);
|
|
|
|
$this->conn->exec("ALTER TABLE $name $query");
|
2006-10-21 01:11:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($changes['remove']) && is_array($changes['remove'])) {
|
|
|
|
foreach ($changes['remove'] as $field_name => $field) {
|
|
|
|
$field_name = $this->conn->quoteIdentifier($field_name, true);
|
|
|
|
$query = 'DROP ' . $field_name;
|
2007-01-06 01:08:05 +03:00
|
|
|
$this->conn->exec("ALTER TABLE $name $query");
|
2006-10-21 01:11:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($changes['change']) && is_array($changes['change'])) {
|
|
|
|
foreach ($changes['change'] as $field_name => $field) {
|
|
|
|
$field_name = $this->conn->quoteIdentifier($field_name, true);
|
|
|
|
if (!empty($field['type'])) {
|
2007-01-06 01:08:05 +03:00
|
|
|
$server_info = $this->conn->getServerVersion();
|
2006-10-21 01:11:50 +04:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if (is_array($server_info) && $server_info['major'] < 8) {
|
2007-01-26 01:36:43 +03:00
|
|
|
throw new Doctrine_Export_Exception('changing column type for "'.$change_name.'\" requires PostgreSQL 8.0 or above');
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2007-01-06 01:08:05 +03:00
|
|
|
$query = "ALTER $field_name TYPE ".$this->conn->datatype->getTypeDeclaration($field['definition']);
|
|
|
|
$this->conn->exec("ALTER TABLE $name $query");
|
2006-10-21 01:11:50 +04:00
|
|
|
}
|
|
|
|
if (array_key_exists('default', $field)) {
|
2007-01-06 01:08:05 +03:00
|
|
|
$query = "ALTER $field_name SET DEFAULT ".$this->conn->quote($field['definition']['default'], $field['definition']['type']);
|
|
|
|
$this->conn->exec("ALTER TABLE $name $query");
|
2006-10-21 01:11:50 +04:00
|
|
|
}
|
|
|
|
if (!empty($field['notnull'])) {
|
|
|
|
$query = "ALTER $field_name ".($field['definition']['notnull'] ? "SET" : "DROP").' NOT NULL';
|
2007-01-06 01:08:05 +03:00
|
|
|
$this->conn->exec("ALTER TABLE $name $query");
|
2006-10-21 01:11:50 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($changes['rename']) && is_array($changes['rename'])) {
|
|
|
|
foreach ($changes['rename'] as $field_name => $field) {
|
|
|
|
$field_name = $this->conn->quoteIdentifier($field_name, true);
|
2007-01-06 01:08:05 +03:00
|
|
|
$this->conn->exec("ALTER TABLE $name RENAME COLUMN $field_name TO ".$this->conn->quoteIdentifier($field['name'], true));
|
2006-10-21 01:11:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = $this->conn->quoteIdentifier($name, true);
|
|
|
|
if (!empty($changes['name'])) {
|
|
|
|
$change_name = $this->conn->quoteIdentifier($changes['name'], true);
|
2007-01-06 01:08:05 +03:00
|
|
|
$this->conn->exec("ALTER TABLE $name RENAME TO ".$change_name);
|
2006-10-21 01:11:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
|