Oracle.php 7.18 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->formatter, '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->formatter, '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
        $sql    = "SELECT column_name, data_type, data_length, nullable, data_default, data_scale, data_precision FROM all_tab_columns"
zYne's avatar
zYne committed
120 121
                . " 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) {
zYne's avatar
zYne committed
125
            $val = array_change_key_case($val, CASE_LOWER);
zYne's avatar
zYne committed
126
            $decl = $this->conn->dataDict->getPortableDeclaration($val);
zYne's avatar
zYne committed
127

zYne's avatar
zYne committed
128

lsmith's avatar
lsmith committed
129
            $descr[$val['column_name']] = array(
zYne's avatar
zYne committed
130
               'name'       => $val['column_name'],
zYne's avatar
zYne committed
131
               'notnull'    => (bool) ($val['nullable'] === 'N'),
zYne's avatar
zYne committed
132 133 134
               'ntype'      => $val['data_type'],
               'type'       => $decl['type'][0],
               'alltypes'   => $decl['type'],
zYne's avatar
zYne committed
135 136 137
               'fixed'      => $decl['fixed'],
               'unsigned'   => $decl['unsigned'],
               'default'    => $val['data_default'],
zYne's avatar
zYne committed
138 139 140
               'length'     => $val['data_length'],
               'precision'  => $val['data_precision'],
               'scale'      => $val['scale'],
zYne's avatar
zYne committed
141
            );
142 143
        }
        return $result;
144 145 146 147 148 149 150
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
151 152
    public function listTableIndexes($table)
    {
153
        $table = $this->conn->quote($table, 'text');
zYne's avatar
zYne committed
154 155 156 157
        $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
158
        $indexes = $this->conn->fetchColumn($query);
159

zYne's avatar
zYne committed
160
        return array_map(array($this->conn->formatter, 'fixIndexName'), $indexes);
161 162 163 164 165 166 167
    }
    /**
     * lists tables
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
168
    public function listTables($database = null)
169 170
    {
        $query = 'SELECT table_name FROM sys.user_tables';
zYne's avatar
zYne committed
171
        return $this->conn->fetchColumn($query);
172 173 174 175 176 177 178
    }
    /**
     * lists table triggers
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
179
    public function listTableTriggers($table)
180
    {
lsmith's avatar
lsmith committed
181

182 183 184 185 186 187 188
    }
    /**
     * lists table views
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
189
    public function listTableViews($table)
190
    {
lsmith's avatar
lsmith committed
191

192 193 194 195 196 197
    }
    /**
     * lists database users
     *
     * @return array
     */
lsmith's avatar
lsmith committed
198
    public function listUsers()
199
    {
zYne's avatar
zYne committed
200
    	/**
201 202 203 204 205 206
        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
207 208 209 210 211
        */

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

lsmith's avatar
lsmith committed
212
        return $this->conn->fetchColumn($query);
213 214 215 216 217 218 219
    }
    /**
     * lists database views
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
220
    public function listViews($database = null)
221 222
    {
        $query = 'SELECT view_name FROM sys.user_views';
zYne's avatar
zYne committed
223
        return $this->conn->fetchColumn($query);
224 225
    }
}