Mysql.php 6.89 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)
    {
50 51 52 53
        $query = "SHOW TABLES";
        if (!is_null($database)) {
            $query .= " FROM $database";
        }
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

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


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

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


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

zYne's avatar
zYne committed
199
        return $this->conn->fetchColumn($query);
200 201
    }
}