My last two columns introduced package Rose::DB::Object (RDBO) as the current “best of breed” object-relational mapper. Over the span of those columns, I created a sample database structure to demonstrate some basic features. Specifically, SQLite is the database engine, and the sample database relates an actor to the films in which he or she appeared, and relates each film to the the studio that produced it. I also showed how to set up the “metadata” for RDBO, so common database operations can be performed with relatively simple Perl code.
With the scaffolding in place, let’s populate the database with a few well-known actors. Adding records is simple: create a new RDBO-derived object with the proper column data, and then call the object’s save() method:
My::RDBO::Person->new( name => ’Mark Hamill’, birthdate => ’1951-09-25’, )->save;
My::RDBO::Person->new( name => ’Harrison Ford’, birthdate => ’1942-07-13’, )->save;
My::RDBO::Person->new( name => ’Carrie Fisher’, birthdate => ’1956-10-21’, )->save;
The date format here is whatever a DateTime object understands, or whatever the native format understands. I’m using YYYY-MM-DD because it seems to work and the dates are human-readable.
Besides the two columns that are explicitly set, each row also has an id field. Once the new row is saved, that value is populated in each new object:
my $george = My::RDBO::Person->new( name => ’George Lucas’, birthdate => ’1944-05-14’, )->save;
print $george->id, “n”;
This prints 4, since its the fourth entry…
Please log in to view this content.
Not Yet a Member?
Register with LinuxMagazine.com and get free access to the entire archive, including: