Extensions to class::dbi for mysql
package Film; use base 'Class::DBI::mysql'; _\|_PACKAGE_\|_->set_db('Main', 'dbi:mysql:dbname', 'user', 'password'); _\|_PACKAGE_\|_->set_up_table("film"); _\|_PACKAGE_\|_->autoinflate(dates => 'Time::Piece'); # Somewhere else ... my $type = $class->column_type('column_name'); my @allowed = $class->enum_vals('column_name'); my $tonights_viewing = Film->retrieve_random;
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.
_\|_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.
_\|_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.
We currently assume that all classess passed will be able to inflate and deflate without needing extra has_a arguments, with the example of Time::Piece objects, which we deal with using Time::Piece::mysql (which you'll have to have installed!).
The special type 'dates' will autoinflate all columns of type date, datetime or timestamp.
$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;
$class->drop_table;
Drops the table for this class, if it exists.
my $type = $class->column_type('column_name');
This returns the 'type' of this table (\s-1VARCHAR\s0(20), \s-1BIGINT\s0, etc.)
my @allowed = $class->enum_vals('column_name');
This returns a list of the allowable values for an \s-1ENUM\s0 column.
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)
Class::DBI. MySQL (http://www.mysql.com/)
Tony Bowden
Please direct all correspondence regarding this module to:
[email protected]
Copyright (C) 2001-2005 Tony Bowden.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.