NAME

Persist::Source - Main class used for accessing Persist data

SYNOPSIS

  use Persist qw(:constants);
  use Persist::Source;

  $source = new Persist::Source(...);

  $source->new_table('folks', {
              folkid        => [ AUTONUMBER ],
              name          => [ VARCHAR, 20 ],
              age           => [ INTEGER ] }, [
              [ PRIMARY, [ 'folkid' ] ],
              [ UNIQUE, [ 'name' ] ] ]);

  $source->new_table('favorites', {
              favid         => [ AUTONUMER ],
              folkid        => [ INTEGER ],
              color         => [ VARCHAR, 20 ] }, [
              [ PRIMARY, [ 'favid' ] ],
              [ UNIQUE, [ 'folkid', 'color' ] ],
              [ LINK, [ 'folkid' ], 'folks', [ 'folkid' ] ] ]);

  $table = $source->folks;
  $join = $source->join([ 'folks', 'favorites' ]);
  $join2 = $source->join(
              [ 'folks', 'favorites' ]
			  "folks.fid = favorites.fid");

DESCRIPTION

This abstraction is the core of the Persist framework. This provides an easy way to access persistent data.

$source = new Persist::Source($driver, @args)

This connects to a Persist data source using the driver named $driver. The @args argument is a special case to the mixed parameter passing syntax use by Persist. Instead of being a literal argument, @args is a set of named (not mixed) arguments that are passed directly to the drivers.

$test = $source->is_dba

Returns true if this object is permitted to use the create_source and delete_source methods.

@conn_args = $source->new_source(@args)

Creates a new persistent source and returns the arguments required to pass to the driver to access the new source. See driver documentation for the arguments required.

The @args list is a special case to the mixed argument syntax of Persist. These are required to be named arguments as they are passed directly on to the driver itself.

$source->delete_source(@args)

Deletes a persistent source. See driver documentation for the arguments required.

The @args list is a special case to the mixed argument syntax of Persist. These are required to be named arguments as they are passed directly on to the driver itself.

$source->new_table($table, \%columns, \@indexes)

Creates a new table in the persistent source. The $table is the name of the table to create. The %columns is a hash where the keys are the column names and the values are the types. The types are specified as an array reference with the first element being the type constant and the rest arguments to that type constant.

The @indexes is an array of array references to each index definition. The first element in an index definition is the index type constant and the rest are arguments to the index definition.

See Persist for information on type and index arguments.

$source->delete_table($table)

Delete the table naemd by $table.

@tables = $source->tables

Returns the names of all available tables in the database.

$join = $source->join(\@tables [, $on, $filter, \@order, $offset, $limit ] )

Returns a reference to a Persist::Join object which may be used to access columns of the joined tables. $tables is a list of tables to join and $filter is a list of filters to apply to the tables, respectively.

If the @on parameter is specified, then the tables will be joined accordinging to user preference. If @on is missing, the driver will attempt to automatically join the given tables. Automatic joining only works when no tables are repeated in the @tables list (repeating tables will require the user to use the @on option.

Automatic joins are performed based upon each table's LINK indexes. This is process is done as intelligently as I know how. It will use the first index it finds between two tables to join them and will not use more than one index during a join. Once all tables have been joined according to the indexes found, the database operation is performed. Automatic joining works well when there is only one index linking two tables. However, if multiple indexes join two tables, the driver may not pick the index you want, so use @on instead.

@tables

This is the list of tables to draw records from.

$filter (optional)

This is a single filter to apply to the tables. Since a join might include two tables with columns of the same name, you need to make sure you use unambiguous column names. This is done by prepending a table identifier to the column. The table identifier can be the name of the table (when the same table is not used twice), the numeric index of the table in @tables plus one, or the name of the table with the occurance number on the end. The table identifier should be separated from the column identifier by a period.

For example, if @tables were set to [ 'A', 'B', 'A' ], these would all be identical filters:

"1.x = 2.x and 1.x = 3.y"
"1.x = B1.x and 1.x = 3.y"
"A1.x = B.x and A1.x = A2.y"
"A1.x = 2.x and A1.x = A2.y"
@order (optional)

A list of columns that will be used for ordering. The list may also contain the ASCENDING or DESCENDING constants following a column name to specify the direction of the ordering. Without these constants the default is ASCENDING. (Note that ASC and DESC, respectively, are aliases to these constants.) In this way, it may be convenient to use hash notation to make these a little more explicit.

$offset (optional)

This is the number of records to skip before returning a result. If this causes every matching row to be skipped, then no rows will be returned. No records are skipped if this is unspecified.

$limit (optional)

This is the number of records to be returned. Once next has been called $limit times on the table, next will return no more records. All records returned until the end of the table is reached if this is either unspecified or set to 0--i.e., you cannot tell the join to return 0 records.

$table = $source->table($table [, $filter, \@order, $offset, $limit ])

Returns a reference to a Persist::Table object which may be used to access columns of the table.

$table

The name of the table to retrieve information about.

$filter (optional)

The filter to use to pick out which records the table object will contain.

@order (optional)

A list of columns that will be used for ordering. The list may also contain the ASCENDING or DESCENDING constants following a column name to specify the direction of the ordering. Without these constants the default is ASCENDING. (Note that ASC and DESC, respectively, are aliases to these constants.) In this way, it may be convenient to use hash notation to make these a little more explicit. For example:

-order => [ rank => DESCENDING, lastname => ASCENDING, firstname => ASCENDING ]
$offset (optional)

This is the (0-based) index of the first record to include in the results. If this index points to an index after the last record that would be returned, the table will be empty.

$limit (optional)

This is the number of records to be returned. Once next has been called $limit times on the table, next will return no more records.

$table = $source-><table>( [ $filter ] )

A shortcut to $source->table('<table>').

$source->delete($table [, $filter ])

Deletes all records matching the given filter.

WARNING: An undefined filter will delete all rows from the table.

Returns the number of rows deleted.

$source->insert($table, \%values)

Inserts a new row into the table named $table with the values given in \%values.

Returns 1 on success.

$rows = $source->update($table, \%set [, $filter ] )

Updates one or more rows in the table named $table. Only rows matching firlster will be updated and will be set to the values in %set.

WARNING: An undefined filter will update all rows.

Returns the number of rows altered.

SEE ALSO

Persist, Persist::Join, Persist::Driver, Persist::Table

AUTHOR

Andrew Sterling Hanenkamp, <hanenkamp@users.sourceforge.net>

COPYRIGHT AND LICENSE

Copyright (c) 2003, Andrew Sterling Hanenkamp
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions 
are met:

  * Redistributions of source code must retain the above copyright 
    notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright 
    notice, this list of conditions and the following disclaimer in 
    the documentation and/or other materials provided with the 
    distribution.
  * Neither the name of the Contentment nor the names of its 
    contributors may be used to endorse or promote products derived 
    from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.