48 require_once
'MDB2/Driver/Reverse/Common.php';
76 $result = $db->loadModule(
'Datatype', null,
true);
81 $query =
'SELECT column_name name, data_type "type", nullable, data_default "default"';
82 $query.=
', COALESCE(data_precision, data_length) "length", data_scale "scale"';
83 $query.=
' FROM user_tab_columns';
84 $query.=
' WHERE (table_name='.$db->quote($table,
'text').
' OR table_name='.$db->quote(strtoupper($table),
'text').
')';
85 $query.=
' AND (column_name='.$db->quote($field_name,
'text').
' OR column_name='.$db->quote(strtoupper($field_name),
'text').
')';
86 $query.=
' ORDER BY column_id';
94 'it was not specified an existing table column', __FUNCTION__);
97 $column = array_change_key_case($column, CASE_LOWER);
99 if ($db->options[
'field_case'] == CASE_LOWER) {
100 $column[
'name'] = strtolower($column[
'name']);
102 $column[
'name'] = strtoupper($column[
'name']);
105 $mapped_datatype = $db->datatype->mapNativeDatatype($column);
106 if (PEAR::IsError($mapped_datatype)) {
107 return $mapped_datatype;
109 list($types, $length, $unsigned, $fixed) = $mapped_datatype;
111 if (!empty($column[
'nullable']) && $column[
'nullable'] ==
'N') {
115 if (array_key_exists(
'default', $column)) {
116 $default = $column[
'default'];
117 if ($default ===
'NULL') {
120 if (is_null($default) && $notnull) {
125 $definition[0] = array(
'notnull' => $notnull,
'nativetype' => $column[
'type']);
126 if (!is_null($length)) {
127 $definition[0][
'length'] = $length;
129 if (!is_null($unsigned)) {
130 $definition[0][
'unsigned'] = $unsigned;
132 if (!is_null($fixed)) {
133 $definition[0][
'fixed'] = $fixed;
135 if ($default !==
false) {
136 $definition[0][
'default'] = $default;
138 foreach ($types as $key => $type) {
139 $definition[$key] = $definition[0];
140 if ($type ==
'clob' || $type ==
'blob') {
141 unset($definition[$key][
'default']);
143 $definition[$key][
'type'] = $type;
144 $definition[$key][
'mdb2type'] = $type;
146 if ($type ==
'integer') {
147 $query =
"SELECT DISTINCT name
150 AND UPPER(text) like '%ON ". strtoupper($db->escape($table,
'text')) .
"%'";
154 $row = array_change_key_case(
$row, CASE_LOWER);
155 $trquery =
'SELECT text
157 WHERE name=' . $db->quote(
$row[
'name'],
'text')
159 $triggersth = $db->query($trquery);
162 $triggerline = array_change_key_case($triggerline,CASE_LOWER);
163 $triggerstr .= $triggerline[
'text'].
' ';
166 if (preg_match(
'/.*\W(.+)\.nextval into :NEW\.'.$field_name.
' FROM dual/i', $triggerstr, $matches)) {
169 $definition[0][
'autoincrement'] = $matches[1];
196 $query =
"SELECT column_name,
199 FROM user_ind_columns
200 WHERE (table_name=".$db->quote($table,
'text').
' OR table_name='.$db->quote(strtoupper($table),
'text').
')
201 AND (index_name=%s OR index_name=%s)
202 AND index_name NOT IN (
203 SELECT constraint_name
205 WHERE (table_name = '.$db->quote($table,
'text').
' OR table_name='.$db->quote(strtoupper($table),
'text').
")
206 AND constraint_type in ('P','U')
208 ORDER BY column_position";
209 $index_name_mdb2 = $db->getIndexName($index_name);
211 $db->quote($index_name_mdb2,
'text'),
212 $db->quote(strtoupper($index_name_mdb2),
'text')
218 $index_name = $index_name_mdb2;
221 $db->quote($index_name,
'text'),
222 $db->quote(strtoupper($index_name),
'text')
224 $result = $db->query($sql);
229 $definition = array();
231 $row = array_change_key_case(
$row, CASE_LOWER);
232 $column_name =
$row[
'column_name'];
234 if ($db->options[
'field_case'] == CASE_LOWER) {
235 $column_name = strtolower($column_name);
237 $column_name = strtoupper($column_name);
240 $definition[
'fields'][$column_name] = array(
241 'position' => (
int)
$row[
'column_position'],
243 if (!empty($row[
'descend'])) {
244 $definition[
'fields'][$column_name][
'sorting'] =
245 ($row[
'descend'] ==
'ASC' ?
'ascending' :
'descending');
249 if (empty($definition[
'fields'])) {
251 'it was not specified an existing table index', __FUNCTION__);
274 $query =
'SELECT alc.constraint_name,
276 alc.search_condition,
277 alc.r_constraint_name,
278 alc.search_condition,
281 FROM all_constraints alc,
282 all_cons_columns cols
283 WHERE (alc.constraint_name=%s OR alc.constraint_name=%s)
284 AND alc.constraint_name = cols.constraint_name
285 AND alc.owner = '.$db->quote(strtoupper($db->dsn[
'username']),
'text');
286 if (!empty($table)) {
287 $query.=
' AND (alc.table_name='.$db->quote($table,
'text').
' OR alc.table_name='.$db->quote(strtoupper($table),
'text').
')';
289 if (strtolower($constraint_name) !=
'primary') {
290 $constraint_name_mdb2 = $db->getIndexName($constraint_name);
292 $db->quote($constraint_name_mdb2,
'text'),
293 $db->quote(strtoupper($constraint_name_mdb2),
'text')
299 $constraint_name = $constraint_name_mdb2;
303 $db->quote($constraint_name,
'text'),
304 $db->quote(strtoupper($constraint_name),
'text')
310 $definition = array();
312 $row = array_change_key_case(
$row, CASE_LOWER);
313 $column_name =
$row[
'column_name'];
315 if ($db->options[
'field_case'] == CASE_LOWER) {
316 $column_name = strtolower($column_name);
318 $column_name = strtoupper($column_name);
321 $definition[
'fields'][$column_name] = array(
322 'position' => (
int)
$row[
'position']
328 if (empty($definition)) {
330 $constraint_name .
' is not an existing table constraint', __FUNCTION__);
332 if ($lastrow[
'constraint_type'] ===
'P') {
333 $definition[
'primary'] =
true;
334 } elseif ($lastrow[
'constraint_type'] ===
'U') {
335 $definition[
'unique'] =
true;
336 } elseif ($lastrow[
'constraint_type'] ===
'R') {
337 $definition[
'foreign'] = $lastrow[
'r_constraint_name'];
338 } elseif ($lastrow[
'constraint_type'] ===
'C') {
339 $definition[
'check'] =
true;
341 $enumregex =
'/'.$lastrow[
'column_name'].
' in \((.+?)\)/i';
342 if (preg_match($enumregex, $lastrow[
'search_condition'], $rangestr)) {
343 $definition[
'fields'][$column_name] = array();
344 $allowed = explode(
',', $rangestr[1]);
345 foreach ($allowed as $val) {
347 $val = preg_replace(
'/^\'/',
'', $val);
348 $val = preg_replace(
'/\'$/',
'', $val);
349 array_push($definition[
'fields'][$column_name], $val);
373 $sequence_name = $db->getSequenceName($sequence);
374 $query =
'SELECT last_number FROM user_sequences';
375 $query.=
' WHERE sequence_name='.$db->quote($sequence_name,
'text');
376 $query.=
' OR sequence_name='.$db->quote(strtoupper($sequence_name),
'text');
377 $start = $db->queryOne(
$query,
'integer');
381 $definition = array();
383 $definition = array(
'start' => $start);
410 $query =
'SELECT trigger_name,
414 triggering_event trigger_event,
415 description trigger_comment,
419 WHERE trigger_name = \''. strtoupper($trigger).
'\'';
421 'trigger_name' =>
'text',
422 'table_name' =>
'text',
423 'trigger_body' =>
'text',
424 'trigger_type' =>
'text',
425 'trigger_event' =>
'text',
426 'trigger_comment' =>
'text',
427 'trigger_enabled' =>
'boolean',
428 'when_clause' =>
'text',
434 if (!empty(
$result[
'trigger_type'])) {
436 $result[
'trigger_type'] = preg_replace(
'/(\S+).*/',
'\\1',
$result[
'trigger_type']);
476 if (!is_resource($resource)) {
478 'Could not generate result resource', __FUNCTION__);
482 if ($db->options[
'field_case'] == CASE_LOWER) {
483 $case_func =
'strtolower';
485 $case_func =
'strtoupper';
488 $case_func =
'strval';
491 $count = @OCINumCols($resource);
495 $res[
'num_fields'] = $count;
498 $db->loadModule(
'Datatype', null,
true);
499 for ($i = 0; $i < $count; $i++) {
502 'name' => $case_func(@OCIColumnName($resource, $i+1)),
503 'type' => @OCIColumnType($resource, $i+1),
504 'length' => @OCIColumnSize($resource, $i+1),
508 $res[$i][
'mdb2type'] = $db->datatype->mapNativeDatatype(
$res[$i]);
510 $res[
'order'][
$res[$i][
'name']] = $i;
513 $res[
'ordertable'][
$res[$i][
'table']][
$res[$i][
'name']] = $i;