2007-02-17 13:00:04 +03: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
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @category Object Relational Mapping
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
|
|
|
class Doctrine_Export_Pgsql extends Doctrine_Export
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* create a new database
|
|
|
|
*
|
|
|
|
* @param string $name name of the database that should be created
|
|
|
|
* @throws PDOException
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-06-19 00:00:44 +04:00
|
|
|
public function createDatabaseSql($name)
|
2007-02-17 13:00:04 +03:00
|
|
|
{
|
|
|
|
$query = 'CREATE DATABASE ' . $this->conn->quoteIdentifier($name);
|
2007-06-19 00:00:44 +04:00
|
|
|
|
|
|
|
return $query;
|
2007-02-17 13:00:04 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* drop an existing database
|
|
|
|
*
|
|
|
|
* @param string $name name of the database that should be dropped
|
|
|
|
* @throws PDOException
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-06-19 00:00:44 +04:00
|
|
|
public function dropDatabaseSql($name)
|
2007-02-17 13:00:04 +03:00
|
|
|
{
|
|
|
|
$query = 'DROP DATABASE ' . $this->conn->quoteIdentifier($name);
|
2007-06-19 00:00:44 +04:00
|
|
|
|
|
|
|
return $query;
|
2007-02-17 13:00:04 +03:00
|
|
|
}
|
2007-06-14 01:28:49 +04:00
|
|
|
/**
|
|
|
|
* getAdvancedForeignKeyOptions
|
|
|
|
* Return the FOREIGN KEY query section dealing with non-standard options
|
|
|
|
* as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
|
|
|
|
*
|
|
|
|
* @param array $definition foreign key definition
|
|
|
|
* @return string
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
public function getAdvancedForeignKeyOptions(array $definition)
|
|
|
|
{
|
|
|
|
$query = '';
|
|
|
|
if (isset($definition['match'])) {
|
2007-06-14 01:37:33 +04:00
|
|
|
$query .= ' MATCH ' . $definition['match'];
|
2007-06-14 01:28:49 +04:00
|
|
|
}
|
|
|
|
if (isset($definition['onUpdate'])) {
|
2007-06-15 00:18:25 +04:00
|
|
|
$query .= ' ON UPDATE ' . $definition['onUpdate'];
|
2007-06-14 01:28:49 +04:00
|
|
|
}
|
|
|
|
if (isset($definition['onDelete'])) {
|
2007-06-15 00:18:25 +04:00
|
|
|
$query .= ' ON DELETE ' . $definition['onDelete'];
|
2007-06-14 01:28:49 +04:00
|
|
|
}
|
|
|
|
if (isset($definition['deferrable'])) {
|
|
|
|
$query .= ' DEFERRABLE';
|
|
|
|
} else {
|
|
|
|
$query .= ' NOT DEFERRABLE';
|
|
|
|
}
|
|
|
|
if (isset($definition['feferred'])) {
|
|
|
|
$query .= ' INITIALLY DEFERRED';
|
|
|
|
} else {
|
|
|
|
$query .= ' INITIALLY IMMEDIATE';
|
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2007-02-17 13:00:04 +03: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.
|
2007-05-18 13:26:38 +04:00
|
|
|
* @throws Doctrine_Connection_Exception
|
2007-02-17 13:00:04 +03:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2007-09-19 23:33:00 +04:00
|
|
|
public function alterTable($name, array $changes, $check = false)
|
2007-02-17 13:00:04 +03:00
|
|
|
{
|
|
|
|
foreach ($changes as $changeName => $change) {
|
|
|
|
switch ($changeName) {
|
|
|
|
case 'add':
|
|
|
|
case 'remove':
|
|
|
|
case 'change':
|
|
|
|
case 'name':
|
|
|
|
case 'rename':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Doctrine_Export_Exception('change type "' . $changeName . '\" not yet supported');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($check) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($changes['add']) && is_array($changes['add'])) {
|
2007-05-18 13:26:38 +04:00
|
|
|
foreach ($changes['add'] as $fieldName => $field) {
|
2007-08-30 02:03:55 +04:00
|
|
|
$query = 'ADD ' . $this->getDeclaration($fieldName, $field);
|
2007-02-17 13:00:04 +03:00
|
|
|
$this->conn->exec('ALTER TABLE ' . $name . ' ' . $query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($changes['remove']) && is_array($changes['remove'])) {
|
2007-05-18 13:26:38 +04:00
|
|
|
foreach ($changes['remove'] as $fieldName => $field) {
|
|
|
|
$fieldName = $this->conn->quoteIdentifier($fieldName, true);
|
|
|
|
$query = 'DROP ' . $fieldName;
|
2007-02-17 13:00:04 +03:00
|
|
|
$this->conn->exec('ALTER TABLE ' . $name . ' ' . $query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($changes['change']) && is_array($changes['change'])) {
|
2007-05-18 13:26:38 +04:00
|
|
|
foreach ($changes['change'] as $fieldName => $field) {
|
|
|
|
$fieldName = $this->conn->quoteIdentifier($fieldName, true);
|
2007-02-17 13:00:04 +03:00
|
|
|
if (isset($field['type'])) {
|
2007-05-18 13:26:38 +04:00
|
|
|
$serverInfo = $this->conn->getServerVersion();
|
2007-02-17 13:00:04 +03:00
|
|
|
|
2007-05-18 13:26:38 +04:00
|
|
|
if (is_array($serverInfo) && $serverInfo['major'] < 8) {
|
2007-02-17 13:00:04 +03:00
|
|
|
throw new Doctrine_Export_Exception('changing column type for "'.$field['type'].'\" requires PostgreSQL 8.0 or above');
|
|
|
|
}
|
2007-05-18 13:26:38 +04:00
|
|
|
$query = 'ALTER ' . $fieldName . ' TYPE ' . $this->conn->datatype->getTypeDeclaration($field['definition']);
|
2007-02-17 13:00:04 +03:00
|
|
|
$this->conn->exec('ALTER TABLE ' . $name . ' ' . $query);;
|
|
|
|
}
|
|
|
|
if (array_key_exists('default', $field)) {
|
2007-05-18 13:26:38 +04:00
|
|
|
$query = 'ALTER ' . $fieldName . ' SET DEFAULT ' . $this->conn->quote($field['definition']['default'], $field['definition']['type']);
|
2007-02-17 13:00:04 +03:00
|
|
|
$this->conn->exec('ALTER TABLE ' . $name . ' ' . $query);
|
|
|
|
}
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! empty($field['notnull'])) {
|
2007-05-18 13:26:38 +04:00
|
|
|
$query = 'ALTER ' . $fieldName . ' ' . ($field['definition']['notnull'] ? 'SET' : 'DROP') . ' NOT NULL';
|
2007-02-17 13:00:04 +03:00
|
|
|
$this->conn->exec('ALTER TABLE ' . $name . ' ' . $query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($changes['rename']) && is_array($changes['rename'])) {
|
|
|
|
foreach ($changes['rename'] as $fieldName => $field) {
|
2007-05-18 13:26:38 +04:00
|
|
|
$fieldName = $this->conn->quoteIdentifier($fieldName, true);
|
2007-02-17 13:00:04 +03:00
|
|
|
$this->conn->exec('ALTER TABLE ' . $name . ' RENAME COLUMN ' . $fieldName . ' TO ' . $this->conn->quoteIdentifier($field['name'], true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = $this->conn->quoteIdentifier($name, true);
|
|
|
|
if (isset($changes['name'])) {
|
|
|
|
$changeName = $this->conn->quoteIdentifier($changes['name'], true);
|
|
|
|
$this->conn->exec('ALTER TABLE ' . $name . ' RENAME TO ' . $changeName);
|
|
|
|
}
|
|
|
|
}
|
2007-06-14 01:28:49 +04:00
|
|
|
/**
|
2007-06-14 02:31:15 +04:00
|
|
|
* return RDBMS specific create sequence statement
|
2007-06-14 01:28:49 +04:00
|
|
|
*
|
2007-06-14 02:31:15 +04:00
|
|
|
* @throws Doctrine_Connection_Exception if something fails at database level
|
|
|
|
* @param string $seqName name of the sequence to be created
|
|
|
|
* @param string $start start value of the sequence; default is 1
|
|
|
|
* @param array $options An associative array of table options:
|
|
|
|
* array(
|
|
|
|
* 'comment' => 'Foo',
|
|
|
|
* 'charset' => 'utf8',
|
|
|
|
* 'collate' => 'utf8_unicode_ci',
|
|
|
|
* );
|
|
|
|
* @return string
|
2007-06-14 01:28:49 +04:00
|
|
|
*/
|
2007-06-14 02:31:15 +04:00
|
|
|
public function createSequenceSql($sequenceName, $start = 1, array $options = array())
|
2007-06-14 01:28:49 +04:00
|
|
|
{
|
|
|
|
$sequenceName = $this->conn->quoteIdentifier($this->conn->formatter->getSequenceName($sequenceName), true);
|
|
|
|
return $this->conn->exec('CREATE SEQUENCE ' . $sequenceName . ' INCREMENT 1' .
|
|
|
|
($start < 1 ? ' MINVALUE ' . $start : '') . ' START ' . $start);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* drop existing sequence
|
|
|
|
*
|
|
|
|
* @param string $sequenceName name of the sequence to be dropped
|
|
|
|
*/
|
2007-06-14 02:31:15 +04:00
|
|
|
public function dropSequenceSql($sequenceName)
|
2007-06-14 01:28:49 +04:00
|
|
|
{
|
|
|
|
$sequenceName = $this->conn->quoteIdentifier($this->conn->formatter->getSequenceName($sequenceName), true);
|
2007-06-14 02:31:15 +04:00
|
|
|
return 'DROP SEQUENCE ' . $sequenceName;
|
2007-06-14 01:28:49 +04:00
|
|
|
}
|
|
|
|
|
2007-07-21 19:17:17 +04:00
|
|
|
/**
|
|
|
|
* Creates a table.
|
|
|
|
*
|
|
|
|
* @param unknown_type $name
|
|
|
|
* @param array $fields
|
|
|
|
* @param array $options
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2007-08-30 01:51:49 +04:00
|
|
|
public function createTableSql($name, array $fields, array $options = array())
|
2007-07-21 19:17:17 +04:00
|
|
|
{
|
|
|
|
if ( ! $name) {
|
|
|
|
throw new Doctrine_Export_Exception('no valid table name specified');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($fields)) {
|
|
|
|
throw new Doctrine_Export_Exception('no fields specified for table ' . $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$queryFields = $this->getFieldDeclarationList($fields);
|
|
|
|
|
|
|
|
|
|
|
|
if (isset($options['primary']) && ! empty($options['primary'])) {
|
|
|
|
$queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')';
|
|
|
|
}
|
|
|
|
|
2007-08-25 04:17:30 +04:00
|
|
|
$query = 'CREATE TABLE ' . $this->conn->quoteIdentifier($name, true) . ' (' . $queryFields . ')';
|
2007-07-21 19:17:17 +04:00
|
|
|
|
|
|
|
$sql[] = $query;
|
|
|
|
|
|
|
|
if (isset($options['indexes']) && ! empty($options['indexes'])) {
|
|
|
|
foreach($options['indexes'] as $index => $definition) {
|
|
|
|
$sql[] = $this->createIndexSql($name, $index, $definition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-30 01:51:49 +04:00
|
|
|
if (isset($options['foreignKeys'])) {
|
2007-07-21 19:17:17 +04:00
|
|
|
|
|
|
|
foreach ((array) $options['foreignKeys'] as $k => $definition) {
|
|
|
|
if (is_array($definition)) {
|
|
|
|
$sql[] = $this->createForeignKeySql($name, $definition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* createForeignKeySql
|
|
|
|
*
|
|
|
|
* @param string $table name of the table on which the foreign key is to be created
|
|
|
|
* @param array $definition associative array that defines properties of the foreign key to be created.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function createForeignKeySql($table, array $definition)
|
|
|
|
{
|
|
|
|
$table = $this->conn->quoteIdentifier($table);
|
|
|
|
|
|
|
|
$query = 'ALTER TABLE ' . $table . ' ADD ' . $this->getForeignKeyDeclaration($definition);
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
2007-02-17 13:00:04 +03:00
|
|
|
}
|
|
|
|
|