OracleSchemaManager.php 9.82 KB
Newer Older
romanb's avatar
romanb committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?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>.
 */

22
namespace Doctrine\DBAL\Schema;
romanb's avatar
romanb committed
23 24

/**
25
 * Oracle Schema Manager
romanb's avatar
romanb committed
26 27 28 29
 *
 * @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)
30
 * @author      Benjamin Eberlei <kontakt@beberlei.de>
romanb's avatar
romanb committed
31 32 33
 * @version     $Revision$
 * @since       2.0
 */
34
class OracleSchemaManager extends AbstractSchemaManager
35 36
{
    protected function _getPortableViewDefinition($view)
romanb's avatar
romanb committed
37
    {
38 39
        $view = \array_change_key_case($view, CASE_LOWER);

40
        return new View($view['view_name'], $view['text']);
romanb's avatar
romanb committed
41 42
    }

43
    protected function _getPortableUserDefinition($user)
romanb's avatar
romanb committed
44
    {
45 46
        $user = \array_change_key_case($user, CASE_LOWER);

47 48 49 50
        return array(
            'user' => $user['username'],
        );
    }
romanb's avatar
romanb committed
51

52 53
    protected function _getPortableTableDefinition($table)
    {
54 55
        $table = \array_change_key_case($table, CASE_LOWER);

56
        return $table['table_name'];
romanb's avatar
romanb committed
57 58
    }

59 60 61 62 63 64 65 66
    /**
     * @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)
romanb's avatar
romanb committed
67
    {
68 69
        $indexBuffer = array();
        foreach ( $tableIndexes as $tableIndex ) {
70 71 72
            $tableIndex = \array_change_key_case($tableIndex, CASE_LOWER);

            $keyName = strtolower($tableIndex['name']);
73

74
            if ( strtolower($tableIndex['is_primary']) == "p" ) {
75 76 77 78 79 80 81 82 83 84 85
                $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;
        }
86
        return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
87
    }
romanb's avatar
romanb committed
88

89 90
    protected function _getPortableTableColumnDefinition($tableColumn)
    {
91 92
        $tableColumn = \array_change_key_case($tableColumn, CASE_LOWER);
        
93
        $dbType = strtolower($tableColumn['data_type']);
94
        if(strpos($dbType, "timestamp(") === 0) {
95 96 97 98 99
            if (strpos($dbType, "WITH TIME ZONE")) {
                $dbType = "timestamptz";
            } else {
                $dbType = "timestamp";
            }
100 101
        }

102 103 104 105 106
        $type = array();
        $length = $unsigned = $fixed = null;
        if ( ! empty($tableColumn['data_length'])) {
            $length = $tableColumn['data_length'];
        }
romanb's avatar
romanb committed
107

108 109 110
        if ( ! isset($tableColumn['column_name'])) {
            $tableColumn['column_name'] = '';
        }
romanb's avatar
romanb committed
111

112 113
        if (stripos($tableColumn['data_default'], 'NULL') !== null) {
            $tableColumn['data_default'] = null;
romanb's avatar
romanb committed
114 115
        }

116 117
        $precision = null;
        $scale = null;
118 119

        $type = $this->_platform->getDoctrineTypeMapping($dbType);
120 121 122
        $type = $this->extractDoctrineTypeFromComment($tableColumn['comments'], $type);
        $tableColumn['comments'] = $this->removeDoctrineTypeFromComment($tableColumn['comments'], $type);

123
        switch ($dbType) {
124
            case 'number':
125 126 127 128 129 130 131 132 133 134 135 136 137
                if ($tableColumn['data_precision'] == 20 && $tableColumn['data_scale'] == 0) {
                    $precision = 20;
                    $scale = 0;
                    $type = 'bigint';
                } elseif ($tableColumn['data_precision'] == 5 && $tableColumn['data_scale'] == 0) {
                    $type = 'smallint';
                    $precision = 5;
                    $scale = 0;
                } elseif ($tableColumn['data_precision'] == 1 && $tableColumn['data_scale'] == 0) {
                    $precision = 1;
                    $scale = 0;
                    $type = 'boolean';
                } elseif ($tableColumn['data_scale'] > 0) {
138 139
                    $precision = $tableColumn['data_precision'];
                    $scale = $tableColumn['data_scale'];
140
                    $type = 'decimal';
141
                }
142 143
                $length = null;
                break;
144 145
            case 'pls_integer':
            case 'binary_integer':
146
                $length = null;
147 148 149 150
                break;
            case 'varchar':
            case 'varchar2':
            case 'nvarchar2':
151
                $length = $tableColumn['char_length'];
152
                $fixed = false;
153
                break;
154 155
            case 'char':
            case 'nchar':
156
                $length = $tableColumn['char_length'];
157
                $fixed = true;
158 159 160 161 162 163
                break;
            case 'date':
            case 'timestamp':
                $length = null;
                break;
            case 'float':
164 165
                $precision = $tableColumn['data_precision'];
                $scale = $tableColumn['data_scale'];
166
                $length = null;
167 168 169
                break;
            case 'clob':
            case 'nclob':
170
                $length = null;
171 172 173 174 175 176
                break;
            case 'blob':
            case 'raw':
            case 'long raw':
            case 'bfile':
                $length = null;
177
                break;
178 179 180 181 182
            case 'rowid':
            case 'urowid':
            default:
                $length = null;
        }
romanb's avatar
romanb committed
183

184
        $options = array(
185 186 187 188 189 190 191
            'notnull'    => (bool) ($tableColumn['nullable'] === 'N'),
            'fixed'      => (bool) $fixed,
            'unsigned'   => (bool) $unsigned,
            'default'    => $tableColumn['data_default'],
            'length'     => $length,
            'precision'  => $precision,
            'scale'      => $scale,
192
            'comment'       => (isset($tableColumn['comments'])) ? $tableColumn['comments'] : null,
193
            'platformDetails' => array(),
194
        );
195

196
        return new Column($tableColumn['column_name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
197 198 199 200 201 202 203 204
    }

    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']])) {
205 206 207 208
                if ($value['delete_rule'] == "NO ACTION") {
                    $value['delete_rule'] = null;
                }

209 210 211 212
                $list[$value['constraint_name']] = array(
                    'name' => $value['constraint_name'],
                    'local' => array(),
                    'foreign' => array(),
213
                    'foreignTable' => $value['references_table'],
214 215 216 217 218 219 220 221 222
                    '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) {
223
            $result[] = new ForeignKeyConstraint(
224 225 226 227 228 229 230 231 232 233 234 235
                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);
236
        return new Sequence($sequence['sequence_name'], $sequence['increment_by'], $sequence['min_value']);
romanb's avatar
romanb committed
237 238
    }

239 240
    protected function _getPortableFunctionDefinition($function)
    {
241
        $function = \array_change_key_case($function, CASE_LOWER);
242 243
        return $function['name'];
    }
romanb's avatar
romanb committed
244

245 246
    protected function _getPortableDatabaseDefinition($database)
    {
247
        $database = \array_change_key_case($database, CASE_LOWER);
248 249
        return $database['username'];
    }
romanb's avatar
romanb committed
250

251 252 253 254 255
    public function createDatabase($database = null)
    {
        if (is_null($database)) {
            $database = $this->_conn->getDatabase();
        }
romanb's avatar
romanb committed
256

257 258 259
        $params = $this->_conn->getParams();
        $username   = $database;
        $password   = $params['password'];
romanb's avatar
romanb committed
260

261
        $query  = 'CREATE USER ' . $username . ' IDENTIFIED BY ' . $password;
262
        $result = $this->_conn->executeUpdate($query);
263

romanb's avatar
romanb committed
264
        $query = 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO ' . $username;
265
        $result = $this->_conn->executeUpdate($query);
266 267

        return true;
romanb's avatar
romanb committed
268
    }
269 270

    public function dropAutoincrement($table)
romanb's avatar
romanb committed
271
    {
272 273
        $sql = $this->_platform->getDropAutoincrementSql($table);
        foreach ($sql as $query) {
274
            $this->_conn->executeUpdate($query);
275 276 277
        }

        return true;
romanb's avatar
romanb committed
278 279 280 281
    }

    public function dropTable($name)
    {
jwage's avatar
jwage committed
282
        $this->dropAutoincrement($name);
romanb's avatar
romanb committed
283

284
        return parent::dropTable($name);
romanb's avatar
romanb committed
285
    }
286
}