| 1 |
NAME |
| 2 |
Class::DBI::mysql - Extensions to Class::DBI for MySQL |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
package Film; |
| 6 |
use base 'Class::DBI::mysql'; |
| 7 |
__PACKAGE__->set_db('Main', 'dbi:mysql:dbname', 'user', 'password'); |
| 8 |
__PACKAGE__->set_up_table("film"); |
| 9 |
|
| 10 |
__PACKAGE__->autoinflate(dates => 'Time::Piece'); |
| 11 |
|
| 12 |
# Somewhere else ... |
| 13 |
|
| 14 |
my $type = $class->column_type('column_name'); |
| 15 |
my @allowed = $class->enum_vals('column_name'); |
| 16 |
|
| 17 |
my $tonights_viewing = Film->retrieve_random; |
| 18 |
|
| 19 |
DESCRIPTION |
| 20 |
This is an extension to Class::DBI, containing several functions and |
| 21 |
optimisations for the MySQL database. Instead of setting Class::DBI as |
| 22 |
your base class, use this instead. |
| 23 |
|
| 24 |
METHODS |
| 25 |
set_up_table |
| 26 |
__PACKAGE__->set_up_table("table_name"); |
| 27 |
|
| 28 |
Traditionally, to use Class::DBI, you have to set up the columns: |
| 29 |
|
| 30 |
__PACKAGE__->columns(All => qw/list of columns/); |
| 31 |
__PACKAGE__->columns(Primary => 'column_name'); |
| 32 |
|
| 33 |
Whilst this allows for more flexibility if you're going to arrange your |
| 34 |
columns into a variety of groupings, sometimes you just want to create |
| 35 |
the 'all columns' list. Well, this information is really simple to |
| 36 |
extract from MySQL itself, so why not just use that? |
| 37 |
|
| 38 |
This call will extract the list of all the columns, and the primary key |
| 39 |
and set them up for you. It will die horribly if the table contains no |
| 40 |
primary key, or has a composite primary key. |
| 41 |
|
| 42 |
autoinflate |
| 43 |
__PACKAGE__->autoinflate(column_type => 'Inflation::Class'); |
| 44 |
|
| 45 |
__PACKAGE__->autoinflate(timestamp => 'Time::Piece'); |
| 46 |
__PACKAGE__->autoinflate(dates => 'Time::Piece'); |
| 47 |
|
| 48 |
This will automatically set up has_a() relationships for all columns of |
| 49 |
the specified type to the given class. |
| 50 |
|
| 51 |
We currently assume that all classess passed will be able to inflate and |
| 52 |
deflate without needing extra has_a arguments, with the example of |
| 53 |
Time::Piece objects, which we deal with using Time::Piece::mysql (which |
| 54 |
you'll have to have installed!). |
| 55 |
|
| 56 |
The special type 'dates' will autoinflate all columns of type date, |
| 57 |
datetime or timestamp. |
| 58 |
|
| 59 |
create_table |
| 60 |
$class->create_table(q{ |
| 61 |
name VARCHAR(40) NOT NULL PRIMARY KEY, |
| 62 |
rank VARCHAR(20) NOT NULL DEFAULT 'Private', |
| 63 |
serial INTEGER NOT NULL |
| 64 |
}); |
| 65 |
|
| 66 |
This creates the table for the class, with the given schema. If the |
| 67 |
table already exists we do nothing. |
| 68 |
|
| 69 |
A typical use would be: |
| 70 |
|
| 71 |
Music::CD->table('cd'); |
| 72 |
Music::CD->create_table(q{ |
| 73 |
cdid MEDIUMINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, |
| 74 |
artist MEDIUMINT UNSIGNED NOT NULL, |
| 75 |
title VARCHAR(255), |
| 76 |
year YEAR, |
| 77 |
INDEX (artist), |
| 78 |
INDEX (title) |
| 79 |
}); |
| 80 |
Music::CD->set_up_table; |
| 81 |
|
| 82 |
drop_table |
| 83 |
$class->drop_table; |
| 84 |
|
| 85 |
Drops the table for this class, if it exists. |
| 86 |
|
| 87 |
column_type |
| 88 |
my $type = $class->column_type('column_name'); |
| 89 |
|
| 90 |
This returns the 'type' of this table (VARCHAR(20), BIGINT, etc.) |
| 91 |
|
| 92 |
enum_vals |
| 93 |
my @allowed = $class->enum_vals('column_name'); |
| 94 |
|
| 95 |
This returns a list of the allowable values for an ENUM column. |
| 96 |
|
| 97 |
retrieve_random |
| 98 |
my $film = Film->retrieve_random; |
| 99 |
|
| 100 |
This will select a random row from the database, and return you the |
| 101 |
relevant object. |
| 102 |
|
| 103 |
(MySQL 3.23 and higher only, at this point) |
| 104 |
|
| 105 |
SEE ALSO |
| 106 |
Class::DBI. MySQL (http://www.mysql.com/) |
| 107 |
|
| 108 |
AUTHOR |
| 109 |
Tony Bowden |
| 110 |
|
| 111 |
BUGS and QUERIES |
| 112 |
Please direct all correspondence regarding this module to: |
| 113 |
[email protected] |
| 114 |
|
| 115 |
COPYRIGHT AND LICENSE |
| 116 |
Copyright (C) 2001-2005 Tony Bowden. |
| 117 |
|
| 118 |
This program is free software; you can redistribute it and/or modify it under |
| 119 |
the terms of the GNU General Public License; either version 2 of the License, |
| 120 |
or (at your option) any later version. |
| 121 |
|
| 122 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 123 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 124 |
FOR A PARTICULAR PURPOSE. |
| 125 |
|