Source for file Pgsql.php
Documentation is available at Pgsql.php
* $Id: Pgsql.php 2291 2007-08-29 22:03:55Z zYne $
* 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>.
* @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
* @version $Revision: 2291 $
* @param string $name name of the database that should be created
$query =
'CREATE DATABASE ' .
$this->conn->quoteIdentifier($name);
* drop an existing database
* @param string $name name of the database that should be dropped
$query =
'DROP DATABASE ' .
$this->conn->quoteIdentifier($name);
* getAdvancedForeignKeyOptions
* Return the FOREIGN KEY query section dealing with non-standard options
* as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
* @param array $definition foreign key definition
if (isset
($definition['match'])) {
$query .=
' MATCH ' .
$definition['match'];
if (isset
($definition['onUpdate'])) {
$query .=
' ON UPDATE ' .
$definition['onUpdate'];
if (isset
($definition['onDelete'])) {
$query .=
' ON DELETE ' .
$definition['onDelete'];
if (isset
($definition['deferrable'])) {
$query .=
' NOT DEFERRABLE';
if (isset
($definition['feferred'])) {
$query .=
' INITIALLY DEFERRED';
$query .=
' INITIALLY IMMEDIATE';
* 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:
* New name for the table.
* 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.
* 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.
* 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.
* 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.
* 'file_limit' => array(),
* 'time_limit' => array()
* @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 Doctrine_Connection_Exception
public function alterTable($name, array $changes, $check)
foreach ($changes as $changeName =>
$change) {
if (isset
($changes['add']) &&
is_array($changes['add'])) {
foreach ($changes['add'] as $fieldName =>
$field) {
$this->conn->exec('ALTER TABLE ' .
$name .
' ' .
$query);
if (isset
($changes['remove']) &&
is_array($changes['remove'])) {
foreach ($changes['remove'] as $fieldName =>
$field) {
$fieldName =
$this->conn->quoteIdentifier($fieldName, true);
$query =
'DROP ' .
$fieldName;
$this->conn->exec('ALTER TABLE ' .
$name .
' ' .
$query);
if (isset
($changes['change']) &&
is_array($changes['change'])) {
foreach ($changes['change'] as $fieldName =>
$field) {
$fieldName =
$this->conn->quoteIdentifier($fieldName, true);
if (isset
($field['type'])) {
$serverInfo =
$this->conn->getServerVersion();
if (is_array($serverInfo) &&
$serverInfo['major'] <
8) {
$query =
'ALTER ' .
$fieldName .
' TYPE ' .
$this->conn->datatype->getTypeDeclaration($field['definition']);
$this->conn->exec('ALTER TABLE ' .
$name .
' ' .
$query);;
$query =
'ALTER ' .
$fieldName .
' SET DEFAULT ' .
$this->conn->quote($field['definition']['default'], $field['definition']['type']);
$this->conn->exec('ALTER TABLE ' .
$name .
' ' .
$query);
if (!empty($field['notnull'])) {
$query =
'ALTER ' .
$fieldName .
' ' .
($field['definition']['notnull'] ?
'SET' :
'DROP') .
' NOT NULL';
$this->conn->exec('ALTER TABLE ' .
$name .
' ' .
$query);
if (isset
($changes['rename']) &&
is_array($changes['rename'])) {
foreach ($changes['rename'] as $fieldName =>
$field) {
$fieldName =
$this->conn->quoteIdentifier($fieldName, true);
$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);
* return RDBMS specific create sequence statement
* @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:
* 'collate' => 'utf8_unicode_ci',
public function createSequenceSql($sequenceName, $start =
1, array $options =
array())
$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);
* @param string $sequenceName name of the sequence to be dropped
$sequenceName =
$this->conn->quoteIdentifier($this->conn->formatter->getSequenceName($sequenceName), true);
return 'DROP SEQUENCE ' .
$sequenceName;
* @param unknown_type $name
public function createTableSql($name, array $fields, array $options =
array())
if (isset
($options['primary']) &&
! empty($options['primary'])) {
$query =
'CREATE TABLE ' .
$this->conn->quoteIdentifier($name, true) .
' (' .
$queryFields .
')';
if (isset
($options['indexes']) &&
! empty($options['indexes'])) {
foreach($options['indexes'] as $index =>
$definition) {
if (isset
($options['foreignKeys'])) {
foreach ((array)
$options['foreignKeys'] as $k =>
$definition) {
* @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.
$table =
$this->conn->quoteIdentifier($table);