| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | ###### PACKAGES ###### | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Getopt::Long; | ||
| 8 | Getopt::Long::Configure('no_ignore_case'); | ||
| 9 | use MySQL::Util; | ||
| 10 | use Data::Dumper; | ||
| 11 | |||
| 12 | ###### CONSTANTS ###### | ||
| 13 | |||
| 14 | ###### GLOBAL VARIABLES ###### | ||
| 15 | |||
| 16 | use vars qw($Host $DbName $User $Pass $Table $RowCount $Util $Defaults $Conf); | ||
| 17 | |||
| 18 | ###### MAIN PROGRAM ###### | ||
| 19 | |||
| 20 | parse_cmd_line(); | ||
| 21 | init(); | ||
| 22 | |||
| 23 | my %defaults = split( /,/, $Defaults ) if $Defaults; | ||
| 24 | |||
| 25 | $Util->create_data( | ||
| 26 | table => $Table, | ||
| 27 | rows => $RowCount, | ||
| 28 | defaults => {%defaults}, | ||
| 29 | conf => $Conf | ||
| 30 | ); | ||
| 31 | |||
| 32 | ###### END MAIN ####### | ||
| 33 | |||
| 34 | sub init { | ||
| 35 | my $dsn = "dbi:mysql:host=$Host;dbname=$DbName"; | ||
| 36 | |||
| 37 | $Util = MySQL::Util->new( | ||
| 38 | dsn => $dsn, | ||
| 39 | user => $User, | ||
| 40 | pass => $Pass | ||
| 41 | ); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub check_required { | ||
| 45 | my $opt = shift; | ||
| 46 | my $arg = shift; | ||
| 47 | |||
| 48 | print_usage("missing arg $opt") if !$arg; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub parse_cmd_line { | ||
| 52 | my @tmp = @ARGV; | ||
| 53 | my $help; | ||
| 54 | |||
| 55 | my $rc = GetOptions( | ||
| 56 | "h=s" => \$Host, | ||
| 57 | "d=s" => \$DbName, | ||
| 58 | "u=s" => \$User, | ||
| 59 | "p=s" => \$Pass, | ||
| 60 | "n=s" => \$Table, | ||
| 61 | "c=s" => \$RowCount, | ||
| 62 | "a=s" => \$Defaults, | ||
| 63 | "f=s" => \$Conf, | ||
| 64 | "help|?" => \$help | ||
| 65 | ); | ||
| 66 | |||
| 67 | print_usage("usage:") if $help; | ||
| 68 | |||
| 69 | check_required( '-u', $User ); | ||
| 70 | check_required( '-h', $Host ); | ||
| 71 | check_required( '-d', $DbName ); | ||
| 72 | check_required( '-n', $Table ); | ||
| 73 | check_required( '-c', $RowCount ); | ||
| 74 | |||
| 75 | if ( !($rc) || ( @ARGV != 0 ) ) { | ||
| 76 | ## if rc is false or args are left on line | ||
| 77 | print_usage("parse_cmd_line failed"); | ||
| 78 | } | ||
| 79 | |||
| 80 | @ARGV = @tmp; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub print_usage { | ||
| 84 | print STDERR "@_\n"; | ||
| 85 | |||
| 86 | print "\n$0\n" | ||
| 87 | . "\t-u <user>\n" | ||
| 88 | . "\t-h <host>\n" | ||
| 89 | . "\t-d <dbname>\n" | ||
| 90 | . "\t-n <table name>\n" | ||
| 91 | . "\t-c <row count>\n" | ||
| 92 | . "\t[-p <pass>]\n" | ||
| 93 | . "\t[-a <default column values in csv key/value pair format>]\n" | ||
| 94 | . "\t[-f <conf file>]\n" | ||
| 95 | . "\t[-?] (usage)\n" . "\n"; | ||
| 96 | |||
| 97 | print "\nExamples:\n" | ||
| 98 | . "\t$0 -u myself -p secret -h myhost -d mydb -n customers -c 50 \n" | ||
| 99 | . "\t\t-a my_id,87,code,xyz\n"; | ||
| 100 | |||
| 101 | print "\n"; | ||
| 102 | |||
| 103 | exit 1; | ||
| 104 | } |