1Class::DBI::mysql(3)  User Contributed Perl Documentation Class::DBI::mysql(3)
2
3
4

NAME

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

SYNOPSIS

9         package Film;
10         use base 'Class::DBI::mysql';
11         __PACKAGE__->set_db('Main', 'dbi:mysql:dbname', 'user', 'password');
12         __PACKAGE__->set_up_table("film");
13
14         __PACKAGE__->autoinflate(dates => 'Time::Piece');
15
16         # Somewhere else ...
17
18         my $type = $class->column_type('column_name');
19         my @allowed = $class->enum_vals('column_name');
20
21         my $tonights_viewing  = Film->retrieve_random;
22

DESCRIPTION

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

METHODS

29       set_up_table
30
31               __PACKAGE__->set_up_table("table_name");
32
33       Traditionally, to use Class::DBI, you have to set up the columns:
34
35               __PACKAGE__->columns(All => qw/list of columns/);
36               __PACKAGE__->columns(Primary => 'column_name');
37
38       Whilst this allows for more flexibility if you're going to arrange your
39       columns into a variety of groupings, sometimes you just want to create
40       the 'all columns' list. Well, this information is really simple to
41       extract from MySQL itself, so why not just use that?
42
43       This call will extract the list of all the columns, and the primary key
44       and set them up for you. It will die horribly if the table contains no
45       primary key, or has a composite primary key.
46
47       autoinflate
48
49         __PACKAGE__->autoinflate(column_type => 'Inflation::Class');
50
51         __PACKAGE__->autoinflate(timestamp => 'Time::Piece');
52         __PACKAGE__->autoinflate(dates => 'Time::Piece');
53
54       This will automatically set up has_a() relationships for all columns of
55       the specified type to the given class.
56
57       We currently assume that all classess passed will be able to inflate
58       and deflate without needing extra has_a arguments, with the example of
59       Time::Piece objects, which we deal with using Time::Piece::mysql (which
60       you'll have to have installed!).
61
62       The special type 'dates' will autoinflate all columns of type date,
63       datetime or timestamp.
64
65       create_table
66
67               $class->create_table(q{
68                       name    VARCHAR(40)     NOT NULL PRIMARY KEY,
69                       rank    VARCHAR(20)     NOT NULL DEFAULT 'Private',
70                       serial  INTEGER         NOT NULL
71               });
72
73       This creates the table for the class, with the given schema. If the ta‐
74       ble already exists we do nothing.
75
76       A typical use would be:
77
78               Music::CD->table('cd');
79               Music::CD->create_table(q{
80                 cdid   MEDIUMINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
81                 artist MEDIUMINT UNSIGNED NOT NULL,
82                       title  VARCHAR(255),
83                       year   YEAR,
84                       INDEX (artist),
85                       INDEX (title)
86               });
87               Music::CD->set_up_table;
88
89       drop_table
90
91               $class->drop_table;
92
93       Drops the table for this class, if it exists.
94
95       column_type
96
97               my $type = $class->column_type('column_name');
98
99       This returns the 'type' of this table (VARCHAR(20), BIGINT, etc.)
100
101       enum_vals
102
103               my @allowed = $class->enum_vals('column_name');
104
105       This returns a list of the allowable values for an ENUM column.
106
107       retrieve_random
108
109               my $film = Film->retrieve_random;
110
111       This will select a random row from the database, and return you the
112       relevant object.
113
114       (MySQL 3.23 and higher only, at this point)
115

SEE ALSO

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

AUTHOR

120       Tony Bowden
121

BUGS and QUERIES

123       Please direct all correspondence regarding this module to:
124         bug-Class-DBI-mysql@rt.cpan.org
125
127         Copyright (C) 2001-2005 Tony Bowden.
128
129         This program is free software; you can redistribute it and/or modify it under
130         the terms of the GNU General Public License; either version 2 of the License,
131         or (at your option) any later version.
132
133         This program is distributed in the hope that it will be useful, but WITHOUT
134         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
135         FOR A PARTICULAR PURPOSE.
136
137
138
139perl v5.8.8                       2005-09-03              Class::DBI::mysql(3)
Impressum