| 1 | #!/usr/local/bin/perl -Tw | ||
| 2 | use vars qw( $version $CLASS $DATABASE $DBUSER $DBPASS $DBHOST $DBTYPE @MODS | ||
| 3 | $opt_h $opt_d $opt_c $opt_u $opt_p $opt_b $opt_v $opt_t $opt_a ); | ||
| 4 | $version = "1.1"; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | xxx_create.pl - Database creation/deletion/status script for DBIx::Frame | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | xxx_create.pl [-hvcd] [-b database] [-u user] [-p password] | ||
| 13 | |||
| 14 | -b database Database to investigate. Defaults to $DATABASE | ||
| 15 | -u user Username to connect with. Defaults to $DBUSER | ||
| 16 | -p password Password to connect with. | ||
| 17 | |||
| 18 | -h Prints this message and exit. | ||
| 19 | -v Prints the version number and exits. | ||
| 20 | |||
| 21 | -t TABLE Work on table TABLE | ||
| 22 | -a Work on all tables. | ||
| 23 | |||
| 24 | -c Create new database. Can be used with -d to reset | ||
| 25 | the database. | ||
| 26 | -d Delete old database. You may lose data with this. | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | Creates, deletes, or just prints off status information about a | ||
| 31 | $CLASS database. Should be fairly self-explanatory. Note that | ||
| 32 | the database must exist first; this only creates the tables. | ||
| 33 | |||
| 34 | =head1 REQUIREMENTS | ||
| 35 | |||
| 36 | B<$CLASS> | ||
| 37 | |||
| 38 | =head1 SEE ALSO | ||
| 39 | |||
| 40 | B<$CLASS>, B<DBIx::Frame::CGI> | ||
| 41 | |||
| 42 | =head1 AUTHOR | ||
| 43 | |||
| 44 | Written by Tim Skirvin <tskirvin@ks.uiuc.edu>. | ||
| 45 | |||
| 46 | =head1 HOMEPAGE | ||
| 47 | |||
| 48 | B<http://www.ks.uiuc.edu/Development/MDTools/dbixframe/> | ||
| 49 | |||
| 50 | =head1 LICENSE | ||
| 51 | |||
| 52 | This code is distributed under the University of Illinois Open Source | ||
| 53 | License. See | ||
| 54 | C<http://www.ks.uiuc.edu/Development/MDTools/dbixframe/license.html> | ||
| 55 | for details. | ||
| 56 | |||
| 57 | =head1 COPYRIGHT | ||
| 58 | |||
| 59 | Copyright 2000-2004 by the University of Illinois Board of Trustees and | ||
| 60 | Tim Skirvin <tskirvin@ks.uiuc.edu>. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | ############################################################################### | ||
| 65 | ### Configuration + Private Data ############################################## | ||
| 66 | ############################################################################### | ||
| 67 | |||
| 68 | ## Load shared configurations and/or private data using 'do' commands, as | ||
| 69 | ## seen below. Note that several 'do's can be run if necessary. | ||
| 70 | |||
| 71 | # do '/FULL/PATH/TO/CODE/TO/RUN'; | ||
| 72 | |||
| 73 | ## This is the perl class that you will be using in this script. | ||
| 74 | |||
| 75 | $CLASS = ""; | ||
| 76 | |||
| 77 | ## The sub-modules that may exist; it will load as many as possible, and | ||
| 78 | ## offer warnings for those it couldn't get. | ||
| 79 | |||
| 80 | @MODS = qw( $CLASS ); | ||
| 81 | |||
| 82 | ## Modify and uncomment this to use user code instead of just system-wide | ||
| 83 | ## modules. Note that this path must be set up as a standard Perl tree; | ||
| 84 | ## I'd personally recommend just installing things system-wide unless you're | ||
| 85 | ## a developer. | ||
| 86 | |||
| 87 | # use lib '/PATH/TO/USER/CODE'; | ||
| 88 | |||
| 89 | ## Database Information | ||
| 90 | ## You may want to set these with a common config file, using 'do FILE'. | ||
| 91 | ## Also, defaults may already be set within the class; only set these if | ||
| 92 | ## you want to override the defaults. | ||
| 93 | ## Note, this database must exist first! This script only creates the | ||
| 94 | ## tables within the database. | ||
| 95 | |||
| 96 | # $DBHOST = ""; # System that hosts the database | ||
| 97 | # $DBTYPE = ""; # The type of database that we're working on | ||
| 98 | # $DATABASE = ""; # Name of the database we're connecting to | ||
| 99 | # $DBUSER = ""; # Username to connect to the database | ||
| 100 | # $DBPASS = ""; # Password to connect to the database | ||
| 101 | |||
| 102 | ############################################################################### | ||
| 103 | ##### main() ################################################################## | ||
| 104 | ############################################################################### | ||
| 105 | |||
| 106 | use strict; | ||
| 107 | use Getopt::Std; | ||
| 108 | |||
| 109 | use DBIx::Frame; | ||
| 110 | |||
| 111 | $0 =~ s%.*/%%; # Clean the path up | ||
| 112 | Usage() unless scalar @ARGV; | ||
| 113 | getopts('cdhvu:p:b:t:a'); | ||
| 114 | |||
| 115 | Usage() if $opt_h; | ||
| 116 | Version() if $opt_v; | ||
| 117 | $DATABASE = $opt_b if $opt_b; | ||
| 118 | $DBUSER = $opt_u if $opt_u; | ||
| 119 | $DBPASS = $opt_p if $opt_p; | ||
| 120 | |||
| 121 | # Load the appropriate class module | ||
| 122 | { local $@; eval "use $CLASS"; die "$@\n" if $@; } | ||
| 123 | |||
| 124 | # Load the sub-modules | ||
| 125 | my @PROBS; | ||
| 126 | foreach (@MODS) { local $@; eval "use $_"; push @PROBS, "$@" if $@; } | ||
| 127 | warn @PROBS if scalar @PROBS; | ||
| 128 | |||
| 129 | # Confirm that the user really meant -d | ||
| 130 | if ($opt_d) { | ||
| 131 | print "This will delete all contents of the current database.\n"; | ||
| 132 | print "Are you *sure* you really want to do this? (y/n) "; | ||
| 133 | my $answer = <STDIN>; chomp $answer; | ||
| 134 | unless ($answer =~ /^\s*y/i) { | ||
| 135 | print "Didn't think so\n"; | ||
| 136 | $opt_d = 0; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | # Connect to the database | ||
| 141 | my $db = $CLASS->connect($DATABASE, $DBUSER, $DBPASS, $DBHOST, $DBTYPE) | ||
| 142 | || Exit("Couldn't connect to '$DATABASE': ", DBI::errstr, "\n"); | ||
| 143 | |||
| 144 | my %tables = %{$db->fieldhash}; | ||
| 145 | |||
| 146 | foreach my $table (sort keys %tables) { | ||
| 147 | next unless ref $tables{$table}; | ||
| 148 | next unless ($opt_a || ( $opt_t && $opt_t eq $table ) ); | ||
| 149 | if ($opt_d) { | ||
| 150 | $db->drop_table($table) | ||
| 151 | ? print "Dropped '$table'\n" | ||
| 152 | : print "Couldn't drop '$table': ", $db->error || "", "\n"; | ||
| 153 | } | ||
| 154 | if ($opt_c) { | ||
| 155 | $db->create_table($table, $tables{$table}) | ||
| 156 | ? print "Created '$table'\n" | ||
| 157 | : print "Couldn't create '$table': ", $db->error || "", "\n"; | ||
| 158 | } | ||
| 159 | unless ($opt_c || $opt_d) { # Don't bother printing here | ||
| 160 | print "Table: $table\n"; | ||
| 161 | foreach my $column (sort keys %{$tables{$table}}) { | ||
| 162 | printf(" %-26s %50s\n", $column, $tables{$table}->{$column}); | ||
| 163 | } | ||
| 164 | print "\n"; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | $db->disconnect; | ||
| 169 | exit(0); | ||
| 170 | |||
| 171 | ############################################################################### | ||
| 172 | ### Subroutines ############################################################### | ||
| 173 | ############################################################################### | ||
| 174 | |||
| 175 | ### Usage() | ||
| 176 | # Prints off help information and exits | ||
| 177 | sub Usage { | ||
| 178 | my $database = $DATABASE || ""; | ||
| 179 | my $dbuser = $DBUSER || ""; | ||
| 180 | print <<EOM; | ||
| 181 | $0 v$version | ||
| 182 | A database creation/deletion/status script for $CLASS | ||
| 183 | Usage: $0 [-hvcd] [-b database] [-u user] [-p password] | ||
| 184 | |||
| 185 | Creates, deletes, or just prints off status information about a | ||
| 186 | $CLASS database. Should be fairly self-explanatory. Note that | ||
| 187 | the database must exist first; this only creates the tables. | ||
| 188 | |||
| 189 | -b database Database to investigate. Defaults to '$database' | ||
| 190 | -u user Username to connect with. Defaults to '$dbuser' | ||
| 191 | -p password Password to connect with. | ||
| 192 | |||
| 193 | -h Prints this message and exit. | ||
| 194 | -v Prints the version number and exits. | ||
| 195 | |||
| 196 | -t TABLE Work on table TABLE | ||
| 197 | -a Work on all tables. | ||
| 198 | |||
| 199 | -c Create new database. Can be used with -d to reset | ||
| 200 | the database. | ||
| 201 | -d Delete old database. You may lose data with this. | ||
| 202 | EOM | ||
| 203 | |||
| 204 | Exit(); | ||
| 205 | } | ||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | ### Version | ||
| 210 | # Prints the version | ||
| 211 | sub Version { Exit("$0 v$version") } | ||
| 212 | |||
| 213 | ### Exit | ||
| 214 | # Prints off whatever it gets to | ||
| 215 | sub Exit { foreach (@_) { print "$_\n" } exit(0); } | ||
| 216 | |||
| 217 | ############################################################################### | ||
| 218 | ### Version History ########################################################### | ||
| 219 | ############################################################################### | ||
| 220 | # v0.5 Fri Jul 13 11:18:07 CDT 2001 | ||
| 221 | ### First commented/properly supported version. | ||
| 222 | # v1.0 Tue Oct 21 16:37:02 CDT 2003 | ||
| 223 | ### Releasing it, ready or not. | ||
| 224 | # v1.1 Wed May 19 15:01:41 CDT 2004 | ||
| 225 | ### Oops, I forgot to include this last time... |