NAME

DbFramework::Table - Table class

SYNOPSIS

use DbFramework::Table;

$t = new DbFramework::Table($name,,,$dbh,\@forms);
$t->init_db_metadata;
$dbh = $t->dbh($dbh);
$pk   = $t->is_identified_by($pk);
@fks  = @{$t->has_foreign_keys_l};
@keys = @{$t->is_accessed_using_l};
@a    = $t->get_attributes(@names);
@n    = $t->get_attribute_names;
$html = $t->as_html_form;
$s    = $t->as_string;
$sql  = $t->as_sql;
$rows = $t->delete($conditions);
$pk   = $t->insert(\%values);
$rows = $t->update(\%values,$conditions);
@lol  = $t->select(\@columns,$conditions,$order);
$tmpl = $t->set_templates(\%templates);
$fill = $t->fill_template($template);
do_something if $t->in_foreign_key($attribute);
do_something if $t->in_key($attribute);
do_something if $t->in_primary_key($attribute);
do_something if $t->in_any_key($attribute);
@a = $t->non_key_attributes;
$t->read_form($form,$dir);

DESCRIPTION

A DbFramework::Table object represents a database table (entity).

Forms and Templates

A table can have a number of associated forms. Each form defines a number of templates which can be used for data entry or display. Forms associated with a table can be configured when calling new() and/or by including the Perl code

$self->form_h([form => 'formfile' ...]);

in the file /usr/local/etc/dbframework/forms/$db/$table/config.pl. form is a name used to identify the form and formfile is the name of the file containing the form definition.

Form files should be place in /usr/local/etc/dbframework/forms/$db/$table. They should contain a list of (template name,template) pairs, where template name is a name used to identify the template and template is an HTML template. Templates can be initialised by reading form files with the read_form() method. The special tags which can be used in a template are described in the fill_template() method.

All lines in config.pl and formfile matching the regular expression /^\s*#/ will be treated as comments and ignored.

SUPERCLASSES

DbFramework::DefinitionObject

DbFramework::DataModelObject

CLASS METHODS

new($name,\@attributes,$primary,$dbh,\@forms)

Create a new DbFramework::Table object. $dbh is a DBI database handle which refers to a database containing a table named $table. @attribues is a list of DbFramework::Attribute objects. $primary is a DbFramework::PrimaryKey object. @attributes and $primary can be omitted if you plan to use the init_db_metadata() object method (see below). @forms is a list of (form name,file) pairs. Form files should contain a list of (template name,template) pairs. (see the object method read_form() below.)

OBJECT METHODS

Foreign keys in a table can be accessed using the HAS_FOREIGN_KEYS_L and HAS_FOREIGN_KEYS_H attributes. Note that foreign key objects will not be created automatically by calling init_db_metadata() on a table object. If you want to automatically create foreign key objects for your tables you should use call init_db_metadata() on a data model object (see "init_db_metadata()" in DbFramework::Datamodel). Other keys (indexes) defined for a table can be accessed using the IS_ACCESSED_USING_L attribute. See "AUTOLOAD()" in DbFramework::Util for the accessor methods for these attributes.

is_identified_by($primary)

$primary is a DbFramework::PrimaryKey object. If supplied, sets the table's primary key to $primary. Returns a DbFramework::PrimaryKey object with is the table's primary key.

get_attributes(@names)

Returns a list of DbFramework::Attribute objects. @names is a list of attribute names to return. If @names is undefined, all attributes associated with the table are returned.

dbh($dbh)

$dbh is a DBI database handle. If supplied, sets the database handle associated with the table. Returns the database handle associated with the table.

get_attribute_names()

Returns a list of attribute names for the table.

as_html_form()

Returns HTML form fields for all attributes in the table.

in_foreign_key($attribute)

$attribute is a DbFramework::Attribute object. Returns true if $attribute is a part of any foreign key in the table.

in_primary_key($attribute)

$attribute is a DbFramework::Attribute object. Returns true if $attribute is a part of the primary key in the table.

in_key($attribute)

$attribute is a DbFramework::Attribute object. Returns true if $attribute is a part of a key (index) in the table.

in_any_key($attribute)

$attribute is a DbFramework::Attribute object. Returns true if $attribute is a part of a key (index), a primary key or a foreign key in the table.

non_key_attributes()

Returns a list of DbFramework::Attribute objects which are not members of any key, primary key or foreign key.

as_string()

Return table details as a string.

init_db_metadata()

Returns an initialised DbFramework::Table object for the table matching this object's name() in the database referenced by dbh().

as_sql()

Returns a string which can be used to create a table in an SQL 'CREATE TABLE' statement.

delete($conditions)

DELETE rows FROM the table associated with this object WHERE the conditions in $conditions are met. Returns the number of rows deleted.

insert(\%values)

INSERT INTO the table columns corresponding to the keys of %values the VALUES corresponding to the values %values. Returns the primary key of the inserted row if it is a Mysql 'AUTO_INCREMENT' column.

update(\%values,$conditions)

UPDATE the table SETting the columns matching the keys in %values to the values in %values WHERE $conditions are met. Returns the number of rows updated.

select(\@columns,$conditions,$order)

Returns a list of lists of values by SELECTing rows FROM the table WHERE $conditions are met ORDERed BY $order.

fill_template($name,\%values)

Return the filled HTML template named $name. A template can contain special placeholders representing columns in a database table. Placeholders in $template can take the following forms:

<DbField table.column>
<DbField table.column value=value>
<DbField table.column value=value type=type>

If the table's name() matches table in a DbField placeholder, the placeholder will be replaced with the corresponding HTML form field for the column named column with arguments value and type (see "html_form_field()" in DbFramework::Attribute). If %values is supplied placeholders will have the values in %values added where a key in %values matches a column name in the table.

<DbFKey table.fk_name[,column...]>

If the table's name() matches table in a DbFKey placeholder, the placeholder will be replaced with the a selection box containing values and labels from the primary key columns in the related table. Primary key attribute values in %values will be used to select the default item in the selection box.

<DbValue table.column[,column...]>

If the table's name() matches table in a DbValue placeholder, the placeholder will be replaced with the values in %values where a key in %values matches a column name in the table.

set_templates(%templates)

Adds the contents of the files which are the values in %templates as templates named by the keys in %templates. Returns a reference to a hash of all templates.

read_form($form,$dir)

Configure templates by evaluating the contents of $dir/$db/$table/$form where $dir is a directory (/usr/local/etc/dbframework by default), $db is the name of the database containing the table, $table is the name of the table and $form is the name of the form containing the template definitions. See "Forms and Templates".

as_html_heading()

Returns a string for use as a table heading row in an HTML table;

SEE ALSO

DbFramework::DefinitionObject, DbFramework::Attribute and DbFramework::DataModelObject.

AUTHOR

Paul Sharpe <paul@miraclefish.com>

COPYRIGHT

Copyright (c) 1997,1998 Paul Sharpe. England. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.