SQLAnywhereSchemaManager.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace Doctrine\DBAL\Schema;
  3. use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
  4. use Doctrine\DBAL\Types\Type;
  5. use function assert;
  6. use function preg_replace;
  7. /**
  8. * SAP Sybase SQL Anywhere schema manager.
  9. */
  10. class SQLAnywhereSchemaManager extends AbstractSchemaManager
  11. {
  12. /**
  13. * {@inheritdoc}
  14. *
  15. * Starts a database after creation
  16. * as SQL Anywhere needs a database to be started
  17. * before it can be used.
  18. *
  19. * @see startDatabase
  20. */
  21. public function createDatabase($database)
  22. {
  23. parent::createDatabase($database);
  24. $this->startDatabase($database);
  25. }
  26. /**
  27. * {@inheritdoc}
  28. *
  29. * Tries stopping a database before dropping
  30. * as SQL Anywhere needs a database to be stopped
  31. * before it can be dropped.
  32. *
  33. * @see stopDatabase
  34. */
  35. public function dropDatabase($database)
  36. {
  37. $this->tryMethod('stopDatabase', $database);
  38. parent::dropDatabase($database);
  39. }
  40. /**
  41. * Starts a database.
  42. *
  43. * @param string $database The name of the database to start.
  44. *
  45. * @return void
  46. */
  47. public function startDatabase($database)
  48. {
  49. assert($this->_platform instanceof SQLAnywherePlatform);
  50. $this->_execSql($this->_platform->getStartDatabaseSQL($database));
  51. }
  52. /**
  53. * Stops a database.
  54. *
  55. * @param string $database The name of the database to stop.
  56. *
  57. * @return void
  58. */
  59. public function stopDatabase($database)
  60. {
  61. assert($this->_platform instanceof SQLAnywherePlatform);
  62. $this->_execSql($this->_platform->getStopDatabaseSQL($database));
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function _getPortableDatabaseDefinition($database)
  68. {
  69. return $database['name'];
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. protected function _getPortableSequenceDefinition($sequence)
  75. {
  76. return new Sequence($sequence['sequence_name'], $sequence['increment_by'], $sequence['start_with']);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. protected function _getPortableTableColumnDefinition($tableColumn)
  82. {
  83. $type = $this->_platform->getDoctrineTypeMapping($tableColumn['type']);
  84. $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
  85. $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
  86. $precision = null;
  87. $scale = null;
  88. $fixed = false;
  89. $default = null;
  90. if ($tableColumn['default'] !== null) {
  91. // Strip quotes from default value.
  92. $default = preg_replace(["/^'(.*)'$/", "/''/"], ['$1', "'"], $tableColumn['default']);
  93. if ($default === 'autoincrement') {
  94. $default = null;
  95. }
  96. }
  97. switch ($tableColumn['type']) {
  98. case 'binary':
  99. case 'char':
  100. case 'nchar':
  101. $fixed = true;
  102. break;
  103. }
  104. switch ($type) {
  105. case 'decimal':
  106. case 'float':
  107. $precision = $tableColumn['length'];
  108. $scale = $tableColumn['scale'];
  109. break;
  110. }
  111. return new Column(
  112. $tableColumn['column_name'],
  113. Type::getType($type),
  114. [
  115. 'length' => $type === 'string' ? $tableColumn['length'] : null,
  116. 'precision' => $precision,
  117. 'scale' => $scale,
  118. 'unsigned' => (bool) $tableColumn['unsigned'],
  119. 'fixed' => $fixed,
  120. 'notnull' => (bool) $tableColumn['notnull'],
  121. 'default' => $default,
  122. 'autoincrement' => (bool) $tableColumn['autoincrement'],
  123. 'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
  124. ? $tableColumn['comment']
  125. : null,
  126. ]
  127. );
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. protected function _getPortableTableDefinition($table)
  133. {
  134. return $table['table_name'];
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
  140. {
  141. return new ForeignKeyConstraint(
  142. $tableForeignKey['local_columns'],
  143. $tableForeignKey['foreign_table'],
  144. $tableForeignKey['foreign_columns'],
  145. $tableForeignKey['name'],
  146. $tableForeignKey['options']
  147. );
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. protected function _getPortableTableForeignKeysList($tableForeignKeys)
  153. {
  154. $foreignKeys = [];
  155. foreach ($tableForeignKeys as $tableForeignKey) {
  156. if (! isset($foreignKeys[$tableForeignKey['index_name']])) {
  157. $foreignKeys[$tableForeignKey['index_name']] = [
  158. 'local_columns' => [$tableForeignKey['local_column']],
  159. 'foreign_table' => $tableForeignKey['foreign_table'],
  160. 'foreign_columns' => [$tableForeignKey['foreign_column']],
  161. 'name' => $tableForeignKey['index_name'],
  162. 'options' => [
  163. 'notnull' => $tableForeignKey['notnull'],
  164. 'match' => $tableForeignKey['match'],
  165. 'onUpdate' => $tableForeignKey['on_update'],
  166. 'onDelete' => $tableForeignKey['on_delete'],
  167. 'check_on_commit' => $tableForeignKey['check_on_commit'],
  168. 'clustered' => $tableForeignKey['clustered'],
  169. 'for_olap_workload' => $tableForeignKey['for_olap_workload'],
  170. ],
  171. ];
  172. } else {
  173. $foreignKeys[$tableForeignKey['index_name']]['local_columns'][] = $tableForeignKey['local_column'];
  174. $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
  175. }
  176. }
  177. return parent::_getPortableTableForeignKeysList($foreignKeys);
  178. }
  179. /**
  180. * {@inheritdoc}
  181. */
  182. protected function _getPortableTableIndexesList($tableIndexes, $tableName = null)
  183. {
  184. foreach ($tableIndexes as &$tableIndex) {
  185. $tableIndex['primary'] = (bool) $tableIndex['primary'];
  186. $tableIndex['flags'] = [];
  187. if ($tableIndex['clustered']) {
  188. $tableIndex['flags'][] = 'clustered';
  189. }
  190. if ($tableIndex['with_nulls_not_distinct']) {
  191. $tableIndex['flags'][] = 'with_nulls_not_distinct';
  192. }
  193. if (! $tableIndex['for_olap_workload']) {
  194. continue;
  195. }
  196. $tableIndex['flags'][] = 'for_olap_workload';
  197. }
  198. return parent::_getPortableTableIndexesList($tableIndexes, $tableName);
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. protected function _getPortableViewDefinition($view)
  204. {
  205. $definition = preg_replace('/^.*\s+as\s+SELECT(.*)/i', 'SELECT$1', $view['view_def']);
  206. return new View($view['table_name'], $definition);
  207. }
  208. }