OracleSchemaManager.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.phpdoctrine.org>.
  20. */
  21. namespace Doctrine\DBAL\Schema;
  22. /**
  23. * Oracle Schema Manager
  24. *
  25. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  26. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  27. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  28. * @author Benjamin Eberlei <kontakt@beberlei.de>
  29. * @version $Revision$
  30. * @since 2.0
  31. */
  32. class OracleSchemaManager extends AbstractSchemaManager
  33. {
  34. protected function _getPortableViewDefinition($view)
  35. {
  36. $view = \array_change_key_case($view, CASE_LOWER);
  37. return new View($view['view_name'], $view['text']);
  38. }
  39. protected function _getPortableUserDefinition($user)
  40. {
  41. $user = \array_change_key_case($user, CASE_LOWER);
  42. return array(
  43. 'user' => $user['username'],
  44. );
  45. }
  46. protected function _getPortableTableDefinition($table)
  47. {
  48. $table = \array_change_key_case($table, CASE_LOWER);
  49. return $table['table_name'];
  50. }
  51. /**
  52. * @license New BSD License
  53. * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
  54. * @param array $tableIndexes
  55. * @param string $tableName
  56. * @return array
  57. */
  58. protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
  59. {
  60. $indexBuffer = array();
  61. foreach ( $tableIndexes as $tableIndex ) {
  62. $tableIndex = \array_change_key_case($tableIndex, CASE_LOWER);
  63. $keyName = strtolower($tableIndex['name']);
  64. if ( strtolower($tableIndex['is_primary']) == "p" ) {
  65. $keyName = 'primary';
  66. $buffer['primary'] = true;
  67. $buffer['non_unique'] = false;
  68. } else {
  69. $buffer['primary'] = false;
  70. $buffer['non_unique'] = ( $tableIndex['is_unique'] == 0 ) ? true : false;
  71. }
  72. $buffer['key_name'] = $keyName;
  73. $buffer['column_name'] = $tableIndex['column_name'];
  74. $indexBuffer[] = $buffer;
  75. }
  76. return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
  77. }
  78. protected function _getPortableTableColumnDefinition($tableColumn)
  79. {
  80. $tableColumn = \array_change_key_case($tableColumn, CASE_LOWER);
  81. $dbType = strtolower($tableColumn['data_type']);
  82. if(strpos($dbType, "timestamp(") === 0) {
  83. if (strpos($dbType, "WITH TIME ZONE")) {
  84. $dbType = "timestamptz";
  85. } else {
  86. $dbType = "timestamp";
  87. }
  88. }
  89. $type = array();
  90. $length = $unsigned = $fixed = null;
  91. if ( ! empty($tableColumn['data_length'])) {
  92. $length = $tableColumn['data_length'];
  93. }
  94. if ( ! isset($tableColumn['column_name'])) {
  95. $tableColumn['column_name'] = '';
  96. }
  97. if (stripos($tableColumn['data_default'], 'NULL') !== null) {
  98. $tableColumn['data_default'] = null;
  99. }
  100. $precision = null;
  101. $scale = null;
  102. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  103. $type = $this->extractDoctrineTypeFromComment($tableColumn['comments'], $type);
  104. $tableColumn['comments'] = $this->removeDoctrineTypeFromComment($tableColumn['comments'], $type);
  105. switch ($dbType) {
  106. case 'number':
  107. if ($tableColumn['data_precision'] == 20 && $tableColumn['data_scale'] == 0) {
  108. $precision = 20;
  109. $scale = 0;
  110. $type = 'bigint';
  111. } elseif ($tableColumn['data_precision'] == 5 && $tableColumn['data_scale'] == 0) {
  112. $type = 'smallint';
  113. $precision = 5;
  114. $scale = 0;
  115. } elseif ($tableColumn['data_precision'] == 1 && $tableColumn['data_scale'] == 0) {
  116. $precision = 1;
  117. $scale = 0;
  118. $type = 'boolean';
  119. } elseif ($tableColumn['data_scale'] > 0) {
  120. $precision = $tableColumn['data_precision'];
  121. $scale = $tableColumn['data_scale'];
  122. $type = 'decimal';
  123. }
  124. $length = null;
  125. break;
  126. case 'pls_integer':
  127. case 'binary_integer':
  128. $length = null;
  129. break;
  130. case 'varchar':
  131. case 'varchar2':
  132. case 'nvarchar2':
  133. $length = $tableColumn['char_length'];
  134. $fixed = false;
  135. break;
  136. case 'char':
  137. case 'nchar':
  138. $length = $tableColumn['char_length'];
  139. $fixed = true;
  140. break;
  141. case 'date':
  142. case 'timestamp':
  143. $length = null;
  144. break;
  145. case 'float':
  146. $precision = $tableColumn['data_precision'];
  147. $scale = $tableColumn['data_scale'];
  148. $length = null;
  149. break;
  150. case 'clob':
  151. case 'nclob':
  152. $length = null;
  153. break;
  154. case 'blob':
  155. case 'raw':
  156. case 'long raw':
  157. case 'bfile':
  158. $length = null;
  159. break;
  160. case 'rowid':
  161. case 'urowid':
  162. default:
  163. $length = null;
  164. }
  165. $options = array(
  166. 'notnull' => (bool) ($tableColumn['nullable'] === 'N'),
  167. 'fixed' => (bool) $fixed,
  168. 'unsigned' => (bool) $unsigned,
  169. 'default' => $tableColumn['data_default'],
  170. 'length' => $length,
  171. 'precision' => $precision,
  172. 'scale' => $scale,
  173. 'comment' => (isset($tableColumn['comments'])) ? $tableColumn['comments'] : null,
  174. 'platformDetails' => array(),
  175. );
  176. return new Column($tableColumn['column_name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  177. }
  178. protected function _getPortableTableForeignKeysList($tableForeignKeys)
  179. {
  180. $list = array();
  181. foreach ($tableForeignKeys as $key => $value) {
  182. $value = \array_change_key_case($value, CASE_LOWER);
  183. if (!isset($list[$value['constraint_name']])) {
  184. if ($value['delete_rule'] == "NO ACTION") {
  185. $value['delete_rule'] = null;
  186. }
  187. $list[$value['constraint_name']] = array(
  188. 'name' => $value['constraint_name'],
  189. 'local' => array(),
  190. 'foreign' => array(),
  191. 'foreignTable' => $value['references_table'],
  192. 'onDelete' => $value['delete_rule'],
  193. );
  194. }
  195. $list[$value['constraint_name']]['local'][$value['position']] = $value['local_column'];
  196. $list[$value['constraint_name']]['foreign'][$value['position']] = $value['foreign_column'];
  197. }
  198. $result = array();
  199. foreach($list AS $constraint) {
  200. $result[] = new ForeignKeyConstraint(
  201. array_values($constraint['local']), $constraint['foreignTable'],
  202. array_values($constraint['foreign']), $constraint['name'],
  203. array('onDelete' => $constraint['onDelete'])
  204. );
  205. }
  206. return $result;
  207. }
  208. protected function _getPortableSequenceDefinition($sequence)
  209. {
  210. $sequence = \array_change_key_case($sequence, CASE_LOWER);
  211. return new Sequence($sequence['sequence_name'], $sequence['increment_by'], $sequence['min_value']);
  212. }
  213. protected function _getPortableFunctionDefinition($function)
  214. {
  215. $function = \array_change_key_case($function, CASE_LOWER);
  216. return $function['name'];
  217. }
  218. protected function _getPortableDatabaseDefinition($database)
  219. {
  220. $database = \array_change_key_case($database, CASE_LOWER);
  221. return $database['username'];
  222. }
  223. public function createDatabase($database = null)
  224. {
  225. if (is_null($database)) {
  226. $database = $this->_conn->getDatabase();
  227. }
  228. $params = $this->_conn->getParams();
  229. $username = $database;
  230. $password = $params['password'];
  231. $query = 'CREATE USER ' . $username . ' IDENTIFIED BY ' . $password;
  232. $result = $this->_conn->executeUpdate($query);
  233. $query = 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO ' . $username;
  234. $result = $this->_conn->executeUpdate($query);
  235. return true;
  236. }
  237. public function dropAutoincrement($table)
  238. {
  239. $sql = $this->_platform->getDropAutoincrementSql($table);
  240. foreach ($sql as $query) {
  241. $this->_conn->executeUpdate($query);
  242. }
  243. return true;
  244. }
  245. public function dropTable($name)
  246. {
  247. $this->dropAutoincrement($name);
  248. return parent::dropTable($name);
  249. }
  250. }