MsSqlSchemaManager.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.phpdoctrine.org>.
  18. */
  19. namespace Doctrine\DBAL\Schema;
  20. /**
  21. * xxx
  22. *
  23. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  24. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  25. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  26. * @author Juozas Kaziukenas <juozas@juokaz.com>
  27. * @version $Revision$
  28. * @since 2.0
  29. */
  30. class MsSqlSchemaManager extends AbstractSchemaManager
  31. {
  32. /**
  33. * @override
  34. */
  35. protected function _getPortableTableColumnDefinition($tableColumn)
  36. {
  37. $dbType = strtolower($tableColumn['TYPE_NAME']);
  38. $autoincrement = false;
  39. if (stripos($dbType, 'identity')) {
  40. $dbType = trim(str_ireplace('identity', '', $dbType));
  41. $autoincrement = true;
  42. }
  43. $type = array();
  44. $unsigned = $fixed = null;
  45. if (!isset($tableColumn['name'])) {
  46. $tableColumn['name'] = '';
  47. }
  48. $default = $tableColumn['COLUMN_DEF'];
  49. while ($default != ($default2 = preg_replace("/^\((.*)\)$/", '$1', $default))) {
  50. $default = $default2;
  51. }
  52. $length = (int) $tableColumn['LENGTH'];
  53. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  54. switch ($type) {
  55. case 'char':
  56. if ($tableColumn['LENGTH'] == '1') {
  57. $type = 'boolean';
  58. if (preg_match('/^(is|has)/', $tableColumn['name'])) {
  59. $type = array_reverse($type);
  60. }
  61. }
  62. $fixed = true;
  63. break;
  64. case 'text':
  65. $fixed = false;
  66. break;
  67. }
  68. switch ($dbType) {
  69. case 'nchar':
  70. case 'nvarchar':
  71. case 'ntext':
  72. // Unicode data requires 2 bytes per character
  73. $length = $length / 2;
  74. break;
  75. }
  76. $options = array(
  77. 'length' => ($length == 0 || !in_array($type, array('text', 'string'))) ? null : $length,
  78. 'unsigned' => (bool) $unsigned,
  79. 'fixed' => (bool) $fixed,
  80. 'default' => $default !== 'NULL' ? $default : null,
  81. 'notnull' => (bool) ($tableColumn['IS_NULLABLE'] != 'YES'),
  82. 'scale' => $tableColumn['SCALE'],
  83. 'precision' => $tableColumn['PRECISION'],
  84. 'autoincrement' => $autoincrement,
  85. );
  86. return new Column($tableColumn['COLUMN_NAME'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  87. }
  88. /**
  89. * @override
  90. */
  91. protected function _getPortableTableIndexesList($tableIndexRows, $tableName=null)
  92. {
  93. $result = array();
  94. foreach ($tableIndexRows AS $tableIndex) {
  95. $indexName = $keyName = $tableIndex['index_name'];
  96. if (strpos($tableIndex['index_description'], 'primary key') !== false) {
  97. $keyName = 'primary';
  98. }
  99. $keyName = strtolower($keyName);
  100. $result[$keyName] = array(
  101. 'name' => $indexName,
  102. 'columns' => explode(', ', $tableIndex['index_keys']),
  103. 'unique' => strpos($tableIndex['index_description'], 'unique') !== false,
  104. 'primary' => strpos($tableIndex['index_description'], 'primary key') !== false,
  105. );
  106. }
  107. $indexes = array();
  108. foreach ($result AS $indexKey => $data) {
  109. $indexes[$indexKey] = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
  110. }
  111. return $indexes;
  112. }
  113. /**
  114. * @override
  115. */
  116. public function _getPortableTableForeignKeyDefinition($tableForeignKey)
  117. {
  118. return new ForeignKeyConstraint(
  119. (array) $tableForeignKey['ColumnName'],
  120. $tableForeignKey['ReferenceTableName'],
  121. (array) $tableForeignKey['ReferenceColumnName'],
  122. $tableForeignKey['ForeignKey'],
  123. array(
  124. 'onUpdate' => str_replace('_', ' ', $tableForeignKey['update_referential_action_desc']),
  125. 'onDelete' => str_replace('_', ' ', $tableForeignKey['delete_referential_action_desc']),
  126. )
  127. );
  128. }
  129. /**
  130. * @override
  131. */
  132. protected function _getPortableTableDefinition($table)
  133. {
  134. return $table['name'];
  135. }
  136. /**
  137. * @override
  138. */
  139. protected function _getPortableDatabaseDefinition($database)
  140. {
  141. return $database['name'];
  142. }
  143. /**
  144. * @override
  145. */
  146. protected function _getPortableViewDefinition($view)
  147. {
  148. // @todo
  149. return new View($view['name'], null);
  150. }
  151. }