NAME
SPOPS::Import::DBI::Table - Import a DBI table structure
SYNOPSIS
#!/usr/bin/perl
use strict;
use SPOPS::Import;
{
my $table_import = SPOPS::Import->new( 'table' );
$table_import->database_type( 'sybase' );
$table_import->read_table_from_fh( \*DATA );
$table_import->print_only( 1 );
$table_import->transforms([ \&table_login ]);
$table_import->run;
}
sub table_login {
my ( $transformer, $sql, $importer ) = @_;
$$sql =~ s/%%LOGIN%%/varchar(25)/g;
}
__DATA__
CREATE TABLE sys_user (
user_id %%INCREMENT%%,
login_name %%LOGIN%% not null,
password varchar(30) not null,
last_login datetime null,
num_logins int null,
theme_id %%INCREMENT_TYPE%% default 1,
first_name varchar(50) null,
last_name varchar(50) null,
title varchar(50) null,
email varchar(100) not null,
language char(2) default 'en',
notes text null,
removal_date %%DATETIME%% null,
primary key ( user_id ),
unique ( login_name )
)
Output:
CREATE TABLE sys_user (
user_id NUMERIC( 10, 0 ) IDENTITY NOT NULL,
login_name varchar(25) not null,
password varchar(30) not null,
last_login datetime null,
num_logins int null,
theme_id NUMERIC( 10, 0 ) default 1,
first_name varchar(50) null,
last_name varchar(50) null,
title varchar(50) null,
email varchar(100) not null,
language char(2) default 'en',
notes text null,
removal_date datetime null,
primary key ( user_id ),
unique ( login_name )
)
DESCRIPTION
This class allows you to transform and import (or simply display) a DBI table structure.
Transformations are done via two means. The first is the database-specific classes and the standard modifications provided by SPOPS::Import::DBI::TableTransform. The second is custom code that you can write.
METHODS
database_type ($)
Type of database to generate a table for. See SPOPS::Import::DBI::TableTransform for the listing and types to use.
transforms (\@ of \&, or \&)
Register with the import object one or more code references that will get called to modify a SQL statement. See "CUSTOM TRANSFORMATIONS" below.
print_only (boolean)
If set to true, the final table will be printed to STDOUT rather than sent to a database.
return_only (boolean)
If set to true, the final table will be returned from run()
rather than sent to a database.
CUSTOM TRANSFORMATIONS
As the example in SYNOPSIS indicates, you can register perl code to modify the contents of a table before it is displayed or sent to a database. When called the code will get three arguments:
- 1. an object that is a subclass of SPOPS::Import::DBI::TableTransform for the database type specified
- 2. a scalar reference to the SQL statement to be transformed
- 3. the SPOPS::Import::DBI::Table object being currently used.
Most of the transformation code will be very simple, along the lines of:
sub my_transform {
my ( $self, $sql, $importer ) = @_;
$$sql =~ s/%%THIS_KEY%%/THAT SQL EXPRESSION/g;
}
BUILT-IN TRANSFORMATIONS
These are the built-in transformations:
increment
Key: %%INCREMENT%%
Replaces the key with an expression to generate a unique ID value with every INSERT. Some databases accomplish this with a sequence, others with an auto-incrementing value.
increment_type
Key: %%INCREMENT_TYPE%%
Datatype of the increment field specified by %%INCREMENT%%. This is necessary when you are creating foreign keys (logical or enforced) and need to know the datatype of the ID you are referencing.
datetime
Key: %%DATETIME%%
Datatype of the field that holds a date and time value. This should not be automatically set with every insert/update (as it is with MySQL).
BUGS
None known.
TO DO
Nothing known.
SEE ALSO
SPOPS::Import::DBI::TableTransform
COPYRIGHT
Copyright (c) 2001-2004 intes.net, inc.. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AUTHORS
Chris Winters <chris@cwinters.com>