NAME

Class::DBI::DB2 - Extensions to Class::DBI for DB2

SYNOPSIS

  package Music::DBI;
  use base 'Class::DBI::DB2';
  __PACKAGE__->set_db( 'Main', 'dbi:DB2:dbname', 'user', 'password', );

  package Artist;
  use base 'Music::DBI';
  __PACKAGE__->set_up_table('Artist');

  __PACKAGE__->autoinflate(dates => 'Time::Piece');

  # Somewhere else ...

  my $type = $class->column_type('column_name');
  my $colno = $class->column_no('column_name');
  my $nulls = $class->column_nulls('column_name');
  
  # ... see the Class::DBI documentation for details on Class::DBI usage

DESCRIPTION

Class::DBI::DB2 automates the setup of Class::DBI columns and primary key for IBM DB2.

This is an extension to Class::DBI that currently implements:

        * Automatic column name discovery.
        
        * Automatic primary key(s) detection.

        * Automatic column type detection (for use with autoinflate).

        * Automatic column number detection (where column order is needed).

Instead of setting Class::DBI as your base class, use this.

OBJECT METHODS

set_up_table

        __PACKAGE__->set_up_table("table_name");

An optional second argument can supply your own alias for your table name.

        __PACKAGE__->set_up_table("table_name", "table_alias");

Traditionally, to use Class::DBI, you have to set up the columns:

        __PACKAGE__->columns(All => qw/list of columns/);
        __PACKAGE__->columns(Primary => 'column_name');

While this allows for more flexibility if you're going to arrange your columns into a variety of groupings, sometimes you just want to create the 'all columns' list.

The columns call will extract the list of all the columns, and the primary key and set them up for you. It will die horribly if the table contains no primary key(s).

autoinflate

  __PACKAGE__->autoinflate(column_type => 'Inflation::Class');

  __PACKAGE__->autoinflate(timestamp => 'Time::Piece');
  __PACKAGE__->autoinflate(dates => 'Time::Piece');

This will automatically set up has_a() relationships for all columns of the specified type to the given class.

It is assumed that all classes passed will be able to inflate and deflate without needing extra has_a arguments, with the example of Time::Piece objects, that uses Time::Piece::DB2 (which you'll have to have installed!).

The special type 'dates' will autoinflate all columns of type date, time or timestamp.

create_table

        $class->create_table(q{
                name    VARCHAR(40)     NOT NULL,
                rank    VARCHAR(20)     NOT NULL,
                serial  INTEGER         NOT NULL
                PRIMARY KEY(name)
        });

This creates the table for the class, with the given schema. If the table already exists we do nothing.

A typical use would be:

        Music::CD->table('cd');
        Music::CD->create_table(q{
          cdid   INTEGER NOT NULL,
          artist INTEGER NOT NULL,
          title  VARCHAR(255) NOT NULL,
          year   DATE,
          PRIMARY KEY(cdid),
          CONSTRAINT TITLE_UNIQ UNIQUE (artist,title)
        });
        Music::CD->set_up_table;

drop_table

        $class->drop_table;

Drops the table for this class, if it exists.

column_type

        my $type = $class->column_type('column_name');

This returns the 'typename' of this table's 'column_name' (VARCHAR(20), INTEGER, etc.)

column_no

        my $colno = $class->column_no('column_name');

This returns the 'colno' of this table's 'column_name' (0..n) Useful when a column order is needed, for example, when loading a table from a flat-file.

column_nulls

        my $null = $class->column_nulls('column_name');

This returns the 'nulls' of this table's 'column_name' (Y,N)

AUTHOR

Mark Ferris, <mark.ferris@geac.com>.

COPYRIGHT

Copyright (C) 2004 Mark Ferris. All rights reserved.

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

SEE ALSO

Class::DBI. IBM DB2 (http://www-4.ibm.com/software/data/db2/)