SQLServerSchemaManager.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
  21. use Doctrine\DBAL\Events;
  22. /**
  23. * SQL Server Schema Manager
  24. *
  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. * @author Juozas Kaziukenas <juozas@juokaz.com>
  29. * @since 2.0
  30. */
  31. class SQLServerSchemaManager extends AbstractSchemaManager
  32. {
  33. /**
  34. * @override
  35. */
  36. protected function _getPortableTableColumnDefinition($tableColumn)
  37. {
  38. $dbType = strtolower($tableColumn['TYPE_NAME']);
  39. $autoincrement = false;
  40. if (stripos($dbType, 'identity')) {
  41. $dbType = trim(str_ireplace('identity', '', $dbType));
  42. $autoincrement = true;
  43. }
  44. $type = array();
  45. $unsigned = $fixed = null;
  46. if (!isset($tableColumn['name'])) {
  47. $tableColumn['name'] = '';
  48. }
  49. $default = $tableColumn['COLUMN_DEF'];
  50. while ($default != ($default2 = preg_replace("/^\((.*)\)$/", '$1', $default))) {
  51. $default = trim($default2, "'");
  52. }
  53. $length = (int) $tableColumn['LENGTH'];
  54. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  55. switch ($type) {
  56. case 'char':
  57. if ($tableColumn['LENGTH'] == '1') {
  58. $type = 'boolean';
  59. if (preg_match('/^(is|has)/', $tableColumn['name'])) {
  60. $type = array_reverse($type);
  61. }
  62. }
  63. $fixed = true;
  64. break;
  65. case 'text':
  66. $fixed = false;
  67. break;
  68. }
  69. switch ($dbType) {
  70. case 'nchar':
  71. case 'nvarchar':
  72. case 'ntext':
  73. // Unicode data requires 2 bytes per character
  74. $length = $length / 2;
  75. break;
  76. }
  77. $options = array(
  78. 'length' => ($length == 0 || !in_array($type, array('text', 'string'))) ? null : $length,
  79. 'unsigned' => (bool) $unsigned,
  80. 'fixed' => (bool) $fixed,
  81. 'default' => $default !== 'NULL' ? $default : null,
  82. 'notnull' => (bool) ($tableColumn['IS_NULLABLE'] != 'YES'),
  83. 'scale' => $tableColumn['SCALE'],
  84. 'precision' => $tableColumn['PRECISION'],
  85. 'autoincrement' => $autoincrement,
  86. );
  87. return new Column($tableColumn['COLUMN_NAME'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  88. }
  89. /**
  90. * @override
  91. */
  92. protected function _getPortableTableIndexesList($tableIndexRows, $tableName=null)
  93. {
  94. $result = array();
  95. foreach ($tableIndexRows AS $tableIndex) {
  96. $indexName = $keyName = $tableIndex['index_name'];
  97. if (strpos($tableIndex['index_description'], 'primary key') !== false) {
  98. $keyName = 'primary';
  99. }
  100. $keyName = strtolower($keyName);
  101. $result[$keyName] = array(
  102. 'name' => $indexName,
  103. 'columns' => explode(', ', $tableIndex['index_keys']),
  104. 'unique' => strpos($tableIndex['index_description'], 'unique') !== false,
  105. 'primary' => strpos($tableIndex['index_description'], 'primary key') !== false,
  106. );
  107. }
  108. $eventManager = $this->_platform->getEventManager();
  109. $indexes = array();
  110. foreach ($result AS $indexKey => $data) {
  111. $index = null;
  112. $defaultPrevented = false;
  113. if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) {
  114. $eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn);
  115. $eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs);
  116. $defaultPrevented = $eventArgs->isDefaultPrevented();
  117. $index = $eventArgs->getIndex();
  118. }
  119. if (!$defaultPrevented) {
  120. $index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
  121. }
  122. if ($index) {
  123. $indexes[$indexKey] = $index;
  124. }
  125. }
  126. return $indexes;
  127. }
  128. /**
  129. * @override
  130. */
  131. public function _getPortableTableForeignKeyDefinition($tableForeignKey)
  132. {
  133. return new ForeignKeyConstraint(
  134. (array) $tableForeignKey['ColumnName'],
  135. $tableForeignKey['ReferenceTableName'],
  136. (array) $tableForeignKey['ReferenceColumnName'],
  137. $tableForeignKey['ForeignKey'],
  138. array(
  139. 'onUpdate' => str_replace('_', ' ', $tableForeignKey['update_referential_action_desc']),
  140. 'onDelete' => str_replace('_', ' ', $tableForeignKey['delete_referential_action_desc']),
  141. )
  142. );
  143. }
  144. /**
  145. * @override
  146. */
  147. protected function _getPortableTableDefinition($table)
  148. {
  149. return $table['name'];
  150. }
  151. /**
  152. * @override
  153. */
  154. protected function _getPortableDatabaseDefinition($database)
  155. {
  156. return $database['name'];
  157. }
  158. /**
  159. * @override
  160. */
  161. protected function _getPortableViewDefinition($view)
  162. {
  163. // @todo
  164. return new View($view['name'], null);
  165. }
  166. /**
  167. * List the indexes for a given table returning an array of Index instances.
  168. *
  169. * Keys of the portable indexes list are all lower-cased.
  170. *
  171. * @param string $table The name of the table
  172. * @return Index[] $tableIndexes
  173. */
  174. public function listTableIndexes($table)
  175. {
  176. $sql = $this->_platform->getListTableIndexesSQL($table, $this->_conn->getDatabase());
  177. try {
  178. $tableIndexes = $this->_conn->fetchAll($sql);
  179. } catch(\PDOException $e) {
  180. if ($e->getCode() == "IMSSP") {
  181. return array();
  182. } else {
  183. throw $e;
  184. }
  185. }
  186. return $this->_getPortableTableIndexesList($tableIndexes, $table);
  187. }
  188. }