OracleSchemaManager.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. /**
  21. * Oracle Schema Manager.
  22. *
  23. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  24. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  25. * @author Benjamin Eberlei <kontakt@beberlei.de>
  26. * @since 2.0
  27. */
  28. class OracleSchemaManager extends AbstractSchemaManager
  29. {
  30. /**
  31. * {@inheritdoc}
  32. */
  33. protected function _getPortableViewDefinition($view)
  34. {
  35. $view = \array_change_key_case($view, CASE_LOWER);
  36. return new View($view['view_name'], $view['text']);
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. protected function _getPortableUserDefinition($user)
  42. {
  43. $user = \array_change_key_case($user, CASE_LOWER);
  44. return array(
  45. 'user' => $user['username'],
  46. );
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. protected function _getPortableTableDefinition($table)
  52. {
  53. $table = \array_change_key_case($table, CASE_LOWER);
  54. return $table['table_name'];
  55. }
  56. /**
  57. * {@inheritdoc}
  58. *
  59. * @license New BSD License
  60. * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
  61. */
  62. protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
  63. {
  64. $indexBuffer = array();
  65. foreach ( $tableIndexes as $tableIndex ) {
  66. $tableIndex = \array_change_key_case($tableIndex, CASE_LOWER);
  67. $keyName = strtolower($tableIndex['name']);
  68. if ( strtolower($tableIndex['is_primary']) == "p" ) {
  69. $keyName = 'primary';
  70. $buffer['primary'] = true;
  71. $buffer['non_unique'] = false;
  72. } else {
  73. $buffer['primary'] = false;
  74. $buffer['non_unique'] = ( $tableIndex['is_unique'] == 0 ) ? true : false;
  75. }
  76. $buffer['key_name'] = $keyName;
  77. $buffer['column_name'] = $tableIndex['column_name'];
  78. $indexBuffer[] = $buffer;
  79. }
  80. return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. protected function _getPortableTableColumnDefinition($tableColumn)
  86. {
  87. $tableColumn = \array_change_key_case($tableColumn, CASE_LOWER);
  88. $dbType = strtolower($tableColumn['data_type']);
  89. if(strpos($dbType, "timestamp(") === 0) {
  90. if (strpos($dbType, "WITH TIME ZONE")) {
  91. $dbType = "timestamptz";
  92. } else {
  93. $dbType = "timestamp";
  94. }
  95. }
  96. $unsigned = $fixed = null;
  97. if ( ! isset($tableColumn['column_name'])) {
  98. $tableColumn['column_name'] = '';
  99. }
  100. if (stripos($tableColumn['data_default'], 'NULL') !== null) {
  101. $tableColumn['data_default'] = null;
  102. }
  103. $precision = null;
  104. $scale = null;
  105. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  106. $type = $this->extractDoctrineTypeFromComment($tableColumn['comments'], $type);
  107. $tableColumn['comments'] = $this->removeDoctrineTypeFromComment($tableColumn['comments'], $type);
  108. switch ($dbType) {
  109. case 'number':
  110. if ($tableColumn['data_precision'] == 20 && $tableColumn['data_scale'] == 0) {
  111. $precision = 20;
  112. $scale = 0;
  113. $type = 'bigint';
  114. } elseif ($tableColumn['data_precision'] == 5 && $tableColumn['data_scale'] == 0) {
  115. $type = 'smallint';
  116. $precision = 5;
  117. $scale = 0;
  118. } elseif ($tableColumn['data_precision'] == 1 && $tableColumn['data_scale'] == 0) {
  119. $precision = 1;
  120. $scale = 0;
  121. $type = 'boolean';
  122. } elseif ($tableColumn['data_scale'] > 0) {
  123. $precision = $tableColumn['data_precision'];
  124. $scale = $tableColumn['data_scale'];
  125. $type = 'decimal';
  126. }
  127. $length = null;
  128. break;
  129. case 'pls_integer':
  130. case 'binary_integer':
  131. $length = null;
  132. break;
  133. case 'varchar':
  134. case 'varchar2':
  135. case 'nvarchar2':
  136. $length = $tableColumn['char_length'];
  137. $fixed = false;
  138. break;
  139. case 'char':
  140. case 'nchar':
  141. $length = $tableColumn['char_length'];
  142. $fixed = true;
  143. break;
  144. case 'date':
  145. case 'timestamp':
  146. $length = null;
  147. break;
  148. case 'float':
  149. $precision = $tableColumn['data_precision'];
  150. $scale = $tableColumn['data_scale'];
  151. $length = null;
  152. break;
  153. case 'clob':
  154. case 'nclob':
  155. $length = null;
  156. break;
  157. case 'blob':
  158. case 'raw':
  159. case 'long raw':
  160. case 'bfile':
  161. $length = null;
  162. break;
  163. case 'rowid':
  164. case 'urowid':
  165. default:
  166. $length = null;
  167. }
  168. $options = array(
  169. 'notnull' => (bool) ($tableColumn['nullable'] === 'N'),
  170. 'fixed' => (bool) $fixed,
  171. 'unsigned' => (bool) $unsigned,
  172. 'default' => $tableColumn['data_default'],
  173. 'length' => $length,
  174. 'precision' => $precision,
  175. 'scale' => $scale,
  176. 'comment' => (isset($tableColumn['comments'])) ? $tableColumn['comments'] : null,
  177. 'platformDetails' => array(),
  178. );
  179. return new Column($tableColumn['column_name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  180. }
  181. /**
  182. * {@inheritdoc}
  183. */
  184. protected function _getPortableTableForeignKeysList($tableForeignKeys)
  185. {
  186. $list = array();
  187. foreach ($tableForeignKeys as $value) {
  188. $value = \array_change_key_case($value, CASE_LOWER);
  189. if (!isset($list[$value['constraint_name']])) {
  190. if ($value['delete_rule'] == "NO ACTION") {
  191. $value['delete_rule'] = null;
  192. }
  193. $list[$value['constraint_name']] = array(
  194. 'name' => $value['constraint_name'],
  195. 'local' => array(),
  196. 'foreign' => array(),
  197. 'foreignTable' => $value['references_table'],
  198. 'onDelete' => $value['delete_rule'],
  199. );
  200. }
  201. $list[$value['constraint_name']]['local'][$value['position']] = $value['local_column'];
  202. $list[$value['constraint_name']]['foreign'][$value['position']] = $value['foreign_column'];
  203. }
  204. $result = array();
  205. foreach($list as $constraint) {
  206. $result[] = new ForeignKeyConstraint(
  207. array_values($constraint['local']), $constraint['foreignTable'],
  208. array_values($constraint['foreign']), $constraint['name'],
  209. array('onDelete' => $constraint['onDelete'])
  210. );
  211. }
  212. return $result;
  213. }
  214. /**
  215. * {@inheritdoc}
  216. */
  217. protected function _getPortableSequenceDefinition($sequence)
  218. {
  219. $sequence = \array_change_key_case($sequence, CASE_LOWER);
  220. return new Sequence($sequence['sequence_name'], $sequence['increment_by'], $sequence['min_value']);
  221. }
  222. /**
  223. * {@inheritdoc}
  224. */
  225. protected function _getPortableFunctionDefinition($function)
  226. {
  227. $function = \array_change_key_case($function, CASE_LOWER);
  228. return $function['name'];
  229. }
  230. /**
  231. * {@inheritdoc}
  232. */
  233. protected function _getPortableDatabaseDefinition($database)
  234. {
  235. $database = \array_change_key_case($database, CASE_LOWER);
  236. return $database['username'];
  237. }
  238. /**
  239. * {@inheritdoc}
  240. */
  241. public function createDatabase($database = null)
  242. {
  243. if (is_null($database)) {
  244. $database = $this->_conn->getDatabase();
  245. }
  246. $params = $this->_conn->getParams();
  247. $username = $database;
  248. $password = $params['password'];
  249. $query = 'CREATE USER ' . $username . ' IDENTIFIED BY ' . $password;
  250. $this->_conn->executeUpdate($query);
  251. $query = 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO ' . $username;
  252. $this->_conn->executeUpdate($query);
  253. return true;
  254. }
  255. /**
  256. * @param string $table
  257. *
  258. * @return boolean
  259. */
  260. public function dropAutoincrement($table)
  261. {
  262. $sql = $this->_platform->getDropAutoincrementSql($table);
  263. foreach ($sql as $query) {
  264. $this->_conn->executeUpdate($query);
  265. }
  266. return true;
  267. }
  268. /**
  269. * {@inheritdoc}
  270. */
  271. public function dropTable($name)
  272. {
  273. $this->dropAutoincrement($name);
  274. parent::dropTable($name);
  275. }
  276. }