Class::AutoDB

Almost automatic object persistence coexisting with human-engineered database
Download

Class::AutoDB Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Publisher Name:
  • Nathan Goodman
  • Publisher web site:
  • http://search.cpan.org/~natg/

Class::AutoDB Tags


Class::AutoDB Description

Almost automatic object persistence coexisting with human-engineered database Class::AutoDB is a Perl module that works closely with Class::AutoClass to provide almost transparent object persistence that can coexist with a human-engineered database. The auto-persistence mechanism provides hooks for connecting the two parts of the database together.SYNOPSIS # code that defines persistent class # package Person; use base qw(Class::AutoClass); use vars qw(@AUTO_ATTRIBUTES %AUTODB); @AUTO_ATTRIBUTES=qw(name sex id friends); %AUTODB= (collection=>'Person', keys=>qq(name string, sex string, id integer)); Class::AutoClass::declare; ######################################## # code that uses persistent class # create and store new objects # use Class::AutoDB; use Person; my $autodb=new Class::AutoDB(database=>'test'); # open database # make some objects. not yet stored in database my $joe=new Person(name=>'Joe',sex=>'M',id=>1); my $mary=new Person(name=>'Mary',sex=>'F',id=>2); my $bill=new Person(name=>'Bill',sex=>'M',id=>3); # set up friends lists. each is a list of Person objects $joe->friends(); $mary->friends(); $bill->friends(); # store objects in database $autodb->put_objects; # retrieve existing objects # use Class::AutoDB; use Person; my $autodb=new Class::AutoDB(database=>'test'); # retrieve list of objects my @persons=$autodb->get(collection=>'Person'); # everyone my @males=$autodb->get(collection=>'Person',sex=>'M'); # just the boys # do something with the retrieved objects, for example, print friends lists for my $person (@persons) { my @friend_names=map {$_->name} @{$person->friends}; print $person->name,"'s friends are @friend_names\n"; } # retrieve and process objects one-by-one my $cursor=$autodb->find(collection=>'Person'); while (my $person=$cursor->get_next) { # do what you want with $person, for example, print friends list my @friend_names=map {$_->name} @{$person->friends}; print $person->name,"'s friends are @friend_names\n"; } # connect auto-persistent objects with engineered tables # assume database has human-engineered tables # Dept(id int, name varchar(255)), EmpDept(emp_id int, dept_id int) # this query retrieves the names of Joe's departments use DBI; my $dbh=$autodb->dbh; my $depts=$dbh->selectcol_arrayref (qq(SELECT Dept.name FROM Dept, EmpDept, Person WHERE Dept.id=EmpDept.dept_id AND EmpDept.emp_id=Person.id AND Person.name='Joe')); ######################################## # new features in verion 1.20 # retrieve objects using SQL # assuming the above database (with human-engineered tables Dept and EmpDept), # this query retrieves Person objects for employees in the toy department my @toy_persons= $autodb->get (sql=>qq(SELECT oid FROM Dept, EmpDept, Person WHERE Dept.id=EmpDept.dept_id AND EmpDept.emp_id=Person.id AND Dept.name='toy')); # retrieve all objects my @all_objects=$autodb->get; # delete objects # $autodb->del(@males); # delete the boys Requirements: · Perl


Class::AutoDB Related Software