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

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

https://github.com/dada888/phpcollab3
PHP | 266 lines | 164 code | 14 blank | 88 comment | 24 complexity | c6aabd01df374aa681e52e73dab0bc09 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: Mssql.php 7490 2010-03-29 19:53:27Z 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.doctrine-project.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. * @author Frank M. Kromann <frank@kromann.info> (PEAR MDB2 Mssql driver)
  28. * @author David Coallier <davidc@php.net> (PEAR MDB2 Mssql driver)
  29. * @version $Revision: 7490 $
  30. * @link www.doctrine-project.org
  31. * @since 1.0
  32. */
  33. class Doctrine_DataDict_Mssql 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. *
  55. * @return string DBMS specific SQL code portion that should be used to
  56. * declare the specified field.
  57. */
  58. public function getNativeDeclaration($field)
  59. {
  60. if ( ! isset($field['type'])) {
  61. throw new Doctrine_DataDict_Exception('Missing column type.');
  62. }
  63. switch ($field['type']) {
  64. case 'enum':
  65. $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255;
  66. case 'array':
  67. case 'object':
  68. case 'text':
  69. case 'char':
  70. case 'varchar':
  71. case 'string':
  72. case 'gzip':
  73. $length = !empty($field['length'])
  74. ? $field['length'] : false;
  75. $fixed = ((isset($field['fixed']) && $field['fixed']) || $field['type'] == 'char') ? true : false;
  76. return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$this->conn->varchar_max_length.')')
  77. : ($length ? 'VARCHAR('.$length.')' : 'TEXT');
  78. case 'clob':
  79. if ( ! empty($field['length'])) {
  80. $length = $field['length'];
  81. if ($length <= 8000) {
  82. return 'VARCHAR('.$length.')';
  83. }
  84. }
  85. return 'TEXT';
  86. case 'blob':
  87. if ( ! empty($field['length'])) {
  88. $length = $field['length'];
  89. if ($length <= 8000) {
  90. return "VARBINARY($length)";
  91. }
  92. }
  93. return 'IMAGE';
  94. case 'integer':
  95. case 'int':
  96. return (isset($field['unsigned']) && $field['unsigned']) ? 'BIGINT' : 'INT';
  97. case 'boolean':
  98. return 'BIT';
  99. case 'date':
  100. return 'CHAR(' . strlen('YYYY-MM-DD') . ')';
  101. case 'time':
  102. return 'CHAR(' . strlen('HH:MM:SS') . ')';
  103. case 'timestamp':
  104. return 'CHAR(' . strlen('YYYY-MM-DD HH:MM:SS') . ')';
  105. case 'float':
  106. return 'FLOAT';
  107. case 'decimal':
  108. $length = !empty($field['length']) ? $field['length'] : 18;
  109. $scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine_Core::ATTR_DECIMAL_PLACES);
  110. return 'DECIMAL('.$length.','.$scale.')';
  111. }
  112. return $field['type'] . (isset($field['length']) ? '('.$field['length'].')':null);
  113. }
  114. /**
  115. * Maps a native array description of a field to a MDB2 datatype and length
  116. *
  117. * @param array $field native field description
  118. * @return array containing the various possible types, length, sign, fixed
  119. */
  120. public function getPortableDeclaration($field)
  121. {
  122. $db_type = preg_replace('/[\d\(\)]/','', strtolower($field['type']) );
  123. $length = (isset($field['length']) && $field['length'] > 0) ? $field['length'] : null;
  124. $type = array();
  125. // todo: unsigned handling seems to be missing
  126. $unsigned = $fixed = null;
  127. if ( ! isset($field['name']))
  128. $field['name'] = '';
  129. switch ($db_type) {
  130. case 'bit':
  131. $type[0] = 'boolean';
  132. break;
  133. case 'tinyint':
  134. case 'smallint':
  135. case 'bigint':
  136. case 'int':
  137. $type[0] = 'integer';
  138. if ($length == 1) {
  139. $type[] = 'boolean';
  140. }
  141. break;
  142. case 'date':
  143. $type[0] = 'date';
  144. break;
  145. case 'datetime':
  146. case 'timestamp':
  147. case 'smalldatetime':
  148. $type[0] = 'timestamp';
  149. break;
  150. case 'float':
  151. case 'real':
  152. case 'numeric':
  153. $type[0] = 'float';
  154. break;
  155. case 'decimal':
  156. case 'money':
  157. case 'smallmoney':
  158. $type[0] = 'decimal';
  159. break;
  160. case 'text':
  161. case 'varchar':
  162. case 'ntext':
  163. case 'nvarchar':
  164. $fixed = false;
  165. case 'char':
  166. case 'nchar':
  167. $type[0] = 'string';
  168. if ($length == '1') {
  169. $type[] = 'boolean';
  170. if (preg_match('/^[is|has]/', $field['name'])) {
  171. $type = array_reverse($type);
  172. }
  173. } elseif (strstr($db_type, 'text')) {
  174. $type[] = 'clob';
  175. }
  176. if ($fixed !== false) {
  177. $fixed = true;
  178. }
  179. break;
  180. case 'image':
  181. case 'varbinary':
  182. $type[] = 'blob';
  183. $length = null;
  184. break;
  185. case 'uniqueidentifier':
  186. $type[] = 'string';
  187. $length = 36;
  188. break;
  189. case 'sql_variant':
  190. case 'sysname':
  191. case 'binary':
  192. $type[] = 'string';
  193. $length = null;
  194. break;
  195. default:
  196. $type[] = $field['type'];
  197. $length = isset($field['length']) ? $field['length']:null;
  198. }
  199. return array('type' => $type,
  200. 'length' => $length,
  201. 'unsigned' => $unsigned,
  202. 'fixed' => $fixed);
  203. }
  204. /**
  205. * Obtain DBMS specific SQL code portion needed to declare an integer type
  206. * field to be used in statements like CREATE TABLE.
  207. *
  208. * @param string $name name the field to be declared.
  209. * @param string $field associative array with the name of the properties
  210. * of the field being declared as array indexes.
  211. * Currently, the types of supported field
  212. * properties are as follows:
  213. *
  214. * unsigned
  215. * Boolean flag that indicates whether the field
  216. * should be declared as unsigned integer if
  217. * possible.
  218. *
  219. * default
  220. * Integer value to be used as default for this
  221. * field.
  222. *
  223. * notnull
  224. * Boolean flag that indicates whether this field is
  225. * constrained to not be set to null.
  226. * @return string DBMS specific SQL code portion that should be used to
  227. * declare the specified field.
  228. */
  229. public function getIntegerDeclaration($name, $field)
  230. {
  231. $default = $autoinc = '';
  232. if ( ! empty($field['autoincrement'])) {
  233. $autoinc = ' identity';
  234. } elseif (array_key_exists('default', $field)) {
  235. if ($field['default'] === '') {
  236. $field['default'] = empty($field['notnull']) ? null : 0;
  237. }
  238. $default = ' DEFAULT ' . (is_null($field['default'])
  239. ? 'NULL'
  240. : $this->conn->quote($field['default']));
  241. }
  242. $notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ' NULL';
  243. //$unsigned = (isset($field['unsigned']) && $field['unsigned']) ? ' UNSIGNED' : '';
  244. // MSSQL does not support the UNSIGNED keyword
  245. $unsigned = '';
  246. $comment = (isset($field['comment']) && $field['comment'])
  247. ? " COMMENT '" . $field['comment'] . "'" : '';
  248. $name = $this->conn->quoteIdentifier($name, true);
  249. return $name . ' ' . $this->getNativeDeclaration($field) . $unsigned
  250. . $default . $notnull . $autoinc . $comment;
  251. }
  252. }