SQLAnywhereSchemaManager.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. public function startDatabase($database)
  46. {
  47. assert($this->_platform instanceof SQLAnywherePlatform);
  48. $this->_execSql($this->_platform->getStartDatabaseSQL($database));
  49. }
  50. /**
  51. * Stops a database.
  52. *
  53. * @param string $database The name of the database to stop.
  54. */
  55. public function stopDatabase($database)
  56. {
  57. assert($this->_platform instanceof SQLAnywherePlatform);
  58. $this->_execSql($this->_platform->getStopDatabaseSQL($database));
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. protected function _getPortableDatabaseDefinition($database)
  64. {
  65. return $database['name'];
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. protected function _getPortableSequenceDefinition($sequence)
  71. {
  72. return new Sequence($sequence['sequence_name'], $sequence['increment_by'], $sequence['start_with']);
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. protected function _getPortableTableColumnDefinition($tableColumn)
  78. {
  79. $type = $this->_platform->getDoctrineTypeMapping($tableColumn['type']);
  80. $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
  81. $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
  82. $precision = null;
  83. $scale = null;
  84. $fixed = false;
  85. $default = null;
  86. if ($tableColumn['default'] !== null) {
  87. // Strip quotes from default value.
  88. $default = preg_replace(["/^'(.*)'$/", "/''/"], ['$1', "'"], $tableColumn['default']);
  89. if ($default === 'autoincrement') {
  90. $default = null;
  91. }
  92. }
  93. switch ($tableColumn['type']) {
  94. case 'binary':
  95. case 'char':
  96. case 'nchar':
  97. $fixed = true;
  98. }
  99. switch ($type) {
  100. case 'decimal':
  101. case 'float':
  102. $precision = $tableColumn['length'];
  103. $scale = $tableColumn['scale'];
  104. }
  105. return new Column(
  106. $tableColumn['column_name'],
  107. Type::getType($type),
  108. [
  109. 'length' => $type === 'string' ? $tableColumn['length'] : null,
  110. 'precision' => $precision,
  111. 'scale' => $scale,
  112. 'unsigned' => (bool) $tableColumn['unsigned'],
  113. 'fixed' => $fixed,
  114. 'notnull' => (bool) $tableColumn['notnull'],
  115. 'default' => $default,
  116. 'autoincrement' => (bool) $tableColumn['autoincrement'],
  117. 'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
  118. ? $tableColumn['comment']
  119. : null,
  120. ]
  121. );
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. protected function _getPortableTableDefinition($table)
  127. {
  128. return $table['table_name'];
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
  134. {
  135. return new ForeignKeyConstraint(
  136. $tableForeignKey['local_columns'],
  137. $tableForeignKey['foreign_table'],
  138. $tableForeignKey['foreign_columns'],
  139. $tableForeignKey['name'],
  140. $tableForeignKey['options']
  141. );
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. protected function _getPortableTableForeignKeysList($tableForeignKeys)
  147. {
  148. $foreignKeys = [];
  149. foreach ($tableForeignKeys as $tableForeignKey) {
  150. if (! isset($foreignKeys[$tableForeignKey['index_name']])) {
  151. $foreignKeys[$tableForeignKey['index_name']] = [
  152. 'local_columns' => [$tableForeignKey['local_column']],
  153. 'foreign_table' => $tableForeignKey['foreign_table'],
  154. 'foreign_columns' => [$tableForeignKey['foreign_column']],
  155. 'name' => $tableForeignKey['index_name'],
  156. 'options' => [
  157. 'notnull' => $tableForeignKey['notnull'],
  158. 'match' => $tableForeignKey['match'],
  159. 'onUpdate' => $tableForeignKey['on_update'],
  160. 'onDelete' => $tableForeignKey['on_delete'],
  161. 'check_on_commit' => $tableForeignKey['check_on_commit'],
  162. 'clustered' => $tableForeignKey['clustered'],
  163. 'for_olap_workload' => $tableForeignKey['for_olap_workload'],
  164. ],
  165. ];
  166. } else {
  167. $foreignKeys[$tableForeignKey['index_name']]['local_columns'][] = $tableForeignKey['local_column'];
  168. $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
  169. }
  170. }
  171. return parent::_getPortableTableForeignKeysList($foreignKeys);
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null)
  177. {
  178. foreach ($tableIndexRows as &$tableIndex) {
  179. $tableIndex['primary'] = (bool) $tableIndex['primary'];
  180. $tableIndex['flags'] = [];
  181. if ($tableIndex['clustered']) {
  182. $tableIndex['flags'][] = 'clustered';
  183. }
  184. if ($tableIndex['with_nulls_not_distinct']) {
  185. $tableIndex['flags'][] = 'with_nulls_not_distinct';
  186. }
  187. if (! $tableIndex['for_olap_workload']) {
  188. continue;
  189. }
  190. $tableIndex['flags'][] = 'for_olap_workload';
  191. }
  192. return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
  193. }
  194. /**
  195. * {@inheritdoc}
  196. */
  197. protected function _getPortableViewDefinition($view)
  198. {
  199. return new View(
  200. $view['table_name'],
  201. preg_replace('/^.*\s+as\s+SELECT(.*)/i', 'SELECT$1', $view['view_def'])
  202. );
  203. }
  204. }