ObjectDBI
view release on metacpan or search on metacpan
lib/ObjectDBI.pm view on Meta::CPAN
create table perlobjects (
obj_id integer unique not null auto_increment,
obj_pid integer references perlobjects (obj_id),
obj_gpid integer references perlobjects (obj_id),
obj_name varchar(255),
obj_type varchar(64),
obj_value varchar(255)
);
Indexes:
create index perlobjects_name_i on perlobjects (obj_name);
create index perlobjects_type_i on perlobjects (obj_type);
create index perlobjects_value_i on perlobjects (obj_value);
create index perlobjects_pid_i on perlobjects (obj_pid);
create index perlobjects_gpid_i on perlobjects (obj_gpid);
Now before y'all start shouting;
obviously, given your particular type of RDBMS, your mileage may vary
with respect to this SQL code, and you may not have primary or foreign
keys. You may not have indexes or sequences, and you may even have an easier
way to store infinite strings. This is all up to you, your cleverness and your
needs.
If you plan to store perlhashes with keys of more than 255 character length
(which is an unwise thing in itself), for example, then you might consider
making 'obj_name' a bit longer. If you plan to store values with characters
outside of the 32-126 range and you're using Postgres, then you might want
to change the data type of 'obj_value' from 'varchar' to 'bytea'.
This module isn't here to lecture you - just to make things easy.
If you're using MySQL, you'll have problems without a sequence, so you'll
have to make the 'obj_id' field auto-incrementing. For those users,
a special piece of code is added to withdraw the id of an
object after the fact of its insertion.
If you're using a RDBMS that doesn't do sequences OR auto-incrementing,
then IDs are generated out of thin air. Be prepared to work with large
numbers though. If your RDBMS can't handle those - well, then I'm at
my wit's end: please provide a 'sequencefnc' to the constructor.
=head1 API
=head2 B<my $objectdbi = ObjectDBI-E<gt>new (%options)>
Returns a blessed instance of this module.
The arguments provide the object with a hash of options, which can be:
'dbh' => DBI database handle.
'dbiuri' => DBI database connection URI.
'dbiuser' => DBI database connection user.
'dbipass' => DBI database connection password.
'dbioptions' => DBI database connection options.
'table' => Table name used ('perlobjects' is the default).
'sequence' => Sequence name for easily retrieving new IDs.
'sequencesql' => Sequence SQL for retrieving a new ID.
'sequencefnc' => A function ref to be used to retrieve a new ID.
'overwrite' => Overwrite objects of the same type and name.
'chunksize' => A number defining at what length values will get split.
'debug' => Setting it at anything will make STDERR a busy stream.
About sequences: the first available method given will be used. So please
do yourself a favour, avoid confusion, and use only one of the available
methods out of 'sequence', 'sequencesql' and 'sequencefnc'.
About chunksize: the default value is 255. If you set it to zero, that'll
be interpreted as 'infinite'. If you set it to anything else, make sure
it matches the storage size of the 'obj_value' field in the RDBMS.
Also about both sequences and chunksize: in the case of postgres and oracle,
autodiscovery of these items will be performed in case they're not given.
=cut
sub new {
my $class = shift;
my $classname = ref($class) || $class;
my $self = {};
my %options = @_;
$self->{objtable} = $options{table} || 'perlobjects';
if ($options{dbh}) {
$self->{dbh} = $options{dbh};
} else {
$self->{dbh} = DBI->connect(
$options{dbiuri},
$options{dbiuser},
$options{dbipass},
$options{dbioptions}
);
}
return undef if (!defined($self->{dbh}));
$self->{sequence} = $options{sequence};
$self->{sequencesql} = $options{sequencesql};
$self->{sequencefnc} = $options{sequencefnc};
$self->{overwrite} = $options{overwrite};
$self->{chunksize} = $options{chunksize};
$self->{dbtype} = $self->{dbh}->get_info($GetInfoType{SQL_DBMS_NAME});
$self->{debug} = $options{debug};
bless $self, $classname;
$self->__auto_discover();
return $self;
}
=head2 B<my $id = $objectdbi-E<gt>put ($ref[,$name[,$overwrite]])>
Store a reference in the database, perhaps under a certain name.
If 'overwrite' is set (either in the object or as a parameter),
and the object with given type and name already
exists, it is removed prior to this object being written.
Returns the ID of the object of the newly created object.
=cut
sub put {
my $self = shift;
my $ref = shift;
my $name = shift;
my $overwrite = shift;
if (ref($name)) {
$name = "$name";
( run in 0.759 second using v1.01-cache-2.11-cpan-39bf76dae61 )