# Coverage for Doctrine_Import_Sqlite

Back to coverage report

1 | <?php
2 | /*
3 | * $Id: Sqlite.php 3018 2007-10-26 14:56:03Z ppetermann $
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.phpdoctrine.org>.
20 | */
21 | Doctrine::autoload('Doctrine_Import');
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: 3018 $
29 | * @link www.phpdoctrine.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::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
83 | $result = array_map(($this->conn->getAttribute(Doctrine::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::ATTR_PORTABILITY) & Doctrine::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::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
119 | $result = array_change_key_case($result, $this->conn->getAttribute(Doctrine::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 | );
154 | $columns[$val['name']] = $description;
155 | }
156 | return $columns;
157 | }
158 |
159 | /**
160 | * lists table constraints
161 | *
162 | * @param string $table database table name
163 | * @return array
164 | */
165 | public function listTableIndexes($table)
166 | {
167 | $sql = 'PRAGMA index_list(' . $table . ')';
168 | return $this->conn->fetchColumn($sql);
169 | }
170 | /**
171 | * lists tables
172 | *
173 | * @param string|null $database
174 | * @return array
175 | */
176 | public function listTables($database = null)
177 | {
178 | $sql = "SELECT name FROM sqlite_master WHERE type = 'table' "
179 | . "UNION ALL SELECT name FROM sqlite_temp_master "
180 | . "WHERE type = 'table' ORDER BY name";
181 |
182 | return $this->conn->fetchColumn($sql);
183 | }
184 |
185 | /**
186 | * lists table triggers
187 | *
188 | * @param string $table database table name
189 | * @return array
190 | */
191 | public function listTableTriggers($table)
192 | {
193 |
194 | }
195 |
196 | /**
197 | * lists table views
198 | *
199 | * @param string $table database table name
200 | * @return array
201 | */
202 | public function listTableViews($table)
203 | {
204 | $query = "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL";
205 | $views = $db->fetchAll($query);
206 |
207 | $result = array();
208 | foreach ($views as $row) {
209 | if (preg_match("/^create view .* \bfrom\b\s+\b{$table}\b /i", $row['sql'])) {
210 | if ( ! empty($row['name'])) {
211 | $result[$row['name']] = true;
212 | }
213 | }
214 | }
215 | return $result;
216 | }
217 |
218 | /**
219 | * lists database users
220 | *
221 | * @return array
222 | */
223 | public function listUsers()
224 | {
225 |
226 | }
227 |
228 | /**
229 | * lists database views
230 | *
231 | * @param string|null $database
232 | * @return array
233 | */
234 | public function listViews($database = null)
235 | {
236 | $query = "SELECT name FROM sqlite_master WHERE type='view' AND sql NOT NULL";
237 |
238 | return $this->conn->fetchColumn($query);
239 | }
240 | }