NAME

MATT::Mysql - Common Mysql functions

head1 SYNOPSIS

MATT::Mysql is a grouping of frequently used functions I've written for Mysql.


DESCRIPTION

I find myself using MySQL for a lot of things. Geographically distributed dns systems (MySQL replication), mail servers, and all the other fun stuff you'd use a RDBMS for. As such, I've got a growing pile of scripts that have lots of duplicated code in them. As such, the need for this Perl module grew.

InstallPhpMyAdmin

Install PhpMyAdmin from FreeBSD ports.

        use MATT::Mysql;
        InstallPhpMyAdmin($conf);

$conf is a hash of configuration values. See toaster-watcher.conf for configuring the optional values to pass along.

MysqlQuery

        my $sth = MysqlQuery ($dbh, $query, $warn)

$dbh is the database handle you've already acquired via MysqlConnect.

$query is the SQL statement to execute.

If $warn is set, we don't die if the query fails. This way you can decide when you call the sub whether you want it to die or return a failed $sth (and likely an error message).

 execute performs whats necessary to execute a statement
 Always returns true regardless of # of rows affected.
 For non-Select, returns # of rows affected: No rows = 0E0
 For Select, simply starts query. Follow with fetch_*

MysqlConnect

        use MATT::Mysql;
        my ($dbh, $dsn, $drh) = MysqlConnect($dot, $warn, $debug);

$dot is a hashref of key/value pairs in the same format you'd find in ~/.my.cnf. No coincidentally, that's where it expects you'll be getting them from.

$warn allows you to determine whether to die or warn on failure or error. To warn, set $warn to a non-zero value.

$debug will print out helpful debugging messages should you be having problems.

MysqlDBVars

This sub is called internally by MysqlConnect and is used principally to set some reasonable defaults should you not pass along enough connection parameters in $dot.

SetAutocommit

ParseDotFile

 use MATT::Mysql;
 my $dot = ParseDotFile(".my.cnf", "[mysql_replicate_manager]", 0);
 ParseDotFile ($file, $start, $debug)
 $file is the file to be parsed.

$start is the [identifier] where we begin looking for settings. This expects the format used in .my.cnf MySQL configuration files.

A hashref is returned wih key value pairs

MysqlSanity

A place to do validation tests on values to make sure they're reasonable

Currently we only check to assure the password is less than 32 characters and the username is less than 16. Many more tests will come.

IsMysqlNewer

        use Matt::Mysql;
        my $ver   = MysqlVersion($dbh);
        my $newer = IsMysqlNewer("4.1.0", $ver);

if ($newer) { print ``you are brave!'' };

As you can see, IsMysqlNewer can be very useful, especially when you need to execute queries with syntax differences between versions of Mysql.

MysqlVersion


        use MATT::Mysql;
        my $ver = MysqlVersion($dbh);

Returns a string representing the version of MySQL running.

LockTables

        use MATT::Mysql;
        LockTables($dbh, $debug);

Takes a statement handle and does a global lock on all tables. Quite useful when you want do do things like make a tarball of the database directory, back up the server, etc.

UnlockTables

        use MATT::Mysql;
        UnlockTables($dbh, $sth, $debug);

Takes a statement handle and does a global unlock on all tables. Quite useful after you've used LockTables, done your deeds and wish to return to normal.

InstallMysql


Dependencies

        DBI.pm     - /usr/ports/databases/p5-DBI
        DBD::mysql - /usr/ports/databases/p5-DBD-mysql

In order to use this module, you must have DBI.pm and DBD::Mysql installed. If they are not installed and you attempt to use this module, you should get some helpful error messages telling you how to install them.


AUTHOR

        Matt Simerson <matt@cadillac.net>


BUGS

        None known. Report any to author.


TODO

Don't export any of the symbols by default. Move all symbols to EXPORT_OK and explicitely pull in the required ones in programs that need them.


SEE ALSO

        http://www.tnpi.biz/internet/sql/

MATT::Apache, MATT::DNS, MATT::FreeBSD, MATT::Mysql, MATT::Passwd, MATT::Perl, MATT::Qmail, MATT::Quota MATT::SSL, MATT::Utility


COPYRIGHT

Copyright 2003, The Network People, Inc. All Rights Reserved.