2008-08-09 13:45:28 +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
|
2009-05-30 15:33:06 +04:00
|
|
|
* <http://www.doctrine-project.org>.
|
2008-08-09 13:45:28 +04:00
|
|
|
*/
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\DBAL\Schema;
|
2008-08-09 13:45:28 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* xxx
|
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
|
2009-11-03 11:58:03 +03:00
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
2008-08-09 13:45:28 +04:00
|
|
|
* @version $Revision$
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class PostgreSqlSchemaManager extends AbstractSchemaManager
|
2009-01-07 20:46:02 +03:00
|
|
|
{
|
2009-05-29 01:34:35 +04:00
|
|
|
protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
|
2008-08-09 13:45:28 +04:00
|
|
|
{
|
2009-11-30 22:02:05 +03:00
|
|
|
$onUpdate = null;
|
|
|
|
$onDelete = null;
|
2009-05-29 01:34:35 +04:00
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
if(preg_match('(ON UPDATE ([a-zA-Z0-9]+))', $tableForeignKey['condef'], $match)) {
|
|
|
|
$onUpdate = $match[1];
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
2009-11-30 22:02:05 +03:00
|
|
|
if(preg_match('(ON DELETE ([a-zA-Z0-9]+))', $tableForeignKey['condef'], $match)) {
|
|
|
|
$onDelete = $match[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(preg_match('/FOREIGN KEY \((.+)\) REFERENCES (.+)\((.+)\)/', $tableForeignKey['condef'], $values)) {
|
|
|
|
$localColumns = explode(",", $values[1]);
|
|
|
|
$foreignColumns = explode(",", $values[3]);
|
|
|
|
$foreignTable = $values[2];
|
|
|
|
}
|
|
|
|
|
2009-12-05 01:41:09 +03:00
|
|
|
return new ForeignKeyConstraint(
|
2009-12-03 16:17:56 +03:00
|
|
|
$localColumns, $foreignTable, $foreignColumns, $tableForeignKey['conname'],
|
2009-11-30 22:02:05 +03:00
|
|
|
array('onUpdate' => $onUpdate, 'onDelete' => $onDelete)
|
|
|
|
);
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
|
|
|
|
2009-11-25 19:27:18 +03:00
|
|
|
public function dropDatabase($database)
|
|
|
|
{
|
|
|
|
$params = $this->_conn->getParams();
|
|
|
|
$params["dbname"] = "postgres";
|
|
|
|
$tmpPlatform = $this->_platform;
|
|
|
|
$tmpConn = $this->_conn;
|
|
|
|
|
|
|
|
$this->_conn = \Doctrine\DBAL\DriverManager::getConnection($params);
|
|
|
|
$this->_platform = $this->_conn->getDatabasePlatform();
|
|
|
|
|
|
|
|
parent::dropDatabase($database);
|
|
|
|
|
|
|
|
$this->_platform = $tmpPlatform;
|
|
|
|
$this->_conn = $tmpConn;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createDatabase($database)
|
|
|
|
{
|
|
|
|
$params = $this->_conn->getParams();
|
|
|
|
$params["dbname"] = "postgres";
|
|
|
|
$tmpPlatform = $this->_platform;
|
|
|
|
$tmpConn = $this->_conn;
|
|
|
|
|
|
|
|
$this->_conn = \Doctrine\DBAL\DriverManager::getConnection($params);
|
|
|
|
$this->_platform = $this->_conn->getDatabasePlatform();
|
|
|
|
|
|
|
|
parent::createDatabase($database);
|
|
|
|
|
|
|
|
$this->_platform = $tmpPlatform;
|
|
|
|
$this->_conn = $tmpConn;
|
2009-12-03 16:17:56 +03:00
|
|
|
}
|
2009-11-25 19:27:18 +03:00
|
|
|
|
2009-05-29 01:34:35 +04:00
|
|
|
protected function _getPortableTriggerDefinition($trigger)
|
2008-08-09 13:45:28 +04:00
|
|
|
{
|
2009-05-29 01:34:35 +04:00
|
|
|
return $trigger['trigger_name'];
|
|
|
|
}
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-05-29 01:34:35 +04:00
|
|
|
protected function _getPortableViewDefinition($view)
|
|
|
|
{
|
2010-02-11 02:41:35 +03:00
|
|
|
return new View($view['viewname'], $view['definition']);
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
|
|
|
|
2009-05-29 01:34:35 +04:00
|
|
|
protected function _getPortableUserDefinition($user)
|
2008-08-09 13:45:28 +04:00
|
|
|
{
|
2009-05-29 01:34:35 +04:00
|
|
|
return array(
|
|
|
|
'user' => $user['usename'],
|
|
|
|
'password' => $user['passwd']
|
|
|
|
);
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
|
|
|
|
2009-05-29 01:34:35 +04:00
|
|
|
protected function _getPortableTableDefinition($table)
|
2008-08-09 13:45:28 +04:00
|
|
|
{
|
2009-05-29 01:34:35 +04:00
|
|
|
return $table['table_name'];
|
|
|
|
}
|
|
|
|
|
2009-11-03 11:58:03 +03:00
|
|
|
/**
|
|
|
|
* @license New BSD License
|
|
|
|
* @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
|
|
|
|
* @param array $tableIndexes
|
|
|
|
* @param string $tableName
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
|
2009-05-29 01:34:35 +04:00
|
|
|
{
|
2009-11-03 11:58:03 +03:00
|
|
|
$buffer = array();
|
|
|
|
foreach($tableIndexes AS $row) {
|
|
|
|
$colNumbers = explode( ' ', $row['indkey'] );
|
|
|
|
$colNumbersSql = 'IN (' . join( ' ,', $colNumbers ) . ' )';
|
2009-12-06 15:13:15 +03:00
|
|
|
$columnNameSql = "SELECT attnum, attname FROM pg_attribute
|
|
|
|
WHERE attrelid={$row['indrelid']} AND attnum $colNumbersSql ORDER BY attnum ASC;";
|
2009-11-03 11:58:03 +03:00
|
|
|
|
2010-04-01 01:13:34 +04:00
|
|
|
$stmt = $this->_conn->executeQuery($columnNameSql);
|
2009-11-03 11:58:03 +03:00
|
|
|
$indexColumns = $stmt->fetchAll();
|
|
|
|
|
2009-12-06 15:13:15 +03:00
|
|
|
// required for getting the order of the columns right.
|
|
|
|
foreach ($colNumbers AS $colNum) {
|
|
|
|
foreach ( $indexColumns as $colRow ) {
|
|
|
|
if ($colNum == $colRow['attnum']) {
|
|
|
|
$buffer[] = array(
|
|
|
|
'key_name' => $row['relname'],
|
|
|
|
'column_name' => $colRow['attname'],
|
|
|
|
'non_unique' => !$row['indisunique'],
|
|
|
|
'primary' => $row['indisprimary']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2009-11-03 11:58:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return parent::_getPortableTableIndexesList($buffer);
|
2009-05-29 01:34:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function _getPortableDatabaseDefinition($database)
|
|
|
|
{
|
|
|
|
return $database['datname'];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _getPortableSequenceDefinition($sequence)
|
|
|
|
{
|
2009-12-03 16:17:56 +03:00
|
|
|
$data = $this->_conn->fetchAll('SELECT min_value, increment_by FROM '.$sequence['relname']);
|
2009-12-05 01:41:09 +03:00
|
|
|
return new Sequence($sequence['relname'], $data[0]['increment_by'], $data[0]['min_value']);
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
|
|
|
|
2009-05-29 01:34:35 +04:00
|
|
|
protected function _getPortableTableColumnDefinition($tableColumn)
|
|
|
|
{
|
|
|
|
$tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
|
|
|
|
|
|
|
|
if (strtolower($tableColumn['type']) === 'varchar') {
|
|
|
|
// get length from varchar definition
|
|
|
|
$length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $tableColumn['complete_type']);
|
|
|
|
$tableColumn['length'] = $length;
|
|
|
|
}
|
|
|
|
|
2009-10-15 00:18:36 +04:00
|
|
|
$matches = array();
|
2009-05-29 01:34:35 +04:00
|
|
|
|
|
|
|
if (preg_match("/^nextval\('(.*)'(::.*)?\)$/", $tableColumn['default'], $matches)) {
|
|
|
|
$tableColumn['sequence'] = $matches[1];
|
|
|
|
$tableColumn['default'] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stripos($tableColumn['default'], 'NULL') !== null) {
|
|
|
|
$tableColumn['default'] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$length = (isset($tableColumn['length'])) ? $tableColumn['length'] : null;
|
|
|
|
if ($length == '-1' && isset($tableColumn['atttypmod'])) {
|
|
|
|
$length = $tableColumn['atttypmod'] - 4;
|
|
|
|
}
|
|
|
|
if ((int)$length <= 0) {
|
|
|
|
$length = null;
|
|
|
|
}
|
|
|
|
$type = array();
|
2009-10-15 00:18:36 +04:00
|
|
|
$fixed = null;
|
2009-05-29 01:34:35 +04:00
|
|
|
|
|
|
|
if ( ! isset($tableColumn['name'])) {
|
|
|
|
$tableColumn['name'] = '';
|
|
|
|
}
|
2009-11-03 19:56:05 +03:00
|
|
|
|
|
|
|
$precision = null;
|
|
|
|
$scale = null;
|
2009-05-29 01:34:35 +04:00
|
|
|
|
|
|
|
$dbType = strtolower($tableColumn['type']);
|
2009-12-01 02:12:26 +03:00
|
|
|
|
|
|
|
$autoincrement = false;
|
2009-05-29 01:34:35 +04:00
|
|
|
switch ($dbType) {
|
|
|
|
case 'smallint':
|
|
|
|
case 'int2':
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'smallint';
|
|
|
|
$length = null;
|
2009-05-29 01:34:35 +04:00
|
|
|
break;
|
2009-12-01 02:12:26 +03:00
|
|
|
case 'serial':
|
|
|
|
case 'serial4':
|
|
|
|
$autoincrement = true;
|
|
|
|
// break missing intentionally
|
2009-05-29 01:34:35 +04:00
|
|
|
case 'int':
|
|
|
|
case 'int4':
|
|
|
|
case 'integer':
|
|
|
|
$type = 'integer';
|
2009-10-15 00:18:36 +04:00
|
|
|
$length = null;
|
2009-05-29 01:34:35 +04:00
|
|
|
break;
|
|
|
|
case 'bigserial':
|
|
|
|
case 'serial8':
|
2009-12-01 02:12:26 +03:00
|
|
|
$autoincrement = true;
|
|
|
|
// break missing intentionally
|
|
|
|
case 'bigint':
|
|
|
|
case 'int8':
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'bigint';
|
|
|
|
$length = null;
|
2009-05-29 01:34:35 +04:00
|
|
|
break;
|
|
|
|
case 'bool':
|
|
|
|
case 'boolean':
|
|
|
|
$type = 'boolean';
|
2009-10-15 00:18:36 +04:00
|
|
|
$length = null;
|
2009-05-29 01:34:35 +04:00
|
|
|
break;
|
|
|
|
case 'text':
|
2009-10-01 16:00:14 +04:00
|
|
|
$fixed = false;
|
|
|
|
$type = 'text';
|
|
|
|
break;
|
2009-05-29 01:34:35 +04:00
|
|
|
case 'varchar':
|
|
|
|
case 'interval':
|
|
|
|
case '_varchar':
|
|
|
|
$fixed = false;
|
|
|
|
case 'tsvector':
|
|
|
|
case 'unknown':
|
|
|
|
case 'char':
|
|
|
|
case 'bpchar':
|
|
|
|
$type = 'string';
|
|
|
|
if ($length == '1') {
|
|
|
|
if (preg_match('/^(is|has)/', $tableColumn['name'])) {
|
|
|
|
$type = 'boolean';
|
|
|
|
}
|
|
|
|
} elseif (strstr($dbType, 'text')) {
|
2009-10-01 16:00:14 +04:00
|
|
|
$type = 'text';
|
2009-05-29 01:34:35 +04:00
|
|
|
}
|
|
|
|
if ($fixed !== false) {
|
|
|
|
$fixed = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'date':
|
|
|
|
$type = 'date';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'datetime':
|
|
|
|
case 'timestamp':
|
|
|
|
case 'timetz':
|
|
|
|
case 'timestamptz':
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'datetime';
|
2009-05-29 01:34:35 +04:00
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'time':
|
|
|
|
$type = 'time';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'float':
|
|
|
|
case 'float4':
|
|
|
|
case 'float8':
|
|
|
|
case 'double':
|
|
|
|
case 'double precision':
|
|
|
|
case 'real':
|
|
|
|
case 'decimal':
|
|
|
|
case 'money':
|
|
|
|
case 'numeric':
|
2009-11-03 19:56:05 +03:00
|
|
|
if(preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['complete_type'], $match)) {
|
|
|
|
$precision = $match[1];
|
|
|
|
$scale = $match[2];
|
|
|
|
$length = null;
|
|
|
|
}
|
2009-05-29 01:34:35 +04:00
|
|
|
$type = 'decimal';
|
|
|
|
break;
|
|
|
|
case 'tinyblob':
|
|
|
|
case 'mediumblob':
|
|
|
|
case 'longblob':
|
|
|
|
case 'blob':
|
|
|
|
case 'bytea':
|
|
|
|
case 'geometry':
|
|
|
|
case 'geometrycollection':
|
|
|
|
case 'point':
|
|
|
|
case 'multipoint':
|
|
|
|
case 'linestring':
|
|
|
|
case 'multilinestring':
|
|
|
|
case 'polygon':
|
|
|
|
case 'multipolygon':
|
|
|
|
$type = 'blob';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'oid':
|
|
|
|
$type = 'blob';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'year':
|
|
|
|
$type = 'date';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$type = 'string';
|
|
|
|
}
|
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
$options = array(
|
2009-11-03 19:56:05 +03:00
|
|
|
'length' => $length,
|
2009-05-29 01:34:35 +04:00
|
|
|
'notnull' => (bool) $tableColumn['isnotnull'],
|
|
|
|
'default' => $tableColumn['default'],
|
|
|
|
'primary' => (bool) ($tableColumn['pri'] == 't'),
|
2009-11-03 19:56:05 +03:00
|
|
|
'precision' => $precision,
|
|
|
|
'scale' => $scale,
|
|
|
|
'fixed' => $fixed,
|
|
|
|
'unsigned' => false,
|
2009-12-01 02:12:26 +03:00
|
|
|
'platformDetails' => array(
|
|
|
|
'autoincrement' => $autoincrement,
|
|
|
|
),
|
2009-05-29 01:34:35 +04:00
|
|
|
);
|
|
|
|
|
2009-12-05 01:41:09 +03:00
|
|
|
return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
}
|