#!/usr/local/bin/perl -w ########################################### # mysql-schema # 2006, Mike Schilli ########################################### use strict; use Getopt::Std; use Pod::Usage; use Expect::Simple; use Sysadm::Install qw(qquote); getopts("hvu:p:", \my %opts); $opts{u} = "root" unless $opts{u}; my $pass = ""; $pass = "-p" . qquote($opts{p}) if exists $opts{p}; pod2usage() if $opts{h}; pod2usage() unless @ARGV == 1; my $obj = new Expect::Simple { Cmd => "mysql -u$opts{u} $pass $ARGV[0]", Prompt => [ -re => 'mysql>' ], DisconnectCmd => 'quit;', Verbose => 0, Debug => 0, Timeout => 100 }; $obj->send("show tables;"); my $grid = snap_box($obj->before()); my($head, @tables) = ($grid =~ /(\w\S*)/g); print $grid, "\n\n"; for my $table (@tables) { $obj->send("describe $table;"); $grid = snap_box($obj->before()); print "$table\n"; print "$grid\n\n"; } ########################################### sub snap_box { ########################################### my($str) = @_; if($str =~ m/^(\+.* ^\+.*?)$/smx) { return $1; } } __END__ =head1 NAME mysql-schema - Show a MySQL database schema in ASCII boxes =head1 DOWNLOAD _SRC_HERE_ =head1 SYNOPSIS mysql-schema [-u user] [-p password] database =head1 DESCRIPTION mysql-schema prints a MySQL database schema, just as if someone was using the C client and typed "show tables" and "describe xyz" for every table in the given database. It uses the MySQL client C to query the database, so make sure that's installed and in your PATH. Also make sure that your C client prompts like mysql> after every command because that's what C is looking for. If you have a setting like C in your C file this won't work for obvious reasons, so make sure it's not the case. =head1 EXAMPLES $ mysql-schema mydb $ mysql-schema -u root mydb $ mysql-schema -u myself -p 123 mydb =head1 THANKS Thanks to Alexander De Bernardi for sending in fixes for bugs with mysql- schema's -u and -p options. =head1 LEGALESE Copyright 2006 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHOR 2006, Mike Schilli