OracleSchemaManager.php 7.38 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 39
        return array(
            'name' => $view['view_name']
        );
romanb's avatar
romanb committed
40 41
    }

42
    protected function _getPortableUserDefinition($user)
romanb's avatar
romanb committed
43
    {
44 45 46 47 48
        return array(
            'user' => $user['username'],
            'password' => $user['password']
        );
    }
romanb's avatar
romanb committed
49

50 51 52
    protected function _getPortableTableDefinition($table)
    {
        return $table['table_name'];
romanb's avatar
romanb committed
53 54
    }

55
    protected function _getPortableTableIndexDefinition($tableIndex)
romanb's avatar
romanb committed
56
    {
57 58 59
        return array(
            'name' => $tableIndex['index_name'],
            'unique' => (isset($tableIndex['uniqueness']) && $tableIndex['uniqueness'] == 'UNIQUE') ? true : false
romanb's avatar
romanb committed
60
        );
61
    }
romanb's avatar
romanb committed
62

63 64 65 66 67 68 69 70
    protected function _getPortableTableColumnDefinition($tableColumn)
    {
        $dbType = strtolower($tableColumn['data_type']);
        $type = array();
        $length = $unsigned = $fixed = null;
        if ( ! empty($tableColumn['data_length'])) {
            $length = $tableColumn['data_length'];
        }
romanb's avatar
romanb committed
71

72 73 74
        if ( ! isset($tableColumn['column_name'])) {
            $tableColumn['column_name'] = '';
        }
romanb's avatar
romanb committed
75

76 77
        if (stripos($tableColumn['data_default'], 'NULL') !== null) {
            $tableColumn['data_default'] = null;
romanb's avatar
romanb committed
78 79
        }

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        switch ($dbType) {
            case 'integer':
            case 'pls_integer':
            case 'binary_integer':
                if ($length == '1' && preg_match('/^(is|has)/', $tableColumn['column_name'])) {
                    $type = 'boolean';
                } else {
                    $type = 'integer';
                }
                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':
                $type = 'timestamp';
                $length = null;
                break;
            case 'float':
                $type = 'float';
                break;
            case 'number':
                if ( ! empty($tableColumn['data_scale'])) {
                    $type = 'decimal';
                } else {
                    if ($length == '1' && preg_match('/^(is|has)/', $tableColumn['column_name'])) {
                        $type = 'boolean';
                    } else {
                        $type = 'integer';
                    }
                }
                break;
            case 'long':
                $type = 'string';
            case 'clob':
            case 'nclob':
                $type = 'clob';
                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
143

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        $decl = array(
            'type'     => $type,
            'length'   => $length,
            'unsigned' => $unsigned,
            'fixed'    => $fixed
        );

        return array(
           'name'       => $tableColumn['column_name'],
           'notnull'    => (bool) ($tableColumn['nullable'] === 'N'),
           'type'       => $decl['type'],
           'fixed'      => (bool) $decl['fixed'],
           'unsigned'   => (bool) $decl['unsigned'],
           'default'    => $tableColumn['data_default'],
           'length'     => $tableColumn['data_length'],
           'precision'  => $tableColumn['data_precision'],
           'scale'      => $tableColumn['data_scale'],
        );
romanb's avatar
romanb committed
162 163
    }

164
    protected function _getPortableTableConstraintDefinition($tableConstraint)
romanb's avatar
romanb committed
165
    {
166 167
        return $tableConstraint['constraint_name'];
    }
romanb's avatar
romanb committed
168

169 170 171 172
    protected function _getPortableFunctionDefinition($function)
    {
        return $function['name'];
    }
romanb's avatar
romanb committed
173

174 175 176 177
    protected function _getPortableDatabaseDefinition($database)
    {
        return $database['username'];
    }
romanb's avatar
romanb committed
178

179 180 181 182 183
    public function createDatabase($database = null)
    {
        if (is_null($database)) {
            $database = $this->_conn->getDatabase();
        }
romanb's avatar
romanb committed
184

185 186 187
        $params = $this->_conn->getParams();
        $username   = $database;
        $password   = $params['password'];
romanb's avatar
romanb committed
188

189 190 191
        $query  = 'CREATE USER ' . $username . ' IDENTIFIED BY ' . $password;
        $result = $this->_conn->exec($query);

romanb's avatar
romanb committed
192 193
        $query = 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO ' . $username;
        $result = $this->_conn->exec($query);
194 195

        return true;
romanb's avatar
romanb committed
196
    }
197 198

    public function dropAutoincrement($table)
romanb's avatar
romanb committed
199
    {
200 201
        $sql = $this->_platform->getDropAutoincrementSql($table);
        foreach ($sql as $query) {
jwage's avatar
jwage committed
202
            $this->_conn->exec($query);
203 204 205
        }

        return true;
romanb's avatar
romanb committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    }

    /**
     * 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
238
        $this->dropAutoincrement($name);
romanb's avatar
romanb committed
239

240
        return parent::dropTable($name);
romanb's avatar
romanb committed
241
    }
242
}