Class:...::mysql(3pm)
π Class::DBI::mysql β the manual page, but make it make sense π
okay bestie so you found yourself on Class:...::mysql(3pm), a lil' page from the User Contributed Perl Documentation collection, and you're like "what is this". Say less. I got you. π«Ά
β¨ NAME β¨
Class::DBI::mysql β Extensions to Class::DBI for MySQL. that's it, that's the whole vibe. it's the glow-up your Class::DBI class gets when your database is MySQL. π
β¨ SYNOPSIS β¨
here's the whole thing in action, no cap:
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;
look at her go. retrieve_random is literally the main character of movie night. π¬
β¨ DESCRIPTION β¨
so this is an extension to Class::DBI β it's got a whole bunch of functions and optimisations specifically for the MySQL database. and here's the tea: instead of setting Class::DBI as your base class, you just use this one instead. same energy, more MySQL, periodt. π
β¨ METHODS β¨
πΈ set_up_table
__PACKAGE__->set_up_table("table_name");
okay so historically, to use Class::DBI, you had to hand-set all your columns like this:
__PACKAGE__->columns(All => qw/list of columns/);
__PACKAGE__->columns(Primary => 'column_name');
and like, that's great if you're gonna arrange your columns into a bunch of different groupings β more flexibility, we love options. but sometimes bestie you just want the "all columns" list and nothing else. and here's the thing: that info is so easy to pull straight out of MySQL itself, so why would you even do it by hand? π
so this call extracts the list of all the columns and the primary key, and sets them all up for you. iconic, effortless, slay.
β οΈ but it will die horribly if the table contains no primary key, or if it has a composite primary key. so like. know your table first. π
πΈ autoinflate
__PACKAGE__->autoinflate(column_type => 'Inflation::Class');
__PACKAGE__->autoinflate(timestamp => 'Time::Piece');
__PACKAGE__->autoinflate(dates => 'Time::Piece');
this one automatically sets up has_a() relationships for all columns of the specified type, pointing at the given class. set it and forget it. β¨
heads up tho β we currently assume that all the classes you pass in will be able to inflate and deflate without needing extra has_a arguments. the example being Time::Piece objects, which we deal with using Time::Piece::mysql (and yes, you'll have to have that installed! not optional, bestie π).
and the special type 'dates' will autoinflate all columns of type date, datetime or timestamp. one word, three column types. efficiency queen. π
πΈ 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. we simply mind our business. π§
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;
see how we create_table and then set_up_table? that's the combo. that's the routine. π₯
πΈ drop_table
$class->drop_table;
drops the table for this class, if it exists. bye bestie, it was real. π
πΈ column_type
my $type = $class->column_type('column_name');
this returns the 'type' of this table β like VARCHAR(20), BIGINT, etc. just the type. that's the whole job. π
πΈ enum_vals
my @allowed = $class->enum_vals('column_name');
this returns a list of the allowable values for an ENUM column. the whole menu of what's allowed. π
πΈ retrieve_random
my $film = Film->retrieve_random;
this will select a random row from the database and return you the relevant object. chaos, but make it an object. π
(MySQL 3.23 and higher only, at this point)
β¨ SEE ALSO β¨
Class::DBI- MySQL (http://www.mysql.com/)
β¨ AUTHOR β¨
Tony Bowden
β¨ BUGS and QUERIES β¨
please direct all correspondence regarding this module to:
bug-Class-DBI-mysql@rt.cpan.org
β¨ COPYRIGHT AND LICENSE β¨
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.
and that's the whole page, bestie!! this one's from perl v5.36.0, dated 2022-12-04, filed under Class:...::mysql(3pm) in the User Contributed Perl Documentation. go set up a table, retrieve something random, and live your truth. π¦πΈπ
π¦ where this page comes from
- debian bookworm: libclass-dbi-mysql-perl 1.00-5 Β· Artistic-1.0-Perl OR GPL-2.0-or-later
- debian trixie: libclass-dbi-mysql-perl 1.00-5 Β· Artistic-1.0-Perl OR GPL-2.0-or-later
- ubuntu noble: libclass-dbi-mysql-perl 1.00-5 Β· Artistic-1.0-Perl OR GPL-2.0-or-later
- ubuntu resolute: libclass-dbi-mysql-perl 1.00-5 Β· Artistic-1.0-Perl OR GPL-2.0-or-later
- ubuntu stonking: libclass-dbi-mysql-perl 1.00-5 Β· Artistic-1.0-Perl OR GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms