Apache-Wyrd

 view release on metacpan or  search on metacpan

Wyrd/Services/MetaTable.pm  view on Meta::CPAN

#Copyright barry king <barry@wyrdwright.com> and released under the GPL.
#See http://www.gnu.org/licenses/gpl.html#TOC1 for details
package Apache::Wyrd::Services::MetaTable;
use strict;
use DBI;
our $VERSION = '0.98';

=pod

=head1 NAME

Apache::Wyrd::Services::MetaTable - MySQL-backed Namespace-based data store

=head1 SYNOPSIS

	use base qw(Apache::Wyrd);
	use Apache::Wyrd::Services::MetaTable;
	my $mt = Apache::Wyrd::Services::MetaTable->new($self->dbl->dbh);
	my $lastrun = $mt->lastrun;

	... do something with $lastrun value ...

	my $thisrun = `localtime`
	$mt->lastrun($thisrun);

=head1 DESCRIPTION

MetaTable is an SQL-backed version of Apache::Wyrd::Services::Tree in
terms of functionality, but is persistent across connections, as it
stores its data in a MySQL server.

It can store hashes, arrays, and scalars.  Pass the first two as references,
the scalars as single values.  It uses the namespace of the caller to decide
the lookup key, so only the base name of the key need be specified, by
referring to $meta_table->valuename (where $metatable stores a reference to
an instance of this class).  To retreive the value of 'valuename', call the
method valuename().  To set it, call the method with a single argument.

Be careful to dispose the MetaTable handler properly across connections,
or you run the risk of "leaking" database handles into your Apache
memory space.  The best way to avoid this is to invoke the MetaTable into
a scoped lexical only.

METHOD

=item new(dbh handle, scalar name)

invoke a new MetaTable.  The dbh handle connection must have create and
insert rights on the database.  The name is optional, and defaults to
_wyrd_meta.

=cut

sub new {
	my ($class, $dbh, $name) = @_;
	goto &AUTOLOAD if (ref($class) && &UNIVERSAL::isa($class, 'Apache::Wyrd::Services::MetaTable'));
	if (ref($dbh)) {
		if (&UNIVERSAL::isa($dbh, 'DBI::db')) {
			unless ($dbh->ping) {
				die "provided DBH handle is inactive.";
			}
		} else {
			die "please provide a valid DBI Database handle as param dbh.  This is a ". ref($dbh);
		}
	} else {
		die "please provide a valid DBI Database handle as param dbh";
	}
	$name = '' if ($name =~ /[^a-z0-9_]/);
	$name ||= '_wyrd_meta';
	my $tables = $dbh->selectall_arrayref('show tables');
	unless (grep {$name eq $_->[0]} @$tables) {
		$dbh->do("create table $name (id char(255) unique not null primary key, value text) ENGINE=MyISAM CHARSET=UTF8");
		if ($dbh->err) {
			die "Could not create table $name";
		}
	}
	return bless {
		dbh => $dbh,
		table => $name
	}, $class;
}

sub AUTOLOAD {
	no strict 'vars';
	return undef if $AUTOLOAD =~ /DESTROY$/;
	my ($package, $filename, $line) = caller;
	die "Method call to undefined sub: $AUTOLOAD" if ($package eq 'Apache::Wyrd::Services::MetaTable');
	my ($self, $newval) = @_;
	$AUTOLOAD =~ s/.+::(.+)/$1/;
	my $parent = $package . '::' . $AUTOLOAD;
	$newval = undef unless (scalar(@_) == 2);
	if ($newval) {
		$self->put_entry($parent, $newval);



( run in 1.514 second using v1.01-cache-2.11-cpan-39bf76dae61 )