diff options
Diffstat (limited to 'phpBB/includes/acp/acp_database.php')
-rw-r--r-- | phpBB/includes/acp/acp_database.php | 202 |
1 files changed, 190 insertions, 12 deletions
diff --git a/phpBB/includes/acp/acp_database.php b/phpBB/includes/acp/acp_database.php index 3dafa54a4b..1572a80c89 100644 --- a/phpBB/includes/acp/acp_database.php +++ b/phpBB/includes/acp/acp_database.php @@ -34,8 +34,7 @@ class acp_database switch ($mode) { - // TODO: Firebird creates EVERYTHING in upper case, should this be changed? - // Oracle support must be written + // TODO: Check the cases of Oracle and Firebird ( they generate everything in uppercase ) // The queries are ugly++, they must get some love so that they follow the CS case 'backup': @@ -154,6 +153,11 @@ class acp_database $sql_data .= '# Table: ' . $table_name . "\n"; $sql_data .= "DROP TABLE IF EXISTS $table_name;\n"; break; + + case 'oracle': + $sql_data .= '# Table: ' . $table_name . "\n"; + $sql_data .= "DROP TABLE $table_name;\n\\\n"; + break; case 'postgres': case 'firebird': @@ -166,10 +170,6 @@ class acp_database $sql_data .= '# Table: ' . $table_name . "\n"; $sql_data .= "DROP TABLE $table_name;\nGO\n"; break; - - default: - trigger_error('KungFuDeathGrip'); - break; } $sql_data .= $this->get_table_structure($table_name); } @@ -749,8 +749,88 @@ class acp_database $db->sql_freeresult($result); break; - default: - trigger_error('KungFuDeathGrip'); + case 'oracle': + $ary_type = $ary_name = array(); + + // Grab all of the data from current table. + $sql = "SELECT * FROM {$table_name}"; + $result = $db->sql_query($sql); + + $i_num_fields = ocinumcols($result); + + for ($i = 0; $i < $i_num_fields; $i++) + { + $ary_type[] = ocicolumntype($result, $i); + $ary_name[] = "'" . ocicolumnname($result, $i) . "'"; + } + + while ($row = $db->sql_fetchrow($result)) + { + $schema_vals = $schema_fields = array(); + + // Build the SQL statement to recreate the data. + for ($i = 0; $i < $i_num_fields; $i++) + { + $str_val = $row[$ary_name[$i]]; + + if (preg_match('#char|text|bool#i', $ary_type[$i])) + { + $str_quote = "'"; + $str_empty = ''; + $str_val = addslashes($str_val); + } + else if (preg_match('#date|timestamp#i', $ary_type[$i])) + { + if (empty($str_val)) + { + $str_quote = ''; + } + else + { + $str_quote = "'"; + } + } + else + { + $str_quote = ''; + $str_empty = 'NULL'; + } + + if (empty($str_val) && $str_val !== '0') + { + $str_val = $str_empty; + } + + $schema_vals[] = $str_quote . $str_val . $str_quote; + $schema_fields[] = $ary_name[$i]; + } + + // Take the ordered fields and their associated data and build it + // into a valid sql statement to recreate that field in the data. + $sql_data .= "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES(' . implode(', ', $schema_vals) . ");\n"; + + if ($store == true) + { + $write($fp, $sql_data); + } + + if ($download == true) + { + if (!empty($oper)) + { + echo $oper($sql_data); + } + else + { + echo $sql_data; + } + } + + $sql_data = ''; + + } + $db->sql_freeresult($result); + break; } } } @@ -862,8 +942,18 @@ class acp_database $db->sql_freeresult($result); break; - default: - trigger_error('KungFuDeathGrip'); + case 'oracle': + $sql = 'SELECT TNAME as table_name FROM TAB'; + $result = $db->sql_query($sql); + while ($row = $db->sql_fetchrow($result)) + { + if (stripos($row['table_name'], $table_prefix) === 0) + { + $tables[] = $row['table_name']; + } + } + $db->sql_freeresult($result); + break; } foreach ($tables as $table) @@ -1491,8 +1581,96 @@ class acp_database $db->sql_freeresult($result); break; - default: - trigger_error('KungFuDeathGrip'); + case 'oracle': + $sql_data .= "\nCREATE TABLE $table_name (\n"; + + $sql = "SELECT COLUMN_NAME, DATA_TYPE, DATA_PRECISION, DATA_LENGTH, NULLABLE, DATA_DEFAULT from ALL_TAB_COLS where table_name = '{$table_name}'"; + $result = $db->sql_query($sql); + + $rows = array(); + while ($row = $db->sql_fetchrow($result)) + { + $line = ' "' . $row['column_name'] . '" ' . $row['data_type']; + + if ($row['data_type'] !== 'CLOB') + { + if ($row['data_type'] !== 'VARCHAR2') + { + $line .= '(' . $row['data_precision'] . ')'; + } + else + { + $line .= '(' . $row['data_length'] . ')'; + } + } + + if (!empty($row['data_default'])) + { + $line .= ' DEFAULT ' . $row['data_default']; + } + + if ($row['nullable'] == 'N') + { + $line .= ' NOT NULL'; + } + $rows[] = $line; + } + $db->sql_freeresult($result); + + $sql = "SELECT A.CONSTRAINT_NAME, A.COLUMN_NAME FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND B.CONSTRAINT_TYPE = 'P' AND A.TABLE_NAME = '{$table_name}'"; + $result = $db->sql_query($sql); + + while ($row = $db->sql_fetchrow($result)) + { + $rows[] = " CONSTRAINT {$row['constraint_name']} PRIMARY KEY ({$row['column_name']})"; + } + $db->sql_freeresult($result); + + $sql = "SELECT A.CONSTRAINT_NAME, A.COLUMN_NAME FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND B.CONSTRAINT_TYPE = 'U' AND A.TABLE_NAME = '{$table_name}'"; + $result = $db->sql_query($sql); + + while ($row = $db->sql_fetchrow($result)) + { + $rows[] = " CONSTRAINT {$row['constraint_name']} UNIQUE ({$row['column_name']})"; + } + $db->sql_freeresult($result); + + $sql_data .= implode(",\n", $rows); + $sql_data .= "\n)\n\\"; + + $sql = "SELECT A.REFERENCED_NAME FROM USER_DEPENDENCIES A, USER_TRIGGERS B WHERE A.REFERENCED_TYPE = 'SEQUENCE' AND A.NAME = B.TRIGGER_NAME AND B. TABLE_NAME = '{$table_name}'"; + $result = $db->sql_query($sql); + while ($row = $db->sql_fetchrow($result)) + { + $sql_data .= "\nCREATE SEQUENCE {$row['referenced_name']}\\\n"; + } + $db->sql_freeresult($result); + + $sql = "SELECT DESCRIPTION, WHEN_CLAUSE, TRIGGER_BODY FROM USER_TRIGGERS WHERE TABLE_NAME = '{$table_name}'"; + $result = $db->sql_query($sql); + while ($row = $db->sql_fetchrow($result)) + { + $sql_data .= "\nCREATE OR REPLACE TRIGGER {$row['description']}WHEN ({$row['when_clause']})\n{$row['trigger_body']}\\"; + } + $db->sql_freeresult($result); + + $sql = "SELECT A.INDEX_NAME, B.COLUMN_NAME FROM USER_INDEXES A, USER_IND_COLUMNS B WHERE A.UNIQUENESS = 'NONUNIQUE' AND A.INDEX_NAME = B.INDEX_NAME AND B.TABLE_NAME = '{$table_name}'"; + $result = $db->sql_query($sql); + + $index = array(); + + while ($row = $db->sql_fetchrow($result)) + { + $index[$row['index_name']][] = $row['column_name']; + } + + foreach ($index as $index_name => $column_names) + { + $sql_data .= "\nCREATE INDEX $index_name ON $table_name(" . implode(', ', $column_names) . ")\n\\"; + } + $db->sql_freeresult($result); + + break; } return $sql_data; |