SQLAnywhereSchemaManager.php 7.3 KB

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