NAME

Class::DBI::mysql - Extensions to Class::DBI for MySQL

SYNOPSIS

package Film.pm;
use base 'Class::DBI::mysql';
__PACKAGE__->set_db('Main', 'dbi:mysql:dbname', 'user', 'password');
__PACKAGE__->set_up_table("film");

# Somewhere else ...

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

my $tonights_viewing  = Film->retrieve_random;

DESCRIPTION

This is an extension to Class::DBI, containing several functions and optimisations for the MySQL database. Instead of setting Class::DBI as your base class, use this instead.

METHODS

set_up_table

__PACKAGE__->set_up_table("table_name");

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

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

Whilst 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. Well, this information is really simple to extract from MySQL itself, so why not just use that?

This 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, or has a composite primary key.

create_table

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

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   MEDIUMINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  artist MEDIUMINT UNSIGNED NOT NULL,
	title  VARCHAR(255),
	year   YEAR,
	INDEX (artist),
	INDEX (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 'type' of this table (VARCHAR(20), BIGINT, etc.)

enum_vals

my @allowed = $class->enum_vals('column_name');

This returns a list of the allowable values for an ENUM column.

retrieve_random

my $film = Film->retrieve_random;

This will select a random row from the database, and return you the relevant object.

(MySQL 3.23 and higher only, at this point)

COPYRIGHT

Copyright (C) 2001-2003 Tony Bowden. All rights reserved.

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

AUTHOR

Tony Bowden, <mysql@tmtm.com>.

SEE ALSO

Class::DBI. MySQL (http://www.mysql.com/)