NAME
Metadata::DB
DESCRIPTION
This is just like Metadata::Base, only we store in a database. An instance of this object represents a metadata record.
SEE ALSO
Metadata::DB
SYNOPSIS
use Metadata::DB;
my $dbh;
my $o = new Metadata::DB($dbh);
$o->load;
$o->set( name => 'jack' );
$o->set( age => 14 );
$o->id_set(4);
$o->save;
use Metadata::DB;
my $dbh;
my $o = new Metadata::DB($dbh);
$o->id_set(4);
$o->load;
$o->get( 'name' );
$o->set( 'age' );
Loading metadata from db
- via constructor
-
If you pass the id to the constructor, it will attempt to load from db.
my $o = new Metadata::DB({ DBH => $dbh, id => 'james' });
- via methods
-
You can directly tell it what the id will be , and then request to load.
my $o = new Metadata::DB({ DBH => $dbh }); $o->id_set('james'); $o->load;
- checking for record
-
my $o = new Metadata::DB({ DBH => $dbh }); $o->id_set('james'); $o->load; # you must call load $o->id_exists;
DESCRIPTION
Inherits Metadata::Base and all its methods.
new()
argument is hash ref with at least a DBH argument, which is a database handle.
my $o = new Metadata::DB::Object({ DBH = $dbh });
Optional argument is 'id'.
id_set()
set the id
id() and id_get()
get the id
id_exists()
returns boolean if the id is in the database
entries_count()
returns number of entries for this id
set()
elements()
add()
Works like set(), only you can provide many entries.
$o->add(
name => 'this',
age => 4,
);
write(), save()
save to db
load(), read()
will attempt to load from db YOU MUST CALL load() to check what is in the database
loaded()
returns boolean if load() was triggered or called or not
get_all()
returns hashref with all meta will attempt to load from db
get()
CAVEATS
WARNING Calling save() before load() will delete all record metadata previously saved.
delete ALL metadata with id 5 and save only 'name marc'.
my $m = Metadata::DB->new({ DBH => $dbh, id => 5 });
$m->set( name=> 'marc' );
$m->save;
After, this will NOT load the metadata 'name marc':
my $m = Metadata::DB->new({ DBH => $dbh, id => 5 });
$m->get( 'name' );
This example WILL load the metadata:
my $m3= Metadata::DB->new({ DBH => $dbh, id => 5 });
$m->load;
$m->get( 'name' );
This example will NOT delete metadata and will add instead:
my $m = Metadata::DB->new({ DBH => $dbh, id => 5 });
$m->load;
$m->set( age => 25 );
$m->save;
Why? Why not just override get and get all to take care of this? Wny not just call load() automatically?? Because that's up to you. You MAY want to NOT do this cpu intensive operation. Maybe you want to insert a million entries really quickly, thus you dont want to load every time, maybe you already know there is nothing in there.
SEE ALSO
Metadata::Base
AUTHOR
Leo Charre
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 200:
You forgot a '=back' before '=head1'