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->explicit_join(
[ O => 'folks', A => 'favorites' ]
"O.fid = A.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
anddelete_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 [, $filters ] )
-
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$filters
is a list of filters to apply to the tables, respectively.This method tries to automatically join tables based upon their
LINK
indexes. This is process is done as intelligently as I could divine, but cannot join a table to itself--at least not currently. It will also use the first indexes it finds between two tables and will not use more than one index during a join. Finally, it will not look for links between tables that will result in a circular joining.For more complicated joining, try
explicit_join
. - $join = $source->explicit_join($tables, $on_exprs [, $filter ])
-
This performs a more explicit form of the join operation. This allows the user to pick the fields joined upon in the case that there is no key constraint to guide the Persist system or when such implicit joins are otherwise inappropriate. The arguments to this method are a little more complicated than most, but should make sense:
- $tables
-
This argument is an array reference with the appearance of a hash. There should be an even number of elements in the array. The even indexed attributes are table name aliases, which are followed by the odd attributes which are table names. This allows a table to be explicitly joined to itself. Here is an example,
[ O => 'Folks', A => 'Favorites' ]
For those that might not know, the arrow (=>) is equivalent to comma (,) except that it causes the preceding value to be stringified (i.e., interpreted as a string). (Wacky Perl syntactic sugar. ;)
- $on_exprs
-
This is an array reference to a set of strings. There should be one expression for each join. Joins are performed in the order they are specified in
$tables
. The first two tables given will be joined first by the first expression in the first element of$on_exprs
. The third table will be joined to the first two by the second element. The fourth table by the third element, etc. The expressions should use the table name aliases given in the$table
argument.If there's only one expression, i.e. only two tables in the join, you may omit the array reference and just include the expression as a string. If a full cross-product (unqualified join) is desired, then the
undef
value should be used in place of the expression. WARNING: A full cross-product join can be extremely inefficient in some contexts. - $filter (optional)
-
This string has the same purpose as the
$filters
argument to the otherjoin
method. However, this method is always a single string. This variable should also use the table name aliases given in the$table
argument.
- $table = $source->table($table [, $filter ])
-
Returns a reference to a Persist::Table object which may be used to access columns of the table.
- $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.