Alzabo (version 0.64) - Alzabo::Runtime::Table

Index



NAME

Alzabo::Runtime::Table - Table objects


SYNOPSIS

  my $table = $schema->table('foo');
  my $row = $table->row_by_pk( pk => 1 );
  my $row_cursor = $table->rows_where( where => [ C<Alzabo::Column> object, '=', 5 } );


DESCRIPTION

This object is able to create rows, either by making objects based on existing data or inserting new data to make new rows.

This object also implements a method of lazy column evaluation that can be used to save memory and database wear and tear, though it needs to be used carefully. Please see methods as well as LAZY COLUMN LOADING for details.


INHERITS FROM

Alzabo::Table

Note: all relevant documentation from the superclass has been merged into this document.


METHODS

Methods that return an Alzabo::Runtime::Row object

insert

Inserts the given values into the table. If no values are given for a primary key column and the column is sequenced then the values will be generated from the sequence.

Parameters

All other parameters given will be passed directly to the Alzabo::Runtime::Row->new method (such as the no_cache parameter).

Returns

A new Alzabo::Runtime::Row object.

Throws

Alzabo::Exception::Params

row_by_pk

The primary key can be either a simple scalar, as when the table has a single primary key, or a hash reference of column names to primary key values when the primary key is more than one column.

Parameters

All other parameters given will be passed directly to the Alzabo::Runtime::Row->new method (such as the no_cache parameter).

Returns

A new Alzabo::Runtime::Row object. If no rows in the database match the value(s) given then an empty list or undef will be returned (for list or scalar context).

Throws

Alzabo::Exception::Params

row_by_id

This method is useful for regenerating a row that has been saved by reference to its id (returned by the Alzabo::Runtime::Row->id method). This may be more convenient than saving a multi-column primary key when trying to maintain state in a web app, for example.

Parameters

A string representation of a row's id (as returned by the Alzabo::Runtime::Row->id method).

All other parameters given will be passed directly to the Alzabo::Runtime::Row->new method (such as the no_cache parameter).

Returns

A new Alzabo::Runtime::Row object. If no rows in the database match the value(s) given then an empty list or undef will be returned (for list or scalar context).

Common Parameters

A number of methods in this clas take the same parameters. These are documented below.

Methods that return an Alzabo::Runtime::RowCursor object

The rows_where and all_rows methods both return an Alzabo::Runtime::RowCursor object representing the results of the query. This is the case even for queries that end up returning one or zero rows, because Alzabo cannot know in advance how many rows these queries will return.

rows_where

A simple way to retrieve a row cursor based on one or more colum values. This does not handle any conditionals besides equality.

Parameters

All other parameters given will be passed directly to the Alzabo::Runtime::Row->new method (such as the no_cache parameter).

Given these items this method generates SQL that will retrieve a set of primary keys for the table.

Returns

An Alzabo::Runtime::RowCursor object representing the query.

all_rows

Simply returns all the rows in the table.

Parameters

All other parameters given will be passed directly to the Alzabo::Runtime::Row->new method (such as the no_cache parameter).

Returns

An Alzabo::Runtime::RowCursor object representing the query.

one_row

This method takes the exact same parameters as the rows_where method but instead of returning a cursor, it returns a single row. This object will be the row for the first id returned by the database.

potential_row

This method is used to create a new Alzabo::Runtime::PotentialRow object.

Parameters

Returns

A new Alzabo::Runtime::PotentialRow object.

Other Methods

row_count

Parameters

Returns

A scalar indicating how many rows the table has.

function and select

These two methods differ only in their return values.

Parameters

This method is used to call arbitrary SQL functions such as 'AVG' or 'MAX'. The function (or functions) should be the return values from the functions exported by the SQLMaker subclass that you are using. Please see Using SQL functions for more details.

Returns

function

The return value of this method is highly context sensitive.

If you only requested a single function ( DISTINCT(foo) ), then it returns the first value in scalar context and all the values in list context.

If you requested multiple functions ( AVG(foo), MAX(foo) ) then it returns a single array reference (the first row of values) in scalar context and a list of array references in list context.

select

This method always returns a new Alzabo::DriverStatement object containing the results of the query.

alias

Returns

An object which can be used in joins to allow a particular table to be involved in the join under multiple aliases. This allows for self-joins as well as more complex joins involving multiple aliases of a given table.

The object returned by this method is more or less identical to a table object in terms of the methods it supports. This includes methods that were generated by Alzabo::MethodMaker.

However, this object should not be used outside the context of a join query because the results will be unpredictable. In addition, the column objects that the alias returns should also not be used outside of the context of a join.

