# -*-cperl-*- # # Persistence::Database::SQL - Object Persistence in SQL Databases. # Copyright (c) 2000 Ashish Gulhati # # All rights reserved. This code is free software; you can # redistribute it and/or modify it under the same terms as Perl # itself. # # $Id: SQL.pm,v 1.9 2001/07/07 00:37:40 cvs Exp $ package Persistence::Database::SQL; use Carp; use strict; use vars qw( $VERSION $AUTOLOAD ); ( $VERSION ) = '$Revision: 1.9 $' =~ /\s+([\d\.]+)/; sub new { my ( $class, %args )=@_; my $self=\%args; for ('Engine', 'Database', 'Table', 'Template') { croak "Must specify $_." unless $self->{$_}; } $self->{Template} = %$self->{Template}; require "Persistence/Object/$self->{Engine}.pm" or croak "Could not load $self->{Engine} object class."; $self->{__DBHandle} = "Persistence::Object::$self->{Engine}"->dbconnect ($self) or croak "Could not initialize database connection."; $self->{Createfields} = !(exists $args{Createfields} && $args{Createfields}==0); $self->{Createtables} = !(exists $args{Createtables} && $args{Createtables}==0); return bless $self, $class; } sub select { my ($self, $where) = @_; return undef unless my @rows = "Persistence::Object::$self->{Engine}"->select($self, $where); map { "Persistence::Object::$self->{Engine}"->load(__Dope => $self, __Oid => $_) } @rows; } sub dbhandle { my $self = shift; return $self->{__DBHandle}; } sub DESTROY { } sub AUTOLOAD { my ($self, $val) = @_; (my $auto = $AUTOLOAD) =~ s/.*:://; if ($auto =~ /^(template|table|create(fields|tables))$/) { if (defined $val) { $self->{"\u$auto"} = $val; $self->{Template} = %$self->{Template} if $auto eq 'template'; } return $self->{"\u$auto"}; } else { croak "Could not AUTOLOAD method $auto."; } } 'True Value' __END__ =head1 NAME Persistence::Database::SQL - Object Persistence in SQL Databases. =head1 SYNOPSIS use Persistence::Database::SQL; my $db = new Persistence::Database::SQL ( Engine => 'Postgres', Database => $database_name, Table => $table_name, Template => $template_hashref ); my (@objects) = $db->search ( Key => $key, Regex => $regex ); for $obj (@objects) { $db->table('expired'); $obj->commit(); $db->table($table_name); $obj->expire(); } my $dbhandle = $db->dbhandle(); my $query = "SELECT oid,* FROM $table_name WHERE $field=$value"; my $sth = $dbhandle->prepare($query); $sth->execute(); while (@row = $sth->fetchrow()) { my $obj = new Persistence::Object::Postgres ( __Dope => $db, __Oid => $row[0] ); $obj->expire(); } =head1 DESCRIPTION This module provides a store of persistent objects using various DBMS engines. It works in association with a lower level persistent object implementation, such as Persistence::Object::Postgres. Using a template mapping object properties to PostgreSQL class fields, it is possible to automatically generate DBMS fields out of the object data, which allows you to use SQL indexing and querying facilities on your database of persistent objects. =head1 CONSTRUCTOR =over 2 =item B Creates a new Database Object. my $database = new Persistence::Database::SQL ( Engine => 'Postgres', # Required Database => $database_name, Table => $table_name, Template => $template_hashref, Host => $db_host, # Optional Port => $db_port, Username => $db_username, Password => $db_password Createfields => $boolean, Createtables => $boolean ); Takes a hash argument with following possible keys: B The name of the underlying DBMS engine, for which there must be a Persistence::Object::Engine class available. Currently, the only available engine is 'Postgres'. This attribute is required. B The name of the database. A database by this name must exist previously within the DBMS system in use with sufficient priveleges for the user. This attribute is required. B The table within the database to use for object storage. A table by this name must exist previously within the DBMS system in use with sufficient priveleges for the user. This attribute is required, and can be later changed with the table() method. B