David Winters > Persistent-DBI-0.50 > Persistent::DBI

Download:
Persistent-DBI-0.50.tar.gz

Dependencies

Annotate this POD

Related Modules

Data::Dumper
Apache::Session
Class::DBI
more...
By perlmonks.org
View/Report Bugs
Module Version: 0.50   Source  

NAME ^

Persistent::DBI - An Abstract Persistent Class implemented using a DBI Data Source

SYNOPSIS ^

  ### we are a subclass of ... ###
  use Persistent::DBI;
  @ISA = qw(Persistent::DBI);

ABSTRACT ^

This is an abstract class used by the Persistent framework of classes to implement persistence using DBI data stores. This class provides the methods and interface for implementing Persistent DBI classes. Refer to the Persistent documentation for a very thorough introduction to using the Persistent framework of classes.

This class is part of the Persistent DBI package which is available from:

  http://www.bigsnow.org/persistent
  ftp://ftp.bigsnow.org/pub/persistent

DESCRIPTION ^

Before we get started describing the methods in detail, it should be noted that all error handling in this class is done with exceptions. So you should wrap an eval block around all of your code. Please see the Persistent documentation for more information on exception handling in Perl.

OVERRIDDEN PUBLIC METHODS PROVIDED IN THIS CLASS ^

new -- Object Constructor

  use Persistent::DBI;

  eval {
    my $obj = new Persistent::DBI($data_source, $username, $password, $table);
    ### or ###
    my $obj = new Persistent::DBI($dbh, $table);
  };
  croak "Exception caught: $@" if $@;

Allocates an object. This method throws Perl execeptions so use it with an eval block.

Parameters:

These are the same as for the datastore method below.

datastore -- Sets/Returns the Data Store Parameters

  eval {
    ### set the data store ###
    $dbh = $obj->datastore($data_source, $username, $password, $table);
    ### or ###
    $dbh = $obj->datastore($dbh, $table);

    ### get the data store ###
    $dbh = $obj->datastore();
  };
  croak "Exception caught: $@" if $@;

Returns (and optionally sets) the data store of the object. This method throws Perl execeptions so use it with an eval block.

Parameters:

$data_source

DBI data source name for the database. Please refer to the DBI documentation for valid data source names.

$username

Connect to the database with this given username

$password

Password for the username

$table

Table in the database that contains the objects. This table should exist prior to instantiating a Persistent class that will use it.

$dbh (also a return value)

DBI handle to the database

insert -- Insert an Object into the Data Store

  eval {
    $person->insert();
  };
  croak "Exception caught: $@" if $@;

Inserts an object into the data store. This method throws Perl execeptions so use it with an eval block.

Parameters:

None.

Returns:

Nothing.

See the Persistent documentation for more information.

update -- Update an Object in the Data Store

  eval {
    $person->update();
  };
  croak "Exception caught: $@" if $@;

Updates an object in the data store. This method throws Perl execeptions so use it with an eval block.

Parameters:

@id

Values of the Identity attributes of the object. This argument is optional and will default to the Identifier values of the object as the default.

This argument is useful if you are updating the Identity attributes of the object and you already have all of the attribute values so you do not need to restore the object (like a CGI request with hidden fields, maybe). So you can just set the Identity attributes of the object to the new values and then pass the old Identity values as arguments to the update method. For example, if Pebbles Flintstone married Bam Bam Rubble, then you could update her last name like this:

  ### Pebbles already exists in the data store, but we don't ###
  ### want to do an extra restore because we already have    ###
  ### all of the attribute values ###

  $person->lastname('Rubble');
  $person->firstname('Pebbles');
  ### set the rest of the attributes ... ###

  $person->update('Flintstone', 'Pebbles');

Or, if don't want to set all of the object's attributes, you can just restore it and then update it like this:

  ### restore object from data store ###
  if ($person->restore('Flintstone', 'Pebbles')) {
    $person->lastname('Rubble');
    $person->update();
  }

Returns:

Nothing.

See the Persistent documentation for more information.

save -- Save an Object to the Data Store

  eval {
    $person->save();
  };
  croak "Exception caught: $@" if $@;

Saves an object to the data store. The object is inserted if it does not already exist in the data store, otherwise, it is updated. This method throws Perl execeptions so use it with an eval block.

Parameters:

None.

Returns:

$flag

A true value if the object previously existed in the data store (it was updated), and a false value if not (it was inserted).

See the Persistent documentation for more information.

delete -- Delete an Object from the Data Store

  eval {
    $person->delete();
  };
  croak "Exception caught: $@" if $@;

Deletes an object from the data store. This method throws Perl execeptions so use it with an eval block.

Parameters:

@id

Values of the Identity attributes of the object. This argument is optional and will default to the Identifier values of the object as the default.

Returns:

Nothing.

See the Persistent documentation for more information.

restore -- Restore an Object from the Data Store

  eval {
    $person->restore(@id);
  };
  croak "Exception caught: $@" if $@;

Restores an object from the data store. This method throws Perl execeptions so use it with an eval block.

Parameters:

@id

Values of the Identity attributes of the object. This method throws Perl execeptions so use it with an eval block.

Returns:

$flag

A true value if the object previously existed in the data store (it was restored), and a false value if not (nothing to restore).

See the Persistent documentation for more information.