schema

Returns

The Alzabo::Runtime::Schema object to which this table belongs.

name

Returns

The name of the table.

column ($name)

Returns

The Alzabo::Runtime::Column object that matches the name given.

columns (@optional_list_of_column_names)

Returns

A list of Alzabo::Runtime::Column objects that match the list of names given. If no list is provided, then it returns all column objects for the table.

has_column ($name)

Returns

A true or false value depending on whether or not the column exists in the table.

primary_key

A primary key is one or more columns which must be unique in each row of the table. For a multi-column primary key, than the values of the columns taken in order must be unique. The order of a multi-column key is significant as most RDBMS's will create an index on the primary key using the same column order as is specified and column order usually matters in indexes.

Returns

An ordered list of column objects that make up the primary key for the table.

column_is_primary_key (Alzabo::Runtime::Column object)

This method is really only needed if you're not sure that column belongs to the table. Otherwise just call the Alzabo::Runtime::Column->is_primary_key method on the column object.

Returns

A boolean value indicating whether or not the column given is part of the table's primary key.

foreign_keys

Parameters

Returns

A list of Alzabo::Runtime::ForeignKey objects from the given column to the given table, if they exist. In scalar context, returns the first item in the list. There is no guarantee as to what the first item will be.

foreign_keys_by_table (Alzabo::Runtime::Table object)

Returns

A list of all the Alzabo::Runtime::ForeignKey objects to the given table. In scalar context, returns the first item in the list. There is no guarantee as to what the first item will be.

foreign_keys_by_column (Alzabo::Runtime::Column object)

Returns a list of all the Alzabo::Runtime::ForeignKey objects that the given column is a part of, if any. In scalar context, returns the first item in the list. There is no guarantee as to what the first item will be.

all_foreign_keys

Returns

A list of all the Alzabo::Runtime::ForeignKey objects for this table. In scalar context, returns the first item in the list. There is no guarantee as to what the first item will be.

index ($index_id)

This method expect an index id as returned by the Alzabo::Runtime::Index->id method.

Returns

The Alzabo::Runtime::Index object matching this id, if it exists in the table.

indexes

Returns

All the Alzabo::Runtime::Index objects for the table.


LAZY COLUMN LOADING

This concept was taken directly from Michael Schwern's Class::DBI module (credit where it is due).

This lazy loading is only done when caching is turned on. Otherwise, Alzabo always fetches data from the database when it is requested and does not store it locally in memory at all. In fact, trying to use lazy column loading without caching will simply slow things down.

By default, Alzabo::Runtime::Row objects only load data from the database as it is requested via the select method. This is stored internally in the object after being fetched. If the object is expired in the cache, it will erase this information and fetch it from the database again as needed.

This is good because it saves on memory and makes object creation quicker, but it is bad because you could potentially end up with one SQL call per column (excluding primary key columns, which are usually not fetched from the database).

This class provides two method to help you handle this potential problem. Basically these methods allow you to declare usage patterns for the table.

The first method, set_prefetch, allows you to specify a list of columns to be fetched immediately after object creation or after an object discovers it is expired in the cache. These should be columns that you expect to use extremely frequently.

The second method, add_group, allows you to group columns together. If you attempt to fetch one of these columns, then all the columns in the group will be fetched. This is useful in cases where you don't often want certain data, but when you do you need several related pieces.

Lazy column loading related methods

set_prefetch (Alzabo::Column objects)

Given a list of column objects, this makes sure that all Alzabo::Runtime::Row objects fetch this data as soon as they are created, as well as immediately after they know they have been expired in the cache.

NOTE: It is pointless (though not an error) to give primary key column here as these are always prefetched (in a sense).

Throws

Alzabo::Exception::Params

add_group (Alzabo::Column objects)

Given a list of Alzabo::Column objects, this method creates a group containing these columns. This means that if any column in the group is fetched from the database, then they will all be fetched. Otherwise column are always fetched singly. Currently, a column cannot be part of more than one group.

NOTE: It is pointless to include a column that was given to the set_prefetch method in a group here, as it always fetched as soon as possible.

Throws

Alzabo::Exception::Params

prefetch

This method primarily exists for use by the Alzabo::Runtime::Row class.

Returns

A list of column names (not objects) that should be prefetched.

group_by_column ($column_name)

This method primarily exists for use by the Alzabo::Runtime::Row class.

Returns

A list of column names representing the group that this column is part of. If the column named is not part of a group, only the name passed in is returned.


AUTHOR

Dave Rolsky, <autarch@urth.org>