PageRenderTime 74ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/plugins/doctrine/lib/Doctrine/Import/Oracle.php

https://github.com/cawago/ci_campusync_auth
PHP | 255 lines | 123 code | 32 blank | 100 comment | 1 complexity | 9a07d31df217a8823f9b14f03437e80d MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: Oracle.php 6151 2009-07-21 21:50:23Z jwage $
  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. /**
  22. * @package Doctrine
  23. * @subpackage Import
  24. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  25. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  26. * @version $Revision: 6151 $
  27. * @link www.phpdoctrine.org
  28. * @since 1.0
  29. */
  30. class Doctrine_Import_Oracle extends Doctrine_Import
  31. {
  32. /**
  33. * lists all databases
  34. *
  35. * @return array
  36. */
  37. public function listDatabases()
  38. {
  39. if ( ! $this->conn->getAttribute(Doctrine::ATTR_EMULATE_DATABASE)) {
  40. throw new Doctrine_Import_Exception('database listing is only supported if the "emulate_database" option is enabled');
  41. }
  42. $query = 'SELECT username FROM sys.user_users';
  43. $result2 = $this->conn->standaloneQuery($query);
  44. $result = $result2->fetchColumn();
  45. return $result;
  46. }
  47. /**
  48. * lists all availible database functions
  49. *
  50. * @return array
  51. */
  52. public function listFunctions()
  53. {
  54. $query = "SELECT name FROM sys.user_source WHERE line = 1 AND type = 'FUNCTION'";
  55. return $this->conn->fetchColumn($query);
  56. }
  57. /**
  58. * lists all database triggers
  59. *
  60. * @param string|null $database
  61. * @return array
  62. */
  63. public function listTriggers($database = null)
  64. {
  65. $query = "SELECT trigger_name FROM sys.user_triggers";
  66. return $this->conn->fetchColumn($query);
  67. }
  68. /**
  69. * lists all database sequences
  70. *
  71. * @param string|null $database
  72. * @return array
  73. */
  74. public function listSequences($database = null)
  75. {
  76. $query = "SELECT sequence_name FROM sys.user_sequences";
  77. $tableNames = $this->conn->fetchColumn($query);
  78. return array_map(array($this->conn->formatter, 'fixSequenceName'), $tableNames);
  79. }
  80. /**
  81. * lists table constraints
  82. *
  83. * @param string $table database table name
  84. * @return array
  85. */
  86. public function listTableConstraints($table)
  87. {
  88. $table = $this->conn->quote($table, 'text');
  89. $query = 'SELECT index_name name FROM user_constraints'
  90. . ' WHERE table_name = ' . $table . ' OR table_name = ' . strtoupper($table);
  91. $constraints = $this->conn->fetchColumn($query);
  92. return array_map(array($this->conn->formatter, 'fixIndexName'), $constraints);
  93. }
  94. /**
  95. * lists table constraints
  96. *
  97. * @param string $table database table name
  98. * @return array
  99. */
  100. public function listTableColumns($table)
  101. {
  102. $sql = <<<QEND
  103. SELECT tc.column_name, data_type,
  104. CASE WHEN data_type = 'NUMBER' THEN data_precision ELSE data_length END AS data_length,
  105. nullable, data_default, data_scale, data_precision, pk.primary
  106. FROM all_tab_columns tc
  107. LEFT JOIN (
  108. select 'primary' primary, cc.table_name, cc.column_name from all_constraints cons
  109. join all_cons_columns cc on cons.constraint_name = cc.constraint_name
  110. where cons.constraint_type = 'P'
  111. ) pk ON pk.column_name = tc.column_name and pk.table_name = tc.table_name
  112. WHERE tc.table_name = :tableName ORDER BY column_id
  113. QEND;
  114. $result = $this->conn->fetchAssoc($sql, array(':tableName' => $table));
  115. $descr = array();
  116. foreach($result as $val) {
  117. $val = array_change_key_case($val, CASE_LOWER);
  118. $decl = $this->conn->dataDict->getPortableDeclaration($val);
  119. $descr[$val['column_name']] = array(
  120. 'name' => $val['column_name'],
  121. 'notnull' => (bool) ($val['nullable'] === 'N'),
  122. 'ntype' => $val['data_type'],
  123. 'type' => $decl['type'][0],
  124. 'alltypes' => $decl['type'],
  125. 'fixed' => $decl['fixed'],
  126. 'unsigned' => $decl['unsigned'],
  127. 'default' => $val['data_default'],
  128. 'length' => $val['data_length'],
  129. 'primary' => $val['primary'] ? true:false,
  130. 'scale' => isset($val['scale']) ? $val['scale']:null,
  131. );
  132. }
  133. return $descr;
  134. }
  135. /**
  136. * lists table constraints
  137. *
  138. * @param string $table database table name
  139. * @return array
  140. */
  141. public function listTableIndexes($table)
  142. {
  143. $table = $this->conn->quote($table, 'text');
  144. $query = 'SELECT index_name name FROM user_indexes'
  145. . ' WHERE table_name = ' . $table . ' OR table_name = ' . strtoupper($table)
  146. . ' AND generated = ' . $this->conn->quote('N', 'text');
  147. $indexes = $this->conn->fetchColumn($query);
  148. return array_map(array($this->conn->formatter, 'fixIndexName'), $indexes);
  149. }
  150. /**
  151. * list table relations
  152. */
  153. public function listTableRelations($table)
  154. {
  155. $relations = array();
  156. $sql = 'SELECT '
  157. . 'rcc.table_name AS referenced_table_name, '
  158. . 'lcc.column_name AS local_column_name, '
  159. . 'rcc.column_name AS referenced_column_name '
  160. . 'FROM user_constraints ac '
  161. . 'JOIN user_cons_columns rcc ON ac.r_constraint_name = rcc.constraint_name '
  162. . 'JOIN user_cons_columns lcc ON ac.constraint_name = lcc.constraint_name '
  163. . "WHERE ac.constraint_type = 'R' AND ac.table_name = :tableName";
  164. $results = $this->conn->fetchAssoc($sql, array(':tableName' => $table));
  165. foreach ($results as $result) {
  166. $result = array_change_key_case($result, CASE_LOWER);
  167. $relations[] = array('table' => $result['referenced_table_name'],
  168. 'local' => $result['local_column_name'],
  169. 'foreign' => $result['referenced_column_name']);
  170. }
  171. return $relations;
  172. }
  173. /**
  174. * lists tables
  175. *
  176. * @param string|null $database
  177. * @return array
  178. */
  179. public function listTables($database = null)
  180. {
  181. $query = "SELECT * FROM user_objects WHERE object_type = 'TABLE'";
  182. return $this->conn->fetchColumn($query);
  183. }
  184. /**
  185. * lists table triggers
  186. *
  187. * @param string $table database table name
  188. * @return array
  189. */
  190. public function listTableTriggers($table)
  191. {
  192. }
  193. /**
  194. * lists table views
  195. *
  196. * @param string $table database table name
  197. * @return array
  198. */
  199. public function listTableViews($table)
  200. {
  201. }
  202. /**
  203. * lists database users
  204. *
  205. * @return array
  206. */
  207. public function listUsers()
  208. {
  209. $query = 'SELECT username FROM sys.all_users';
  210. return $this->conn->fetchColumn($query);
  211. }
  212. /**
  213. * lists database views
  214. *
  215. * @param string|null $database
  216. * @return array
  217. */
  218. public function listViews($database = null)
  219. {
  220. $query = 'SELECT view_name FROM sys.user_views';
  221. return $this->conn->fetchColumn($query);
  222. }
  223. }