schema documentation, ";
$body .= "but you don't have the 'schemadoc' priv.";
return;
}
sub magic_links
{
my $des = shift;
$$des =~ s!\[dbtable\[(\w+?)\]\]!$1!g;
}
if ($FORM{'mode'} eq "")
{
my %table;
$sth = $dbh->prepare("SELECT tablename, public_browsable, des FROM schematables");
$sth->execute;
while (my ($name, $public, $des) = $sth->fetchrow_array) {
$table{$name} = { 'public' => $public, 'des' => $des };
}
$body .= "
>Table | Description |
\n";
$sth = $dbh->prepare("SHOW TABLES");
$sth->execute;
while (my ($table) = $sth->fetchrow_array) {
my $des = $table{$table}->{'des'} || "no description, yet";
magic_links(\$des);
$body .= "$table";
if ($table{$table}->{'public'}) {
$body .= " (data)";
}
$body .= " | $des |
\n";
}
$body .= "
\n";
return;
}
if ($FORM{'mode'} eq "viewtable")
{
my $table = $FORM{'table'};
if ($table !~ /^\w+$/) { $body = "Invalid table name!\n"; return; }
my $qtable = $dbh->quote($table);
$sth = $dbh->prepare("SELECT des, public_browsable FROM schematables WHERE tablename=$qtable");
$sth->execute;
my ($tabledes, $browsable) = $sth->fetchrow_array;
$body .= "<< Tables";
if ($browsable) {
$body .= " | View Data";
}
if ($can_doc) {
$body .= " | Edit Documentation";
}
$body .= "$table table.";
}
$body .= " p?>\n";
my %coldes;
$sth = $dbh->prepare("SELECT colname, des FROM schemacols WHERE tablename=$qtable");
$sth->execute;
while (my ($col, $des) = $sth->fetchrow_array) { $coldes{$col} = $des; }
$sth = $dbh->prepare("DESCRIBE $table");
$sth->execute;
$body .= "
>";
$body .= "Key? | ";
$body .= "Column | ";
$body .= "Type | ";
$body .= "Null | ";
$body .= "Default | ";
$body .= "Description | ";
$body .= "
\n";
while (my $row = $sth->fetchrow_hashref)
{
my $name = $row->{'Field'};
my $type = $row->{'Type'};
my $key = $row->{'Key'};
my $null = $row->{'Null'};
my $def = $row->{'Default'};
my $des = BML::eall($coldes{$name});
magic_links(\$des);
$type =~ s/int\(\d+\)/int/g;
$body .= "";
$body .= "$key | ";
$body .= "$name | ";
$body .= "$type | ";
$body .= "$null | ";
$body .= "$def | ";
$body .= "$des | ";
$body .= "
\n";
}
$body .= "
\n";
return;
}
if ($FORM{'mode'} eq "viewdata") {
my $table = $FORM{'table'};
if ($table !~ /^\w+$/) { $body = "Invalid table name!\n"; return; }
my $MAX_ROWS = 100;
$body .= "<< Tables
<< Table: $table$table table. If the table has more than $MAX_ROWS records, only the top $MAX_ROWS are shown. p?>\n";
$sth = $dbh->prepare("SELECT tablename, public_browsable, des FROM schematables WHERE tablename='$table'");
$sth->execute;
my ($tablename, $public, $des) = $sth->fetchrow_array;
unless ($public) { $body .= "This table's data is not public.\n"; return; }
$sth = $dbh->prepare("SELECT * FROM $table LIMIT $MAX_ROWS");
$sth->execute;
$body .= "
>";
foreach my $col (@{$sth->{'NAME'}}) {
$body .= "$col | \n";
}
$body .= "
\n";
while (my $row = $sth->fetchrow_arrayref) {
$body .= "\n";
foreach my $val (@$row) {
$body .= "$val | \n";
}
$body .= "
\n";
}
$body .= "
\n";
return;
}
# show form to enter documentation
if ($FORM{'mode'} eq "doc")
{
unless ($can_doc) { $body .= "You don't have permissions to document the schema."; return; }
my $table = $FORM{'table'};
if ($table !~ /^\w+$/) { $body = "Invalid table name!\n"; return; }
$body .= "<< Tables";
my $qtable = $dbh->quote($table);
my $sth;
$sth = $dbh->prepare("SELECT des FROM schematables WHERE tablename=$qtable");
$sth->execute;
my ($tabledes) = $sth->fetchrow_array;
my %coldes;
$sth = $dbh->prepare("SELECT colname, des FROM schemacols WHERE tablename=$qtable");
$sth->execute;
while (my ($col, $des) = $sth->fetchrow_array) { $coldes{$col} = $des; }
$body .= "";
return;
}
# save documentation
if ($FORM{'mode'} eq "docsave")
{
unless ($can_doc) { $body .= "You don't have permissions to document the schema."; return; }
my $table = $FORM{'table'};
if ($table !~ /^\w+$/) { $body = "Invalid table name!\n"; return; }
$body .= "<< Tables";
my $qtable = $dbh->quote($table);
my $sth;
$sth = $dbh->prepare("SELECT tablename, des FROM schematables WHERE tablename=$qtable");
$sth->execute;
my ($tablename, $tabledes) = $sth->fetchrow_array;
$FORM{'table-des'} =~ s/\r//;
my $qdes = $dbh->quote($FORM{'table-des'});
if ($tablename) {
# row exists, update.
$dbh->do("UPDATE schematables SET des=$qdes WHERE tablename=$qtable");
} else {
# no row exists, so insert
$dbh->do("INSERT INTO schematables (tablename, public_browsable, des) VALUES ($qtable, '0', $qdes)");
}
if ($dbh->err) { $body .= $dbh->errstr; return; }
my %olddes;
my %newdes;
### load old descriptions
$sth = $dbh->prepare("SELECT colname, des FROM schemacols WHERE tablename=$qtable");
$sth->execute;
while (my ($col, $des) = $sth->fetchrow_array) { $olddes{$col} = $des; }
### check new descriptions (only for valid columns)
$sth = $dbh->prepare("DESCRIBE $table");
$sth->execute;
if ($dbh->err) { $body .= $dbh->errstr; return; }
while (my $row = $sth->fetchrow_hashref)
{
my $name = $row->{'Field'};
my $type = $row->{'Type'};
$FORM{"col-$name"} =~ s/\r//;
if ($FORM{"col-$name"} ne $olddes{$name}) {
$newdes{$name} = $FORM{"col-$name"};
}
}
if (%newdes) {
my $sql = "REPLACE INTO schemacols (tablename, colname, des) VALUES ";
foreach my $col (keys %newdes) {
my $qcol = $dbh->quote($col);
my $qdes = $dbh->quote($newdes{$col});
$sql .= "($qtable, $qcol, $qdes),";
}
chop $sql;
$dbh->do($sql);
if ($dbh->err) { $body .= "[3] ($sql)" . $dbh->errstr; return; }
}
$body .= "View. p?>";
return;
}
return;
_code?>
body=>
page?>
link: htdocs/admin/scheme/index.bml
_c?>