The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Fey::Table - Represents a table (or view)

SYNOPSIS

  my $table = Fey::Table->new( name => 'User' );

DESCRIPTION

This class represents a table or view in a schema. From the standpoint of SQL construction in Fey, a table and a view are basically the same thing.

METHODS

This class provides the following methods:

Fey::Table->new()

  my $table = Fey::Table->new( name => 'User' );

  my $table = Fey::Table->new( name    => 'ActiveUser',
                               is_view => 1,
                             );

This method constructs a new Fey::Table object. It takes the following parameters:

  • name - required

    The name of the table.

  • is_view - defaults to 0

    A boolean indicating whether this table is a view.

$table->name()

Returns the name of the table.

$table->is_view()

Returns a boolean indicating whether the object is a view.

$table->schema()

Returns the Fey::Schema object that this table belongs to. This is set when the table is added to a schema via the Fey::Schema->add_table() method.

$table->add_column($column)

This adds a new column to the schema. The column must be a Fey::Column object. Adding the column to the table sets the table for the column, so that $column->table() returns the correct object.

If the table already has a column with the same name, an exception is thrown.

$table->remove_column($column)

Remove the specified column from the table. If the column was part of any foreign keys, these are remvoed from the schema. If this column is part of any keys for the table, those keys will be removed. Removing the column unsets the table for the column.

The table can be specified either by name or by passing in a Fey::Column object.

$table->column($name)

Given a column name, this method returns the matching column object, if one exists.

$table->columns

$table->columns(@names)

When this method is called with no arguments, it returns all of the columns in the table. If given a list of names, it returns only the specified columns. If a name is given which doesn't match a column in the table, then it is ignored.

$table->candidate_keys()

Returns all of the candidate keys for the table as an array reference. Each element of the reference is in turn an array reference containing one or more columns.

$table->has_candidate_key(@columns)

This method returns true if the table has the given key. A key is identified as a list of names or Fey::Column objects.

$table->add_candidate_key(@columns)

This method adds a new candidate key to the table. The list of columns can contain either names or Fey::Column objects.

A candidate key is one or more columns which uniquely identify a row in that table.

If a name or column is specified which doesn't belong to the table, an exception will be thrown.

$table->remove_candidate_key(@columns)

This method removes a candidate key for the table. The list of columns can contain either names or Fey::Column objects.

If a name or column is specified which doesn't belong to the table, an exception will be thrown.

$table->primary_key()

This is a convenience method that simply returns the first candidate key added to the table. The key is returned as an array reference of column objects.

$table->alias(%p)

$table->alias($alias_name)

This method returns a new Fey::Table::Alias object based on the table. Any parameters passed to this method will be passed through to Fey::Table::Alias->new().

As a shortcut, if you pass a single argument to this method, it will be passed as the "alias_name" parameter to Fey::Table::Alias->new().

$table->is_alias()

Always returns false.

$table->aliased_column( $prefix, $column_name )

This method returns a new Fey::Column::Alias object. The alias's name is generated by concatenating the specified prefix and the column's real name.

$table->aliased_columns( $prefix, @column_names )

This method returns a list of new Fey::Column::Alias objects. The alias names are generated by concatenating the specified prefix and the column's real name.

$table->sql()

$table->sql_with_alias()

Returns the appropriate SQL snippet for the table.

$table->id()

Returns a unique identifier for the table.

ROLES

This class does the Fey::Role::TableLike and Fey::Role::Named roles.

AUTHOR

Dave Rolsky, <autarch@urth.org>

BUGS

See Fey for details on how to report bugs.

COPYRIGHT & LICENSE

Copyright 2006-2009 Dave Rolsky, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.