NAME
DBIx::Admin::TableInfo
- A wrapper for table_info(), column_info(), *_key_info()
Synopsis
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use DBI;
use DBIx::Admin::TableInfo;
# ---------------------
my($dbh) = DBI -> connect($ENV{'DBI_DSN'}, $ENV{'DBI_USER'}, $ENV{'DBI_PASS'});
my($schema) = $ENV{'DBI_DSN'} =~ /^dbi:Oracle/i ? uc $ENV{'DBI_USER'} : undef;
print Data::Dumper -> Dump
([
DBIx::Admin::TableInfo -> new(dbh => $dbh, schema => $schema) -> info()
]);
Description
DBIx::Admin::TableInfo
is a pure Perl module.
It is a convenient wrapper around these DBI methods:
Warnings:
- MySQL
-
The MySQL client
DBD::mysql
V 3.0002 does not supportprimary_key_info()
, so this module emulates it by stockpiling a list of columns which have the attribute 'mysql_is_pri_key' set.The problem with this is that if a primary key consists of more than 1 column,
DBD::mysql
does not indicate the order of these columns within the key, so this module pretends that they are in the same order as the order of columns returned by the call tocolumn_info()
.Likewise,
DBD::mysql
does not supportforeign_key_info()
, so in the case of MySQL, nothing is reported for foreign keys.For MySQL V 5.0.18, section 14.2.6.4 of the manual says that for InnoDB tables, the SQL "show table status from 'db name' like 'table name'" will display the foreign key info in the column called 'Comment', but this is simply not true. The 'Comment' column contains a string such as 'InnoDB free: 4096 kB'.
Likewise, the SQL "show create table 'table name'" reveals than MySQL does not preserve 'create table' clauses such as 'references other_table(other_column)'.
So, at the moment, I see no way of displaying foreign key information under MySQL.
- Oracle
-
Oracle table names matching /^BIN\$.+\$./ are ignored by this module.
Distributions
This module is available both as a Unix-style distro (*.tgz) and an ActiveState-style distro (*.ppd). The latter is shipped in a *.zip file.
See http://savage.net.au/Perl-modules.html for details.
See http://savage.net.au/Perl-modules/html/installing-a-module.html for help on unpacking and installing each type of distro.
Constructor and initialization
new(...) returns a DBIx::Admin::TableInfo
object.
This is the class's contructor.
Usage: DBIx::Admin::TableInfo -> new().
This method takes a set of parameters. Only the dbh parameter is mandatory.
For each parameter you wish to use, call new as new(param_1 => value_1, ...).
- catalog
-
This is the value passed in as the catalog parameter to table_info() and column_info().
The default value is undef.
undef was chosen because it given the best results with MySQL.
Note: The MySQL driver DBD::mysql V 2.9002 has a bug in it, in that it aborts if an empty string is used here, even though the DBI docs say an empty string can be used for the catalog parameter to
table_info()
.This parameter is optional.
- dbh
-
This is a database handle.
This parameter is mandatory.
- schema
-
This is the value passed in as the schema parameter to table_info() and column_info().
Note: If you are using Oracle, call
new()
with schema set to uc $user_name.Note: If you are using Postgres, call
new()
with schema set to 'public'.The default value is undef.
This parameter is optional.
- table
-
This is the value passed in as the table parameter to table_info().
The default value is '%'.
This parameter is optional.
- type
-
This is the value passed in as the type parameter to table_info().
The default value is 'TABLE'.
Note: If you are using Postgres, call
new()
with table set to 'table'.This parameter is optional.
Method: columns($table_name, $by_position)
Returns an array ref of column names.
By default they are sorted by name.
However, if you pass in a true value for $by_position, they are sorted by the column attribute ORDINAL_POSITION.
Method: info()
Returns a hash ref of all available data.
The structure of this hash is described next:
- First level: The keys are the names of the tables
-
my($info) = $obj -> info(); my(@table_name) = sort keys %$info;
I use singular names for my arrays, hence @table_name rather than @table_names.
- Second level: The keys are 'attributes', 'columns', 'foreign_keys' and 'primary_keys'
-
my($table_attributes) = $$info{$table_name}{'attributes'};
This is a hash ref of the table's attributes. The keys of this hash ref are determined by the database server.
my($columns) = $$info{$table_name}{'columns'};
This is a hash ref of the table's columns. The keys of this hash ref are the names of the columns.
my($foreign_keys) = $$info{$table_name}{'foreign_keys'};
This is a hash ref of the table's foreign keys. The keys of this hash ref are the names of the tables which contain foreign keys pointing to $table_name.
For MySQL, $foreign_keys will be the empty hash ref {}, as explained above.
my($primary_keys) = $$info{$table_name}{'primary_keys'};
This is a hash ref of the table's primary keys. The keys of this hash ref are the names of the columns which make up the primary key of $table_name.
For any database server, if there is more than 1 column in the primary key, they will be numbered (ordered) according to the hash key 'KEY_SEQ'.
For MySQL, if there is more than 1 column in the primary key, they will be artificially numbered according to the order in which they are returned by
column_info()
, as explained above. - Third level, after 'attributes': Table attributes
-
my($table_attributes) = $$info{$table_name}{'attributes'}; while ( ($name, $value) = each(%$table_attributes) ) { Use... }
For the attributes of the tables, there are no more levels in the hash ref.
- Third level, after 'columns': The keys are the names of the columns.
-
my($columns) = $$info{$table_name}{'columns'}; my(@column_name) = sort keys %$columns;
- Third level, after 'foreign_keys': The keys are the names of tables
-
These tables have foreign keys which point to the current table.
my($foreign_keys) = $$info{$table_name}{'foreign_keys'}; for $foreign_table (sort keys %$foreign_keys) { $foreign_key = $$foreign_keys{$foreign_table}; for $attribute (sort keys %$foreign_key) { Use... } }
- Third level, after 'primary_keys': The keys are the names of columns
-
These columns make up the primary key of the current table.
my($primary_keys) = $$info{$table_name}{'primary_keys'}; for $primary_key (sort{$$a{'KEY_SEQ'} <=> $$b{'KEY_SEQ'} } keys %$primary_keys) { $primary = $$primary_keys{$primary_key}; for $attribute (sort keys %$primary) { Use... } }
Method: refresh()
Returns the same hash ref as info().
Use this after changing the database schema, when you want this module to re-interrogate the database server.
Method: tables()
Returns an array ref of table names.
They are sorted by name.
Warning: Oracle table names matching /^BIN\$.+\$./ are ignored by this module.
Example code
Here are tested parameter values for various database vendors:
- MS Access
-
my($admin) = DBIx::Admin::TableInfo -> new(dbh => $dbh); In other words, the default values for catalog, schema, table and type will Just Work.
- MySQL
-
my($admin) = DBIx::Admin::TableInfo -> new(dbh => $dbh); In other words, the default values for catalog, schema, table and type will Just Work.
- Oracle
-
my($dbh) = DBI -> connect($dsn, $username, $password); my($admin) = DBIx::Admin::TableInfo -> new ( dbh => $dbh, schema => uc $username, # Yep, upper case. ); For Oracle, you probably want to ignore table names matching /^BIN\$.+\$./.
- PostgreSQL
-
my($admin) = DBIx::Admin::TableInfo -> new ( dbh => $dbh, schema => 'public', table => 'table', # Yep, lower case. ); For PostgreSQL, you probably want to ignore table names matching /^(pg_|sql_)/.
See the examples/ directory in the distro.
Tested Database Formats
I have used the program in the synopsis to read databases in these formats:
- MS Access V 2
-
Yes, some businesses were still running V 2 as of July, 2004.
- MS Access V 2002 and V 2003
- MySQL V 4 and V 5
- Oracle V 9.2.0
- PostgreSQL V 7.3 and 8.1
Related Modules
I have written a set of modules - which are still being tested - under the DBIx::Admin::* namespace.
They will form the core of myadmin.cgi V 2. See http://savage.net.au/Perl-tutorials.html#tut_41
Changes
See Changes.txt.
Author
DBIx::Admin::TableInfo
was written by Ron Savage <ron@savage.net.au> in 2004.
Home page: http://savage.net.au/index.html
Copyright
Australian copyright (c) 2004, Ron Savage. All rights reserved.
All Programs of mine are 'OSI Certified Open Source Software';
you can redistribute them and/or modify them under the terms of
The Artistic License, a copy of which is available at:
http://www.opensource.org/licenses/index.html