Alzabo (version 0.92)

Doc index

TOC | Top

NAME

Alzabo::Runtime::Table - Table objects

TOC | Top

SYNOPSIS

  my $table = $schema->table('foo');
  my $row = $table->row_by_pk( pk => 1 );
  my $row_cursor =
      $table->rows_where
          ( where =>
            [ Alzabo::Column object, '=', 5 ] );
TOC | Top

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. Please see the LAZY COLUMN LOADING section for details.

TOC | Top

INHERITS FROM

Alzabo::Table

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

TOC | Top

METHODS

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

All of these methods accept the "no_cache" parameter, which will be passed on to Alzabo::Runtime::Row->new.

TOC | Top

insert

Inserts the given values into the table. If no value is given for a primary key column and the column is "sequenced" then the primary key will be auto-generated.

It takes the following parameters:

This methods return a new Alzabo::Runtime::Row object.

Throws: Alzabo::Exception::Logic, Alzabo::Exception::NotNullable, Alzabo::Exception::Params

TOC | Top

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, for multi-column primary keys.

It takes the following parameters:

It 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::Logic, Alzabo::Exception::Params

TOC | Top

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.

Throws: Alzabo::Exception::Logic, Alzabo::Exception::Params

This method takes a single parameter, "row_id", which is the string representation of a row's id, as returned by the Alzabo::Runtime::Row->id_as_string() method.

It 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).

TOC | Top

Insert Handles

If you are going to be inserting many rows at once, it is more efficient to create an insert handle and re-use that. This is similar to how DBI allows you to create statement handles and execute them multiple times.

TOC | Top

insert_handle

This method takes the following parameters:

The return value of this method is an Alzabo::Runtime::InsertHandle object. This object has a single method, insert(). See the Alzabo::Runtime::InsertHandle docs for details.

Throws: Alzabo::Exception::NotNullable, Alzabo::Exception::Params

TOC | Top

Common Parameters

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

TOC | Top

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.

TOC | Top

rows_where

This method provides a simple way to retrieve a row cursor based on one or more colum values.

It takes the following parameters, all of which were described in the Common Parameters section.

It returns n Alzabo::Runtime::RowCursor object representing the query.

Throws: Alzabo::Exception::Logic, Alzabo::Exception::Params

TOC | Top

all_rows

This method simply returns all the rows in the table.

It takes the following parameters:

It returns an Alzabo::Runtime::RowCursor object representing the query.

Throws: Alzabo::Exception::Logic, Alzabo::Exception::Params

TOC | Top

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 row represents the first row returned by the database.

Throws: Alzabo::Exception::Logic, Alzabo::Exception::Params

TOC | Top

potential_row

This method is used to create a new Alzabo::Runtime::Row object, in the "potential" state.

It takes the following parameters.

Throws: Alzabo::Exception::Logic, Alzabo::Exception::Params

TOC | Top

Other Methods

This method returns a count of the rows in the table. It takes the following parameters:

TOC | Top

row_count

Throws: Alzabo::Exception::Logic, Alzabo::Exception::Params

TOC | Top

function and select

These two methods differ only in their return values.

They both take the following parameters:

This method is used to call arbitrary SQL functions such as 'AVG' or 'MAX', or to select arbitrary column data. 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.

Throws: Alzabo::Exception::Logic, Alzabo::Exception::Params

TOC | Top

function() return values

The return value of this method is highly context sensitive.

If you only requested a single element in your "select" parameter, such as "DISTINCT(foo)", then it returns the first value in scalar context and all the values as an array in list context.

If you requested multiple functions such as "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.

TOC | Top

select() return values

This method always returns a new Alzabo::DriverStatement object containing the results of the query. This object has an interface very similar to the Alzabo cursor interface, and has methods such as next(), next_as_hash(), etc.

TOC | Top

alias

This 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 to 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 aliased table object returns should also not be used outside of the context of a join.

TOC | Top

schema

Returns the Alzabo::Runtime::Schema object to which this table belongs.

TOC | Top

name

Returns the name of the table.

TOC | Top

column ($name)

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

An Alzabo::Exception::Params exception is throws if the table does not contain the column.

TOC | Top

columns (@optional_list_of_column_names)

If no arguments are given, returns a list of all Alzabo::Runtime::Column objects in the schema, or in a scalar context the number of such tables. If one or more arguments are given, returns a list of table objects with those names, in the same order given.

An Alzabo::Exception::Params exception is throws if the table does not contain one or more of the specified columns.

