I want to write a DBI wrapper, provide select/insert/update/delete, and users can choose which database to use. I’m very new to perl OO, I dont know what I’m doing right or not? Could you please review it, and tell me? And any advice is appreciated.
My wrapper directory look like:
MyDBI.pm
MyDBI/SQLite.pm
MyDBI/MySQL.pm
MyDBI.pm:
package MyDBI;
sub new {
shift; # discard parent class
my $database=shift || 'MySQL';
eval {
require "MyDBI/$database.pm";
} or die "$database not found\n";
my $self="MyDBI::$database"->new;
bless($self,"MyDBI::$database");
return $self;
}
sub insert { print "parent insert"; } # children will override it
MyDBI/MySQL.pm:
package MyDBI::MySQL;
require MyDBI;
@ISA=qw(MyDBI);
use DBI;use DBD::mysql;
sub new { #...} # not special
sub insert { print "mysql insert"; }
user script:
use MyDBI;
my $dbi=MyDBI->new('SQLite');
$dbi->insert;
Is this will work? MyDBI::new is different from perltoot, I’m not quite understand it right now, just copy and simulate it.
Thanks.
If you are looking for an ORM where the db tables and records map to objects and which offer data manipulation methods like insert, update and select, then see DBIx::Class or Rose::DB::Object. Each has its pros and cons, but Rose::DB::Object might be slightly easier to get started on.