NAME

SPOPS::Import::DBI::TableTransform - Factory class for database-specific transformations

SYNOPSIS

my $table = qq/ CREATE TABLE blah ( id %%INCREMENT%% primary key,
                                    name varchar(50) ) /;
my $transformer = SPOPS::Import::DBI::TableTransform->new( 'sybase' );
$transformer->increment( \$table );
print $table;

DESCRIPTION

This class is a factory class for database-specific transformations. This means that SPOPS::Import::DBI::Table supports certain keys that can be replaced by database-specific values. This class is a factory for objects that take SQL data and do the replacements.

METHODS

new( $database_type )

Create a new transformer using the database type $database_type.

Available database types are:

asany: Sybase Adaptive Server Anywhere
interbase: InterBase family (also: 'firebird')
mssql: Microsoft SQL Server
mysql: MySQL
oracle: Oracle
postgres: PostgreSQL (also: 'pg')
sybase: Sybase SQL Server/ASE

register_factory_type( $database_type, $transform_class )

Registers a new database type for a transformation class. You will need to run this every time you run the program.

If you develop a transformation class for a database not represented here, please email the author so it can be included with future distributions.

CREATING A TRANSFORMATION CLASS

Creating a new subclass is extremely easy. You just need to subclass this class, then create a subroutine for each of the built-in transformations specified in SPOPS::Import::DBI::Table.

Each transformation takes two arguments: $self and a scalar reference to the SQL to be transformed. For example, here is a subclass for a made-up database:

package SPOPS::Import::DBI::TableTransform::SavMor;

use strict;
use base qw( SPOPS::Import::DBI::TableTransform );

sub increment {
   my ( $self, $sql ) = @_;
   $$sql =~ s/%%INCREMENT%%/UNIQUE_VALUE/g;
}

sub increment_type {
   my ( $self, $sql ) = @_;
   $$sql =~ s/%%INCREMENT_TYPE%%/INT/g;
}

sub datetime {
   my ( $self, $sql ) = @_;
   $$sql =~ s/%%DATETIME%%/timestamp/g;
}

1;

And then we could register the transformation agent with every run:

SPOPS::Import::DBI::TableTransform->register_factory_type(
         'savmor', 'SPOPS::Import::DBI::TableTransform::SavMor' );
my $transformer = SPOPS::Import::DBI::TableTransform->new( 'savmor' );
my $sql = qq/ CREATE TABLE ( id %%INCREMENT%% primary key ) /;
$transformer->increment( \$sql );
print $sql;

Output:

CREATE TABLE ( id UNIQUE_VALUE primary key )

BUGS

None known.

TO DO

Nothing known.

SEE ALSO

SPOPS::Import::DBI::Table

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>