TOC | Top

has_column ($name)

Returns a voolean value indicating whether the column exists in the table.

TOC | Top

primary_key

In array context, return an ordered list of column objects that make up the primary key for the table. In scalar context, it returns the first element of that list.

TOC | Top

primary_key_size

The number of columns in the table's primary key.

TOC | Top

column_is_primary_key (Alzabo::Runtime::Column object)

Returns a boolean value indicating whether the column given is part of the table's primary key.

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

TOC | Top

foreign_keys

Thie method takes two parameters:

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

An Alzabo::Exception::Params exception is throws if the table does not contain the specified column.

TOC | Top

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, it returns the first item in the list. There is no guarantee as to what the first item will be.

TOC | Top

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, it returns the first item in the list. There is no guarantee as to what the first item will be.

An Alzabo::Exception::Params exception is throws if the table does not contain the specified column.

TOC | Top

all_foreign_keys

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

TOC | Top

index ($index_id)

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

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

An Alzabo::Exception::Params exception is throws if the table does not contain the specified index.

TOC | Top

has_index ($index_id)

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

Returns a boolean indicating whether the table has an index with the same id.

TOC | Top

indexes

Returns all the Alzabo::Runtime::Index objects for the table.

TOC | Top

attributes

A table's attributes are strings describing the table (for example, valid attributes in MySQL are thing like "TYPE = INNODB".

Returns a list of strings.

TOC | Top

has_attribute

This method can be used to test whether or not a table has a particular attribute. By default, the check is case-insensitive.

Returns a boolean value indicating whether the table has this particular attribute.

TOC | Top

comment

Returns the comment associated with the table object, if any.

TOC | Top

LAZY COLUMN LOADING

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

By default, Alzabo::Runtime::Row objects load all data from the database except blob type columns (columns with an unbounded length). This data is stored internally in the object after being fetched.

If you want to change what data is prefetched, there are two methods you can use.

The first method, set_prefetch(), allows you to specify a list of columns to be fetched immediately after object creation. 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.

TOC | Top

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.

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

TOC | Top

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

TOC | Top

prefetch

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

It returns a list of column names (not objects) that should be prefetched.

TOC | Top

group_by_column ($column_name)

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

It returns a list of column names representing the group that the given column is part of. If the column is not part of a group, only the name passed in is returned.

TOC | Top

AUTHOR

Dave Rolsky, <autarch@urth.org>


Table of Contents

- NAME
- SYNOPSIS
- DESCRIPTION
- INHERITS FROM
- METHODS
      - Methods that return an Alzabo::Runtime::Row object
      - insert
                  - values => $hashref
      - row_by_pk
                  - pk => $pk_val or \%pk_val
      - row_by_id
      - Insert Handles
      - insert_handle
                  - columns => $arrayref
                  - values => $hashref
      - Common Parameters
                  - where =>
                  - order_by => see below
                  - group_by => see below
                  - having => same as "where"
                  - limit => $limit or [ $limit, $offset ]
      - Methods that return an Alzabo::Runtime::RowCursor object
      - rows_where
                  - where
                  - order_by
                  - limit
      - all_rows
                  - order_by
                  - limit
      - one_row
      - potential_row
                  - values => \%values
      - Other Methods
      - row_count
                  - where
      - function and select
                  - select => $function or [ scalars, SQL functions and/or Alzabo::Column objects ]
                  - where
                  - order_by
                  - group_by
                  - limit
            - function() return values
            - select() return values
      - alias
      - schema
      - name
      - column ($name)
      - columns (@optional_list_of_column_names)
      - has_column ($name)
      - primary_key
      - primary_key_size
      - column_is_primary_key (Alzabo::Runtime::Column object)
      - foreign_keys
                  - column => Alzabo::Runtime::Column object
                  - table => Alzabo::Runtime::Table object
      - foreign_keys_by_table (Alzabo::Runtime::Table object)
      - foreign_keys_by_column (Alzabo::Runtime::Column object)
      - all_foreign_keys
      - index ($index_id)
      - has_index ($index_id)
      - indexes
      - attributes
      - has_attribute
                  - attribute => $attribute
                  - case_sensitive => 0 or 1 (defaults to 0)
      - comment
- LAZY COLUMN LOADING
      - Lazy column loading related methods
            - set_prefetch (Alzabo::Column objects)
            - add_group (Alzabo::Column objects)
      - prefetch
      - group_by_column ($column_name)
- AUTHOR