# Source for file Sqlite.php

Documentation is available at Sqlite.php

1. <?php
2. /*
3. * $Id: Sqlite.php 2196 2007-08-10 20:29:07Z meus $
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_DataDict');
22. /**
23. * @package Doctrine
24. * @subpackage Doctrine_DataDict
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: 2196 $
29. * @category Object Relational Mapping
30. * @link www.phpdoctrine.com
31. * @since 1.0
32. */
33. class Doctrine_DataDict_Sqlite extends Doctrine_DataDict
34. {
35. /**
36. * Obtain DBMS specific SQL code portion needed to declare an text type
37. * field to be used in statements like CREATE TABLE.
38. *
39. * @param array $field associative array with the name of the properties
40. * of the field being declared as array indexes. Currently, the types
41. * of supported field properties are as follows:
42. *
43. * length
44. * Integer value that determines the maximum length of the text
45. * field. If this argument is missing the field should be
46. * declared to have the longest length allowed by the DBMS.
47. *
48. * default
49. * Text value to be used as default for this field.
50. *
51. * notnull
52. * Boolean flag that indicates whether this field is constrained
53. * to not be set to null.
54. * @author Lukas Smith (PEAR MDB2 library)
55. * @return string DBMS specific SQL code portion that should be used to
56. * declare the specified field.
57. */
58. public function getNativeDeclaration(array $field)
59. {
60. if ( ! isset($field['type'])) {
61. throw new Doctrine_DataDict_Exception('Missing column type.');
62. }
63. switch ($field['type']) {
64. case 'text':
65. case 'object':
66. case 'array':
67. case 'string':
68. case 'char':
69. case 'gzip':
70. case 'varchar':
71. $length = (isset($field['length']) && $field['length']) ? $field['length'] : null;
73. $fixed = ((isset($field['fixed']) && $field['fixed']) || $field['type'] == 'char') ? true : false;
75. return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$this->conn->getAttribute(Doctrine::ATTR_DEFAULT_TEXTFLD_LENGTH).')')
76. : ($length ? 'VARCHAR('.$length.')' : 'TEXT');
77. case 'clob':
78. if (!empty($field['length'])) {
79. $length = $field['length'];
80. if ($length <= 255) {
81. return 'TINYTEXT';
82. } elseif ($length <= 65535) {
83. return 'TEXT';
84. } elseif ($length <= 16777215) {
85. return 'MEDIUMTEXT';
86. }
87. }
88. return 'LONGTEXT';
89. case 'blob':
90. if (!empty($field['length'])) {
91. $length = $field['length'];
92. if ($length <= 255) {
93. return 'TINYBLOB';
94. } elseif ($length <= 65535) {
95. return 'BLOB';
96. } elseif ($length <= 16777215) {
97. return 'MEDIUMBLOB';
98. }
99. }
100. return 'LONGBLOB';
101. case 'enum':
102. case 'integer':
103. case 'boolean':
104. case 'int':
105. return 'INTEGER';
106. case 'date':
107. return 'DATE';
108. case 'time':
109. return 'TIME';
110. case 'timestamp':
111. return 'DATETIME';
112. case 'float':
113. case 'double':
114. return 'DOUBLE';//($this->conn->options['fixed_float'] ? '('.
115. //($this->conn->options['fixed_float']+2).','.$this->conn->options['fixed_float'].')' : '');
116. case 'decimal':
117. $length = !empty($field['length']) ? $field['length'] : 18;
118. $scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine::ATTR_DECIMAL_PLACES);
119. return 'DECIMAL('.$length.','.$scale.')';
120. }
121. throw new Doctrine_DataDict_Exception('Unknown field type \'' . $field['type'] . '\'.');
122. }
123. /**
124. * Maps a native array description of a field to Doctrine datatype and length
125. *
126. * @param array $field native field description
127. * @return array containing the various possible types, length, sign, fixed
128. */
129. public function getPortableDeclaration(array $field)
130. {
131. $dbType = strtolower($field['type']);
132. $length = (isset($field['length'])) ? $field['length'] : null;
133. $unsigned = (isset($field['unsigned'])) ? $field['unsigned'] : null;
134. $fixed = null;
135. $type = array();
137. if ( ! isset($field['name'])) {
138. $field['name'] = '';
139. }
141. switch ($dbType) {
142. case 'boolean':
143. $type[] = 'boolean';
144. break;
145. case 'tinyint':
146. $type[] = 'integer';
147. $type[] = 'boolean';
148. if (preg_match('/^(is|has)/', $field['name'])) {
149. $type = array_reverse($type);
150. }
151. $unsigned = preg_match('/ unsigned/i', $field['type']);
152. $length = 1;
153. break;
154. case 'smallint':
155. $type[] = 'integer';
156. $unsigned = preg_match('/ unsigned/i', $field['type']);
157. $length = 2;
158. break;
159. case 'mediumint':
160. $type[] = 'integer';
161. $unsigned = preg_match('/ unsigned/i', $field['type']);
162. $length = 3;
163. break;
164. case 'int':
165. case 'integer':
166. case 'serial':
167. $type[] = 'integer';
168. $unsigned = preg_match('/ unsigned/i', $field['type']);
169. $length = 4;
170. break;
171. case 'bigint':
172. case 'bigserial':
173. $type[] = 'integer';
174. $unsigned = preg_match('/ unsigned/i', $field['type']);
175. $length = 8;
176. break;
177. case 'clob':
178. case 'tinytext':
179. case 'mediumtext':
180. case 'longtext':
181. case 'text':
182. case 'varchar':
183. case 'varchar2':
184. $fixed = false;
185. case 'char':
186. $type[] = 'text';
187. if ($length == '1') {
188. $type[] = 'boolean';
189. if (preg_match('/^(is|has)/', $field['name'])) {
190. $type = array_reverse($type);
191. }
192. } elseif (strstr($dbType, 'text')) {
193. $type[] = 'clob';
194. }
195. if ($fixed !== false) {
196. $fixed = true;
197. }
198. break;
199. case 'date':
200. $type[] = 'date';
201. $length = null;
202. break;
203. case 'datetime':
204. case 'timestamp':
205. $type[] = 'timestamp';
206. $length = null;
207. break;
208. case 'time':
209. $type[] = 'time';
210. $length = null;
211. break;
212. case 'float':
213. case 'double':
214. case 'real':
215. $type[] = 'float';
216. $length = null;
217. break;
218. case 'decimal':
219. case 'numeric':
220. $type[] = 'decimal';
221. $length = null;
222. break;
223. case 'tinyblob':
224. case 'mediumblob':
225. case 'longblob':
226. case 'blob':
227. $type[] = 'blob';
228. $length = null;
229. break;
230. case 'year':
231. $type[] = 'integer';
232. $type[] = 'date';
233. $length = null;
234. break;
235. default:
236. throw new Doctrine_DataDict_Exception('unknown database attribute type: '.$dbType);
237. }
239. return array('type' => $type,
240. 'length' => $length,
241. 'unsigned' => $unsigned,
242. 'fixed' => $fixed);
243. }
244. /**
245. * Obtain DBMS specific SQL code portion needed to declare an integer type
246. * field to be used in statements like CREATE TABLE.
247. *
248. * @param string $name name the field to be declared.
249. * @param array $field associative array with the name of the properties
250. * of the field being declared as array indexes.
251. * Currently, the types of supported field
252. * properties are as follows:
253. *
254. * unsigned
255. * Boolean flag that indicates whether the field
256. * should be declared as unsigned integer if
257. * possible.
258. *
259. * default
260. * Integer value to be used as default for this
261. * field.
262. *
263. * notnull
264. * Boolean flag that indicates whether this field is
265. * constrained to not be set to null.
266. * @return string DBMS specific SQL code portion that should be used to
267. * declare the specified field.
268. * @access protected
269. */
270. public function getIntegerDeclaration($name, array $field)
271. {
272. $default = $autoinc = '';
273. $type = $this->getNativeDeclaration($field);
275. $autoincrement = isset($field['autoincrement']) && $field['autoincrement'];
277. if ($autoincrement){
278. $autoinc = ' PRIMARY KEY AUTOINCREMENT';
279. $type = 'INTEGER';
280. } elseif (array_key_exists('default', $field)) {
281. if ($field['default'] === '') {
282. $field['default'] = empty($field['notnull']) ? null : 0;
283. }
284. $default = ' DEFAULT ' . $this->conn->quote($field['default'], $field['type']);
285. }/**
286. elseif (empty($field['notnull'])) {
287. $default = ' DEFAULT NULL';
288. }
289. */
291. $notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
293. // sqlite does not support unsigned attribute for autoinremented fields
294. $unsigned = (isset($field['unsigned']) && $field['unsigned'] && !$autoincrement) ? ' UNSIGNED' : '';
296. $name = $this->conn->quoteIdentifier($name, true);
297. return $name . ' ' . $type . $unsigned . $default . $notnull . $autoinc;
298. }
299. }