NAME
MySQL::Util - Utility functions for working with MySQL.
VERSION
Version 0.01
SYNOPSIS
my $util = MySQL::Util->new( dsn => $ENV{DBI_DSN},
user => $ENV{DBI_USER} );
my $aref = $util->describe_table('mytable');
print "table: mytable\n";
foreach my $href (@$aref) {
print "\t", $href->{FIELD}, "\n";
}
my $href = $util->get_ak_constraints('mytable');
my $href = $util->get_ak_indexes('mytable');
my $href = $util->get_constraints('mytable');
...
METHODS
All methods croak in the event of failure unless otherwise noted.
- new(dsn => $dsn, user => $user, [pass => $pass])
-
constructor
- describe_table($table);
-
Returns an arrayref of column info for a given table.
The structure of the returned data is:
$arrayref->[ { col1 }, { col2 } ]
Hash elements for each column:
DEFAULT EXTRA FIELD KEY NULL TYPE
See MySQL documentation for more info on "describe <table>".
- get_ak_constraints($table)
-
Returns a hashref of the alternate key constraints for a given table. Returns an empty hashref if none were found. The primary key is excluded from the returned data.
The structure of the returned data is:
$hashref->{constraint_name}->[ { col1 }, { col2 } ]
See "get_constraints" for a list of the hash elements in each column.
- get_ak_indexes($table)
-
Returns a hashref of the alternate key indexes for a given table. Returns an empty hashref if none were found.
The structure of the returned data is:
$href->{index_name}->[ { col1 }, { col2 } ]
See get_indexes for a list of hash elements in each column.
- get_constraints($table)
-
Returns a hashref of the constraints for a given table. Returns an empty hashref if none were found.
The structure of the returned data is:
$hashref->{constraint_name}->[ { col1 }, { col2 } ]
Hash elements for each column:
CONSTRAINT_TYPE COLUMN_NAME ORDINAL_POSITION POSITION_IN_UNIQUE_CONSTRAINT REFERENCED_TABLE_NAME REFERENCED_COLUMN_NAME
- get_depth($table)
-
Returns the table depth within the data model hierarchy. The depth is zero based.
For example:
----------- ----------- | table A |------<| table B | ----------- -----------
-
Table A has a depth of 0 and table B has a depth of 1. In other words, table B is one level down in the model hierarchy.
If a table has multiple parents, the parent with the highest depth wins.
- get_fk_constraints($table)
-
Returns a hashref of the foreign key constraints for a given table. Returns an empty hashref if none were found.
The structure of the returned data is:
$hashref->{constraint_name}->[ { col1 }, { col2 } ]
See "get_constraints" for a list of the hash elements in each column.
- get_fk_indexes($table)
-
Returns a hashref of the foreign key indexes for a given table. Returns an empty hashref if none were found. In order to qualify as a fk index, it must have a corresponding fk constraint.
The structure of the returned data is:
$hashref->{index_name}->[ { col1 }, { col2 } ]
See "get_indexes" for a list of the hash elements in each column.
- get_indexes($table)
-
Returns a hashref of the indexes for a given table. Returns an empty hashref if none were found.
The structure of the returned data is:
$href->{index_name}->[ { col1 }, { col2 } ]
Hash elements for each column:
CARDINALITY COLLATION COLUMN_NAME COMMENT INDEX_TYPE KEY_NAME NON_UNIQUE NULL PACKED SEQ_IN_INDEX SUB_PART TABLE
- get_max_depth()
-
Returns the max table depth for all tables in the database.
See "get_depth" for additional info.
- get_other_constraints($table)
-
Returns a hashref of the constraints that are not pk, ak, or fk for a given table. Returns an empty hashref if none were found.
The structure of the returned data is:
$hashref->{constraint_name}->[ { col1 }, { col2 } ]
See "get_constraints" for a list of the hash elements in each column.
- get_other_indexes($table)
-
Returns a hashref of the indexes that are not pk, ak, or fk for a given table. Returns an empty hashref if none were found.
The structure of the returned data is:
$hashref->{index_name}->[ { col1 }, { col2 } ]
See "get_indexes" for a list of the hash elements in each column.
- get_pk_constraint($table)
-
Returns an arrayref of the primary key constraint for a given table. Returns an empty arrayref if none were found.
The structure of the returned data is:
$aref->[ { col1 }, { col2 }, ... ]
See "get_constraints" for a list of hash elements in each column.
- get_pk_index($table)
-
Returns an arrayref of the primary key index for a given table. Returns an empty arrayref if none were found.
The structure of the returned data is:
$aref->[ { col1 }, { col2 }, ... ]
See "get_indexes" for a list of the hash elements in each column.
- get_tables( )
-
Returns an arrayref of tables in the current database. Returns undef if no tables were found.
- table_exists($table)
-
Returns true if table exists. Otherwise returns false.
- use_dbh($dbname)
-
Used for switching database context. Returns true on success.
AUTHOR
John Gravatt, <gravattj at cpan.org>
BUGS
Please report any bugs or feature requests to bug-mysql-util at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MySQL-Util. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc MySQL::Util
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
ACKNOWLEDGEMENTS
LICENSE AND COPYRIGHT
Copyright 2011 John Gravatt.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 352:
Expected text after =item, not a bullet