NAME
Data::HandyGen::mysql::TableDef - Manages table definition in mysql
VERSION
This documentation refers to Data::HandyGen::mysql::TableDef version 0.0.5
SYNOPSIS
use Data::HandyGen::mysql::TableDef;
use DBI;
my $dbh = DBI->connect('dbi:mysql:dbname=testdb', 'username', 'password');
my $table_def = Data::HandyGen::mysql::TableDef->new( dbh => $dbh, table_name => 'table1');
CAUTION
This module is not intended for use outside Data::HandyGen. Its interface may be changed in the future.
DESCRIPTION
This module manages a table definition in one table in Mysql.
METHODS
new(%params)
Constructor.
dbh($dbh)
Setter/getter for database handle generated by DBI.
table_name($name)
Setter/getter for table name.
colnames()
Returns all columns in this table. If you have a table such as
mysql> desc table1;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| category_id | int(11) | YES | MUL | NULL | |
| name | varchar(20) | NO | | NULL | |
| price | int(11) | NO | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
colnames() returns an arrayref containing 'id', 'category_id', 'name' and 'price'. Order won't be guaranteed.
def()
Returns a table definition. It is a hashref which information is originally retrieved from information_schema.columns, which contains fields such as:
+--------------------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+---------------------+------+-----+---------+-------+
| TABLE_CATALOG | varchar(512) | NO | | | |
| TABLE_SCHEMA | varchar(64) | NO | | | |
| TABLE_NAME | varchar(64) | NO | | | |
| COLUMN_NAME | varchar(64) | NO | | | |
| ORDINAL_POSITION | bigint(21) unsigned | NO | | 0 | |
| COLUMN_DEFAULT | longtext | YES | | NULL | |
| IS_NULLABLE | varchar(3) | NO | | | |
| DATA_TYPE | varchar(64) | NO | | | |
| CHARACTER_MAXIMUM_LENGTH | bigint(21) unsigned | YES | | NULL | |
| CHARACTER_OCTET_LENGTH | bigint(21) unsigned | YES | | NULL | |
| NUMERIC_PRECISION | bigint(21) unsigned | YES | | NULL | |
| NUMERIC_SCALE | bigint(21) unsigned | YES | | NULL | |
| CHARACTER_SET_NAME | varchar(32) | YES | | NULL | |
| COLLATION_NAME | varchar(32) | YES | | NULL | |
| COLUMN_TYPE | longtext | NO | | NULL | |
| COLUMN_KEY | varchar(3) | NO | | | |
| EXTRA | varchar(27) | NO | | | |
| PRIVILEGES | varchar(80) | NO | | | |
| COLUMN_COMMENT | varchar(1024) | NO | | | |
+--------------------------+---------------------+------+-----+---------+-------+
Table definition returned by def() is like the following:
$ret = {
'column_1' => {
TABLE_CATALOG => 'def',
TABLE_SCHEMA => 'test',
...
},
'column_2' => {
TABLE_CATALOG => 'def',
TABLE_SCHEMA => 'test',
...
},
....
}
Field names in the hashref are all converted to uppercase.
is_pk($colname)
Returns 1 if $colname is one of primary key columns. Otherwise returns 0.
is_fk($colname)
If $colname is a foreign key, returns referenced table/column name like this:
# In case only one foreign key found
$ret = {
table => 'table name',
column => 'column name'
}
# In case multiple foreign keys found
$ret = [
{ table => 'table1', column => 'column1' },
{ table => 'table2', column => 'column2' },
{ table => 'table3', column => 'column3' },
]
Otherwise, returns undef.
pk_columns()
Returns arrayref of column names of primary keys.
column_def($column_name)
Returns column definition (ColumnDef object)
get_auto_increment_value()
Returns a value which auto_increment will generate next time.
AUTHOR
Takashi Egawa
LICENCE AND COPYRIGHT
Copyright (c)2013-2014 Takashi Egawa. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
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.