| DbFramework documentation | Contained in the DbFramework distribution. |
DbFramework::Attribute - Attribute class
use DbFramework::Attribute; my $a = new DbFramework::Attribute($name,$default_value,$is_optional,$data_type); $name = $a->name($name); $value = $a->default_value($value); $opt = $a->is_optional($boolean); $type = $a->references($data_type); $bgc = $a->bgcolor($bgcolor); $sql = $a->as_sql; $s = $a->as_string; $html = $a->as_html_form_field($value,$type); $html = $a->as_html_heading($bgcolor);
A DbFramework::Attribute object represents an attribute (column) in a table (entity).
DbFramework::Util
Create a new DbFramework::Attribute object. $name is the name of the attribute. $default_value is the default value for the attribute. $is_optional should be set to true if the attribute is optional or false. $data_type is a DbFramework::DataType object.
If $name is supplied sets the attribute name. Returns the attribute name.
If $value is supplied sets the default value for the attribute. $value should be compatible with the the data type set by references(). Returns the default value for the attribute.
If $boolean is supplied sets the optionality of the attribute (i.e. whether it can contain NULLs.) $boolean should evaluate to true or false. Returns the optionality of the attribute.
If $data_type is supplied sets the data type of the attribute. $data_type is an ANSII data type object i.e. DbFramework::DataType::ANSII or a driver-specific object e.g. DbFramework::DataType::Mysql. Returns the data type object.
If $color is supplied sets the background colour for HTML table headings returned by as_html_heading(). Returns the current background colour.
Returns a string which can be used to create a column in an SQL 'CREATE TABLE' statement. $dbh is a DBI handle.
Returns an HTML form field representation of the attribute. The HTML field type produced depends on the name of object returned by data_type(). This can be overidden by setting $type. $value is the default value to be entered in the generated field.
Return attribute details as a text string.
Returns a string for use as a column heading cell in an HTML table. $bgcolor is the background colour to use for the heading.
DbFramework::DataType::ANSII and DbFramework::DataType::Mysql.
Paul Sharpe <paul@miraclefish.com>
Copyright (c) 1997,1998,1999 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.
| DbFramework documentation | Contained in the DbFramework distribution. |
package DbFramework::Attribute; use strict; use base qw(DbFramework::Util); use vars qw($_DEBUG $NAME $DEFAULT_VALUE $IS_OPTIONAL $REFERENCES $BGCOLOR); use Alias; ## CLASS DATA my %fields = ( NAME => undef, DEFAULT_VALUE => undef, IS_OPTIONAL => undef, # Attribute 0:N References 0:1 DefinitionObject (DataType) REFERENCES => undef, BGCOLOR => '#007777', ); ##----------------------------------------------------------------------------- ## CLASS METHODS ##-----------------------------------------------------------------------------
sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = bless { _PERMITTED => \%fields, %fields, }, $class; $self->name(shift); $self->default_value(shift); $self->is_optional(shift); $self->references(shift); return $self; } ##----------------------------------------------------------------------------- ## OBJECT METHODS ##-----------------------------------------------------------------------------
sub as_sql { my($self,$dbh) = (attr shift,shift); my $sql = "$NAME "; $sql .= $REFERENCES->name; $sql .= '(' . $REFERENCES->length . ')' if $REFERENCES->length; $sql .= " NOT NULL" unless $IS_OPTIONAL; $sql .= " DEFAULT " . $dbh->quote($DEFAULT_VALUE) if $DEFAULT_VALUE; $sql .= ' ' . $REFERENCES->extra if $REFERENCES->extra; return $sql; } ##-----------------------------------------------------------------------------
sub as_html_form_field { my $self = attr shift; my $value = defined($_[0]) ? $_[0] : ''; my $type = defined($_[1]) ? uc($_[1]) : $REFERENCES->name; my $length = $REFERENCES->length || 10; my $html; SWITCH: { $type =~ /(INT|DATE)/ && do { $html = qq{<INPUT NAME="$NAME" VALUE="$value" SIZE=10 TYPE="text">}; last SWITCH; }; $type =~ /CHAR$/ && do { $html = qq{<INPUT NAME="$NAME" VALUE='$value' SIZE=30 TYPE="text" MAXLENGTH=$length>}; last SWITCH; }; $type eq 'TEXT' && do { $value =~ s/'//g; # remove quotes $html = qq{<TEXTAREA COLS=60 NAME="$NAME" ROWS=4>$value</TEXTAREA>}; last SWITCH; }; $type eq 'BOOLEAN' && do { my $y = qq{Yes <INPUT TYPE="RADIO" NAME="$NAME" VALUE=1}; my $n = qq{No <INPUT TYPE="RADIO" NAME="$NAME" VALUE=0}; $html = $value ? qq{$y CHECKED>\n$n>\n} : qq{$y>\n$n CHECKED>\n}; last SWITCH; }; # default my $size = ($length < 30) ? $length : 30; $html = qq{<INPUT MAXLENGTH=$size NAME='$NAME' VALUE='$value' SIZE=$size TYPE="text">}; } return $html; } ##-----------------------------------------------------------------------------
sub as_string { my $self = attr shift; my $s = "$NAME(" . $REFERENCES->name; $s .= ' ('. $REFERENCES->length . ')' if $REFERENCES->length; $s .= " '$DEFAULT_VALUE'" if $DEFAULT_VALUE; $s .= ' NOT NULL' unless $IS_OPTIONAL; $s .= ' ' . $REFERENCES->extra if $REFERENCES->extra; #print " $FUNCTION" if $FUNCTION; return "$s)\n"; } ##---------------------------------------------------------------------------- sub _input_template { my($self,$t_name) = (attr shift,shift); return qq{<TD><DbField ${t_name}.$NAME></TD>}; } ##---------------------------------------------------------------------------- sub _output_template { my($self,$t_name) = (attr shift,shift); return qq{<TD BGCOLOR='$BGCOLOR'><DbValue ${t_name}.$NAME></TD>}; } #-----------------------------------------------------------------------------
sub as_html_heading { my $self = attr shift; my $bgcolor = shift || $BGCOLOR; my $text = $NAME; $text .= ' ('.$REFERENCES->extra.')' if $REFERENCES->extra; qq{<TD BGCOLOR='$bgcolor'>$text</TD>}; } 1;