AddressBook
view release on metacpan or search on metacpan
lib/AddressBook.pm view on Meta::CPAN
AddressBook::DB::DBI
AddressBook::DB::PDB
AddressBook::DB::Text
AddressBook::DB::HTML
More will be added in the future.
=cut
use strict;
use Carp;
use Date::Manip;
use AddressBook::Entry;
use AddressBook::Config;
use vars qw($VERSION @ISA);
$VERSION = '0.16';
=head2 new
Create a new AddressBook object.
AddressBook->new(source=$source,\%args)
See the appropriate backend documentation for constructor details.
=cut
sub new {
my $class = shift;
my $self;
my %args = @_;
if ($args{config}) {
$self->{config} = $args{config};
} else {
$self->{config} = AddressBook::Config->new(config_file=>$args{config_file});
}
if(defined $args{source}) {
my ($type, $dsn) = split(':', $args{source}, 2);
$dsn = '' unless $dsn;
delete $args{source};
my (%bedb_args,$k,$v);
foreach ($self->{config}->{db}->{$type}, \%args) {
next if (ref($_) ne "HASH" || ! %{$_} );
while (($k,$v) = each %{$_}) {
$bedb_args{$k} = $v;
}
}
my $driverName = $self->{config}->{db}->{$type}->{driver} || croak "Uknown driver type for source = \"$type\"";
eval qq{
require AddressBook::DB::$driverName;
\$self = AddressBook::DB::$driverName->new(dsn => "$dsn",
config => \$self->{config},
\%bedb_args,
);
};
croak "Couldn't load backend `$driverName': $@" if $@;
$self->{db_name}=$type;
} else {
bless ($self,$class);
}
return $self;
}
=head2 sync
AddressBook::sync(master=>$master_db, slave=>$slave_db)
AddressBook::sync(master=>$master_db, slave=>$slave_db,debug=>1)
Synchronizes the "master" and "slave" databases. The "master" database type must be
one that supports random-access methods. The "slave" database type must
be one that supports sequential-access methods.
When the 'debug' option is true, debug messages will be printed to stdout. The
msg_function paramater, if included, should be a subroutine reference which will
be called with a status message is the argument.
=over 4
=item 1
For each record in the slave, look for a corresponding record in the master, using
the key_fields of each.
=over 6
=item Z<>
If no match is found, the entry is added to the master.
=item Z<>
If multiple matches are found, an error occurrs.
=item Z<>
If one match is found, then:
=over 8
=item Z<>
If the records match, nothing is done.
=item Z<>
If the records do not match, then:
=over 10
=item Z<>
If the slave record's timestamp is newer, the master's entry is merged (see below)
with the slave entry's data.
=item Z<>
If the master record's timestamp is newer, nothing is done.
=back
( run in 2.317 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )