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