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
|
|
|
|
* <http://www.phpdoctrine.org>.
|
|
|
|
*/
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\DBAL\Schema;
|
2008-08-09 13:45:28 +04:00
|
|
|
|
|
|
|
/**
|
2010-02-07 15:36:30 +03:00
|
|
|
* Oracle Schema Manager
|
2008-08-09 13:45:28 +04:00
|
|
|
*
|
|
|
|
* @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)
|
2010-02-07 15:36:30 +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 OracleSchemaManager extends AbstractSchemaManager
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
|
|
|
protected function _getPortableViewDefinition($view)
|
2008-08-09 13:45:28 +04:00
|
|
|
{
|
2009-11-03 19:56:05 +03:00
|
|
|
$view = \array_change_key_case($view, CASE_LOWER);
|
|
|
|
|
2010-02-11 02:41:35 +03:00
|
|
|
return new View($view['view_name'], $view['text']);
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
protected function _getPortableUserDefinition($user)
|
2008-08-09 13:45:28 +04:00
|
|
|
{
|
2009-11-03 19:56:05 +03:00
|
|
|
$user = \array_change_key_case($user, CASE_LOWER);
|
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
return array(
|
|
|
|
'user' => $user['username'],
|
|
|
|
);
|
|
|
|
}
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
protected function _getPortableTableDefinition($table)
|
|
|
|
{
|
2009-11-03 19:56:05 +03:00
|
|
|
$table = \array_change_key_case($table, CASE_LOWER);
|
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
return $table['table_name'];
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
|
|
|
|
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)
|
2008-08-09 13:45:28 +04:00
|
|
|
{
|
2009-11-03 11:58:03 +03:00
|
|
|
$indexBuffer = array();
|
|
|
|
foreach ( $tableIndexes as $tableIndex ) {
|
2009-11-30 22:02:05 +03:00
|
|
|
$tableIndex = \array_change_key_case($tableIndex, CASE_LOWER);
|
|
|
|
|
|
|
|
$keyName = strtolower($tableIndex['name']);
|
2009-11-03 11:58:03 +03:00
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
if ( strtolower($tableIndex['is_primary']) == "p" ) {
|
2009-11-03 11:58:03 +03:00
|
|
|
$keyName = 'primary';
|
|
|
|
$buffer['primary'] = true;
|
|
|
|
$buffer['non_unique'] = false;
|
|
|
|
} else {
|
|
|
|
$buffer['primary'] = false;
|
|
|
|
$buffer['non_unique'] = ( $tableIndex['is_unique'] == 0 ) ? true : false;
|
|
|
|
}
|
|
|
|
$buffer['key_name'] = $keyName;
|
|
|
|
$buffer['column_name'] = $tableIndex['column_name'];
|
|
|
|
$indexBuffer[] = $buffer;
|
|
|
|
}
|
2009-11-30 22:02:05 +03:00
|
|
|
return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
protected function _getPortableTableColumnDefinition($tableColumn)
|
|
|
|
{
|
2009-11-03 19:56:05 +03:00
|
|
|
$tableColumn = \array_change_key_case($tableColumn, CASE_LOWER);
|
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
$dbType = strtolower($tableColumn['data_type']);
|
2009-11-03 19:56:05 +03:00
|
|
|
if(strpos($dbType, "timestamp(") === 0) {
|
|
|
|
$dbType = "timestamp";
|
|
|
|
}
|
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
$type = array();
|
|
|
|
$length = $unsigned = $fixed = null;
|
|
|
|
if ( ! empty($tableColumn['data_length'])) {
|
|
|
|
$length = $tableColumn['data_length'];
|
|
|
|
}
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
if ( ! isset($tableColumn['column_name'])) {
|
|
|
|
$tableColumn['column_name'] = '';
|
|
|
|
}
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
if (stripos($tableColumn['data_default'], 'NULL') !== null) {
|
|
|
|
$tableColumn['data_default'] = null;
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
|
|
|
|
2009-11-03 19:56:05 +03:00
|
|
|
$precision = null;
|
|
|
|
$scale = null;
|
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
switch ($dbType) {
|
|
|
|
case 'integer':
|
2009-10-15 00:18:36 +04:00
|
|
|
case 'number':
|
2009-11-03 19:56:05 +03:00
|
|
|
if($tableColumn['data_scale'] > 0) {
|
|
|
|
$type = 'decimal';
|
|
|
|
$precision = $tableColumn['data_precision'];
|
|
|
|
$scale = $tableColumn['data_scale'];
|
|
|
|
} else {
|
|
|
|
$type = 'integer';
|
|
|
|
}
|
2009-10-15 00:18:36 +04:00
|
|
|
$length = null;
|
|
|
|
break;
|
2009-05-30 06:27:50 +04:00
|
|
|
case 'pls_integer':
|
|
|
|
case 'binary_integer':
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'boolean';
|
|
|
|
$length = null;
|
2009-05-30 06:27:50 +04:00
|
|
|
break;
|
|
|
|
case 'varchar':
|
|
|
|
case 'varchar2':
|
|
|
|
case 'nvarchar2':
|
|
|
|
$fixed = false;
|
|
|
|
case 'char':
|
|
|
|
case 'nchar':
|
|
|
|
if ($length == '1' && preg_match('/^(is|has)/', $tableColumn['column_name'])) {
|
|
|
|
$type = 'boolean';
|
|
|
|
} else {
|
|
|
|
$type = 'string';
|
|
|
|
}
|
|
|
|
if ($fixed !== false) {
|
|
|
|
$fixed = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'date':
|
|
|
|
case 'timestamp':
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'datetime';
|
2009-05-30 06:27:50 +04:00
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'float':
|
2009-11-03 19:56:05 +03:00
|
|
|
$precision = $tableColumn['data_precision'];
|
|
|
|
$scale = $tableColumn['data_scale'];
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'decimal';
|
|
|
|
$length = null;
|
2009-05-30 06:27:50 +04:00
|
|
|
break;
|
|
|
|
case 'long':
|
|
|
|
$type = 'string';
|
|
|
|
case 'clob':
|
|
|
|
case 'nclob':
|
2009-11-03 19:56:05 +03:00
|
|
|
$length = null;
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'text';
|
2009-05-30 06:27:50 +04:00
|
|
|
break;
|
|
|
|
case 'blob':
|
|
|
|
case 'raw':
|
|
|
|
case 'long raw':
|
|
|
|
case 'bfile':
|
|
|
|
$type = 'blob';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'rowid':
|
|
|
|
case 'urowid':
|
|
|
|
default:
|
|
|
|
$type = 'string';
|
|
|
|
$length = null;
|
|
|
|
}
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
$options = array(
|
2009-11-03 19:56:05 +03:00
|
|
|
'notnull' => (bool) ($tableColumn['nullable'] === 'N'),
|
|
|
|
'fixed' => (bool) $fixed,
|
|
|
|
'unsigned' => (bool) $unsigned,
|
|
|
|
'default' => $tableColumn['data_default'],
|
|
|
|
'length' => $length,
|
|
|
|
'precision' => $precision,
|
|
|
|
'scale' => $scale,
|
|
|
|
'platformDetails' => array(),
|
2009-05-30 06:27:50 +04:00
|
|
|
);
|
2009-11-30 22:02:05 +03:00
|
|
|
|
2009-12-05 01:41:09 +03:00
|
|
|
return new Column($tableColumn['column_name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
|
2009-11-30 22:02:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function _getPortableTableForeignKeysList($tableForeignKeys)
|
|
|
|
{
|
|
|
|
$list = array();
|
|
|
|
foreach ($tableForeignKeys as $key => $value) {
|
|
|
|
$value = \array_change_key_case($value, CASE_LOWER);
|
|
|
|
if (!isset($list[$value['constraint_name']])) {
|
2010-02-07 15:36:30 +03:00
|
|
|
if ($value['delete_rule'] == "NO ACTION") {
|
|
|
|
$value['delete_rule'] = null;
|
|
|
|
}
|
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
$list[$value['constraint_name']] = array(
|
|
|
|
'name' => $value['constraint_name'],
|
|
|
|
'local' => array(),
|
|
|
|
'foreign' => array(),
|
2010-02-07 15:36:30 +03:00
|
|
|
'foreignTable' => $value['references_table'],
|
2009-11-30 22:02:05 +03:00
|
|
|
'onDelete' => $value['delete_rule'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$list[$value['constraint_name']]['local'][$value['position']] = $value['local_column'];
|
|
|
|
$list[$value['constraint_name']]['foreign'][$value['position']] = $value['foreign_column'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = array();
|
|
|
|
foreach($list AS $constraint) {
|
2009-12-05 01:41:09 +03:00
|
|
|
$result[] = new ForeignKeyConstraint(
|
2009-11-30 22:02:05 +03:00
|
|
|
array_values($constraint['local']), $constraint['foreignTable'],
|
|
|
|
array_values($constraint['foreign']), $constraint['name'],
|
|
|
|
array('onDelete' => $constraint['onDelete'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _getPortableSequenceDefinition($sequence)
|
|
|
|
{
|
|
|
|
$sequence = \array_change_key_case($sequence, CASE_LOWER);
|
2009-12-05 01:41:09 +03:00
|
|
|
return new Sequence($sequence['sequence_name'], $sequence['increment_by'], $sequence['min_value']);
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
protected function _getPortableFunctionDefinition($function)
|
|
|
|
{
|
2009-11-03 19:56:05 +03:00
|
|
|
$function = \array_change_key_case($function, CASE_LOWER);
|
2009-05-30 06:27:50 +04:00
|
|
|
return $function['name'];
|
|
|
|
}
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
protected function _getPortableDatabaseDefinition($database)
|
|
|
|
{
|
2009-11-03 19:56:05 +03:00
|
|
|
$database = \array_change_key_case($database, CASE_LOWER);
|
2009-05-30 06:27:50 +04:00
|
|
|
return $database['username'];
|
|
|
|
}
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
public function createDatabase($database = null)
|
|
|
|
{
|
|
|
|
if (is_null($database)) {
|
|
|
|
$database = $this->_conn->getDatabase();
|
|
|
|
}
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
$params = $this->_conn->getParams();
|
|
|
|
$username = $database;
|
|
|
|
$password = $params['password'];
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
$query = 'CREATE USER ' . $username . ' IDENTIFIED BY ' . $password;
|
2009-08-28 21:25:28 +04:00
|
|
|
$result = $this->_conn->executeUpdate($query);
|
2009-05-30 06:27:50 +04:00
|
|
|
|
2009-05-30 15:33:06 +04:00
|
|
|
$query = 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO ' . $username;
|
2009-08-28 21:25:28 +04:00
|
|
|
$result = $this->_conn->executeUpdate($query);
|
2009-05-30 06:27:50 +04:00
|
|
|
|
|
|
|
return true;
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
2009-05-30 06:27:50 +04:00
|
|
|
|
|
|
|
public function dropAutoincrement($table)
|
2008-08-09 13:45:28 +04:00
|
|
|
{
|
2009-05-30 06:27:50 +04:00
|
|
|
$sql = $this->_platform->getDropAutoincrementSql($table);
|
|
|
|
foreach ($sql as $query) {
|
2009-08-28 21:25:28 +04:00
|
|
|
$this->_conn->executeUpdate($query);
|
2009-05-30 06:27:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function dropTable($name)
|
|
|
|
{
|
2009-06-20 21:02:02 +04:00
|
|
|
$this->dropAutoincrement($name);
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-05-30 06:27:50 +04:00
|
|
|
return parent::dropTable($name);
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
}
|