SQLAnywhereSchemaManager.php 7.9 KB

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