Mysql.php 6.24 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
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
103 104
    public function listTableColumns($table)
    {
zYne's avatar
zYne committed
105 106
        $sql = 'DESCRIBE ' . $table;
        $result = $this->conn->fetchAssoc($sql);
107
        $description = array();
zYne's avatar
zYne committed
108 109 110 111
        foreach ($result as $key => $val) {

            array_change_key_case($val, CASE_LOWER);

112 113 114 115 116 117
            $description = array(
                'name'    => $val['field'],
                'type'    => $val['type'],
                'primary' => (strtolower($val['key']) == 'pri'),
                'default' => $val['default'],
                'notnull' => (bool) ($val['null'] != 'YES'),
zYne's avatar
zYne committed
118
                'autoinc' => (bool) (strpos($val['extra'], 'auto_increment') !== false),
119
            );
zYne's avatar
zYne committed
120
            $columns[$val['field']] = $description;
121 122 123 124 125 126 127 128 129 130 131
        }


        return $columns;
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
132
    public function listTableIndexes($table)
133
    {
zYne's avatar
zYne committed
134 135
        $keyName = 'Key_name';
        $nonUnique = 'Non_unique';
lsmith's avatar
lsmith committed
136 137
        if ($this->conn->options['portability'] & Doctrine::PORTABILITY_FIX_CASE) {
            if ($this->conn->options['field_case'] == CASE_LOWER) {
zYne's avatar
zYne committed
138 139
                $keyName = strtolower($keyName);
                $nonUnique = strtolower($nonUnique);
140
            } else {
zYne's avatar
zYne committed
141 142
                $keyName = strtoupper($key_name);
                $nonUnique = strtoupper($nonUnique);
143 144 145
            }
        }

lsmith's avatar
lsmith committed
146
        $table = $this->conn->quoteIdentifier($table, true);
147
        $query = "SHOW INDEX FROM $table";
lsmith's avatar
lsmith committed
148
        $indexes = $this->conn->fetchAssoc($query);
149 150 151


        $result = array();
zYne's avatar
zYne committed
152 153 154
        foreach ($indexes as $indexData) {
            if ($indexData[$nonUnique] && ($index = $this->conn->fixIndexName($indexData[$keyName]))) {
                $result[] = $index;
155 156
            }
        }
zYne's avatar
zYne committed
157
        return $result;
158 159 160 161 162 163 164
    }
    /**
     * lists tables
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
165
    public function listTables($database = null)
166
    {
zYne's avatar
zYne committed
167
        return $this->conn->fetchColumn($this->sql['listTables']);
168 169 170 171 172 173 174
    }
    /**
     * lists database views
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
175
    public function listViews($database = null)
176 177
    {
        if (!is_null($database)) {
zYne's avatar
zYne committed
178
            $query = sprintf($this->sql['listViews'], ' FROM ' . $database);
lsmith's avatar
lsmith committed
179
        }
180

zYne's avatar
zYne committed
181
        return $this->conn->fetchColumn($query);
182 183
    }
}