OracleSchemaManager.php 10.1 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
<?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
Benjamin Eberlei's avatar
Benjamin Eberlei committed
18
 * and is licensed under the MIT license. For more information, see
romanb's avatar
romanb committed
19 20 21
 * <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
        $tableColumn = \array_change_key_case($tableColumn, CASE_LOWER);
92

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
        if ($tableColumn['data_default'] === 'NULL') {
113
            $tableColumn['data_default'] = null;
romanb's avatar
romanb committed
114 115
        }

116 117 118 119 120 121
        if (null !== $tableColumn['data_default']) {
            // Default values returned from database are enclosed in single quotes.
            // Sometimes trailing spaces are also encountered.
            $tableColumn['data_default'] = trim(trim($tableColumn['data_default']), "'");
        }

122 123
        $precision = null;
        $scale = null;
124 125

        $type = $this->_platform->getDoctrineTypeMapping($dbType);
126 127 128
        $type = $this->extractDoctrineTypeFromComment($tableColumn['comments'], $type);
        $tableColumn['comments'] = $this->removeDoctrineTypeFromComment($tableColumn['comments'], $type);

129
        switch ($dbType) {
130
            case 'number':
131 132 133 134 135 136 137 138 139 140 141 142 143
                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) {
144 145
                    $precision = $tableColumn['data_precision'];
                    $scale = $tableColumn['data_scale'];
146
                    $type = 'decimal';
147
                }
148 149
                $length = null;
                break;
150 151
            case 'pls_integer':
            case 'binary_integer':
152
                $length = null;
153 154 155 156
                break;
            case 'varchar':
            case 'varchar2':
            case 'nvarchar2':
157
                $length = $tableColumn['char_length'];
158
                $fixed = false;
159
                break;
160 161
            case 'char':
            case 'nchar':
162
                $length = $tableColumn['char_length'];
163
                $fixed = true;
164 165 166 167 168 169
                break;
            case 'date':
            case 'timestamp':
                $length = null;
                break;
            case 'float':
170 171
                $precision = $tableColumn['data_precision'];
                $scale = $tableColumn['data_scale'];
172
                $length = null;
173 174 175
                break;
            case 'clob':
            case 'nclob':
176
                $length = null;
177 178 179 180 181 182
                break;
            case 'blob':
            case 'raw':
            case 'long raw':
            case 'bfile':
                $length = null;
183
                break;
184 185 186 187 188
            case 'rowid':
            case 'urowid':
            default:
                $length = null;
        }
romanb's avatar
romanb committed
189

190
        $options = array(
191 192 193 194 195 196 197
            'notnull'    => (bool) ($tableColumn['nullable'] === 'N'),
            'fixed'      => (bool) $fixed,
            'unsigned'   => (bool) $unsigned,
            'default'    => $tableColumn['data_default'],
            'length'     => $length,
            'precision'  => $precision,
            'scale'      => $scale,
198
            'comment'       => (isset($tableColumn['comments'])) ? $tableColumn['comments'] : null,
199
            'platformDetails' => array(),
200
        );
201

202
        return new Column($tableColumn['column_name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
203 204 205 206 207
    }

    protected function _getPortableTableForeignKeysList($tableForeignKeys)
    {
        $list = array();
208
        foreach ($tableForeignKeys as $value) {
209 210
            $value = \array_change_key_case($value, CASE_LOWER);
            if (!isset($list[$value['constraint_name']])) {
211 212 213 214
                if ($value['delete_rule'] == "NO ACTION") {
                    $value['delete_rule'] = null;
                }

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

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

251 252
    protected function _getPortableDatabaseDefinition($database)
    {
253
        $database = \array_change_key_case($database, CASE_LOWER);
254 255
        return $database['username'];
    }
romanb's avatar
romanb committed
256

257 258 259 260 261
    public function createDatabase($database = null)
    {
        if (is_null($database)) {
            $database = $this->_conn->getDatabase();
        }
romanb's avatar
romanb committed
262

263 264 265
        $params = $this->_conn->getParams();
        $username   = $database;
        $password   = $params['password'];
romanb's avatar
romanb committed
266

267
        $query  = 'CREATE USER ' . $username . ' IDENTIFIED BY ' . $password;
268
        $result = $this->_conn->executeUpdate($query);
269

romanb's avatar
romanb committed
270
        $query = 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO ' . $username;
271
        $result = $this->_conn->executeUpdate($query);
272 273

        return true;
romanb's avatar
romanb committed
274
    }
275 276

    public function dropAutoincrement($table)
romanb's avatar
romanb committed
277
    {
278 279
        $sql = $this->_platform->getDropAutoincrementSql($table);
        foreach ($sql as $query) {
280
            $this->_conn->executeUpdate($query);
281 282 283
        }

        return true;
romanb's avatar
romanb committed
284 285 286 287
    }

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

290
        return parent::dropTable($name);
romanb's avatar
romanb committed
291
    }
292
}