2007-07-17 07:23:17 +00:00
|
|
|
<?php
|
2007-07-26 14:52:02 +00:00
|
|
|
page_header(lang('Database schema') . ": " . htmlspecialchars($_GET["db"]));
|
2007-07-17 07:23:17 +00:00
|
|
|
|
2007-07-26 14:52:02 +00:00
|
|
|
$table_pos = array();
|
|
|
|
$table_pos_js = array();
|
|
|
|
preg_match_all('~([^:]+):([-0-9.]+)x([-0-9.]+)(_|$)~', $_COOKIE["schema"], $matches, PREG_SET_ORDER); //! ':' in table name
|
|
|
|
foreach ($matches as $i => $match) {
|
|
|
|
$table_pos[$match[1]] = "$match[2]em; left: $match[3]";
|
|
|
|
$table_pos_js[] = "\n\t'" . addcslashes($match[1], "\r\n'\\") . "': [ $match[2], $match[3] ]";
|
|
|
|
}
|
|
|
|
|
|
|
|
$top = 0;
|
|
|
|
$left = -14;
|
2007-07-17 07:23:17 +00:00
|
|
|
$schema = array();
|
2007-07-26 14:52:02 +00:00
|
|
|
$referenced = array();
|
2007-07-17 07:23:17 +00:00
|
|
|
$result = $mysql->query("SHOW TABLE STATUS");
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
|
|
|
if (!isset($row["Engine"])) { // view
|
|
|
|
continue;
|
|
|
|
}
|
2007-07-26 14:52:02 +00:00
|
|
|
$pos = 0;
|
|
|
|
$schema[$row["Name"]]["fields"] = array();
|
|
|
|
foreach (fields($row["Name"]) as $name => $field) {
|
|
|
|
$pos += 1.25;
|
|
|
|
$field["pos"] = $pos;
|
|
|
|
$schema[$row["Name"]]["fields"][$name] = $field;
|
|
|
|
}
|
|
|
|
$schema[$row["Name"]]["pos"] = ($table_pos[$row["Name"]] ? $table_pos[$row["Name"]] : $top);
|
2007-07-17 07:23:17 +00:00
|
|
|
if ($row["Engine"] == "InnoDB") {
|
|
|
|
foreach (foreign_keys($row["Name"]) as $val) {
|
|
|
|
if (!$val["db"]) {
|
2007-07-26 14:52:02 +00:00
|
|
|
$schema[$row["Name"]]["references"][$val["table"]][$left] = array_combine($val["source"], $val["target"]);
|
|
|
|
$referenced[$val["table"]][$left] = $val["target"];
|
|
|
|
$left -= 2;
|
2007-07-17 07:23:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-26 14:52:02 +00:00
|
|
|
$top = max($top, $schema[$row["Name"]]["pos"] + 2.5 + $pos);
|
2007-07-17 07:23:17 +00:00
|
|
|
}
|
|
|
|
$result->free();
|
2007-07-26 14:52:02 +00:00
|
|
|
|
2007-07-17 07:23:17 +00:00
|
|
|
?>
|
2007-07-26 14:52:02 +00:00
|
|
|
<script type="text/javascript">
|
|
|
|
var that, x, y, em;
|
|
|
|
var table_pos = {<?php echo implode(",", $table_pos_js) . "\n"; ?>};
|
|
|
|
|
|
|
|
function mousedown(el, event) {
|
|
|
|
that = el;
|
|
|
|
em = document.getElementById('breadcrumb').offsetHeight / 1.25;
|
|
|
|
x = event.clientX - el.offsetLeft;
|
|
|
|
y = event.clientY - el.offsetTop;
|
|
|
|
}
|
|
|
|
function mousemove(event) {
|
|
|
|
if (that !== undefined) {
|
|
|
|
that.style.left = (event.clientX - x) / em + 'em';
|
|
|
|
that.style.top = (event.clientY - y) / em + 'em';
|
|
|
|
//! drag lines
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function mouseup(event) {
|
|
|
|
if (that !== undefined) {
|
|
|
|
table_pos[that.firstChild.firstChild.firstChild.data] = [ (event.clientY - y) / em, (event.clientX - x) / em ];
|
|
|
|
that = undefined;
|
|
|
|
var date = new Date();
|
|
|
|
date.setMonth(date.getMonth() + 1);
|
|
|
|
var s = '';
|
|
|
|
for (var key in table_pos) {
|
|
|
|
s += '_' + key + ':' + Math.round(table_pos[key][0] * 10000) / 10000 + 'x' + Math.round(table_pos[key][1] * 10000) / 10000;
|
|
|
|
}
|
|
|
|
document.cookie = 'schema=' + encodeURIComponent(s.substr(1)) + '; expires=' + date + '; path=' + location.pathname + location.search;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<div id="schema" style="height: <?php echo $top; ?>em;" onmousemove="mousemove(event);" onmouseup="mouseup(event);">
|
2007-07-17 07:23:17 +00:00
|
|
|
<?php
|
2007-07-17 10:47:58 +00:00
|
|
|
foreach ($schema as $name => $table) {
|
2007-07-26 14:52:02 +00:00
|
|
|
echo "<div class='table' style='top: $table[pos]em;' onmousedown='mousedown(this, event);'>";
|
2007-07-17 07:23:17 +00:00
|
|
|
echo '<a href="' . htmlspecialchars($SELF) . 'table=' . urlencode($name) . '"><strong>' . htmlspecialchars($name) . "</strong></a><br />\n";
|
2007-07-26 14:52:02 +00:00
|
|
|
foreach ($table["fields"] as $field) {
|
2007-07-17 07:23:17 +00:00
|
|
|
$val = htmlspecialchars($field["field"]);
|
|
|
|
if (preg_match('~char|text~', $field["type"])) {
|
|
|
|
$val = "<span class='char'>$val</span>";
|
|
|
|
} elseif (preg_match('~date|time|year~', $field["type"])) {
|
|
|
|
$val = "<span class='date'>$val</span>";
|
|
|
|
} elseif (preg_match('~binary|blob~', $field["type"])) {
|
|
|
|
$val = "<span class='binary'>$val</span>";
|
|
|
|
} elseif (preg_match('~enum|set~', $field["type"])) {
|
|
|
|
$val = "<span class='enum'>$val</span>";
|
|
|
|
}
|
|
|
|
echo ($field["primary"] ? "<em>$val</em>" : $val) . "<br />\n";
|
2007-07-26 14:52:02 +00:00
|
|
|
}
|
|
|
|
foreach ((array) $table["references"] as $target_name => $refs) {
|
|
|
|
foreach ($refs as $left => $columns) {
|
|
|
|
foreach ($columns as $source => $target) {
|
|
|
|
echo "<div class='references' style='left: " . ($left+1) . "px; top: " . $table["fields"][$source]["pos"] . "em; padding-top: .5em;'><div style='border-top: 1px solid Black; width: " . (-$left-2) . "px;'></div></div>\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ((array) $referenced[$name] as $left => $columns) {
|
|
|
|
foreach ($columns as $target) {
|
|
|
|
echo "<div class='references' style='left: " . ($left+1) . "px; top: " . $table["fields"][$target]["pos"] . "em; width: " . (-$left-2) . "px; height: 1.25em; background: url(arrow.gif) no-repeat right center;'><div style='height: .5em; border-bottom: 1px solid Black; width: " . (-$left-14) . "px;'></div></div>\n";
|
|
|
|
}
|
2007-07-17 07:23:17 +00:00
|
|
|
}
|
|
|
|
echo "</div>\n";
|
|
|
|
}
|
|
|
|
foreach ($schema as $name => $table) {
|
2007-07-17 12:26:57 +00:00
|
|
|
foreach ((array) $table["references"] as $target_name => $refs) {
|
2007-07-26 14:52:02 +00:00
|
|
|
foreach ($refs as $left => $ref) {
|
2007-07-17 10:47:58 +00:00
|
|
|
$min_pos = $top;
|
2007-07-26 14:52:02 +00:00
|
|
|
$max_pos = -10;
|
2007-07-17 10:47:58 +00:00
|
|
|
foreach ($ref as $source => $target) {
|
2007-07-26 14:52:02 +00:00
|
|
|
$pos1 = $table["pos"] + $table["fields"][$source]["pos"];
|
|
|
|
$pos2 = $schema[$target_name]["pos"] + $schema[$target_name]["fields"][$target]["pos"];
|
2007-07-17 10:47:58 +00:00
|
|
|
$min_pos = min($min_pos, $pos1, $pos2);
|
|
|
|
$max_pos = max($max_pos, $pos1, $pos2);
|
|
|
|
}
|
2007-07-26 14:52:02 +00:00
|
|
|
echo "<div class='references' style='left: $left" . "px; top: $min_pos" . "em; padding: .5em 0;' /><div style='border-right: 1px solid Black; height: " . ($max_pos - $min_pos) . "em;'></div></div>\n";
|
2007-07-17 10:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-17 07:23:17 +00:00
|
|
|
}
|
|
|
|
?>
|
|
|
|
</div>
|