OracleSchemaManager.php 8.81 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 26 27 28 29 30 31 32

/**
 * 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)
 * @version     $Revision$
 * @since       2.0
 */
33
class OracleSchemaManager extends AbstractSchemaManager
34 35
{
    protected function _getPortableViewDefinition($view)
romanb's avatar
romanb committed
36
    {
37 38
        $view = \array_change_key_case($view, CASE_LOWER);

39
        return array(
40 41
            'name' => $view['view_name'],
            'sql' => '',
42
        );
romanb's avatar
romanb committed
43 44
    }

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

49 50 51 52 53
        return array(
            'user' => $user['username'],
            'password' => $user['password']
        );
    }
romanb's avatar
romanb committed
54

55 56
    protected function _getPortableTableDefinition($table)
    {
57 58
        $table = \array_change_key_case($table, CASE_LOWER);

59
        return $table['table_name'];
romanb's avatar
romanb committed
60 61
    }

62 63 64 65 66 67 68 69
    /**
     * @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
70
    {
71 72
        $tableIndexes = \array_change_key_case($tableIndexes, CASE_LOWER);

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        $indexBuffer = array();
        foreach ( $tableIndexes as $tableIndex ) {
            $keyName = $tableIndex['name'];

            if ( $keyName == $tableName.'_pkey' ) {
                $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;
        }
        parent::_getPortableTableIndexesList($indexBuffer, $tableName);
90
    }
romanb's avatar
romanb committed
91

92 93
    protected function _getPortableTableColumnDefinition($tableColumn)
    {
94 95
        $tableColumn = \array_change_key_case($tableColumn, CASE_LOWER);
        
96
        $dbType = strtolower($tableColumn['data_type']);
97 98 99 100
        if(strpos($dbType, "timestamp(") === 0) {
            $dbType = "timestamp";
        }

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

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

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

115 116 117
        $precision = null;
        $scale = null;
        
118 119
        switch ($dbType) {
            case 'integer':
120
            case 'number':
121 122 123 124 125 126 127
                if($tableColumn['data_scale'] > 0) {
                    $type = 'decimal';
                    $precision = $tableColumn['data_precision'];
                    $scale = $tableColumn['data_scale'];
                } else {
                    $type = 'integer';
                }
128 129
                $length = null;
                break;
130 131
            case 'pls_integer':
            case 'binary_integer':
132 133
                $type = 'boolean';
                $length = null;
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
                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':
152
                $type = 'datetime';
153 154 155
                $length = null;
                break;
            case 'float':
156 157
                $precision = $tableColumn['data_precision'];
                $scale = $tableColumn['data_scale'];
158 159
                $type = 'decimal';
                $length = null;
160 161 162 163 164
                break;
            case 'long':
                $type = 'string';
            case 'clob':
            case 'nclob':
165
                $length = null;
166
                $type = 'text';
167 168 169 170 171 172 173 174 175 176 177 178 179 180
                break;
            case 'blob':
            case 'raw':
            case 'long raw':
            case 'bfile':
                $type = 'blob';
                $length = null;
            break;
            case 'rowid':
            case 'urowid':
            default:
                $type = 'string';
                $length = null;
        }
romanb's avatar
romanb committed
181

182 183 184 185 186 187 188 189
        $decl = array(
            'type'     => $type,
            'length'   => $length,
            'unsigned' => $unsigned,
            'fixed'    => $fixed
        );

        return array(
190 191 192 193 194 195 196 197 198 199
            'name'       => $tableColumn['column_name'],
            'notnull'    => (bool) ($tableColumn['nullable'] === 'N'),
            'type'       => $type,
            'fixed'      => (bool) $fixed,
            'unsigned'   => (bool) $unsigned,
            'default'    => $tableColumn['data_default'],
            'length'     => $length,
            'precision'  => $precision,
            'scale'      => $scale,
            'platformDetails' => array(),
200
        );
romanb's avatar
romanb committed
201 202
    }

203
    protected function _getPortableTableConstraintDefinition($tableConstraint)
romanb's avatar
romanb committed
204
    {
205
        $tableConstraint = \array_change_key_case($tableConstraint, CASE_LOWER);
206 207
        return $tableConstraint['constraint_name'];
    }
romanb's avatar
romanb committed
208

209 210
    protected function _getPortableFunctionDefinition($function)
    {
211
        $function = \array_change_key_case($function, CASE_LOWER);
212 213
        return $function['name'];
    }
romanb's avatar
romanb committed
214

215 216
    protected function _getPortableDatabaseDefinition($database)
    {
217
        $database = \array_change_key_case($database, CASE_LOWER);
218 219
        return $database['username'];
    }
romanb's avatar
romanb committed
220

221 222 223 224 225
    public function createDatabase($database = null)
    {
        if (is_null($database)) {
            $database = $this->_conn->getDatabase();
        }
romanb's avatar
romanb committed
226

227 228 229
        $params = $this->_conn->getParams();
        $username   = $database;
        $password   = $params['password'];
romanb's avatar
romanb committed
230

231
        $query  = 'CREATE USER ' . $username . ' IDENTIFIED BY ' . $password;
232
        $result = $this->_conn->executeUpdate($query);
233

romanb's avatar
romanb committed
234
        $query = 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO ' . $username;
235
        $result = $this->_conn->executeUpdate($query);
236 237

        return true;
romanb's avatar
romanb committed
238
    }
239 240

    public function dropAutoincrement($table)
romanb's avatar
romanb committed
241
    {
242 243
        $sql = $this->_platform->getDropAutoincrementSql($table);
        foreach ($sql as $query) {
244
            $this->_conn->executeUpdate($query);
245 246 247
        }

        return true;
romanb's avatar
romanb committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    }

    /**
     * getAdvancedForeignKeyOptions
     * Return the FOREIGN KEY query section dealing with non-standard options
     * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
     *
     * @param array $definition         foreign key definition
     * @return string
     * @access protected
     */
    public function getAdvancedForeignKeyOptions(array $definition)
    {
        $query = '';
        if (isset($definition['onDelete'])) {
            $query .= ' ON DELETE ' . $definition['onDelete'];
        }
        if (isset($definition['deferrable'])) {
            $query .= ' DEFERRABLE';
        } else {
            $query .= ' NOT DEFERRABLE';
        }
        if (isset($definition['feferred'])) {
            $query .= ' INITIALLY DEFERRED';
        } else {
            $query .= ' INITIALLY IMMEDIATE';
        }
        return $query;
    }

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

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