#!/usr/bin/perl
# -*-cperl-*-
$| = 1;

use DBI;

use FindBin qw($Bin);
use lib "$Bin/lib_files", "$Bin/../lib_files";

use bncConfigXML;
use bncLibCQPXML;

my $dbh = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd", {PrintError => 0, RaiseError => 1})
or die <<'STOP';
Error: Can't access MySQL database.  Make sure that the server is running and that your
       configuration settings are correct (in lib_files/bncConfigXML.pm).
STOP

my $table_list = $dbh->selectcol_arrayref("SHOW TABLES");
die "Error: database '$bwMYSQLtable' appears to be empty. Aborted.\n"
  unless @$table_list > 0;

foreach my $table (sort @$table_list) {
  my $fields = $dbh->selectall_arrayref("DESCRIBE `$table`");
  print "===== $table =====\n";
  foreach my $row (@$fields) {
    my ($name, $def, $null, $key, $default, $extra) = @$row;
    printf "%-20s %-24s %-7s %-7s %-7s %s\n", $name, $def, $null, $key, $default, $extra;
  }
  print "\n";
}

$dbh->disconnect;

show_sample($bwMYSQLusertable, '%_col');
show_sample($bwMYSQLusertable, '%_adj');
show_sample($bwMYSQLusertable, '%_stat');
show_sample($bwMYSQLcategorizetable, '%');
show_sample($bwMYSQLfrequency, 'allLemma%');
show_sample($bwMYSQLfrequency, 'allWords%');
show_sample($bwMYSQLfrequency, 'tag_%');



sub show_sample {
  my ($dbname, $table_pattern) = @_;
  print "-" x 60, " $dbname LIKE $table_pattern\n";
  my $dbh = DBI->connect("DBI:mysql:$dbname:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd", {PrintError => 0, RaiseError => 1})
    or die "Error: can't connect to database '$dbname': " . $dbh->errstr;
  my ($table) = $dbh->selectrow_array("SHOW TABLES LIKE '$table_pattern'");
  if ($table) {
    my $fields = $dbh->selectall_arrayref("DESCRIBE `$table`");
    foreach my $row (@$fields) {
      my ($name, $def, $null, $key, $default, $extra) = @$row;
      printf "%-20s %-24s %-7s %-7s %-7s %s\n", $name, $def, $null, $key, $default, $extra;
    }
  }
  else {
    print "NO DATA\n";
  }
  print "\n";
}

