Oracle.php 6.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<?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.com>.
 */
Doctrine::autoload('Doctrine_Import');
/**
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @version     $Revision$
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 */
lsmith's avatar
lsmith committed
31 32
class Doctrine_Import_Oracle extends Doctrine_Import
{
33 34 35 36 37
    /**
     * lists all databases
     *
     * @return array
     */
lsmith's avatar
lsmith committed
38
    public function listDatabases()
39
    {
zYne's avatar
zYne committed
40 41
        if ( ! $this->conn->getAttribute(Doctrine::ATTR_EMULATE_DATABASE)) {
            throw new Doctrine_Import_Exception('database listing is only supported if the "emulate_database" option is enabled');
42
        }
zYne's avatar
zYne committed
43
        /**
44 45
        if ($this->conn->options['database_name_prefix']) {
            $query = 'SELECT SUBSTR(username, ';
zYne's avatar
zYne committed
46
            $query.= (strlen($this->conn->getAttribute(['database_name_prefix'])+1);
47 48 49
            $query.= ") FROM sys.dba_users WHERE username LIKE '";
            $query.= $this->conn->options['database_name_prefix']."%'";
        } else {
zYne's avatar
zYne committed
50 51 52 53 54
        */
        $query   = 'SELECT username FROM sys.dba_users';

        $result2 = $this->conn->standaloneQuery($query);
        $result  = $result2->fetchColumn();
55

56
        return $result;
57 58 59 60 61 62
    }
    /**
     * lists all availible database functions
     *
     * @return array
     */
lsmith's avatar
lsmith committed
63
    public function listFunctions()
64 65
    {
        $query = "SELECT name FROM sys.user_source WHERE line = 1 AND type = 'FUNCTION'";
zYne's avatar
zYne committed
66

zYne's avatar
zYne committed
67
        return $this->conn->fetchColumn($query);
68 69 70 71 72 73 74
    }
    /**
     * lists all database triggers
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
75
    public function listTriggers($database = null)
76
    {
77 78 79 80 81 82 83 84

    }
    /**
     * lists all database sequences
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
85
    public function listSequences($database = null)
86 87
    {
        $query = "SELECT sequence_name FROM sys.user_sequences";
zYne's avatar
zYne committed
88

lsmith's avatar
lsmith committed
89
        $tableNames = $this->conn->fetchColumn($query);
90

zYne's avatar
zYne committed
91
        return array_map(array($this->conn, 'fixSequenceName'), $tableNames);
92 93 94 95 96 97 98
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
99
    public function listTableConstraints($table)
100 101
    {
        $table = $this->conn->quote($table, 'text');
zYne's avatar
zYne committed
102 103 104 105

        $query = 'SELECT index_name name FROM user_constraints'
               . ' WHERE table_name = ' . $table . ' OR table_name = ' . strtoupper($table);

lsmith's avatar
lsmith committed
106
        $constraints = $this->conn->fetchColumn($query);
107

zYne's avatar
zYne committed
108
        return array_map(array($this->conn, 'fixIndexName'), $constraints);
109 110 111 112 113 114 115
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
116
    public function listTableColumns($table)
117
    {
zYne's avatar
zYne committed
118
        $table  = strtoupper($table);
zYne's avatar
zYne committed
119 120 121
        $sql    = "SELECT column_name, data_type, data_length, nullable, data_default from all_tab_columns"
                . " WHERE table_name = '" . $table . "' ORDER BY column_name";

zYne's avatar
zYne committed
122
        $result = $this->conn->fetchAssoc($sql);
123

zYne's avatar
zYne committed
124
        foreach($result as $val) {
lsmith's avatar
lsmith committed
125
            $descr[$val['column_name']] = array(
zYne's avatar
zYne committed
126 127 128 129 130 131
               'name'    => $val['column_name'],
               'notnull' => (bool) ($val['nullable'] === 'N'), // nullable is N when mandatory
               'type'    => $val['data_type'],
               'default' => $val['data_default'],
               'length'  => $val['data_length']
            );
132 133
        }
        return $result;
134 135 136 137 138 139 140
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
141 142
    public function listTableIndexes($table)
    {
143
        $table = $this->conn->quote($table, 'text');
zYne's avatar
zYne committed
144 145 146 147
        $query = 'SELECT index_name name FROM user_indexes'
               . ' WHERE table_name = ' . $table . ' OR table_name = ' . strtoupper($table)
               . ' AND generated = ' . $this->conn->quote('N', 'text');

lsmith's avatar
lsmith committed
148
        $indexes = $this->conn->fetchColumn($query);
149

zYne's avatar
zYne committed
150
        return array_map(array($this->conn, 'fixIndexName'), $indexes);
151 152 153 154 155 156 157
    }
    /**
     * lists tables
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
158
    public function listTables($database = null)
159 160
    {
        $query = 'SELECT table_name FROM sys.user_tables';
zYne's avatar
zYne committed
161
        return $this->conn->fetchColumn($query);
162 163 164 165 166 167 168
    }
    /**
     * lists table triggers
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
169
    public function listTableTriggers($table)
170
    {
lsmith's avatar
lsmith committed
171

172 173 174 175 176 177 178
    }
    /**
     * lists table views
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
179
    public function listTableViews($table)
180
    {
lsmith's avatar
lsmith committed
181

182 183 184 185 186 187
    }
    /**
     * lists database users
     *
     * @return array
     */
lsmith's avatar
lsmith committed
188
    public function listUsers()
189
    {
zYne's avatar
zYne committed
190
    	/**
191 192 193 194 195 196
        if ($this->conn->options['emulate_database'] && $this->conn->options['database_name_prefix']) {
            $query = 'SELECT SUBSTR(username, ';
            $query.= (strlen($this->conn->options['database_name_prefix'])+1);
            $query.= ") FROM sys.dba_users WHERE username NOT LIKE '";
            $query.= $this->conn->options['database_name_prefix']."%'";
        } else {
zYne's avatar
zYne committed
197 198 199 200 201
        */

        $query = 'SELECT username FROM sys.dba_users';
        //}

lsmith's avatar
lsmith committed
202
        return $this->conn->fetchColumn($query);
203 204 205 206 207 208 209
    }
    /**
     * lists database views
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
210
    public function listViews($database = null)
211 212
    {
        $query = 'SELECT view_name FROM sys.user_views';
zYne's avatar
zYne committed
213
        return $this->conn->fetchColumn($query);
214 215
    }
}