123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- <?php
- namespace Doctrine\DBAL\Schema;
- use Doctrine\DBAL\DBALException;
- use Doctrine\DBAL\Driver\Exception;
- use Doctrine\DBAL\Platforms\OraclePlatform;
- use Doctrine\DBAL\Types\Type;
- use Throwable;
- use function array_change_key_case;
- use function array_values;
- use function assert;
- use function preg_match;
- use function sprintf;
- use function str_replace;
- use function strpos;
- use function strtolower;
- use function strtoupper;
- use function trim;
- use const CASE_LOWER;
- /**
- * Oracle Schema Manager.
- */
- class OracleSchemaManager extends AbstractSchemaManager
- {
- /**
- * {@inheritdoc}
- */
- public function dropDatabase($database)
- {
- try {
- parent::dropDatabase($database);
- } catch (DBALException $exception) {
- $exception = $exception->getPrevious();
- assert($exception instanceof Throwable);
- if (! $exception instanceof Exception) {
- throw $exception;
- }
- // If we have a error code 1940 (ORA-01940), the drop database operation failed
- // because of active connections on the database.
- // To force dropping the database, we first have to close all active connections
- // on that database and issue the drop database operation again.
- if ($exception->getErrorCode() !== 1940) {
- throw $exception;
- }
- $this->killUserSessions($database);
- parent::dropDatabase($database);
- }
- }
- /**
- * {@inheritdoc}
- */
- protected function _getPortableViewDefinition($view)
- {
- $view = array_change_key_case($view, CASE_LOWER);
- return new View($this->getQuotedIdentifierName($view['view_name']), $view['text']);
- }
- /**
- * {@inheritdoc}
- */
- protected function _getPortableUserDefinition($user)
- {
- $user = array_change_key_case($user, CASE_LOWER);
- return [
- 'user' => $user['username'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- protected function _getPortableTableDefinition($table)
- {
- $table = array_change_key_case($table, CASE_LOWER);
- return $this->getQuotedIdentifierName($table['table_name']);
- }
- /**
- * {@inheritdoc}
- *
- * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
- */
- protected function _getPortableTableIndexesList($tableIndexes, $tableName = null)
- {
- $indexBuffer = [];
- foreach ($tableIndexes as $tableIndex) {
- $tableIndex = array_change_key_case($tableIndex, CASE_LOWER);
- $keyName = strtolower($tableIndex['name']);
- $buffer = [];
- if (strtolower($tableIndex['is_primary']) === 'p') {
- $keyName = 'primary';
- $buffer['primary'] = true;
- $buffer['non_unique'] = false;
- } else {
- $buffer['primary'] = false;
- $buffer['non_unique'] = ! $tableIndex['is_unique'];
- }
- $buffer['key_name'] = $keyName;
- $buffer['column_name'] = $this->getQuotedIdentifierName($tableIndex['column_name']);
- $indexBuffer[] = $buffer;
- }
- return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
- }
- /**
- * {@inheritdoc}
- */
- protected function _getPortableTableColumnDefinition($tableColumn)
- {
- $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
- $dbType = strtolower($tableColumn['data_type']);
- if (strpos($dbType, 'timestamp(') === 0) {
- if (strpos($dbType, 'with time zone')) {
- $dbType = 'timestamptz';
- } else {
- $dbType = 'timestamp';
- }
- }
- $unsigned = $fixed = $precision = $scale = $length = null;
- if (! isset($tableColumn['column_name'])) {
- $tableColumn['column_name'] = '';
- }
- // Default values returned from database sometimes have trailing spaces.
- $tableColumn['data_default'] = trim($tableColumn['data_default']);
- if ($tableColumn['data_default'] === '' || $tableColumn['data_default'] === 'NULL') {
- $tableColumn['data_default'] = null;
- }
- if ($tableColumn['data_default'] !== null) {
- // Default values returned from database are represented as literal expressions
- if (preg_match('/^\'(.*)\'$/s', $tableColumn['data_default'], $matches)) {
- $tableColumn['data_default'] = str_replace("''", "'", $matches[1]);
- }
- }
- if ($tableColumn['data_precision'] !== null) {
- $precision = (int) $tableColumn['data_precision'];
- }
- if ($tableColumn['data_scale'] !== null) {
- $scale = (int) $tableColumn['data_scale'];
- }
- $type = $this->_platform->getDoctrineTypeMapping($dbType);
- $type = $this->extractDoctrineTypeFromComment($tableColumn['comments'], $type);
- $tableColumn['comments'] = $this->removeDoctrineTypeFromComment($tableColumn['comments'], $type);
- switch ($dbType) {
- case 'number':
- if ($precision === 20 && $scale === 0) {
- $type = 'bigint';
- } elseif ($precision === 5 && $scale === 0) {
- $type = 'smallint';
- } elseif ($precision === 1 && $scale === 0) {
- $type = 'boolean';
- } elseif ($scale > 0) {
- $type = 'decimal';
- }
- break;
- case 'varchar':
- case 'varchar2':
- case 'nvarchar2':
- $length = $tableColumn['char_length'];
- $fixed = false;
- break;
- case 'char':
- case 'nchar':
- $length = $tableColumn['char_length'];
- $fixed = true;
- break;
- }
- $options = [
- 'notnull' => $tableColumn['nullable'] === 'N',
- 'fixed' => (bool) $fixed,
- 'unsigned' => (bool) $unsigned,
- 'default' => $tableColumn['data_default'],
- 'length' => $length,
- 'precision' => $precision,
- 'scale' => $scale,
- 'comment' => isset($tableColumn['comments']) && $tableColumn['comments'] !== ''
- ? $tableColumn['comments']
- : null,
- ];
- return new Column($this->getQuotedIdentifierName($tableColumn['column_name']), Type::getType($type), $options);
- }
- /**
- * {@inheritdoc}
- */
- protected function _getPortableTableForeignKeysList($tableForeignKeys)
- {
- $list = [];
- foreach ($tableForeignKeys as $value) {
- $value = array_change_key_case($value, CASE_LOWER);
- if (! isset($list[$value['constraint_name']])) {
- if ($value['delete_rule'] === 'NO ACTION') {
- $value['delete_rule'] = null;
- }
- $list[$value['constraint_name']] = [
- 'name' => $this->getQuotedIdentifierName($value['constraint_name']),
- 'local' => [],
- 'foreign' => [],
- 'foreignTable' => $value['references_table'],
- 'onDelete' => $value['delete_rule'],
- ];
- }
- $localColumn = $this->getQuotedIdentifierName($value['local_column']);
- $foreignColumn = $this->getQuotedIdentifierName($value['foreign_column']);
- $list[$value['constraint_name']]['local'][$value['position']] = $localColumn;
- $list[$value['constraint_name']]['foreign'][$value['position']] = $foreignColumn;
- }
- $result = [];
- foreach ($list as $constraint) {
- $result[] = new ForeignKeyConstraint(
- array_values($constraint['local']),
- $this->getQuotedIdentifierName($constraint['foreignTable']),
- array_values($constraint['foreign']),
- $this->getQuotedIdentifierName($constraint['name']),
- ['onDelete' => $constraint['onDelete']]
- );
- }
- return $result;
- }
- /**
- * {@inheritdoc}
- */
- protected function _getPortableSequenceDefinition($sequence)
- {
- $sequence = array_change_key_case($sequence, CASE_LOWER);
- return new Sequence(
- $this->getQuotedIdentifierName($sequence['sequence_name']),
- (int) $sequence['increment_by'],
- (int) $sequence['min_value']
- );
- }
- /**
- * {@inheritdoc}
- *
- * @deprecated
- */
- protected function _getPortableFunctionDefinition($function)
- {
- $function = array_change_key_case($function, CASE_LOWER);
- return $function['name'];
- }
- /**
- * {@inheritdoc}
- */
- protected function _getPortableDatabaseDefinition($database)
- {
- $database = array_change_key_case($database, CASE_LOWER);
- return $database['username'];
- }
- /**
- * {@inheritdoc}
- *
- * @param string|null $database
- *
- * Calling this method without an argument or by passing NULL is deprecated.
- */
- public function createDatabase($database = null)
- {
- if ($database === null) {
- $database = $this->_conn->getDatabase();
- }
- $statement = 'CREATE USER ' . $database;
- $params = $this->_conn->getParams();
- if (isset($params['password'])) {
- $statement .= ' IDENTIFIED BY ' . $params['password'];
- }
- $this->_conn->executeStatement($statement);
- $statement = 'GRANT DBA TO ' . $database;
- $this->_conn->executeStatement($statement);
- }
- /**
- * @param string $table
- *
- * @return bool
- */
- public function dropAutoincrement($table)
- {
- assert($this->_platform instanceof OraclePlatform);
- $sql = $this->_platform->getDropAutoincrementSql($table);
- foreach ($sql as $query) {
- $this->_conn->executeStatement($query);
- }
- return true;
- }
- /**
- * {@inheritdoc}
- */
- public function dropTable($name)
- {
- $this->tryMethod('dropAutoincrement', $name);
- parent::dropTable($name);
- }
- /**
- * Returns the quoted representation of the given identifier name.
- *
- * Quotes non-uppercase identifiers explicitly to preserve case
- * and thus make references to the particular identifier work.
- *
- * @param string $identifier The identifier to quote.
- *
- * @return string The quoted identifier.
- */
- private function getQuotedIdentifierName($identifier)
- {
- if (preg_match('/[a-z]/', $identifier)) {
- return $this->_platform->quoteIdentifier($identifier);
- }
- return $identifier;
- }
- /**
- * Kills sessions connected with the given user.
- *
- * This is useful to force DROP USER operations which could fail because of active user sessions.
- *
- * @param string $user The name of the user to kill sessions for.
- *
- * @return void
- */
- private function killUserSessions($user)
- {
- $sql = <<<SQL
- SELECT
- s.sid,
- s.serial#
- FROM
- gv\$session s,
- gv\$process p
- WHERE
- s.username = ?
- AND p.addr(+) = s.paddr
- SQL;
- $activeUserSessions = $this->_conn->fetchAllAssociative($sql, [strtoupper($user)]);
- foreach ($activeUserSessions as $activeUserSession) {
- $activeUserSession = array_change_key_case($activeUserSession, CASE_LOWER);
- $this->_execSql(
- sprintf(
- "ALTER SYSTEM KILL SESSION '%s, %s' IMMEDIATE",
- $activeUserSession['sid'],
- $activeUserSession['serial#']
- )
- );
- }
- }
- /**
- * {@inheritdoc}
- */
- public function listTableDetails($name): Table
- {
- $table = parent::listTableDetails($name);
- $platform = $this->_platform;
- assert($platform instanceof OraclePlatform);
- $sql = $platform->getListTableCommentsSQL($name);
- $tableOptions = $this->_conn->fetchAssociative($sql);
- if ($tableOptions !== false) {
- $table->addOption('comment', $tableOptions['COMMENTS']);
- }
- return $table;
- }
- }
|