#!/usr/bin/perl -w # note Perl 5.005 or later will be required to run this program # # the tables used in this program can be created with the following SQL # statements: # create table booleans (name varchar(80) NOT NULL, val int(1), PRIMARY KEY(name)); # create table scalars (name varchar(80) NOT NULL, val varchar(255), PRIMARY KEY(name)); # create table hashes (name varchar(80) NOT NULL, parameter varchar(40), var varchar(40), val varchar(255), PRIMARY KEY(name)); # create table arrays (name varchar(80) NOT NULL, var varchar(40), i int(10), val varchar(255), PRIMARY KEY(name)); use Persistent::MySQL; use AppConfig qw/:expand :argcount/; use English; use strict; # be good, li'l program. $| = 1; my $config = AppConfig->new(); # AppConfig object # define all the variables we will use $config->define( 'DEBUG' => { ARGCOUNT => ARGCOUNT_NONE, DEFAULT => 0 }, 'USERNAME' => { ARGCOUNT => ARGCOUNT_ONE, DEFAULT => 'nobody' }, 'HOSTS' => { ARGCOUNT => ARGCOUNT_LIST }, 'PROPERTIES' => { ARGCOUNT => ARGCOUNT_HASH }, ); $config->args(); # process command-line options # set up some values in the array and hash elements $config->HOSTS()->[0] = 'backuphost'; $config->HOSTS()->[1] = 'dbhost'; $config->PROPERTIES()->{joe} = '222-333-4444'; $config->PROPERTIES()->{marge} = '555-666-7777'; # use eval, in case an exception is thrown eval { # create the persistent objects (one table per data type) my $database = 'persistent'; my $host = 'xyz'; my $user = 'xyz'; my $password = 'xyz'; my $booleans = new Persistent::MySQL( "DBI:mysql:database=$database;host=$host", $user, $password, 'booleans'); my $scalars = new Persistent::MySQL( "DBI:mysql:database=$database;host=$host", $user, $password, 'scalars'); my $arrays = new Persistent::MySQL( "DBI:mysql:database=$database;host=$host", $user, $password, 'arrays'); my $hashes = new Persistent::MySQL( "DBI:mysql:database=$database;host=$host", $user, $password, 'hashes'); # this is the primary object identifier key, a 80-character id $booleans->add_attribute('name', 'ID', 'VarChar', undef, 80); $scalars->add_attribute('name', 'ID', 'VarChar', undef, 80); $arrays->add_attribute('name', 'ID', 'VarChar', undef, 80); $hashes->add_attribute('name', 'ID', 'VarChar', undef, 80); # this is the value, a 255-character string (except for booleans) $booleans->add_attribute('val', 'Persistent', 'Number', undef, 1); $scalars->add_attribute('val', 'Persistent', 'VarChar', undef, 255); $arrays->add_attribute('val', 'Persistent', 'VarChar', undef, 255); $hashes->add_attribute('val', 'Persistent', 'VarChar', undef, 255); # for hashes, a variable name and a key are always needed # (40 chars. max each) $hashes->add_attribute('var', 'Persistent', 'VarChar', undef, 40); $hashes->add_attribute('parameter', 'Persistent', 'VarChar', undef, 40); # for arrays, an index and a variable name are always needed # (40 and 10 chars max, respectively) $arrays->add_attribute('var', 'Persistent', 'VarChar', undef, 40); $arrays->add_attribute('i', 'Persistent', 'Number', undef, 10); # clear all the tables - could also be done in SQL, but this is OK for small tables foreach my $p ($booleans, $scalars, $arrays, $hashes) { $p->restore_all(); $p->delete while $p->restore_next(); } # get the list of AppConfig variables and store each one my %varlist = $config->varlist('.*'); foreach my $varname (keys %varlist) { # here $varname is the name of a variable we are interested in $config->DEBUG && print "Storing variable $varname\n"; if (ARGCOUNT_NONE == $config->_argcount($varname)) # store the booleans { $booleans->clear; $booleans->name($varname); # we set the value to 1 or 0, regardless of what was stored in the variable # this is overly cautious, but caution is a virtue sometimes $booleans->val( (1 == $config->get($varname)) ); $booleans->save(); } elsif (ARGCOUNT_ONE == $config->_argcount($varname)) # store the scalars { $scalars->clear; $scalars->name($varname); $scalars->val( $config->get($varname) ); $scalars->save(); } elsif (ARGCOUNT_LIST == $config->_argcount($varname)) # store the arrays { # store each value in the array to its own $arrays instance my $counter = 0; foreach my $value (@{$config->get($varname)}) { $arrays->clear; $arrays->name($varname . $counter); # this ID could be better $arrays->var($varname); $arrays->val($value); $arrays->i($counter); $arrays->save; $counter++; } } elsif (ARGCOUNT_HASH == $config->_argcount($varname)) # store the hashes { my $hash = $config->get($varname); foreach my $key (keys %$hash) { $hashes->clear; $hashes->name($varname . $key); # this ID could be better $hashes->var($varname); $hashes->val($hash->{$key}); $hashes->parameter($key); $hashes->save; } } else { die "Impossible argcount($varname) = " . $config->_argcount($varname) . ", quitting"; } } # create new AppConfig my $config = AppConfig->new(); # AppConfig object # define all the variables we will use. Note that this could be done automatically # from the database tables, but it's easier this way. $config->define( 'DEBUG' => { ARGCOUNT => ARGCOUNT_NONE, DEFAULT => 0 }, 'USERNAME' => { ARGCOUNT => ARGCOUNT_ONE, DEFAULT => 'nobody' }, 'HOSTS' => { ARGCOUNT => ARGCOUNT_LIST }, 'PROPERTIES' => { ARGCOUNT => ARGCOUNT_HASH }, ); # restore the booleans $booleans->restore_all(); while ($booleans->restore_next()) { $config->DEBUG && print "Restoring boolean ", $booleans->name, "\n"; $config->set($booleans->name, $booleans->val); } # restore the scalars $scalars->restore_all(); while ($scalars->restore_next()) { $config->DEBUG && print "Restoring scalar ", $scalars->name, "\n"; $config->set($scalars->name, $scalars->val); } # restore the arrays $arrays->restore_all(); while ($arrays->restore_next()) { $config->DEBUG && print "Restoring array element ", $arrays->name, "\n"; my $array = $config->get($arrays->var()); # note that the array location will be autovivified $array->[$arrays->i()] = $arrays->val; } # restore the hashes $hashes->restore_all(); while ($hashes->restore_next()) { $config->DEBUG && print "Restoring hash element ", $hashes->name, "\n"; my $hash = $config->get($hashes->var()); $hash->{$hashes->parameter()} = $hashes->val(); } }; print "An error occurred: $EVAL_ERROR\n" if $EVAL_ERROR;