Adding Columns
Adding columns
package OME::SemanticType; use strict; use OME; our $VERSION = $OME::VERSION; use OME::DBObject; use base qw(OME::DBObject); __PACKAGE__->newClass(); __PACKAGE__->setDefaultTable('semantic_types'); __PACKAGE__->setSequence('semantic_type_seq'); __PACKAGE__->addPrimaryKey('semantic_type_id'); __PACKAGE__->addColumn(name => 'name', { SQLType => 'varchar(64)', NotNull => 1, Unique => 1, }); __PACKAGE__->addColumn(granularity => 'granularity', { SQLType => 'char(1)', NotNull => 1, Check => "(granularity in ('G','D','I','F'))", }); __PACKAGE__->addColumn(description => 'description',{SQLType => 'text'}); __PACKAGE__->hasMany('semantic_elements', 'OME::SemanticType::Element' => 'semantic_type');
OME::SemanticType
definition
After these preliminary calls, the package defines the columns
in the database table. These columns can fall into one of
four categories: primary keys, standard
columns, has-one foreign keys, and has-many
foreign keys. Each column has at least one alias, which
is how other pieces of code refer to the column. Most of the
time, the alias and the name of the underlying database column
are identical, but they don't have to be. Has-many
foreign keys are defined by the hasMany
method.
Standard columns and has-one foreign keys are both defined by
the addColumn
method.
Primary keys are defined by the addPrimaryKey
method. The only parameter to this method is the primary
key's column name; the database column is automatically
assigned an SQL type of integer
(or a
database-vendor-specific type appropriate to primary keys),
and it is automatically given NOT NULL
and PRIMARY KEY
SQL clauses. The primary key's
alias is always id
.
Standard columns are defined by the addColumn
method. This method defines a database column in the default
table and creates an accessor/mutator method in the current
package to read that column. The name of this method is the
same as the column's alias. For standard columns, the
addColumn
method takes in three parameters: the
column's alias, the name of the database column, and a hash of
SQL options. These SQL options provide the necessary
information to completely define the database column. A list
of valid SQL options can be found in
the OME::DBObject
reference page.
The SQL options parameter can be left out; doing so adds an
accessor/mutator method, but prevents the database creation
code from modifying the table structure. This is use-ful if
you want to define more than one accessor/mutator method for a
given column. If the SQL options were specified for both, the
database creation code would try to place both column
definitions into the generated CREATE TABLE
statement. Defining two col-umns with the same name is
erroneous SQL, so the database creation code would throw an
error trying to create the class.