# Source for file Sqlite.php

Documentation is available at Sqlite.php

1. <?php
2. /*
3. * $Id: Sqlite.php 1889 2007-06-28 12:11:55Z zYne $
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.com>.
20. */
21. Doctrine::autoload('Doctrine_Import');
22. /**
23. * @package Doctrine
24. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
25. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
26. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
27. * @version $Revision: 1889 $
28. * @category Object Relational Mapping
29. * @link www.phpdoctrine.com
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. {
42. }
43. /**
44. * lists all availible database functions
45. *
46. * @return array
47. */
48. public function listFunctions()
49. {
51. }
52. /**
53. * lists all database triggers
54. *
55. * @param string|null$database
56. * @return array
57. */
58. public function listTriggers($database = null)
59. {
61. }
62. /**
63. * lists all database sequences
64. *
65. * @param string|null$database
66. * @return array
67. */
68. public function listSequences($database = null)
69. {
70. $query = "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
71. $tableNames = $this->conn->fetchColumn($query);
73. $result = array();
74. foreach ($tableNames as $tableName) {
75. if ($sqn = $this->conn->fixSequenceName($tableName, true)) {
76. $result[] = $sqn;
77. }
78. }
79. if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
80. $result = array_map(($this->conn->getAttribute(Doctrine::ATTR_FIELD_CASE) == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
81. }
82. return $result;
83. }
84. /**
85. * lists table constraints
86. *
87. * @param string $table database table name
88. * @return array
89. */
90. public function listTableConstraints($table)
91. {
92. $table = $this->conn->quote($table, 'text');
94. $query = "SELECT sql FROM sqlite_master WHERE type='index' AND ";
96. if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
97. $query .= 'LOWER(tbl_name) = ' . strtolower($table);
98. } else {
99. $query .= 'tbl_name = ' . $table;
100. }
101. $query .= ' AND sql NOT NULL ORDER BY name';
102. $indexes = $this->conn->fetchColumn($query);
104. $result = array();
105. foreach ($indexes as $sql) {
106. if (preg_match("/^create unique index ([^ ]+) on /i", $sql, $tmp)) {
107. $index = $this->conn->fixIndexName($tmp[1]);
108. if ( ! empty($index)) {
109. $result[$index] = true;
110. }
111. }
112. }
114. if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
115. $result = array_change_key_case($result, $this->conn->getAttribute(Doctrine::ATTR_FIELD_CASE));
116. }
117. return array_keys($result);
118. }
119. /**
120. * lists table constraints
121. *
122. * @param string $table database table name
123. * @return array
124. */
125. public function listTableColumns($table)
126. {
127. $sql = 'PRAGMA table_info(' . $table . ')';
128. $result = $this->conn->fetchAll($sql);
130. $description = array();
131. $columns = array();
132. foreach ($result as $key => $val) {
133. $val = array_change_key_case($val, CASE_LOWER);
134. $decl = $this->conn->dataDict->getPortableDeclaration($val);
136. $description = array(
137. 'name' => $val['name'],
138. 'ntype' => $val['type'],
139. 'type' => $decl['type'][0],
140. 'alltypes' => $decl['type'],
141. 'notnull' => (bool) $val['notnull'],
142. 'default' => $val['dflt_value'],
143. 'primary' => (bool) $val['pk'],
144. 'length' => null,
145. 'scale' => null,
146. 'precision' => null,
147. 'unsigned' => null,
148. );
149. $columns[$val['name']] = $description;
150. }
151. return $columns;
152. }
153. /**
154. * lists table constraints
155. *
156. * @param string $table database table name
157. * @return array
158. */
159. public function listTableIndexes($table)
160. {
161. $sql = 'PRAGMA index_list(' . $table . ')';
162. return $this->conn->fetchColumn($sql);
163. }
164. /**
165. * lists tables
166. *
167. * @param string|null$database
168. * @return array
169. */
170. public function listTables($database = null)
171. {
172. $sql = "SELECT name FROM sqlite_master WHERE type = 'table' "
173. . "UNION ALL SELECT name FROM sqlite_temp_master "
174. . "WHERE type = 'table' ORDER BY name";
176. return $this->conn->fetchColumn($sql);
177. }
178. /**
179. * lists table triggers
180. *
181. * @param string $table database table name
182. * @return array
183. */
184. public function listTableTriggers($table)
185. {
187. }
188. /**
189. * lists table views
190. *
191. * @param string $table database table name
192. * @return array
193. */
194. public function listTableViews($table)
195. {
196. $query = "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL";
197. $views = $db->fetchAll($query);
199. $result = array();
200. foreach ($views as $row) {
201. if (preg_match("/^create view .* \bfrom\b\s+\b{$table}\b /i", $row['sql'])) {
202. if ( ! empty($row['name'])) {
203. $result[$row['name']] = true;
204. }
205. }
206. }
207. return $result;
208. }
209. /**
210. * lists database users
211. *
212. * @return array
213. */
214. public function listUsers()
215. {
217. }
218. /**
219. * lists database views
220. *
221. * @param string|null$database
222. * @return array
223. */
224. public function listViews($database = null)
225. {
226. $query = "SELECT name FROM sqlite_master WHERE type='view' AND sql NOT NULL";
228. return $this->conn->fetchColumn($query);
229. }
230. }