PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/symfony/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/DataDict/Sqlite.php

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