Mysql.php 7.01 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 31
<?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>
 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
 * @version     $Revision$
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 */
lsmith's avatar
lsmith committed
32 33
class Doctrine_Import_Mysql extends Doctrine_Import
{
zYne's avatar
zYne committed
34 35 36 37 38 39 40 41
    protected $sql  = array(
                            'showDatabases'   => 'SHOW DATABASES',
                            'listTableFields' => 'DESCRIBE %s',
                            'listSequences'   => 'SHOW TABLES',
                            'listTables'      => 'SHOW TABLES',
                            'listUsers'       => 'SELECT DISTINCT USER FROM USER',
                            'listViews'       => "SHOW FULL TABLES %sWHERE Table_type = 'VIEW'",
                            );
42 43 44 45 46 47
    /**
     * lists all database sequences
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
48 49
    public function listSequences($database = null)
    {
zYne's avatar
zYne committed
50
        $query = 'SHOW TABLES';
51
        if (!is_null($database)) {
zYne's avatar
zYne committed
52
            $query .= ' FROM ' . $database;
53
        }
zYne's avatar
zYne committed
54
        $tableNames = $this->conn->fetchColumn($query);
55

zYne's avatar
zYne committed
56
        return array_map(array($this->conn, 'fixSequenceName'), $tableNames);
57 58 59 60 61 62 63
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
64 65
    public function listTableConstraints($table)
    {
zYne's avatar
zYne committed
66 67 68
        $keyName = 'Key_name';
        $nonUnique = 'Non_unique';
        if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
lsmith's avatar
lsmith committed
69
            if ($this->conn->options['field_case'] == CASE_LOWER) {
zYne's avatar
zYne committed
70 71
                $keyName = strtolower($keyName);
                $nonUnique = strtolower($nonUnique);
72
            } else {
zYne's avatar
zYne committed
73 74
                $keyName = strtoupper($keyName);
                $nonUnique = strtoupper($nonUnique);
75 76 77
            }
        }

lsmith's avatar
lsmith committed
78
        $table = $this->conn->quoteIdentifier($table, true);
zYne's avatar
zYne committed
79
        $query = 'SHOW INDEX FROM ' . $table;
lsmith's avatar
lsmith committed
80
        $indexes = $this->conn->fetchAssoc($query);
81 82

        $result = array();
zYne's avatar
zYne committed
83 84 85 86
        foreach ($indexes as $indexData) {
            if (!$indexData[$nonUnique]) {
                if ($indexData[$keyName] !== 'PRIMARY') {
                    $index = $this->conn->fixIndexName($indexData[$keyName]);
87 88 89
                } else {
                    $index = 'PRIMARY';
                }
zYne's avatar
zYne committed
90 91
                if ( ! empty($index)) {
                    $result[] = $index;
92 93 94
                }
            }
        }
zYne's avatar
zYne committed
95
        return $result;
96
    }
97 98 99 100 101 102 103 104 105 106
    /**
     * lists table foreign keys
     *
     * @param string $table     database table name
     * @return array
     */
    public function listTableForeignKeys($table) 
    {
        $sql = 'SHOW CREATE TABLE ' . $this->conn->quoteIdentifier($table, true);	
    }
107 108 109 110 111 112
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
113 114
    public function listTableColumns($table)
    {
115
        $sql = 'DESCRIBE ' . $this->conn->quoteIdentifier($table, true);
zYne's avatar
zYne committed
116
        $result = $this->conn->fetchAssoc($sql);
zYne's avatar
zYne committed
117

118
        $description = array();
zYne's avatar
zYne committed
119 120
        foreach ($result as $key => $val) {

zYne's avatar
zYne committed
121 122 123
            $val = array_change_key_case($val, CASE_LOWER);

            $decl = $this->conn->dataDict->getPortableDeclaration($val);
zYne's avatar
zYne committed
124

zYne's avatar
zYne committed
125 126
            $values = isset($decl['values']) ? $decl['values'] : array();

127
            $description = array(
zYne's avatar
zYne committed
128
                'name'      => $val['field'],
zYne's avatar
zYne committed
129 130 131
                'type'      => $decl['type'][0],
                'alltypes'  => $decl['type'],
                'ntype'     => $val['type'],
zYne's avatar
zYne committed
132 133 134
                'length'    => $decl['length'],
                'fixed'     => $decl['fixed'],
                'unsigned'  => $decl['unsigned'],
zYne's avatar
zYne committed
135
                'values'    => $values,
zYne's avatar
zYne committed
136 137 138 139
                'primary'   => (strtolower($val['key']) == 'pri'),
                'default'   => $val['default'],
                'notnull'   => (bool) ($val['null'] != 'YES'),
                'autoinc'   => (bool) (strpos($val['extra'], 'auto_increment') !== false),
140
            );
zYne's avatar
zYne committed
141
            $columns[$val['field']] = $description;
142 143 144 145 146 147 148 149 150 151 152
        }


        return $columns;
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
153
    public function listTableIndexes($table)
154
    {
zYne's avatar
zYne committed
155 156
        $keyName = 'Key_name';
        $nonUnique = 'Non_unique';
lsmith's avatar
lsmith committed
157 158
        if ($this->conn->options['portability'] & Doctrine::PORTABILITY_FIX_CASE) {
            if ($this->conn->options['field_case'] == CASE_LOWER) {
zYne's avatar
zYne committed
159 160
                $keyName = strtolower($keyName);
                $nonUnique = strtolower($nonUnique);
161
            } else {
gnat's avatar
gnat committed
162
                $keyName = strtoupper($keyName);
zYne's avatar
zYne committed
163
                $nonUnique = strtoupper($nonUnique);
164 165 166
            }
        }

lsmith's avatar
lsmith committed
167
        $table = $this->conn->quoteIdentifier($table, true);
zYne's avatar
zYne committed
168
        $query = 'SHOW INDEX FROM ' . $table;
lsmith's avatar
lsmith committed
169
        $indexes = $this->conn->fetchAssoc($query);
170 171 172


        $result = array();
zYne's avatar
zYne committed
173 174 175
        foreach ($indexes as $indexData) {
            if ($indexData[$nonUnique] && ($index = $this->conn->fixIndexName($indexData[$keyName]))) {
                $result[] = $index;
176 177
            }
        }
zYne's avatar
zYne committed
178
        return $result;
179 180 181 182 183 184 185
    }
    /**
     * lists tables
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
186
    public function listTables($database = null)
187
    {
zYne's avatar
zYne committed
188
        return $this->conn->fetchColumn($this->sql['listTables']);
189 190 191 192 193 194 195
    }
    /**
     * lists database views
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
196
    public function listViews($database = null)
197 198
    {
        if (!is_null($database)) {
zYne's avatar
zYne committed
199
            $query = sprintf($this->sql['listViews'], ' FROM ' . $database);
lsmith's avatar
lsmith committed
200
        }
201

zYne's avatar
zYne committed
202
        return $this->conn->fetchColumn($query);
203 204
    }
}