Class:...::mysql(3pm)

πŸ“¦
version 1.00 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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 ✨


✨ AUTHOR ✨

Tony Bowden


✨ BUGS and QUERIES ✨

please direct all correspondence regarding this module to:

bug-Class-DBI-mysql@rt.cpan.org

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