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