restore_where -- Conditionally Restoring Objects

  use Persistent::DBI;

  eval {
    my $emp = new Persistent::DBI('ORCL', 'scott', 'tiger', 'emp');
    $emp->restore_where(
      " job = 'CLERK' and sal > 1000",
      "sal DESC, ename"
    );
    while ($emp->restore_next()) {
      print "Restored: ";  print_person($person);
    }
  };
  croak "Exception caught: $@" if $@;

Restores objects from the data store that meet the specified conditions. The objects are returned one at a time by using the restore_next method and in a sorted order if specified. This method throws Perl execeptions so use it with an eval block.

Since this is a SQL based Persistent class, the restore_where method expects a valid SQL WHERE clause for the first argument, $where, and a valid SQL ORDER BY clause for the optional second argument, $order_by.

Parameters:

$where

Conditional expression for the requested objects. The format of this expression is a SQL WHERE clause without the WHERE keyword. This argument is optional.

$order_by

Sort expression for the requested objects. The format of this expression is a SQL ORDER BY clause with the ORDER BY keywords. This argument is optional.

Returns:

$num_of_objs

The number of objects that match the conditions.

See the Persistent documentation for more information.

quote -- Quotes a String Literal for Use in a SQL Query

  $person->restore_where(sprintf("lastname = %s",
                                 $person->quote($lastname)));

Quotes a string literal for use in an SQL statement by escaping any special characters (such as quotation marks) contained within the string and adding the required type of outer quotation marks.

Parameters:

$str

String to quote and escape.

Returns:

$quoted_str

Quoted and escaped string.

ABSTRACT METHODS THAT NEED TO BE OVERRIDDEN IN THE SUBCLASS ^

datastore -- Sets/Returns the Data Store Parameters

  eval {
    ### set the data store ###
    $obj->datastore(@args);

    ### get the data store ###
    $dbh = $obj->datastore();
  };
  croak "Exception caught: $@" if $@;

Returns (and optionally sets) the data store of the object. This method throws Perl execeptions so use it with an eval block.

Setting the data store usually involves formatting the arguments into a DBI connect string and passing them to the Persistent::DBI::datastore method. Getting the data store usually involves returning whatever the Persistent::DBI::datastore method returns.

This method requires implementing.

Parameters:

Varies by implementation.

Returns:

Varies by implementation.

_get_sql_for_string_to_date -- Returns the SQL to Convert a Perl String into a RDBMS specific Date Format

  eval {
    $sql = $obj->_get_sql_for_string_to_date($dt_str);
  };
  croak "Exception caught: $@" if $@;

Returns the SQL to convert a Perl string into a database specific date format. This method is abstract and should be implemented in the children of this class. This method throws Perl execeptions so use it with an eval block.

This method requires implementing.

Parameters:

$dt_str

Date string in the following format:

  YYYY-MM-DD HH24:MI:SS

where YYYY is a 4 digit year, MM is a 2 digit month (1-12), DD is a 2 digit day (1-31), HH24 is the hour using a 24 hour clock (0-23), MI is minutes (0-59), and SS is seconds (0-59).

Returns:

$sql

SQL that will convert the date string into a valid date format for the database. For example, an Oracle database would need some SQL returned that looked something like this:

  "TO_DATE('$dt_str', 'YYYY-MM-DD HH24:MI:SS')"

_get_sql_for_date_to_string -- Returns the SQL to Convert a RDBMS specific Date Column into a Perl string

  eval {
    $sql = $obj->_get_sql_for_date_to_string($dt_col);
  };
  croak "Exception caught: $@" if $@;

Returns the SQL to convert a database specific date column into a Perl string that is formatted for a Persistent::DataType::DateTime constructor. This method is abstract and should be implemented in the children of this class. This method throws Perl execeptions so use it with an eval block.

This method requires implementing.

Parameters:

$dt_col

Name of a date column from the database to be converted into a string.

Returns:

$sql

SQL that will convert the date column into a valid format for the Persistent::DataType::DateTime constructor such as the following format:

  YYYY-MM-DD HH24:MI:SS

where YYYY is a 4 digit year, MM is a 2 digit month (1-12), DD is a 2 digit day (1-31), HH24 is the hour using a 24 hour clock (0-23), MI is minutes (0-59), and SS is seconds (0-59).

_get_column_name -- Returns the Column Name for the Attribute

  eval {
    $column = $obj->_get_column_name($field);
  };
  croak "Exception caught: $@" if $@;

Returns the name of the column from the table for the attribute of the object. This is database dependent since some databases are case-sensitive and others are not. This method is abstract and should be implemented in the children of this class. This method throws Perl execeptions so use it with an eval block.

This method requires implementing.

Parameters:

$field

Name of an attribute (or field) of an object.

Returns:

$column

Name of the column in the table that stores the attribute of the object.

SEE ALSO ^

Persistent, Persistent::Oracle, Persistent::MySQL, Persistent::mSQL, Persistent::DataType::DateTime

BUGS ^

This software is definitely a work in progress. So if you find any bugs please email them to me with a subject of 'Persistent Bug' at:

  winters@bigsnow.org

And you know, include the regular stuff, OS, Perl version, snippet of code, etc.

AUTHORS ^

  David Winters <winters@bigsnow.org>

COPYRIGHT ^

Copyright (c) 1998-2000 David Winters. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: