PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/MDB2/Driver/Datatype/sqlite.php

https://github.com/chregu/fluxcms
PHP | 354 lines | 198 code | 19 blank | 137 comment | 30 complexity | 28ec36469e2a0126877d680c6dc9ef29 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. // vim: set et ts=4 sw=4 fdm=marker:
  3. // +----------------------------------------------------------------------+
  4. // | PHP versions 4 and 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox, |
  7. // | Stig. S. Bakken, Lukas Smith |
  8. // | All rights reserved. |
  9. // +----------------------------------------------------------------------+
  10. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
  11. // | API as well as database abstraction for PHP applications. |
  12. // | This LICENSE is in the BSD license style. |
  13. // | |
  14. // | Redistribution and use in source and binary forms, with or without |
  15. // | modification, are permitted provided that the following conditions |
  16. // | are met: |
  17. // | |
  18. // | Redistributions of source code must retain the above copyright |
  19. // | notice, this list of conditions and the following disclaimer. |
  20. // | |
  21. // | Redistributions in binary form must reproduce the above copyright |
  22. // | notice, this list of conditions and the following disclaimer in the |
  23. // | documentation and/or other materials provided with the distribution. |
  24. // | |
  25. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
  26. // | Lukas Smith nor the names of his contributors may be used to endorse |
  27. // | or promote products derived from this software without specific prior|
  28. // | written permission. |
  29. // | |
  30. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  31. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  32. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
  33. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
  34. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
  35. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  36. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  37. // | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
  38. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  39. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  40. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
  41. // | POSSIBILITY OF SUCH DAMAGE. |
  42. // +----------------------------------------------------------------------+
  43. // | Author: Lukas Smith <smith@pooteeweet.org> |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id$
  47. //
  48. require_once 'MDB2/Driver/Datatype/Common.php';
  49. /**
  50. * MDB2 SQLite driver
  51. *
  52. * @package MDB2
  53. * @category Database
  54. * @author Lukas Smith <smith@pooteeweet.org>
  55. */
  56. class MDB2_Driver_Datatype_sqlite extends MDB2_Driver_Datatype_Common
  57. {
  58. // }}}
  59. // {{{ getTypeDeclaration()
  60. /**
  61. * Obtain DBMS specific SQL code portion needed to declare an text type
  62. * field to be used in statements like CREATE TABLE.
  63. *
  64. * @param array $field associative array with the name of the properties
  65. * of the field being declared as array indexes. Currently, the types
  66. * of supported field properties are as follows:
  67. *
  68. * length
  69. * Integer value that determines the maximum length of the text
  70. * field. If this argument is missing the field should be
  71. * declared to have the longest length allowed by the DBMS.
  72. *
  73. * default
  74. * Text value to be used as default for this field.
  75. *
  76. * notnull
  77. * Boolean flag that indicates whether this field is constrained
  78. * to not be set to null.
  79. * @return string DBMS specific SQL code portion that should be used to
  80. * declare the specified field.
  81. * @access public
  82. */
  83. function getTypeDeclaration($field)
  84. {
  85. $db =& $this->getDBInstance();
  86. if (PEAR::isError($db)) {
  87. return $db;
  88. }
  89. switch ($field['type']) {
  90. case 'text':
  91. return array_key_exists('length', $field) ? 'CHAR ('.$field['length'].')' : 'TEXT';
  92. case 'clob':
  93. if (array_key_exists('length', $field)) {
  94. $length = $field['length'];
  95. if ($length <= 255) {
  96. return 'TINYTEXT';
  97. } else {
  98. if ($length <= 65535) {
  99. return 'TEXT';
  100. } elseif ($length <= 16777215) {
  101. return 'MEDIUMTEXT';
  102. }
  103. }
  104. }
  105. return 'LONGTEXT';
  106. case 'blob':
  107. if (array_key_exists('length', $field)) {
  108. $length = $field['length'];
  109. if ($length <= 255) {
  110. return 'TINYBLOB';
  111. } else {
  112. if ($length <= 65535) {
  113. return 'BLOB';
  114. } elseif ($length <= 16777215) {
  115. $type = 'MEDIUMBLOB';
  116. }
  117. }
  118. }
  119. return 'LONGBLOB';
  120. case 'integer':
  121. return 'INT';
  122. case 'boolean':
  123. return 'BOOLEAN';
  124. case 'date':
  125. return 'DATE';
  126. case 'time':
  127. return 'TIME';
  128. case 'timestamp':
  129. return 'DATETIME';
  130. case 'float':
  131. return 'DOUBLE'.($db->options['fixed_float'] ? '('.
  132. ($db->options['fixed_float']+2).','.$db->options['fixed_float'].')' : '');
  133. case 'decimal':
  134. return 'BIGINT';
  135. }
  136. return '';
  137. }
  138. // }}}
  139. // {{{ _getIntegerDeclaration()
  140. /**
  141. * Obtain DBMS specific SQL code portion needed to declare an integer type
  142. * field to be used in statements like CREATE TABLE.
  143. *
  144. * @param string $name name the field to be declared.
  145. * @param string $field associative array with the name of the properties
  146. * of the field being declared as array indexes.
  147. * Currently, the types of supported field
  148. * properties are as follows:
  149. *
  150. * unsigned
  151. * Boolean flag that indicates whether the field
  152. * should be declared as unsigned integer if
  153. * possible.
  154. *
  155. * default
  156. * Integer value to be used as default for this
  157. * field.
  158. *
  159. * notnull
  160. * Boolean flag that indicates whether this field is
  161. * constrained to not be set to null.
  162. * @return string DBMS specific SQL code portion that should be used to
  163. * declare the specified field.
  164. * @access protected
  165. */
  166. function _getIntegerDeclaration($name, $field)
  167. {
  168. $db =& $this->getDBInstance();
  169. if (PEAR::isError($db)) {
  170. return $db;
  171. }
  172. $default = $autoinc = $notnull = '';
  173. if (array_key_exists('autoincrement', $field) && $field['autoincrement']) {
  174. $autoinc = ' PRIMARY KEY';
  175. } else {
  176. if (array_key_exists('default', $field)) {
  177. if ($field['default'] === '') {
  178. $field['default'] = (array_key_exists('notnull', $field) && $field['notnull'])
  179. ? 0 : null;
  180. }
  181. $default = ' DEFAULT '.$this->quote($field['default'], 'integer');
  182. }
  183. $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
  184. }
  185. $unsigned = (array_key_exists('unsigned', $field) && $field['unsigned']) ? ' UNSIGNED' : '';
  186. $name = $db->quoteIdentifier($name, true);
  187. return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull.$autoinc;
  188. }
  189. // }}}
  190. // {{{ _quoteFloat()
  191. /**
  192. * Convert a text value into a DBMS specific format that is suitable to
  193. * compose query statements.
  194. *
  195. * @param string $value text string value that is intended to be converted.
  196. * @return string text string that represents the given argument value in
  197. * a DBMS specific format.
  198. * @access protected
  199. */
  200. function _quoteFloat($value)
  201. {
  202. $db =& $this->getDBInstance();
  203. if (PEAR::isError($db)) {
  204. return $db;
  205. }
  206. return $db->escape($value);
  207. }
  208. // }}}
  209. // {{{ _quoteDecimal()
  210. /**
  211. * Convert a text value into a DBMS specific format that is suitable to
  212. * compose query statements.
  213. *
  214. * @param string $value text string value that is intended to be converted.
  215. * @return string text string that represents the given argument value in
  216. * a DBMS specific format.
  217. * @access protected
  218. */
  219. function _quoteDecimal($value)
  220. {
  221. $db =& $this->getDBInstance();
  222. if (PEAR::isError($db)) {
  223. return $db;
  224. }
  225. return $db->escape($value);
  226. }
  227. // }}}
  228. // {{{ mapNativeDatatype()
  229. /**
  230. * Maps a native array description of a field to a MDB2 datatype and length
  231. *
  232. * @param array $field native field description
  233. * @return array containing the various possible types and the length
  234. * @access public
  235. */
  236. function mapNativeDatatype($field)
  237. {
  238. $db_type = strtolower($field['type']);
  239. $length = array_key_exists('length', $field) ? $field['length'] : null;
  240. $unsigned = array_key_exists('unsigned', $field) ? $field['unsigned'] : null;
  241. $type = array();
  242. switch ($db_type) {
  243. case 'boolean':
  244. $type[] = 'boolean';
  245. break;
  246. case 'tinyint':
  247. case 'smallint':
  248. case 'mediumint':
  249. case 'int':
  250. case 'integer':
  251. case 'bigint':
  252. $type[] = 'integer';
  253. if ($length == '1') {
  254. $type[] = 'boolean';
  255. if (preg_match('/^[is|has]/', $field['name'])) {
  256. $type = array_reverse($type);
  257. }
  258. }
  259. $type[] = 'decimal';
  260. break;
  261. case 'tinytext':
  262. case 'mediumtext':
  263. case 'longtext':
  264. case 'text':
  265. case 'char':
  266. case 'varchar':
  267. case "varchar2":
  268. if ($length == '1') {
  269. $type[] = 'boolean';
  270. if (preg_match('/[is|has]/', $field['name'])) {
  271. $type = array_reverse($type);
  272. }
  273. } elseif (strstr($db_type, 'text')) {
  274. $type[] = 'clob';
  275. }
  276. $type[] = 'text';
  277. break;
  278. case 'enum':
  279. preg_match_all('/\'.+\'/U',$row[$type_column], $matches);
  280. $length = 0;
  281. if (is_array($matches)) {
  282. foreach ($matches[0] as $value) {
  283. $length = max($length, strlen($value)-2);
  284. }
  285. }
  286. case 'set':
  287. $type[] = 'text';
  288. $type[] = 'integer';
  289. break;
  290. case 'date':
  291. $type[] = 'date';
  292. $length = null;
  293. break;
  294. case 'datetime':
  295. case 'timestamp':
  296. $type[] = 'timestamp';
  297. $length = null;
  298. break;
  299. case 'time':
  300. $type[] = 'time';
  301. $length = null;
  302. break;
  303. case 'float':
  304. case 'double':
  305. case 'real':
  306. $type[] = 'float';
  307. break;
  308. case 'decimal':
  309. case 'numeric':
  310. $type[] = 'decimal';
  311. break;
  312. case 'tinyblob':
  313. case 'mediumblob':
  314. case 'longblob':
  315. case 'blob':
  316. $type[] = 'blob';
  317. $length = null;
  318. break;
  319. case 'year':
  320. $type[] = 'integer';
  321. $type[] = 'date';
  322. $length = null;
  323. break;
  324. default:
  325. $db =& $this->getDBInstance();
  326. if (PEAR::isError($db)) {
  327. return $db;
  328. }
  329. return $db->raiseError(MDB2_ERROR, null, null,
  330. 'getTableFieldDefinition: unknown database attribute type: '.$db_type);
  331. }
  332. return array($type, $length, $unsigned);
  333. }
  334. }
  335. ?>