Sqlite.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_Sqlite extends Doctrine_Import
{
34 35 36 37 38
    /**
     * lists all databases
     *
     * @return array
     */
lsmith's avatar
lsmith committed
39 40
    public function listDatabases()
    {
41 42 43 44 45 46 47

    }
    /**
     * lists all availible database functions
     *
     * @return array
     */
lsmith's avatar
lsmith committed
48 49 50
    public function listFunctions()
    {

51 52 53 54 55 56 57
    }
    /**
     * lists all database triggers
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
58 59
    public function listTriggers($database = null)
    {
60 61 62 63 64 65 66 67

    }
    /**
     * lists all database sequences
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
68 69
    public function listSequences($database = null)
    {
zYne's avatar
zYne committed
70 71
        $query      = "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
        $tableNames = $this->conn->fetchColumn($query);
lsmith's avatar
lsmith committed
72

73
        $result = array();
zYne's avatar
zYne committed
74 75
        foreach ($tableNames as $tableName) {
            if ($sqn = $this->conn->fixSequenceName($tableName, true)) {
76 77 78
                $result[] = $sqn;
            }
        }
zYne's avatar
zYne committed
79 80
        if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
            $result = array_map(($this->conn->getAttribute(Doctrine::ATTR_FIELD_CASE) == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
81 82
        }
        return $result;
83 84 85 86 87 88 89
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
90 91
    public function listTableConstraints($table)
    {
lsmith's avatar
lsmith committed
92
        $table = $this->conn->quote($table, 'text');
zYne's avatar
zYne committed
93

94
        $query = "SELECT sql FROM sqlite_master WHERE type='index' AND ";
zYne's avatar
zYne committed
95 96 97

        if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
            $query .= 'LOWER(tbl_name) = ' . strtolower($table);
98
        } else {
zYne's avatar
zYne committed
99
            $query .= 'tbl_name = ' . $table;
100
        }
zYne's avatar
zYne committed
101
        $query  .= ' AND sql NOT NULL ORDER BY name';
lsmith's avatar
lsmith committed
102
        $indexes = $this->conn->fetchColumn($query);
103 104 105 106

        $result = array();
        foreach ($indexes as $sql) {
            if (preg_match("/^create unique index ([^ ]+) on /i", $sql, $tmp)) {
zYne's avatar
zYne committed
107 108
                $index = $this->conn->fixIndexName($tmp[1]);
                if ( ! empty($index)) {
109 110 111 112 113
                    $result[$index] = true;
                }
            }
        }

zYne's avatar
zYne committed
114 115
        if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
            $result = array_change_key_case($result, $this->conn->getAttribute(Doctrine::ATTR_FIELD_CASE));
116
        }
lsmith's avatar
lsmith committed
117
        return array_keys($result);
118 119 120 121 122 123 124
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
125 126
    public function listTableColumns($table)
    {
127
        $sql    = 'PRAGMA table_info(' . $table . ')';
zYne's avatar
zYne committed
128
        $result = $this->conn->fetchAll($sql);
129 130 131

        $description = array();
        $columns     = array();
lsmith's avatar
lsmith committed
132
        foreach ($result as $key => $val) {
133
            $description = array(
zYne's avatar
zYne committed
134 135 136 137 138 139 140 141
                    'name'      => $val['name'],
                    'type'      => $val['type'],
                    'notnull'   => (bool) $val['notnull'],
                    'default'   => $val['dflt_value'],
                    'primary'   => (bool) $val['pk'],
                    'length'    => null,
                    'scale'     => null,
                    'precision' => null,
142
                    'unsigned'  => null,
143
                    );
zYne's avatar
zYne committed
144
            $columns[$val['name']] = $description;
145 146 147 148 149 150 151 152 153
        }
        return $columns;
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
154 155
    public function listTableIndexes($table)
    {
zYne's avatar
zYne committed
156
        $sql  = 'PRAGMA index_list(' . $table . ')';
zYne's avatar
zYne committed
157 158
        return $this->conn->fetchColumn($sql);
   }
159 160 161 162 163 164
    /**
     * lists tables
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
165 166
    public function listTables($database = null)
    {
167 168 169 170
        $sql = "SELECT name FROM sqlite_master WHERE type = 'table' "
             . "UNION ALL SELECT name FROM sqlite_temp_master "
             . "WHERE type = 'table' ORDER BY name";

zYne's avatar
zYne committed
171
        return $this->conn->fetchColumn($sql);
172 173 174 175 176 177 178
    }
    /**
     * lists table triggers
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
179 180 181
    public function listTableTriggers($table)
    {

182 183 184 185 186 187 188
    }
    /**
     * lists table views
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
189 190
    public function listTableViews($table)
    {
191 192
        $query = "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL";
        $views = $db->fetchAll($query);
lsmith's avatar
lsmith committed
193

194 195 196 197 198 199 200 201 202
        $result = array();
        foreach ($views as $row) {
            if (preg_match("/^create view .* \bfrom\b\s+\b{$table}\b /i", $row['sql'])) {
                if ( ! empty($row['name'])) {
                    $result[$row['name']] = true;
                }
            }
        }
        return $result;
203 204 205 206 207 208
    }
    /**
     * lists database users
     *
     * @return array
     */
lsmith's avatar
lsmith committed
209 210 211
    public function listUsers()
    {

212 213 214 215 216 217 218
    }
    /**
     * lists database views
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
219 220
    public function listViews($database = null)
    {
221
        $query = "SELECT name FROM sqlite_master WHERE type='view' AND sql NOT NULL";
lsmith's avatar
lsmith committed
222

223
        return $this->conn->fetchColumn($query);
224 225 226
    }
}