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