Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Sqlite.php 7644 2010-06-08 15:12:02Z jwage $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * @package     Doctrine
24
 * @subpackage  Import
25
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
26
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
27
 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
28
 * @version     $Revision: 7644 $
29
 * @link        www.doctrine-project.org
30
 * @since       1.0
31
 */
32
class Doctrine_Import_Sqlite extends Doctrine_Import
33
{
34
    /**
35
     * lists all databases
36
     *
37
     * @return array
38
     */
39
    public function listDatabases()
40
    {
41
 
42
    }
43
 
44
    /**
45
     * lists all availible database functions
46
     *
47
     * @return array
48
     */
49
    public function listFunctions()
50
    {
51
 
52
    }
53
 
54
    /**
55
     * lists all database triggers
56
     *
57
     * @param string|null $database
58
     * @return array
59
     */
60
    public function listTriggers($database = null)
61
    {
62
 
63
    }
64
 
65
    /**
66
     * lists all database sequences
67
     *
68
     * @param string|null $database
69
     * @return array
70
     */
71
    public function listSequences($database = null)
72
    {
73
        $query      = "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
74
        $tableNames = $this->conn->fetchColumn($query);
75
 
76
        $result = array();
77
        foreach ($tableNames as $tableName) {
78
            if ($sqn = $this->conn->fixSequenceName($tableName, true)) {
79
                $result[] = $sqn;
80
            }
81
        }
82
        if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) && ($this->conn->getAttribute(Doctrine_Core::ATTR_PORTABILITY) & Doctrine_Core::PORTABILITY_FIX_CASE)) {
83
            $result = array_map(($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
84
        }
85
        return $result;
86
    }
87
 
88
    /**
89
     * lists table constraints
90
     *
91
     * @param string $table     database table name
92
     * @return array
93
     */
94
    public function listTableConstraints($table)
95
    {
96
        $table = $this->conn->quote($table, 'text');
97
 
98
        $query = "SELECT sql FROM sqlite_master WHERE type='index' AND ";
99
 
100
        if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) && ($this->conn->getAttribute(Doctrine_Core::ATTR_PORTABILITY) & Doctrine_Core::PORTABILITY_FIX_CASE)) {
101
            $query .= 'LOWER(tbl_name) = ' . strtolower($table);
102
        } else {
103
            $query .= 'tbl_name = ' . $table;
104
        }
105
        $query  .= ' AND sql NOT NULL ORDER BY name';
106
        $indexes = $this->conn->fetchColumn($query);
107
 
108
        $result = array();
109
        foreach ($indexes as $sql) {
110
            if (preg_match("/^create unique index ([^ ]+) on /i", $sql, $tmp)) {
111
                $index = $this->conn->formatter->fixIndexName($tmp[1]);
112
                if ( ! empty($index)) {
113
                    $result[$index] = true;
114
                }
115
            }
116
        }
117
 
118
        if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) && ($this->conn->getAttribute(Doctrine_Core::ATTR_PORTABILITY) & Doctrine_Core::PORTABILITY_FIX_CASE)) {
119
            $result = array_change_key_case($result, $this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE));
120
        }
121
        return array_keys($result);
122
    }
123
 
124
    /**
125
     * lists table constraints
126
     *
127
     * @param string $table     database table name
128
     * @return array
129
     */
130
    public function listTableColumns($table)
131
    {
132
        $sql    = 'PRAGMA table_info(' . $table . ')';
133
        $result = $this->conn->fetchAll($sql);
134
 
135
        $description = array();
136
        $columns     = array();
137
        foreach ($result as $key => $val) {
138
            $val = array_change_key_case($val, CASE_LOWER);
139
            $decl = $this->conn->dataDict->getPortableDeclaration($val);
140
 
141
            $description = array(
142
                    'name'          => $val['name'],
143
                    'ntype'         => $val['type'],
144
                    'type'          => $decl['type'][0],
145
                    'alltypes'      => $decl['type'],
146
                    'notnull'       => (bool) $val['notnull'],
147
                    'default'       => $val['dflt_value'],
148
                    'primary'       => (bool) $val['pk'],
149
                    'length'        => null,
150
                    'scale'         => null,
151
                    'precision'     => null,
152
                    'unsigned'      => null,
153
                    'autoincrement' => (bool) ($val['pk'] == 1 && $decl['type'][0] == 'integer'),
154
                    );
155
            $columns[$val['name']] = $description;
156
        }
157
        return $columns;
158
    }
159
 
160
    /**
161
     * lists table constraints
162
     *
163
     * @param string $table     database table name
164
     * @return array
165
     */
166
    public function listTableIndexes($table)
167
    {
168
        $sql  = 'PRAGMA index_list(' . $table . ')';
169
        return $this->conn->fetchColumn($sql);
170
   }
171
    /**
172
     * lists tables
173
     *
174
     * @param string|null $database
175
     * @return array
176
     */
177
    public function listTables($database = null)
178
    {
179
        $sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name != 'sqlite_sequence' "
180
             . "UNION ALL SELECT name FROM sqlite_temp_master "
181
             . "WHERE type = 'table' ORDER BY name";
182
 
183
        return $this->conn->fetchColumn($sql);
184
    }
185
 
186
    /**
187
     * lists table triggers
188
     *
189
     * @param string $table     database table name
190
     * @return array
191
     */
192
    public function listTableTriggers($table)
193
    {
194
 
195
    }
196
 
197
    /**
198
     * lists table views
199
     *
200
     * @param string $table     database table name
201
     * @return array
202
     */
203
    public function listTableViews($table)
204
    {
205
        $query = "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL";
206
        $views = $db->fetchAll($query);
207
 
208
        $result = array();
209
        foreach ($views as $row) {
210
            if (preg_match("/^create view .* \bfrom\b\s+\b{$table}\b /i", $row['sql'])) {
211
                if ( ! empty($row['name'])) {
212
                    $result[$row['name']] = true;
213
                }
214
            }
215
        }
216
        return $result;
217
    }
218
 
219
    /**
220
     * lists database users
221
     *
222
     * @return array
223
     */
224
    public function listUsers()
225
    {
226
 
227
    }
228
 
229
    /**
230
     * lists database views
231
     *
232
     * @param string|null $database
233
     * @return array
234
     */
235
    public function listViews($database = null)
236
    {
237
        $query = "SELECT name FROM sqlite_master WHERE type='view' AND sql NOT NULL";
238
 
239
        return $this->conn->fetchColumn($query);
240
    }
241
}